command/fix: add fixer to auto set guest_additions_mode
This commit is contained in:
parent
aee93096b1
commit
03321c7cdb
|
@ -53,6 +53,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
|
|||
fixers := []string{
|
||||
"iso-md5",
|
||||
"createtime",
|
||||
"virtualbox-gaattach",
|
||||
}
|
||||
|
||||
input := templateData
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue