Skip to content

Commit

Permalink
disk: update TestPartitionTableFeatures() to catch missing test case
Browse files Browse the repository at this point in the history
Update the TestPartitionTableFeatures() to match other tests that use
the TestPartitionTables.

- Switch to require from assert to stop test execution if something
  fails.  This makes it easier to see the important error message.  With
  assert, execution would continue when `ok` is false, which would make
  the assert.Equal() test print an error too (exp would be an empty
  initialised struct of PartitionTableFeatures) and can make it hard to
  notice that the "expected test result not defined" error was printed
  first.
- Use a map for testCases so we can get each test case by name (the
  order is not important).
- Print the same error message as the other similar tests, using
  t.Name() to refer to the test function name.
- Add plain-noboot test case, which was missing from the test.
  • Loading branch information
achilleas-k committed Nov 29, 2024
1 parent 1934e3e commit dd8fa2c
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions pkg/disk/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/internal/testdisk"
"github.com/osbuild/images/pkg/blueprint"
Expand Down Expand Up @@ -2284,21 +2285,24 @@ func TestNewCustomPartitionTableErrors(t *testing.T) {
}

func TestPartitionTableFeatures(t *testing.T) {
type testCase struct {
partitionType string
expectedFeatures disk.PartitionTableFeatures
}
testCases := []testCase{
{"plain", disk.PartitionTableFeatures{XFS: true, FAT: true}},
{"plain-swap", disk.PartitionTableFeatures{XFS: true, FAT: true, Swap: true}},
{"luks", disk.PartitionTableFeatures{XFS: true, FAT: true, LUKS: true}},
{"luks+lvm", disk.PartitionTableFeatures{XFS: true, FAT: true, LUKS: true, LVM: true}},
{"btrfs", disk.PartitionTableFeatures{XFS: true, FAT: true, Btrfs: true}},
}
require := require.New(t)

for _, tc := range testCases {
pt := testdisk.TestPartitionTables[tc.partitionType]
assert.Equal(t, tc.expectedFeatures, disk.GetPartitionTableFeatures(pt))
testCases := map[string]disk.PartitionTableFeatures{
"plain": {XFS: true, FAT: true},
"plain-noboot": {XFS: true, FAT: true},
"plain-swap": {XFS: true, FAT: true, Swap: true},
"luks": {XFS: true, FAT: true, LUKS: true},
"luks+lvm": {XFS: true, FAT: true, LUKS: true, LVM: true},
"btrfs": {XFS: true, FAT: true, Btrfs: true},
}

for name := range testdisk.TestPartitionTables {
// print an informative failure message if a new test partition
// table is added and this test is not updated (instead of failing
// at the final Equal() check)
exp, ok := testCases[name]
require.True(ok, "expected test result not defined for test partition table %q: please update the %s test", name, t.Name())
pt := testdisk.TestPartitionTables[name]
require.Equal(exp, disk.GetPartitionTableFeatures(pt))
}
}

0 comments on commit dd8fa2c

Please sign in to comment.