Skip to content

Commit

Permalink
disk: make PartitionTableType a string for otk-*
Browse files Browse the repository at this point in the history
This commit switches the `PartitionTableType` from an enum to a
string. The reason is that the `otk` utils serialize this type
as a JSON. While the json is strictly internal and we make no
gurantees about it is still nicer to keep the strings because
otherwise the diff in the json looks something like:
```json
@@ -147,7 +147,7 @@ func TestUnmarshalOutput(t *testing.T) {
       "partition-table": {
         "Size": 911,
         "UUID": "",
-        "Type": "gpt",
+        "Type": 2,
         "Partitions": [
           {
             "Start": 0,
```
a perversing slightly easier dumability seems worth it (plus less
test code in cmd/otk/* needs to change).
  • Loading branch information
mvo5 committed Nov 7, 2024
1 parent 508729c commit 7086d75
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cmd/otk/osbuild-gen-partition-table/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestUnmarshalOutput(t *testing.T) {
"partition-table": {
"Size": 911,
"UUID": "",
"Type": 0,
"Type": "",
"Partitions": [
{
"Start": 0,
Expand Down Expand Up @@ -227,7 +227,7 @@ var expectedSimplePartOutput = `{
"partition-table": {
"Size": 11821645824,
"UUID": "dbd21911-1c4e-4107-8a9f-14fe6e751358",
"Type": 2,
"Type": "gpt",
"Partitions": [
{
"Start": 9048576,
Expand Down
36 changes: 15 additions & 21 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,39 +119,33 @@ func NewFSType(s string) (FSType, error) {
}
}

// PartitionTableType is the partition table type enum.
type PartitionTableType uint64
// PartitionTableType is the partition table type
type PartitionTableType string

const (
PT_NONE PartitionTableType = iota
PT_DOS
PT_GPT
PT_NONE PartitionTableType = ""
PT_DOS PartitionTableType = "dos"
PT_GPT PartitionTableType = "gpt"
)

func (t PartitionTableType) String() string {
switch t {
func NewPartitionTableType(s string) (PartitionTableType, error) {
switch PartitionTableType(s) {
case PT_NONE:
return ""
return PT_NONE, nil
case PT_DOS:
return "dos"
return PT_DOS, nil
case PT_GPT:
return "gpt"
return PT_GPT, nil
default:
panic(fmt.Sprintf("unknown or unsupported partition table type with enum value %d", t))
return PT_NONE, fmt.Errorf("unknown or unsupported partition table type name: %s", s)
}
}

func NewPartitionTableType(s string) (PartitionTableType, error) {
switch s {
case "":
return PT_NONE, nil
case "dos":
return PT_DOS, nil
case "gpt":
return PT_GPT, nil
default:
return PT_NONE, fmt.Errorf("unknown or unsupported partition table type name: %s", s)
func (t PartitionTableType) String() string {
if _, err := NewPartitionTableType(string(t)); err != nil {
panic(fmt.Sprintf("unknown or unsupported partition table type with value: %v", string(t)))
}
return string(t)
}

// Entity is the base interface for all disk-related entities.
Expand Down
4 changes: 2 additions & 2 deletions pkg/disk/enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestEnumPartitionTableType(t *testing.T) {
}

// error test: bad value
badPtt := disk.PartitionTableType(3)
assert.PanicsWithValue("unknown or unsupported partition table type with enum value 3", func() { _ = badPtt.String() })
badPtt := disk.PartitionTableType("bad")
assert.PanicsWithValue("unknown or unsupported partition table type with value: bad", func() { _ = badPtt.String() })

// error test: bad name
_, err := disk.NewPartitionTableType("not-a-type")
Expand Down
4 changes: 2 additions & 2 deletions pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ func mkBIOSBoot(ptType PartitionTableType) (Partition, error) {
case PT_GPT:
partType = BIOSBootPartitionGUID
default:
return Partition{}, fmt.Errorf("error creating BIOS boot partition: unknown or unsupported partition table enum: %d", ptType)
return Partition{}, fmt.Errorf("error creating BIOS boot partition: unknown or unsupported partition table type: %q", string(ptType))
}
return Partition{
Size: 1 * datasizes.MiB,
Expand All @@ -1127,7 +1127,7 @@ func mkESP(size uint64, ptType PartitionTableType) (Partition, error) {
case PT_GPT:
partType = EFISystemPartitionGUID
default:
return Partition{}, fmt.Errorf("error creating EFI system partition: unknown or unsupported partition table enum: %d", ptType)
return Partition{}, fmt.Errorf("error creating EFI system partition: unknown or unsupported partition table type: %q", string(ptType))
}
return Partition{
Size: size,
Expand Down
12 changes: 6 additions & 6 deletions pkg/disk/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,19 +1185,19 @@ func TestAddPartitionsForBootMode(t *testing.T) {
},
},
"bad-pttype-bios": {
pt: disk.PartitionTable{Type: disk.PartitionTableType(911)},
pt: disk.PartitionTable{Type: disk.PartitionTableType("bad")},
bootMode: platform.BOOT_LEGACY,
errmsg: "error creating BIOS boot partition: unknown or unsupported partition table enum: 911",
errmsg: `error creating BIOS boot partition: unknown or unsupported partition table type: "bad"`,
},
"bad-pttype-uefi": {
pt: disk.PartitionTable{Type: disk.PartitionTableType(911)},
pt: disk.PartitionTable{Type: disk.PartitionTableType("bad")},
bootMode: platform.BOOT_UEFI,
errmsg: "error creating EFI system partition: unknown or unsupported partition table enum: 911",
errmsg: `error creating EFI system partition: unknown or unsupported partition table type: "bad"`,
},
"bad-pttype-hybrid": {
pt: disk.PartitionTable{Type: disk.PartitionTableType(911)},
pt: disk.PartitionTable{Type: disk.PartitionTableType("bad")},
bootMode: platform.BOOT_HYBRID,
errmsg: "error creating BIOS boot partition: unknown or unsupported partition table enum: 911",
errmsg: `error creating BIOS boot partition: unknown or unsupported partition table type: "bad"`,
},
"bad-bootmode": {
pt: disk.PartitionTable{Type: disk.PT_GPT},
Expand Down

0 comments on commit 7086d75

Please sign in to comment.