|
Packit Service |
15f37d |
package osbuild1
|
|
Packit Service |
15f37d |
|
|
Packit Service |
15f37d |
import (
|
|
Packit Service |
15f37d |
"encoding/json"
|
|
Packit Service |
15f37d |
"testing"
|
|
Packit Service |
15f37d |
|
|
Packit Service |
15f37d |
"github.com/google/uuid"
|
|
Packit Service |
15f37d |
"github.com/stretchr/testify/assert"
|
|
Packit Service |
15f37d |
)
|
|
Packit Service |
15f37d |
|
|
Packit Service |
15f37d |
func TestAssembler_UnmarshalJSON(t *testing.T) {
|
|
Packit Service |
15f37d |
tests := []struct {
|
|
Packit Service |
15f37d |
name string
|
|
Packit Service |
15f37d |
assembler Assembler
|
|
Packit Service |
15f37d |
data []byte
|
|
Packit Service |
15f37d |
errorExpected bool
|
|
Packit Service |
15f37d |
}{
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
// invalid JSON - note the missing brace at the end of the string
|
|
Packit Service |
15f37d |
name: "invalid json",
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.tar","options":{"filename":""}`),
|
|
Packit Service |
15f37d |
errorExpected: true,
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
// valid JSON, but with an unknown assembler (org.osbuild.foo)
|
|
Packit Service |
15f37d |
name: "unknown assembler",
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.foo","options":{"bar":null}}`),
|
|
Packit Service |
15f37d |
errorExpected: true,
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "missing options",
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.rawfs"`),
|
|
Packit Service |
15f37d |
errorExpected: true,
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "missing name",
|
|
Packit Service |
15f37d |
data: []byte(`{"options":{"bar":null}}`),
|
|
Packit Service |
15f37d |
errorExpected: true,
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "qemu assembler empty",
|
|
Packit Service |
15f37d |
assembler: Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.qemu",
|
|
Packit Service |
15f37d |
Options: &QEMUAssemblerOptions{},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.qemu","options":{"format":"","filename":"","size":0,"ptuuid":"","pttype":"","partitions":null}}`),
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "qemu assembler full",
|
|
Packit Service |
15f37d |
assembler: Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.qemu",
|
|
Packit Service |
15f37d |
Options: &QEMUAssemblerOptions{
|
|
Packit Service |
15f37d |
Format: "qcow2",
|
|
Packit Service |
15f37d |
Filename: "disk.qcow2",
|
|
Packit Service |
15f37d |
Size: 2147483648,
|
|
Packit Service |
15f37d |
PTUUID: "0x14fc63d2",
|
|
Packit Service |
15f37d |
PTType: "mbr",
|
|
Packit Service |
15f37d |
Partitions: []QEMUPartition{QEMUPartition{
|
|
Packit Service |
15f37d |
Start: 2048,
|
|
Packit Service |
15f37d |
Bootable: true,
|
|
Packit Service |
15f37d |
Filesystem: &QEMUFilesystem{
|
|
Packit Service |
15f37d |
Type: "ext4",
|
|
Packit Service |
15f37d |
UUID: "76a22bf4-f153-4541-b6c7-0332c0dfaeac",
|
|
Packit Service |
15f37d |
Label: "root",
|
|
Packit Service |
15f37d |
Mountpoint: "/",
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
}},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.qemu","options":{"format":"qcow2","filename":"disk.qcow2","size":2147483648,"ptuuid":"0x14fc63d2","pttype":"mbr","partitions":[{"start":2048,"bootable":true,"filesystem":{"type":"ext4","uuid":"76a22bf4-f153-4541-b6c7-0332c0dfaeac","label":"root","mountpoint":"/"}}]}}`),
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "tar assembler empty",
|
|
Packit Service |
15f37d |
assembler: Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.tar",
|
|
Packit Service |
15f37d |
Options: &TarAssemblerOptions{},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.tar","options":{"filename":""}}`),
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "tar assembler full",
|
|
Packit Service |
15f37d |
assembler: Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.tar",
|
|
Packit Service |
15f37d |
Options: &TarAssemblerOptions{
|
|
Packit Service |
15f37d |
Filename: "root.tar.xz",
|
|
Packit Service |
15f37d |
Compression: "xz",
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.tar","options":{"filename":"root.tar.xz","compression":"xz"}}`),
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "rawfs assembler empty",
|
|
Packit Service |
15f37d |
assembler: Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.rawfs",
|
|
Packit Service |
15f37d |
Options: &RawFSAssemblerOptions{},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.rawfs","options":{"filename":"","root_fs_uuid":"00000000-0000-0000-0000-000000000000","size":0}}`),
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "rawfs assembler full",
|
|
Packit Service |
15f37d |
assembler: Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.rawfs",
|
|
Packit Service |
15f37d |
Options: &RawFSAssemblerOptions{
|
|
Packit Service |
15f37d |
Filename: "filesystem.img",
|
|
Packit Service |
15f37d |
RootFilesystemUUID: uuid.MustParse("76a22bf4-f153-4541-b6c7-0332c0dfaeac"),
|
|
Packit Service |
15f37d |
Size: 2147483648,
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.rawfs","options":{"filename":"filesystem.img","root_fs_uuid":"76a22bf4-f153-4541-b6c7-0332c0dfaeac","size":2147483648}}`),
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
{
|
|
Packit Service |
15f37d |
name: "ostree commit assembler",
|
|
Packit Service |
15f37d |
assembler: Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.ostree.commit",
|
|
Packit Service |
15f37d |
Options: &OSTreeCommitAssemblerOptions{
|
|
Packit Service |
15f37d |
Ref: "foo",
|
|
Packit Service |
15f37d |
Tar: OSTreeCommitAssemblerTarOptions{
|
|
Packit Service |
15f37d |
Filename: "foo.tar",
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
data: []byte(`{"name":"org.osbuild.ostree.commit","options":{"ref":"foo","tar":{"filename":"foo.tar"}}}`),
|
|
Packit Service |
15f37d |
},
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
|
|
Packit Service |
15f37d |
assert := assert.New(t)
|
|
Packit Service |
15f37d |
for _, tt := range tests {
|
|
Packit Service |
15f37d |
t.Run(tt.name, func(t *testing.T) {
|
|
Packit Service |
15f37d |
assembler := &tt.assembler
|
|
Packit Service |
15f37d |
var gotAssembler Assembler
|
|
Packit Service |
15f37d |
err := gotAssembler.UnmarshalJSON(tt.data)
|
|
Packit Service |
15f37d |
if tt.errorExpected {
|
|
Packit Service |
15f37d |
assert.NotNil(err)
|
|
Packit Service |
15f37d |
return
|
|
Packit Service |
15f37d |
} else {
|
|
Packit Service |
15f37d |
assert.Nil(err)
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
gotBytes, err := json.Marshal(assembler)
|
|
Packit Service |
15f37d |
assert.Nilf(err, "Could not marshal assembler: %v", err)
|
|
Packit Service |
15f37d |
assert.Equal(tt.data, gotBytes)
|
|
Packit Service |
15f37d |
assert.Equal(&gotAssembler, assembler)
|
|
Packit Service |
15f37d |
})
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
|
|
Packit Service |
15f37d |
func TestNewQEMUAssembler(t *testing.T) {
|
|
Packit Service |
15f37d |
options := &QEMUAssemblerOptions{}
|
|
Packit Service |
15f37d |
expectedAssembler := &Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.qemu",
|
|
Packit Service |
15f37d |
Options: &QEMUAssemblerOptions{},
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
assert.Equal(t, expectedAssembler, NewQEMUAssembler(options))
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
|
|
Packit Service |
15f37d |
func TestNewTarAssembler(t *testing.T) {
|
|
Packit Service |
15f37d |
options := &TarAssemblerOptions{}
|
|
Packit Service |
15f37d |
expectedAssembler := &Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.tar",
|
|
Packit Service |
15f37d |
Options: &TarAssemblerOptions{},
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
assert.Equal(t, expectedAssembler, NewTarAssembler(options))
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
|
|
Packit Service |
15f37d |
func TestNewRawFSAssembler(t *testing.T) {
|
|
Packit Service |
15f37d |
options := &RawFSAssemblerOptions{}
|
|
Packit Service |
15f37d |
expectedAssembler := &Assembler{
|
|
Packit Service |
15f37d |
Name: "org.osbuild.rawfs",
|
|
Packit Service |
15f37d |
Options: &RawFSAssemblerOptions{},
|
|
Packit Service |
15f37d |
}
|
|
Packit Service |
15f37d |
assert.Equal(t, expectedAssembler, NewRawFSAssembler(options))
|
|
Packit Service |
15f37d |
}
|