Commit 03321c7c authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

command/fix: add fixer to auto set guest_additions_mode

parent aee93096
...@@ -53,6 +53,7 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -53,6 +53,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
fixers := []string{ fixers := []string{
"iso-md5", "iso-md5",
"createtime", "createtime",
"virtualbox-gaattach",
} }
input := templateData input := templateData
......
...@@ -15,5 +15,6 @@ func init() { ...@@ -15,5 +15,6 @@ func init() {
Fixers = map[string]Fixer{ Fixers = map[string]Fixer{
"iso-md5": new(FixerISOMD5), "iso-md5": new(FixerISOMD5),
"createtime": new(FixerCreateTime), "createtime": new(FixerCreateTime),
"virtualbox-gaattach": new(FixerVirtualBoxGAAttach),
} }
} }
package fix
import (
"github.com/mitchellh/mapstructure"
)
// FixerVirtualBoxGAAttach changes the "guest_additions_attach" of a template
// to "guest_additions_mode".
type FixerVirtualBoxGAAttach struct{}
func (FixerVirtualBoxGAAttach) 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{}
}
// 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 != "virtualbox" {
continue
}
gaAttachRaw, ok := builder["guest_additions_attach"]
if !ok {
continue
}
gaAttach, ok := gaAttachRaw.(bool)
if !ok {
continue
}
gaMode := "upload"
if gaAttach {
gaMode = "attach"
}
delete(builder, "guest_additions_attach")
builder["guest_additions_mode"] = gaMode
}
input["builders"] = tpl.Builders
return input, nil
}
package fix
import (
"reflect"
"testing"
)
func TestFixerVirtualBoxGAAttach_Impl(t *testing.T) {
var _ Fixer = new(FixerVirtualBoxGAAttach)
}
func TestFixerVirtualBoxGAAttach_Fix(t *testing.T) {
cases := []struct {
Input map[string]interface{}
Expected map[string]interface{}
}{
// No attach field
{
Input: map[string]interface{}{
"type": "virtualbox",
},
Expected: map[string]interface{}{
"type": "virtualbox",
},
},
// Attach field == false
{
Input: map[string]interface{}{
"type": "virtualbox",
"guest_additions_attach": false,
},
Expected: map[string]interface{}{
"type": "virtualbox",
"guest_additions_mode": "upload",
},
},
// Attach field == true
{
Input: map[string]interface{}{
"type": "virtualbox",
"guest_additions_attach": true,
},
Expected: map[string]interface{}{
"type": "virtualbox",
"guest_additions_mode": "attach",
},
},
// Attach field is not a bool
{
Input: map[string]interface{}{
"type": "virtualbox",
"guest_additions_attach": "what",
},
Expected: map[string]interface{}{
"type": "virtualbox",
"guest_additions_attach": "what",
},
},
}
for _, tc := range cases {
var f FixerVirtualBoxGAAttach
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)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment