From 3b3912bae10672b54b05933d7f047e9761e35084 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 22 Nov 2024 08:43:52 +0100 Subject: [PATCH] blueprint: add test for disk customization min-size The disk customizations min-size setting is not accepting strings or strings with units. This commit adds a failing test that will validate the fix. --- pkg/blueprint/disk_customizations_test.go | 52 ++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/pkg/blueprint/disk_customizations_test.go b/pkg/blueprint/disk_customizations_test.go index 980190015b..01cee76f1d 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,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) + } + }) + } +}