From 82f03fca7ca41533012608b074a3bf398772333d Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 13 Jan 2020 15:52:05 -0800 Subject: [PATCH] fix to work with hcl2; update tests --- builder/vsphere/clone/builder.go | 6 +++--- builder/vsphere/clone/config_test.go | 12 ++++++++---- builder/vsphere/iso/builder.go | 7 ++++--- builder/vsphere/iso/config.go | 4 ++-- helper/config/decode.go | 5 +++++ 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/builder/vsphere/clone/builder.go b/builder/vsphere/clone/builder.go index 4c33f76aa..8eb7a2e2a 100644 --- a/builder/vsphere/clone/builder.go +++ b/builder/vsphere/clone/builder.go @@ -19,12 +19,12 @@ type Builder struct { func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() } func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { - warnings, errs := b.config.Prepare() + warnings, errs := b.config.Prepare(raws...) if errs != nil { - return warnings, nil, errs + return nil, warnings, errs } - return warnings, nil, nil + return nil, warnings, nil } func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { diff --git a/builder/vsphere/clone/config_test.go b/builder/vsphere/clone/config_test.go index a7de982c7..a1da7013d 100644 --- a/builder/vsphere/clone/config_test.go +++ b/builder/vsphere/clone/config_test.go @@ -6,7 +6,8 @@ import ( ) func TestCloneConfig_MinimalConfig(t *testing.T) { - _, warns, errs := NewConfig(minimalConfig()) + c := new(Config) + warns, errs := c.Prepare(minimalConfig()) testConfigOk(t, warns, errs) } @@ -15,7 +16,8 @@ func TestCloneConfig_MandatoryParameters(t *testing.T) { for _, param := range params { raw := minimalConfig() raw[param] = "" - _, warns, err := NewConfig(raw) + c := new(Config) + warns, err := c.Prepare(raw) testConfigErr(t, param, warns, err) } } @@ -23,7 +25,8 @@ func TestCloneConfig_MandatoryParameters(t *testing.T) { func TestCloneConfig_Timeout(t *testing.T) { raw := minimalConfig() raw["shutdown_timeout"] = "3m" - conf, warns, err := NewConfig(raw) + conf := new(Config) + warns, err := conf.Prepare(raw) testConfigOk(t, warns, err) if conf.ShutdownConfig.Timeout != 3*time.Minute { t.Fatalf("shutdown_timeout sould be equal 3 minutes, got %v", conf.ShutdownConfig.Timeout) @@ -34,7 +37,8 @@ func TestCloneConfig_RAMReservation(t *testing.T) { raw := minimalConfig() raw["RAM_reservation"] = 1000 raw["RAM_reserve_all"] = true - _, warns, err := NewConfig(raw) + c := new(Config) + warns, err := c.Prepare(raw) testConfigErr(t, "RAM_reservation", warns, err) } diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index 5c33d10f6..d1882fa26 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -2,6 +2,7 @@ package iso import ( "context" + "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/builder/vsphere/common" "github.com/hashicorp/packer/builder/vsphere/driver" @@ -19,12 +20,12 @@ type Builder struct { func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() } func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { - warnings, errs := b.config.Prepare() + warnings, errs := b.config.Prepare(raws...) if errs != nil { - return warnings, nil, errs + return nil, warnings, errs } - return warnings, nil, nil + return nil, warnings, nil } func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { diff --git a/builder/vsphere/iso/config.go b/builder/vsphere/iso/config.go index dd04c8052..e3c297fa4 100644 --- a/builder/vsphere/iso/config.go +++ b/builder/vsphere/iso/config.go @@ -74,8 +74,8 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...) if len(errs.Errors) > 0 { - return nil, errs + return warnings, errs } - return nil, nil + return warnings, nil } diff --git a/helper/config/decode.go b/helper/config/decode.go index 5205bf266..ba022e7e3 100644 --- a/helper/config/decode.go +++ b/helper/config/decode.go @@ -3,6 +3,7 @@ package config import ( "encoding/json" "fmt" + "log" "reflect" "sort" "strings" @@ -158,6 +159,9 @@ func DetectContextData(raws ...interface{}) (map[interface{}]interface{}, []inte // In provisioners, the last value pulled from raws is the placeholder data // for build-specific variables. Pull these out to add to interpolation // context. + if len(raws) == 0 { + return nil, raws + } // Internally, our tests may cause this to be read as a map[string]string placeholderData := raws[len(raws)-1] @@ -201,6 +205,7 @@ func DetectContext(raws ...interface{}) (*interpolate.Context, error) { for _, r := range raws { if err := mapstructure.Decode(r, &s); err != nil { + log.Printf("Error detecting context: %s", err) return nil, err } }