diff --git a/internal/blueprint/blueprint_test.go b/internal/blueprint/blueprint_test.go index 21f71a6fc..719ae3ffa 100644 --- a/internal/blueprint/blueprint_test.go +++ b/internal/blueprint/blueprint_test.go @@ -1,6 +1,7 @@ package blueprint import ( + "encoding/json" "testing" "github.com/BurntSushi/toml" @@ -21,6 +22,10 @@ version = "2.4.*" [[customizations.filesystem]] mountpoint = "/var" size = 2147483648 + +[[customizations.filesystem]] +mountpoint = "/opt" +size = "20 GB" ` var bp Blueprint @@ -29,6 +34,23 @@ size = 2147483648 assert.Equal(t, bp.Name, "test") assert.Equal(t, "/var", bp.Customizations.Filesystem[0].Mountpoint) assert.Equal(t, uint64(2147483648), bp.Customizations.Filesystem[0].MinSize) + assert.Equal(t, "/opt", bp.Customizations.Filesystem[1].Mountpoint) + assert.Equal(t, uint64(20*1000*1000*1000), bp.Customizations.Filesystem[1].MinSize) + + blueprint = `{ + "name": "test", + "customizations": { + "filesystem": [{ + "mountpoint": "/opt", + "minsize": "20 GiB" + }] + } + }` + err = json.Unmarshal([]byte(blueprint), &bp) + require.Nil(t, err) + assert.Equal(t, bp.Name, "test") + assert.Equal(t, "/opt", bp.Customizations.Filesystem[0].Mountpoint) + assert.Equal(t, uint64(20*1024*1024*1024), bp.Customizations.Filesystem[0].MinSize) } func TestDeepCopy(t *testing.T) { diff --git a/internal/blueprint/customizations.go b/internal/blueprint/customizations.go index a0768c2dc..58ace04ad 100644 --- a/internal/blueprint/customizations.go +++ b/internal/blueprint/customizations.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "reflect" + + "github.com/osbuild/osbuild-composer/internal/common" ) type Customizations struct { @@ -90,8 +92,14 @@ func (fsc *FilesystemCustomization) UnmarshalTOML(data interface{}) error { switch d["size"].(type) { case int64: fsc.MinSize = uint64(d["size"].(int64)) + case string: + size, err := common.DataSizeToUint64(d["size"].(string)) + if err != nil { + return fmt.Errorf("TOML unmarshal: size is not valid filesystem size (%w)", err) + } + fsc.MinSize = size default: - return fmt.Errorf("TOML unmarshal: size must be integer, got %v of type %T", d["size"], d["size"]) + return fmt.Errorf("TOML unmarshal: size must be integer or string, got %v of type %T", d["size"], d["size"]) } return nil @@ -116,8 +124,14 @@ func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error { case float64: // Note that it uses different key than the TOML version fsc.MinSize = uint64(d["minsize"].(float64)) + case string: + size, err := common.DataSizeToUint64(d["minsize"].(string)) + if err != nil { + return fmt.Errorf("JSON unmarshal: size is not valid filesystem size (%w)", err) + } + fsc.MinSize = size default: - return fmt.Errorf("JSON unmarshal: minsize must be float64 number, got %v of type %T", d["minsize"], d["minsize"]) + return fmt.Errorf("JSON unmarshal: minsize must be float64 number or string, got %v of type %T", d["minsize"], d["minsize"]) } return nil