From 49bc2e353f7837086200b3e72ee7c24d023d50d8 Mon Sep 17 00:00:00 2001 From: DanHam Date: Wed, 18 Jan 2017 18:11:20 +0000 Subject: [PATCH 1/2] Fix to allow equals in value of environment variable. Mirrors #4328 --- provisioner/windows-shell/provisioner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provisioner/windows-shell/provisioner.go b/provisioner/windows-shell/provisioner.go index 60d085008..76cae1162 100644 --- a/provisioner/windows-shell/provisioner.go +++ b/provisioner/windows-shell/provisioner.go @@ -301,7 +301,7 @@ func (p *Provisioner) createFlattenedEnvVars() (flattened string, err error) { // Split vars into key/value components for _, envVar := range p.config.Vars { - keyValue := strings.Split(envVar, "=") + keyValue := strings.SplitN(envVar, "=", 2) if len(keyValue) != 2 || keyValue[0] == "" { err = errors.New(fmt.Sprintf("Shell provisioner environment variables must be in key=value format. Currently it is '%s'", envVar)) return From c33cef9c125aa436e03ccbbec5e64cbebd8bda3c Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 18 Jan 2017 12:51:14 -0800 Subject: [PATCH 2/2] tests for equals in windows-shell --- provisioner/windows-shell/provisioner_test.go | 79 ++++++++++++++----- 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/provisioner/windows-shell/provisioner_test.go b/provisioner/windows-shell/provisioner_test.go index 5c4dddd90..5c89e4d8e 100644 --- a/provisioner/windows-shell/provisioner_test.go +++ b/provisioner/windows-shell/provisioner_test.go @@ -207,34 +207,55 @@ func TestProvisionerPrepare_EnvironmentVars(t *testing.T) { if err != nil { t.Fatalf("should not have error: %s", err) } + // Test when the env variable value contains an equals sign + config["environment_vars"] = []string{"good=withequals=true"} + p = new(Provisioner) + err = p.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + // Test when the env variable value starts with an equals sign + config["environment_vars"] = []string{"good==true"} + p = new(Provisioner) + err = p.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } } func TestProvisionerQuote_EnvironmentVars(t *testing.T) { config := testConfig() - config["environment_vars"] = []string{"keyone=valueone", "keytwo=value\ntwo", "keythree='valuethree'", "keyfour='value\nfour'"} + config["environment_vars"] = []string{ + "keyone=valueone", + "keytwo=value\ntwo", + "keythree='valuethree'", + "keyfour='value\nfour'", + "keyfive='value=five'", + "keysix='=six'", + } + + expected := []string{ + + "keyone=valueone", + "keytwo=value\ntwo", + "keythree='valuethree'", + "keyfour='value\nfour'", + "keyfive='value=five'", + "keysix='=six'", + } + p := new(Provisioner) p.Prepare(config) - expectedValue := "keyone=valueone" - if p.config.Vars[0] != expectedValue { - t.Fatalf("%s should be equal to %s", p.config.Vars[0], expectedValue) + for i, expectedValue := range expected { + + if p.config.Vars[i] != expectedValue { + t.Fatalf("%s should be equal to %s", p.config.Vars[0], expectedValue) + } } - expectedValue = "keytwo=value\ntwo" - if p.config.Vars[1] != expectedValue { - t.Fatalf("%s should be equal to %s", p.config.Vars[1], expectedValue) - } - - expectedValue = "keythree='valuethree'" - if p.config.Vars[2] != expectedValue { - t.Fatalf("%s should be equal to %s", p.config.Vars[2], expectedValue) - } - - expectedValue = "keyfour='value\nfour'" - if p.config.Vars[3] != expectedValue { - t.Fatalf("%s should be equal to %s", p.config.Vars[3], expectedValue) - } } func testUi() *packer.BasicUi { @@ -403,6 +424,28 @@ func TestProvisioner_createFlattenedEnvVars_windows(t *testing.T) { if flattenedEnvVars != expectedEnvVars { t.Fatalf("expected flattened env vars to be: %s, got: %s", expectedEnvVars, flattenedEnvVars) } + + // Environment variable with value containing equals. No Packer web server + p.config.Vars = []string{"FOO=bar=baz"} + flattenedEnvVars, err = p.createFlattenedEnvVars() + if err != nil { + t.Fatalf("should not have error creating flattened env vars: %s", err) + } + expectedEnvVars = `set "FOO=bar=baz" && set "PACKER_BUILDER_TYPE=iso" && set "PACKER_BUILD_NAME=vmware" && ` + if flattenedEnvVars != expectedEnvVars { + t.Fatalf("expected flattened env vars to be: %s, got: %s", expectedEnvVars, flattenedEnvVars) + } + + // Environment variable with value starting with equals. No Packer web server + p.config.Vars = []string{"FOO==baz"} + flattenedEnvVars, err = p.createFlattenedEnvVars() + if err != nil { + t.Fatalf("should not have error creating flattened env vars: %s", err) + } + expectedEnvVars = `set "FOO==baz" && set "PACKER_BUILDER_TYPE=iso" && set "PACKER_BUILD_NAME=vmware" && ` + if flattenedEnvVars != expectedEnvVars { + t.Fatalf("expected flattened env vars to be: %s, got: %s", expectedEnvVars, flattenedEnvVars) + } } func TestRetryable(t *testing.T) {