Skip to content

Commit

Permalink
blueprint: fix DiskCustomization.MinSize using datasize.Size
Browse files Browse the repository at this point in the history
This commit fixes the issue that minsize cannot be a string
by using the existing pattern.

See osbuild#1055 for the alternative
approach.
  • Loading branch information
mvo5 committed Nov 22, 2024
1 parent 718e50c commit bf18923
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
24 changes: 23 additions & 1 deletion pkg/blueprint/disk_customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
63 changes: 62 additions & 1 deletion pkg/blueprint/disk_customizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit bf18923

Please sign in to comment.