osbuild2: unit tests for new stages
This commit is contained in:
parent
dc62275d4d
commit
ce0fddf4c2
5 changed files with 297 additions and 0 deletions
42
internal/osbuild2/copy_stage_test.go
Normal file
42
internal/osbuild2/copy_stage_test.go
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
package osbuild2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewCopyStage(t *testing.T) {
|
||||||
|
|
||||||
|
paths := []CopyStagePath{
|
||||||
|
{
|
||||||
|
From: "input://tree-input/",
|
||||||
|
To: "mount://root/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
devices := make(map[string]Device)
|
||||||
|
mounts := make(map[string]Mount)
|
||||||
|
devices["root"] = Device{
|
||||||
|
Type: "org.osbuild.loopback",
|
||||||
|
Options: LoopbackDeviceOptions{
|
||||||
|
Filename: "/somekindofimage.img",
|
||||||
|
Start: 0,
|
||||||
|
Size: 1073741824,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
treeInput := CopyStageInput{}
|
||||||
|
treeInput.Type = "org.osbuild.tree"
|
||||||
|
treeInput.Origin = "org.osbuild.pipeline"
|
||||||
|
treeInput.References = []string{"name:input-pipeline"}
|
||||||
|
copyStageMounts := CopyStageMounts(mounts)
|
||||||
|
copyStageDevices := CopyStageDevices(devices)
|
||||||
|
expectedStage := &Stage{
|
||||||
|
Type: "org.osbuild.copy",
|
||||||
|
Options: &CopyStageOptions{paths},
|
||||||
|
Inputs: &CopyStageInputs{"tree-input": treeInput},
|
||||||
|
Devices: ©StageDevices,
|
||||||
|
Mounts: ©StageMounts,
|
||||||
|
}
|
||||||
|
actualStage := NewCopyStage(&CopyStageOptions{paths}, &CopyStageInputs{"tree-input": treeInput}, ©StageDevices, ©StageMounts)
|
||||||
|
assert.Equal(t, expectedStage, actualStage)
|
||||||
|
}
|
||||||
112
internal/osbuild2/grub2_inst_stage_test.go
Normal file
112
internal/osbuild2/grub2_inst_stage_test.go
Normal file
|
|
@ -0,0 +1,112 @@
|
||||||
|
package osbuild2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/osbuild/osbuild-composer/internal/common"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewGrub2InstStage(t *testing.T) {
|
||||||
|
options := Grub2InstStageOptions{
|
||||||
|
Filename: "img.raw",
|
||||||
|
Platform: "i386-pc",
|
||||||
|
Location: 2048,
|
||||||
|
Core: CoreMkImage{
|
||||||
|
Type: "mkimage",
|
||||||
|
PartLabel: "gpt",
|
||||||
|
Filesystem: "ext4",
|
||||||
|
},
|
||||||
|
Prefix: PrefixPartition{
|
||||||
|
Type: "partition",
|
||||||
|
PartLabel: "gpt",
|
||||||
|
Number: 1,
|
||||||
|
Path: "/boot/grub2",
|
||||||
|
},
|
||||||
|
SectorSize: common.Uint64ToPtr(512),
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedStage := &Stage{
|
||||||
|
Type: "org.osbuild.grub2.inst",
|
||||||
|
Options: &options,
|
||||||
|
}
|
||||||
|
|
||||||
|
actualStage := NewGrub2InstStage(&options)
|
||||||
|
assert.Equal(t, expectedStage, actualStage)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalGrub2InstStage(t *testing.T) {
|
||||||
|
goodOptions := func() Grub2InstStageOptions {
|
||||||
|
return Grub2InstStageOptions{
|
||||||
|
Filename: "img.raw",
|
||||||
|
Platform: "i386-pc",
|
||||||
|
Location: 2048,
|
||||||
|
Core: CoreMkImage{
|
||||||
|
Type: "mkimage",
|
||||||
|
PartLabel: "gpt",
|
||||||
|
Filesystem: "ext4",
|
||||||
|
},
|
||||||
|
Prefix: PrefixPartition{
|
||||||
|
Type: "partition",
|
||||||
|
PartLabel: "gpt",
|
||||||
|
Number: 1,
|
||||||
|
Path: "/boot/grub2",
|
||||||
|
},
|
||||||
|
SectorSize: common.Uint64ToPtr(512),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
options := goodOptions()
|
||||||
|
|
||||||
|
stage := NewGrub2InstStage(&options)
|
||||||
|
_, err := json.Marshal(stage)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
options := goodOptions()
|
||||||
|
options.Core.Type = "notmkimage"
|
||||||
|
|
||||||
|
stage := NewGrub2InstStage(&options)
|
||||||
|
_, err := json.Marshal(stage)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
options := goodOptions()
|
||||||
|
options.Core.PartLabel = "notgpt"
|
||||||
|
|
||||||
|
stage := NewGrub2InstStage(&options)
|
||||||
|
_, err := json.Marshal(stage)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
options := goodOptions()
|
||||||
|
options.Core.Filesystem = "apfs"
|
||||||
|
|
||||||
|
stage := NewGrub2InstStage(&options)
|
||||||
|
_, err := json.Marshal(stage)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
options := goodOptions()
|
||||||
|
options.Prefix.Type = "notpartition"
|
||||||
|
|
||||||
|
stage := NewGrub2InstStage(&options)
|
||||||
|
_, err := json.Marshal(stage)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
options := goodOptions()
|
||||||
|
options.Prefix.PartLabel = "notdos"
|
||||||
|
|
||||||
|
stage := NewGrub2InstStage(&options)
|
||||||
|
_, err := json.Marshal(stage)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
72
internal/osbuild2/mkfs_stages_test.go
Normal file
72
internal/osbuild2/mkfs_stages_test.go
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
package osbuild2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/osbuild/osbuild-composer/internal/common"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewMkfsStage(t *testing.T) {
|
||||||
|
devOpts := LoopbackDeviceOptions{
|
||||||
|
Filename: "file.img",
|
||||||
|
Start: 0,
|
||||||
|
Size: 1024,
|
||||||
|
SectorSize: common.Uint64ToPtr(512),
|
||||||
|
}
|
||||||
|
device := NewLoopbackDevice(&devOpts)
|
||||||
|
|
||||||
|
btrfsOptions := &MkfsBtrfsStageOptions{
|
||||||
|
UUID: uuid.New().String(),
|
||||||
|
Label: "test",
|
||||||
|
}
|
||||||
|
btrfsDevices := &MkfsBtrfsStageDevices{Device: *device}
|
||||||
|
mkbtrfs := NewMkfsBtrfsStage(btrfsOptions, btrfsDevices)
|
||||||
|
mkbtrfsExpected := &Stage{
|
||||||
|
Type: "org.osbuild.mkfs.btrfs",
|
||||||
|
Options: btrfsOptions,
|
||||||
|
Devices: btrfsDevices,
|
||||||
|
}
|
||||||
|
assert.Equal(t, mkbtrfsExpected, mkbtrfs)
|
||||||
|
|
||||||
|
ext4Options := &MkfsExt4StageOptions{
|
||||||
|
UUID: uuid.New().String(),
|
||||||
|
Label: "test",
|
||||||
|
}
|
||||||
|
ext4Devices := &MkfsExt4StageDevices{Device: *device}
|
||||||
|
mkext4 := NewMkfsExt4Stage(ext4Options, ext4Devices)
|
||||||
|
mkext4Expected := &Stage{
|
||||||
|
Type: "org.osbuild.mkfs.ext4",
|
||||||
|
Options: ext4Options,
|
||||||
|
Devices: ext4Devices,
|
||||||
|
}
|
||||||
|
assert.Equal(t, mkext4Expected, mkext4)
|
||||||
|
|
||||||
|
fatOptions := &MkfsFATStageOptions{
|
||||||
|
VolID: "7B7795E7",
|
||||||
|
Label: "test",
|
||||||
|
FATSize: common.IntToPtr(12),
|
||||||
|
}
|
||||||
|
fatDevices := &MkfsFATStageDevices{Device: *device}
|
||||||
|
mkfat := NewMkfsFATStage(fatOptions, fatDevices)
|
||||||
|
mkfatExpected := &Stage{
|
||||||
|
Type: "org.osbuild.mkfs.fat",
|
||||||
|
Options: fatOptions,
|
||||||
|
Devices: fatDevices,
|
||||||
|
}
|
||||||
|
assert.Equal(t, mkfatExpected, mkfat)
|
||||||
|
|
||||||
|
xfsOptions := &MkfsXfsStageOptions{
|
||||||
|
UUID: uuid.New().String(),
|
||||||
|
Label: "test",
|
||||||
|
}
|
||||||
|
xfsDevices := &MkfsXfsStageDevices{Device: *device}
|
||||||
|
mkxfs := NewMkfsXfsStage(xfsOptions, xfsDevices)
|
||||||
|
mkxfsExpected := &Stage{
|
||||||
|
Type: "org.osbuild.mkfs.xfs",
|
||||||
|
Options: xfsOptions,
|
||||||
|
Devices: xfsDevices,
|
||||||
|
}
|
||||||
|
assert.Equal(t, mkxfsExpected, mkxfs)
|
||||||
|
}
|
||||||
51
internal/osbuild2/mount_test.go
Normal file
51
internal/osbuild2/mount_test.go
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
package osbuild2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewMounts(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
{ // btrfs
|
||||||
|
actual := NewBtrfsMount("/dev/sda1", "/mnt/btrfs")
|
||||||
|
expected := &Mount{
|
||||||
|
Type: "org.osbuild.btrfs",
|
||||||
|
Source: "/dev/sda1",
|
||||||
|
Target: "/mnt/btrfs",
|
||||||
|
}
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // ext4
|
||||||
|
actual := NewExt4Mount("/dev/sda2", "/mnt/ext4")
|
||||||
|
expected := &Mount{
|
||||||
|
Type: "org.osbuild.ext4",
|
||||||
|
Source: "/dev/sda2",
|
||||||
|
Target: "/mnt/ext4",
|
||||||
|
}
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // fat
|
||||||
|
actual := NewFATMount("/dev/sda3", "/mnt/fat")
|
||||||
|
expected := &Mount{
|
||||||
|
Type: "org.osbuild.fat",
|
||||||
|
Source: "/dev/sda3",
|
||||||
|
Target: "/mnt/fat",
|
||||||
|
}
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // xfs
|
||||||
|
actual := NewXfsMount("/dev/sda4", "/mnt/xfs")
|
||||||
|
expected := &Mount{
|
||||||
|
Type: "org.osbuild.xfs",
|
||||||
|
Source: "/dev/sda4",
|
||||||
|
Target: "/mnt/xfs",
|
||||||
|
}
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
20
internal/osbuild2/truncate_stage_test.go
Normal file
20
internal/osbuild2/truncate_stage_test.go
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
package osbuild2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewTruncateStage(t *testing.T) {
|
||||||
|
options := TruncateStageOptions{
|
||||||
|
Filename: "image.raw",
|
||||||
|
Size: "42G",
|
||||||
|
}
|
||||||
|
expectedStage := &Stage{
|
||||||
|
Type: "org.osbuild.truncate",
|
||||||
|
Options: &options,
|
||||||
|
}
|
||||||
|
actualStage := NewTruncateStage(&options)
|
||||||
|
assert.Equal(t, expectedStage, actualStage)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue