Skip to content

Commit

Permalink
disk: add tests for PartitionTableType and FSType enums
Browse files Browse the repository at this point in the history
  • Loading branch information
achilleas-k committed Nov 4, 2024
1 parent 78d183f commit 6354e01
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pkg/disk/enums_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package disk_test

import (
"fmt"
"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, fmt.Sprintf("unknown or unsupported partition table type name: not-a-type"))

Check failure on line 35 in pkg/disk/enums_test.go

View workflow job for this annotation

GitHub Actions / ⌨ Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
}

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, fmt.Sprintf("unknown or unsupported filesystem type name: not-a-type"))

Check failure on line 64 in pkg/disk/enums_test.go

View workflow job for this annotation

GitHub Actions / ⌨ Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
}

0 comments on commit 6354e01

Please sign in to comment.