Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
blueprint: fix DiskCustomization.MinSize using datasize.Size
This commit fixes the issue that minsize cannot be a string
by using the existing pattern.

See #1055 for the alternative
approach.
  • Loading branch information
mvo5 committed Nov 22, 2024
commit bf18923198a45e211513442f0c5636c4fc08925f
24 changes: 23 additions & 1 deletion pkg/blueprint/disk_customizations.go
Original file line number Diff line number Diff line change
@@ -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).
63 changes: 62 additions & 1 deletion pkg/blueprint/disk_customizations_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}