Skip to content

Commit

Permalink
fix: virtualbox rename fixes overrides [hashicorpGH-1828]
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jun 13, 2015
1 parent 7eff6b1 commit c3f54ba
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
40 changes: 36 additions & 4 deletions fix/fixer_virtualbox_rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
42 changes: 42 additions & 0 deletions fix/fixer_virtualbox_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit c3f54ba

Please sign in to comment.