Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #176 from cHYzZQo/env-vars
Browse files Browse the repository at this point in the history
docker-runtime: Use single quotes in bash env file template
  • Loading branch information
sargun committed Oct 3, 2018
2 parents 741621e + b547610 commit 3a71951
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
8 changes: 6 additions & 2 deletions executor/runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const (
const envFileTemplateStr = `
# This file was autogenerated by the titus executor
{{ range $key, $val := . }}
export {{ $key }}="{{ $val }}"
export {{ $key }}='{{ $val | escape_sq }}'
{{ end }}
`

Expand Down Expand Up @@ -187,8 +187,12 @@ func GenerateConfiguration(args []string) (*Config, error) {
return cfg, app.Run(args)
}

func escapeSQ(str string) string {
return strings.Replace(str, "'", "'\"'\"'", -1)
}

var (
envFileTemplate = template.Must(template.New("").Parse(envFileTemplateStr))
envFileTemplate = template.Must(template.New("").Funcs(template.FuncMap{"escape_sq": escapeSQ}).Parse(envFileTemplateStr))
)

// NoEntrypointError indicates that the Titus job does not have an entrypoint, or command
Expand Down
43 changes: 43 additions & 0 deletions executor/runtime/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,49 @@ func TestWriteTitusEnvironmentComplicatedVariable(t *testing.T) {
assert.Equal(t, "", buf.String())
}

func TestEnvFileTemplate(t *testing.T) {
var buf bytes.Buffer
env := map[string]string{"NORMAL": "normal"}
assert.NoError(t, envFileTemplate.Execute(&buf, env))
assert.Equal(t, `
# This file was autogenerated by the titus executor
export NORMAL='normal'
`, buf.String())

buf.Reset()
env = map[string]string{"JSON": `{"json": "value"}`}
assert.NoError(t, envFileTemplate.Execute(&buf, env))
assert.Equal(t, `
# This file was autogenerated by the titus executor
export JSON='{"json": "value"}'
`, buf.String())

buf.Reset()
env = map[string]string{"NL": "new\nline"}
assert.NoError(t, envFileTemplate.Execute(&buf, env))
assert.Equal(t, `
# This file was autogenerated by the titus executor
export NL='new
line'
`, buf.String())

buf.Reset()
env = map[string]string{"SQ": "I'm an env var"}
assert.NoError(t, envFileTemplate.Execute(&buf, env))
assert.Equal(t, `
# This file was autogenerated by the titus executor
export SQ='I'"'"'m an env var'
`, buf.String())
}

func TestValidEnvironmentKeys(t *testing.T) {
assert.True(t, environmentVariableKeyRegexp.MatchString("HELLO"))
assert.True(t, environmentVariableKeyRegexp.MatchString("HELLO1"))
Expand Down

0 comments on commit 3a71951

Please sign in to comment.