internal/blueprint: allow filesystem size specified with units

Allow users to specify filesystem size with units such as kB, MB, etc.
This commit is contained in:
Martin Sehnoutka 2021-10-18 13:56:37 +02:00 committed by Ondřej Budai
parent f84beee04d
commit 708d985194
2 changed files with 38 additions and 2 deletions

View file

@ -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) {

View file

@ -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