Commit 5835ca42 authored by Josh Frye's avatar Josh Frye

Move quoting to else block only for valid vars. Add test case.

parent a667282e
......@@ -170,20 +170,17 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
// Do a check for bad environment variables, such as '=foo', 'foobar'
for _, kv := range p.config.Vars {
for idx, kv := range p.config.Vars {
vs := strings.SplitN(kv, "=", 2)
if len(vs) != 2 || vs[0] == "" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Environment variable not in format 'key=value': %s", kv))
} else {
// Single quote env var values
p.config.Vars[idx] = fmt.Sprintf("%s='%s'", vs[0], vs[1])
}
}
// Single quote env var values
for key := range p.config.Vars {
vs := strings.SplitN(p.config.Vars[key], "=", 2)
p.config.Vars[key] = fmt.Sprintf("%s='%s'", vs[0], vs[1])
}
if p.config.RawStartRetryTimeout != "" {
p.config.startRetryTimeout, err = time.ParseDuration(p.config.RawStartRetryTimeout)
if err != nil {
......
......@@ -199,3 +199,21 @@ func TestProvisionerPrepare_EnvironmentVars(t *testing.T) {
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"}
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)
}
expectedValue = "keytwo='value\ntwo'"
if p.config.Vars[1] != expectedValue {
t.Fatalf("%s should be equal to %s", p.config.Vars[1], expectedValue)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment