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

This commit is contained in:
Josh Frye 2014-10-14 14:20:36 -07:00
parent a667282e00
commit 5835ca42b4
2 changed files with 22 additions and 7 deletions

View File

@ -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 {

View File

@ -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)
}
}