From c3f54ba5a9650a39f89e1471154b1292db34a9c0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 13 Jun 2015 16:39:12 -0400 Subject: [PATCH] fix: virtualbox rename fixes overrides [GH-1828] --- fix/fixer_virtualbox_rename.go | 40 ++++++++++++++++++++++++--- fix/fixer_virtualbox_rename_test.go | 42 +++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/fix/fixer_virtualbox_rename.go b/fix/fixer_virtualbox_rename.go index 292bf0bf1..34187ac2c 100644 --- a/fix/fixer_virtualbox_rename.go +++ b/fix/fixer_virtualbox_rename.go @@ -8,14 +8,14 @@ import ( type FixerVirtualBoxRename struct{} func (FixerVirtualBoxRename) Fix(input map[string]interface{}) (map[string]interface{}, error) { - // The type we'll decode into; we only care about builders type template struct { - Builders []map[string]interface{} + Builders []map[string]interface{} + Provisioners []interface{} } // Decode the input into our structure, if we can var tpl template - if err := mapstructure.Decode(input, &tpl); err != nil { + if err := mapstructure.WeakDecode(input, &tpl); err != nil { return nil, err } @@ -37,7 +37,39 @@ func (FixerVirtualBoxRename) Fix(input map[string]interface{}) (map[string]inter builder["type"] = "virtualbox-iso" } - input["builders"] = tpl.Builders + for i, raw := range tpl.Provisioners { + var m map[string]interface{} + if err := mapstructure.WeakDecode(raw, &m); err != nil { + // Ignore errors, could be a non-map + continue + } + + raw, ok := m["override"] + if !ok { + continue + } + + var override map[string]interface{} + if err := mapstructure.WeakDecode(raw, &override); err != nil { + return nil, err + } + + if raw, ok := override["virtualbox"]; ok { + override["virtualbox-iso"] = raw + delete(override, "virtualbox") + + // Set the change + m["override"] = override + tpl.Provisioners[i] = m + } + } + + if len(tpl.Builders) > 0 { + input["builders"] = tpl.Builders + } + if len(tpl.Provisioners) > 0 { + input["provisioners"] = tpl.Provisioners + } return input, nil } diff --git a/fix/fixer_virtualbox_rename_test.go b/fix/fixer_virtualbox_rename_test.go index 78b7bccf2..355e5276a 100644 --- a/fix/fixer_virtualbox_rename_test.go +++ b/fix/fixer_virtualbox_rename_test.go @@ -46,3 +46,45 @@ func TestFixerVirtualBoxRename_Fix(t *testing.T) { } } } + +func TestFixerVirtualBoxRenameFix_provisionerOverride(t *testing.T) { + cases := []struct { + Input map[string]interface{} + Expected map[string]interface{} + }{ + { + Input: map[string]interface{}{ + "provisioners": []interface{}{ + map[string]interface{}{ + "override": map[string]interface{}{ + "virtualbox": map[string]interface{}{}, + }, + }, + }, + }, + + Expected: map[string]interface{}{ + "provisioners": []interface{}{ + map[string]interface{}{ + "override": map[string]interface{}{ + "virtualbox-iso": map[string]interface{}{}, + }, + }, + }, + }, + }, + } + + for _, tc := range cases { + var f FixerVirtualBoxRename + + output, err := f.Fix(tc.Input) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !reflect.DeepEqual(output, tc.Expected) { + t.Fatalf("unexpected:\n\n%#v\nexpected:\n\n%#v\n", output, tc.Expected) + } + } +}