diff --git a/pkg/blueprint/disk_customizations.go b/pkg/blueprint/disk_customizations.go index 09b92099fd..4d20ca06cf 100644 --- a/pkg/blueprint/disk_customizations.go +++ b/pkg/blueprint/disk_customizations.go @@ -9,15 +9,37 @@ import ( "slices" "strings" + "github.com/osbuild/images/pkg/datasizes" "github.com/osbuild/images/pkg/pathpolicy" ) type DiskCustomization struct { // TODO: Add partition table type: gpt or dos - MinSize uint64 `json:"minsize,omitempty" toml:"minsize,omitempty"` + MinSize uint64 + Partitions []PartitionCustomization +} + +type diskCustomizationMarshaler struct { + // TODO: Add partition table type: gpt or dos + MinSize datasizes.Size `json:"minsize,omitempty" toml:"minsize,omitempty"` Partitions []PartitionCustomization `json:"partitions,omitempty" toml:"partitions,omitempty"` } +func (dc *DiskCustomization) UnmarshalJSON(data []byte) error { + var dcm diskCustomizationMarshaler + if err := json.Unmarshal(data, &dcm); err != nil { + return err + } + dc.MinSize = dcm.MinSize.Uint64() + dc.Partitions = dcm.Partitions + + return nil +} + +func (dc *DiskCustomization) UnmarshalTOML(data any) error { + return unmarshalTOMLviaJSON(dc, data) +} + // PartitionCustomization defines a single partition on a disk. The Type // defines the kind of "payload" for the partition: plain, lvm, or btrfs. // - plain: the payload will be a filesystem on a partition (e.g. xfs, ext4). diff --git a/pkg/blueprint/disk_customizations_test.go b/pkg/blueprint/disk_customizations_test.go index 980190015b..45a3924a93 100644 --- a/pkg/blueprint/disk_customizations_test.go +++ b/pkg/blueprint/disk_customizations_test.go @@ -5,10 +5,12 @@ import ( "testing" "github.com/BurntSushi/toml" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/osbuild/images/pkg/blueprint" "github.com/osbuild/images/pkg/datasizes" "github.com/osbuild/images/pkg/pathpolicy" - "github.com/stretchr/testify/assert" ) func TestPartitioningValidation(t *testing.T) { @@ -1648,3 +1650,62 @@ func TestPartitionCustomizationUnmarshalTOML(t *testing.T) { }) } } + +func TestDiskCustomizationUnmarshalJSON(t *testing.T) { + type testCase struct { + inputJSON string + inputTOML string + expected *blueprint.DiskCustomization + } + + testCases := map[string]testCase{ + "nothing": { + inputJSON: "{}", + inputTOML: "", + expected: &blueprint.DiskCustomization{ + MinSize: 0, + }, + }, + "minsize/int": { + inputJSON: `{ + "minsize": 1234 + }`, + inputTOML: "minsize = 1234", + expected: &blueprint.DiskCustomization{ + MinSize: 1234, + }, + }, + "minsize/str": { + inputJSON: `{ + "minsize": "1234" + }`, + inputTOML: `minsize = "1234"`, + expected: &blueprint.DiskCustomization{ + MinSize: 1234, + }, + }, + "minsize/str-with-unit": { + inputJSON: `{ + "minsize": "1 GiB" + }`, + inputTOML: `minsize = "1 GiB"`, + expected: &blueprint.DiskCustomization{ + MinSize: 1 * datasizes.GiB, + }, + }, + } + + for name := range testCases { + tc := testCases[name] + t.Run(name, func(t *testing.T) { + var dc blueprint.DiskCustomization + + err := json.Unmarshal([]byte(tc.inputJSON), &dc) + require.NoError(t, err) + assert.Equal(t, tc.expected, &dc) + err = toml.Unmarshal([]byte(tc.inputTOML), &dc) + require.NoError(t, err) + assert.Equal(t, tc.expected, &dc) + }) + } +}