Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blueprint: fix DiskCustomization.MinSize with existing pattern #1055

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions pkg/blueprint/disk_customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ type DiskCustomization struct {
Partitions []PartitionCustomization `json:"partitions,omitempty" toml:"partitions,omitempty"`
}

func (dc *DiskCustomization) UnmarshalJSON(data []byte) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meh, this lacks the toml unmarshaler :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also needs some error tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably can close this in favor of #1057 which archives this with less code (but some boilerplate).

var dcAnySize struct {
MinSize any `json:"minsize,omitempty" toml:"minsize,omitempty"`
Partitions []PartitionCustomization `json:"partitions,omitempty" toml:"partitions,omitempty"`
}
if err := json.Unmarshal(data, &dcAnySize); err != nil {
return err
}

dc.Partitions = dcAnySize.Partitions

if dcAnySize.MinSize != nil {
size, err := decodeSize(dcAnySize.MinSize)
if err != nil {
return err
}
dc.MinSize = size
}
return nil
}

// 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
52 changes: 51 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,51 @@ func TestPartitionCustomizationUnmarshalTOML(t *testing.T) {
})
}
}

func TestDiskCustomizationUnmarshalJSON(t *testing.T) {
type testCase struct {
input string
expected *blueprint.DiskCustomization
errorMsg string
}

testCases := map[string]testCase{
"nothing": {
input: "{}",
expected: &blueprint.DiskCustomization{
MinSize: 0,
},
},
"minsize/str": {
input: `{
"minsize": "1234"
}`,
expected: &blueprint.DiskCustomization{
MinSize: 1234,
},
},
"minsize/str-with-unit": {
input: `{
"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.input), &dc)
if tc.errorMsg == "" {
require.NoError(t, err)
assert.Equal(t, tc.expected, &dc)
} else {
assert.EqualError(t, err, tc.errorMsg)
}
})
}
}
Loading