Skip to content

Commit

Permalink
osbuild,internal: create new MakeFakePartitionTable helper
Browse files Browse the repository at this point in the history
Both the osbuild and the image package need/will need a helper
to make fake partition tables. So this creates a new `testdisk` package
that contains a helper for this. It can not be part of the existing
`test` package because of circular imports.
  • Loading branch information
mvo5 authored and achilleas-k committed Jan 18, 2024
1 parent 537ead4 commit b4905b5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 42 deletions.
34 changes: 34 additions & 0 deletions internal/testdisk/partition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package testdisk

import (
"github.com/osbuild/images/pkg/disk"
)

// MakeFakePartitionTable is a helper to create partition table structs
// for tests. It uses sensible defaults for common scenarios.
func MakeFakePartitionTable(mntPoints ...string) *disk.PartitionTable {
var partitions []disk.Partition
for _, mntPoint := range mntPoints {
payload := &disk.Filesystem{
Type: "ext4",
Mountpoint: mntPoint,
}
switch mntPoint {
case "/":
payload.UUID = disk.RootPartitionUUID
case "/boot/efi":
payload.UUID = disk.EFIFilesystemUUID
payload.Type = "vfat"
default:
payload.UUID = disk.FilesystemDataUUID
}
partitions = append(partitions, disk.Partition{
Payload: payload,
})

}
return &disk.PartitionTable{
Type: "gpt",
Partitions: partitions,
}
}
19 changes: 2 additions & 17 deletions pkg/image/bootc_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/internal/testdisk"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/disk"
"github.com/osbuild/images/pkg/image"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/platform"
Expand Down Expand Up @@ -43,21 +43,6 @@ func makeFakePlatform() platform.Platform {
}
}

func makeFakePartitionTable() *disk.PartitionTable {
return &disk.PartitionTable{
Type: "gpt",
Partitions: []disk.Partition{
{
Payload: &disk.Filesystem{
Type: "ext4",
UUID: disk.RootPartitionUUID,
Mountpoint: "/",
},
},
},
}
}

func TestBootcDiskImageInstantiateNoBuildpipelineForQcow2(t *testing.T) {
containerSource := container.SourceSpec{
Source: "some-src",
Expand All @@ -68,7 +53,7 @@ func TestBootcDiskImageInstantiateNoBuildpipelineForQcow2(t *testing.T) {
img := image.NewBootcDiskImage(containerSource)
require.NotNil(t, img)
img.Platform = makeFakePlatform()
img.PartitionTable = makeFakePartitionTable()
img.PartitionTable = testdisk.MakeFakePartitionTable("/")

m := &manifest.Manifest{}
runi := &runner.Fedora{}
Expand Down
29 changes: 4 additions & 25 deletions pkg/osbuild/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/internal/testdisk"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/disk"
)
Expand Down Expand Up @@ -170,7 +171,7 @@ func TestPathEscape(t *testing.T) {

func TestMountsDeviceFromPtEmptyErrors(t *testing.T) {
filename := "fake-disk.img"
fakePt := &disk.PartitionTable{}
fakePt := testdisk.MakeFakePartitionTable()
fsRootMntName, mounts, devices, err := genMountsDevicesFromPt(filename, fakePt)
assert.ErrorContains(t, err, "no mount found for the filesystem root")
assert.Equal(t, fsRootMntName, "")
Expand All @@ -180,36 +181,14 @@ func TestMountsDeviceFromPtEmptyErrors(t *testing.T) {

func TestMountsDeviceFromPtNoRootErrors(t *testing.T) {
filename := "fake-disk.img"
fakePt := &disk.PartitionTable{
Type: "gpt",
Partitions: []disk.Partition{
{
Payload: &disk.Filesystem{
Type: "ext4",
UUID: disk.RootPartitionUUID,
Mountpoint: "/not-root",
},
},
},
}
fakePt := testdisk.MakeFakePartitionTable("/not-root")
_, _, _, err := genMountsDevicesFromPt(filename, fakePt)
assert.ErrorContains(t, err, "no mount found for the filesystem root")
}

func TestMountsDeviceFromPtHappy(t *testing.T) {
filename := "fake-disk.img"
fakePt := &disk.PartitionTable{
Type: "gpt",
Partitions: []disk.Partition{
{
Payload: &disk.Filesystem{
Type: "ext4",
UUID: disk.RootPartitionUUID,
Mountpoint: "/",
},
},
},
}
fakePt := testdisk.MakeFakePartitionTable("/")
fsRootMntName, mounts, devices, err := genMountsDevicesFromPt(filename, fakePt)
require.Nil(t, err)
assert.Equal(t, fsRootMntName, "-")
Expand Down

0 comments on commit b4905b5

Please sign in to comment.