From 03321c7cdb40ca1c2e04fcd6da55b7b50d3c8531 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 2 Nov 2013 12:43:57 +0400 Subject: [PATCH] command/fix: add fixer to auto set guest_additions_mode --- command/fix/command.go | 1 + command/fix/fixer.go | 5 +- command/fix/fixer_virtualbox_gaattach.go | 59 +++++++++++++ command/fix/fixer_virtualbox_gaattach_test.go | 88 +++++++++++++++++++ 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 command/fix/fixer_virtualbox_gaattach.go create mode 100644 command/fix/fixer_virtualbox_gaattach_test.go diff --git a/command/fix/command.go b/command/fix/command.go index e77a928bb..5de003cea 100644 --- a/command/fix/command.go +++ b/command/fix/command.go @@ -53,6 +53,7 @@ func (c Command) Run(env packer.Environment, args []string) int { fixers := []string{ "iso-md5", "createtime", + "virtualbox-gaattach", } input := templateData diff --git a/command/fix/fixer.go b/command/fix/fixer.go index 58e172800..a1980d432 100644 --- a/command/fix/fixer.go +++ b/command/fix/fixer.go @@ -13,7 +13,8 @@ var Fixers map[string]Fixer func init() { Fixers = map[string]Fixer{ - "iso-md5": new(FixerISOMD5), - "createtime": new(FixerCreateTime), + "iso-md5": new(FixerISOMD5), + "createtime": new(FixerCreateTime), + "virtualbox-gaattach": new(FixerVirtualBoxGAAttach), } } diff --git a/command/fix/fixer_virtualbox_gaattach.go b/command/fix/fixer_virtualbox_gaattach.go new file mode 100644 index 000000000..6341394c8 --- /dev/null +++ b/command/fix/fixer_virtualbox_gaattach.go @@ -0,0 +1,59 @@ +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 +} diff --git a/command/fix/fixer_virtualbox_gaattach_test.go b/command/fix/fixer_virtualbox_gaattach_test.go new file mode 100644 index 000000000..f7e8285e1 --- /dev/null +++ b/command/fix/fixer_virtualbox_gaattach_test.go @@ -0,0 +1,88 @@ +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) + } + } +}