From 3cde601e1bdb284df35dc299953b790b66d0db30 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Mon, 4 Nov 2024 17:53:03 +0100 Subject: [PATCH] disk: add tests for PartitionTableType and FSType enums --- pkg/disk/enums_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 pkg/disk/enums_test.go diff --git a/pkg/disk/enums_test.go b/pkg/disk/enums_test.go new file mode 100644 index 000000000..eb5cba638 --- /dev/null +++ b/pkg/disk/enums_test.go @@ -0,0 +1,64 @@ +package disk_test + +import ( + "testing" + + "github.com/osbuild/images/pkg/disk" + "github.com/stretchr/testify/assert" +) + +func TestEnumPartitionTableType(t *testing.T) { + enumMap := map[string]uint64{ + "": 0, + "dos": 1, + "gpt": 2, + } + + assert := assert.New(t) + for name, num := range enumMap { + ptt, err := disk.NewPartitionTableType(name) + expected := disk.PartitionTableType(num) + + assert.NoError(err) + assert.Equal(expected, ptt) + + assert.Equal(name, ptt.String()) + } + + // error test: bad value + badPtt := disk.PartitionTableType(3) + assert.PanicsWithValue("unknown or unsupported partition table type with enum value 3", func() { _ = badPtt.String() }) + + // error test: bad name + _, err := disk.NewPartitionTableType("not-a-type") + assert.EqualError(err, "unknown or unsupported partition table type name: not-a-type") +} + +func TestEnumFSType(t *testing.T) { + enumMap := map[string]uint64{ + "": 0, + "vfat": 1, + "ext4": 2, + "xfs": 3, + "btrfs": 4, + } + + assert := assert.New(t) + for name, num := range enumMap { + fst, err := disk.NewFSType(name) + expected := disk.FSType(num) + + assert.NoError(err) + assert.Equal(expected, fst) + + assert.Equal(name, fst.String()) + } + + // error test: bad value + badFst := disk.FSType(5) + assert.PanicsWithValue("unknown or unsupported filesystem type with enum value 5", func() { _ = badFst.String() }) + + // error test: bad name + _, err := disk.NewFSType("not-a-type") + assert.EqualError(err, "unknown or unsupported filesystem type name: not-a-type") +}