Merge pull request #4328 from DanHam/gh-3108

Allow equals sign in value of environment variable for Powershell provisioner
This commit is contained in:
Matthew Hooker 2017-01-03 12:38:17 -08:00 committed by GitHub
commit 6d6c7864b4
2 changed files with 57 additions and 2 deletions

View File

@ -347,7 +347,7 @@ func (p *Provisioner) createFlattenedEnvVars(elevated bool) (flattened string, e
// 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))

View File

@ -280,12 +280,36 @@ 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'",
}
p := new(Provisioner)
p.Prepare(config)
@ -308,6 +332,16 @@ func TestProvisionerQuote_EnvironmentVars(t *testing.T) {
if p.config.Vars[3] != expectedValue {
t.Fatalf("%s should be equal to %s", p.config.Vars[3], expectedValue)
}
expectedValue = "keyfive='value=five'"
if p.config.Vars[4] != expectedValue {
t.Fatalf("%s should be equal to %s", p.config.Vars[4], expectedValue)
}
expectedValue = "keysix='=six'"
if p.config.Vars[5] != expectedValue {
t.Fatalf("%s should be equal to %s", p.config.Vars[5], expectedValue)
}
}
func testUi() *packer.BasicUi {
@ -572,6 +606,27 @@ func TestProvisioner_createFlattenedElevatedEnvVars_windows(t *testing.T) {
if flattenedEnvVars != `$env:BAZ="qux"; $env:FOO="bar"; $env:PACKER_BUILDER_TYPE="iso"; $env:PACKER_BUILD_NAME="vmware"; ` {
t.Fatalf("unexpected flattened env vars: %s", flattenedEnvVars)
}
// Environment variable with value containing equals
p.config.Vars = []string{"FOO=bar=baz"}
flattenedEnvVars, err = p.createFlattenedEnvVars(true)
if err != nil {
t.Fatalf("should not have error creating flattened env vars: %s", err)
}
if flattenedEnvVars != `$env:FOO="bar=baz"; $env:PACKER_BUILDER_TYPE="iso"; $env:PACKER_BUILD_NAME="vmware"; ` {
t.Fatalf("unexpected flattened env vars: %s", flattenedEnvVars)
}
// Environment variable with value starting with equals
p.config.Vars = []string{"FOO==baz"}
flattenedEnvVars, err = p.createFlattenedEnvVars(true)
if err != nil {
t.Fatalf("should not have error creating flattened env vars: %s", err)
}
if flattenedEnvVars != `$env:FOO="=baz"; $env:PACKER_BUILDER_TYPE="iso"; $env:PACKER_BUILD_NAME="vmware"; ` {
t.Fatalf("unexpected flattened env vars: %s", flattenedEnvVars)
}
}
func TestProvisioner_createFlattenedEnvVars_windows(t *testing.T) {