Merge pull request #8390 from vicyap/f-fixer-for-qemu-disk-size-option
[WIP] Implement fixer to convert qemu disk size type from int to string
This commit is contained in:
commit
ca4bb238f7
|
@ -44,6 +44,7 @@ func init() {
|
|||
"vmware-compaction": new(FixerVMwareCompaction),
|
||||
"clean-image-name": new(FixerCleanImageName),
|
||||
"spot-price-auto-product": new(FixerAmazonSpotPriceProductDeprecation),
|
||||
"qemu-disk-size": new(FixerQEMUDiskSize),
|
||||
}
|
||||
|
||||
FixerOrder = []string{
|
||||
|
@ -71,5 +72,6 @@ func init() {
|
|||
"hyperv-cpu-and-ram",
|
||||
"clean-image-name",
|
||||
"spot-price-auto-product",
|
||||
"qemu-disk-size",
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
// FixerQEMUDiskSize updates disk_size from a string to int for QEMU builders
|
||||
type FixerQEMUDiskSize struct{}
|
||||
|
||||
func (FixerQEMUDiskSize) Fix(input map[string]interface{}) (map[string]interface{}, error) {
|
||||
type template struct {
|
||||
Builders []map[string]interface{}
|
||||
}
|
||||
|
||||
// Decode the input into our structure, if we can
|
||||
var tpl template
|
||||
if err := mapstructure.Decode(input, &tpl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, builder := range tpl.Builders {
|
||||
builderTypeRaw, ok := builder["type"]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
builderType, ok := builderTypeRaw.(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if builderType != "qemu" {
|
||||
continue
|
||||
}
|
||||
|
||||
switch diskSize := builder["disk_size"].(type) {
|
||||
case float64:
|
||||
builder["disk_size"] = strconv.Itoa(int(diskSize)) + "M"
|
||||
case int:
|
||||
builder["disk_size"] = strconv.Itoa(diskSize) + "M"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
input["builders"] = tpl.Builders
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func (FixerQEMUDiskSize) Synopsis() string {
|
||||
return `Updates "disk_size" from int to string in QEMU builders.`
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFixerQEMUDiskSize_impl(t *testing.T) {
|
||||
var _ Fixer = new(FixerQEMUDiskSize)
|
||||
}
|
||||
|
||||
func TestFixerQEMUDiskSize(t *testing.T) {
|
||||
cases := []struct {
|
||||
Input map[string]interface{}
|
||||
Expected map[string]interface{}
|
||||
}{
|
||||
{
|
||||
Input: map[string]interface{}{
|
||||
"type": "qemu",
|
||||
"disk_size": int(40960),
|
||||
},
|
||||
|
||||
Expected: map[string]interface{}{
|
||||
"type": "qemu",
|
||||
"disk_size": "40960M",
|
||||
},
|
||||
},
|
||||
{
|
||||
Input: map[string]interface{}{
|
||||
"type": "qemu",
|
||||
"disk_size": float64(50000),
|
||||
},
|
||||
|
||||
Expected: map[string]interface{}{
|
||||
"type": "qemu",
|
||||
"disk_size": "50000M",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
var f FixerQEMUDiskSize
|
||||
|
||||
input := map[string]interface{}{
|
||||
"builders": []map[string]interface{}{tc.Input},
|
||||
}
|
||||
|
||||
expected := map[string]interface{}{
|
||||
"builders": []map[string]interface{}{tc.Expected},
|
||||
}
|
||||
|
||||
output, err := f.Fix(input)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(output, expected) {
|
||||
t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue