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 filesystem customization parsing #885

Merged
merged 3 commits into from
Sep 2, 2024
Merged
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
8 changes: 4 additions & 4 deletions pkg/blueprint/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ version = "2.4.*"

[[customizations.filesystem]]
mountpoint = "/var"
size = 2147483648
minsize = 2147483648
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume there is no risk here that there are "wrong" blueprint tomls out there that we would break with this change?

Copy link
Member

Choose a reason for hiding this comment

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

Those cases are handled by the custom unmarshaller in osbuild-composer before being sent through to the code here in images.


[[customizations.filesystem]]
mountpoint = "/opt"
size = "20 GB"
minsize = "20 GB"
mvo5 marked this conversation as resolved.
Show resolved Hide resolved
`

var bp Blueprint
err := toml.Unmarshal([]byte(blueprint), &bp)
require.Nil(t, err)
require.NoError(t, err)
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)
Expand All @@ -49,7 +49,7 @@ size = "20 GB"
}
}`
err = json.Unmarshal([]byte(blueprint), &bp)
require.Nil(t, err)
require.NoError(t, err)
assert.Equal(t, bp.Name, "test")
assert.Equal(t, "/opt", bp.Customizations.Filesystem[0].Mountpoint)
assert.Equal(t, uint64(20*common.GiB), bp.Customizations.Filesystem[0].MinSize)
Expand Down
19 changes: 9 additions & 10 deletions pkg/blueprint/filesystem_customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ func (fsc *FilesystemCustomization) UnmarshalTOML(data interface{}) error {
return fmt.Errorf("TOML unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"])
}

switch d["size"].(type) {
switch d["minsize"].(type) {
mvo5 marked this conversation as resolved.
Show resolved Hide resolved
case int64:
fsc.MinSize = uint64(d["size"].(int64))
fsc.MinSize = uint64(d["minsize"].(int64))
case string:
size, err := common.DataSizeToUint64(d["size"].(string))
minSize, err := common.DataSizeToUint64(d["minsize"].(string))
if err != nil {
return fmt.Errorf("TOML unmarshal: size is not valid filesystem size (%w)", err)
return fmt.Errorf("TOML unmarshal: minsize is not valid filesystem size (%w)", err)
}
fsc.MinSize = size
fsc.MinSize = minSize
default:
return fmt.Errorf("TOML unmarshal: size must be integer or string, got %v of type %T", d["size"], d["size"])
return fmt.Errorf("TOML unmarshal: minsize must be integer or string, got %v of type %T", d["minsize"], d["minsize"])
}

return nil
Expand All @@ -57,14 +57,13 @@ func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error {
// The JSON specification only mentions float64 and Go defaults to it: https://go.dev/blog/json
switch d["minsize"].(type) {
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))
minSize, err := common.DataSizeToUint64(d["minsize"].(string))
if err != nil {
return fmt.Errorf("JSON unmarshal: size is not valid filesystem size (%w)", err)
return fmt.Errorf("JSON unmarshal: minsize is not valid filesystem size (%w)", err)
}
fsc.MinSize = size
fsc.MinSize = minSize
default:
return fmt.Errorf("JSON unmarshal: minsize must be float64 number or string, got %v of type %T", d["minsize"], d["minsize"])
}
Expand Down
70 changes: 70 additions & 0 deletions pkg/blueprint/filesystem_customizations_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,83 @@
package blueprint

import (
"encoding/json"
"testing"

"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"

"github.com/osbuild/images/pkg/pathpolicy"
)

// Happy tests for unmarshallers are in blueprint_test.go
func TestFilesystemCustomizationUnmarshalTOMLUnhappy(t *testing.T) {
mvo5 marked this conversation as resolved.
Show resolved Hide resolved
cases := []struct {
name string
input string
err string
}{
{
name: "mountpoint not string",
input: `mountpoint = 42
minsize = 42`,
err: "toml: line 0: TOML unmarshal: mountpoint must be string, got 42 of type int64",
},
{
name: "misize nor string nor int",
input: `mountpoint="/"
minsize = true`,
err: "toml: line 0: TOML unmarshal: minsize must be integer or string, got true of type bool",
},
{
name: "misize not parseable",
input: `mountpoint="/"
minsize = "20 KG"`,
err: "toml: line 0: TOML unmarshal: minsize is not valid filesystem size (unknown data size units in string: 20 KG)",
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var fsc FilesystemCustomization
err := toml.Unmarshal([]byte(c.input), &fsc)
assert.EqualError(t, err, c.err)
})
}
}

func TestFilesystemCustomizationUnmarshalJSONUnhappy(t *testing.T) {
cases := []struct {
name string
input string
err string
}{
{
name: "mountpoint not string",
input: `{"mountpoint": 42, "minsize": 42}`,
err: "JSON unmarshal: mountpoint must be string, got 42 of type float64",
},
{
name: "misize nor string nor int",
input: `{"mountpoint":"/", "minsize": true}`,
err: "JSON unmarshal: minsize must be float64 number or string, got true of type bool",
},
{
name: "misize not parseable",
input: `{ "mountpoint": "/", "minsize": "20 KG"}`,
err: "JSON unmarshal: minsize is not valid filesystem size (unknown data size units in string: 20 KG)",
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var fsc FilesystemCustomization
err := json.Unmarshal([]byte(c.input), &fsc)
assert.EqualError(t, err, c.err)
})
}
}

func TestCheckMountpointsPolicy(t *testing.T) {
policy := pathpolicy.NewPathPolicies(map[string]pathpolicy.PathPolicy{
"/": {Exact: true},
Expand Down
Loading