From eeeee3ec351a20b6a48cce10e8d1e950ae274af9 Mon Sep 17 00:00:00 2001 From: Erlend Graff Date: Sun, 5 Nov 2017 14:55:56 +0100 Subject: [PATCH] hyper-v/vmcx: add missing InterpolateContext --- builder/hyperv/vmcx/builder.go | 1 + builder/hyperv/vmcx/builder_test.go | 54 ++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index a9583327a..6ba93caac 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -98,6 +98,7 @@ type Config struct { func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { err := config.Decode(&b.config, &config.DecodeOpts{ Interpolate: true, + InterpolateContext: &b.config.ctx, InterpolateFilter: &interpolate.RenderFilter{ Exclude: []string{ "boot_command", diff --git a/builder/hyperv/vmcx/builder_test.go b/builder/hyperv/vmcx/builder_test.go index 209094bf3..8c0be09a4 100644 --- a/builder/hyperv/vmcx/builder_test.go +++ b/builder/hyperv/vmcx/builder_test.go @@ -1,11 +1,13 @@ package vmcx import ( + "fmt" "reflect" "testing" - "fmt" + hypervcommon "github.com/hashicorp/packer/builder/hyperv/common" "github.com/hashicorp/packer/packer" + "github.com/mitchellh/multistep" "io/ioutil" "os" ) @@ -486,3 +488,53 @@ func TestBuilderPrepare_CommConfig(t *testing.T) { } } } + +func TestUserVariablesInBootCommand(t *testing.T) { + var b Builder + config := testConfig() + + //Create vmxc folder + td, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(td) + config["clone_from_vmxc_path"] = td + + config[packer.UserVariablesConfigKey] = map[string]string{"test-variable": "test"} + config["boot_command"] = []string{"blah {{user `test-variable`}} blah"} + + warns, err := b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + ui := packer.TestUi(t) + cache := &packer.FileCache{CacheDir: os.TempDir()} + hook := &packer.MockHook{} + driver := &hypervcommon.DriverMock{} + + // Set up the state. + state := new(multistep.BasicStateBag) + state.Put("cache", cache) + state.Put("config", &b.config) + state.Put("driver", driver) + state.Put("hook", hook) + state.Put("http_port", uint(0)) + state.Put("ui", ui) + state.Put("vmName", "packer-foo") + + step := &hypervcommon.StepTypeBootCommand{ + BootCommand: b.config.BootCommand, + SwitchName: b.config.SwitchName, + Ctx: b.config.ctx, + } + + ret := step.Run(state) + if ret != multistep.ActionContinue { + t.Fatalf("should not have error: %s", ret) + } +}