Skip to content

Commit

Permalink
Replace "-" in env var keys with "_"
Browse files Browse the repository at this point in the history
Fixes #2405
  • Loading branch information
schmichael committed Mar 7, 2017
1 parent be1b4c4 commit a9e3f2c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions client/driver/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
t.TaskEnv[k] = v
}

// Clean keys (see #2405)
cleanedEnv := make(map[string]string, len(t.TaskEnv))
for k, v := range t.TaskEnv {
cleanedK := strings.Replace(k, "-", "_", -1)
cleanedEnv[cleanedK] = v
}
t.TaskEnv = cleanedEnv

return t
}

Expand Down
23 changes: 23 additions & 0 deletions client/driver/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,26 @@ func TestEnvironment_AppendHostEnvVars(t *testing.T) {
t.Fatalf("Didn't filter environment variable %q", skip)
}
}

// TestEnvironment_DashesInTaskName asserts dashes in port labels are properly
// converted to underscores in environment variables.
// See: https://github.com/hashicorp/nomad/issues/2405
func TestEnvironment_DashesInTaskName(t *testing.T) {
env := testTaskEnvironment()
env.SetNetworks([]*structs.NetworkResource{
{
Device: "eth0",
DynamicPorts: []structs.Port{
{
Label: "just-some-dashes",
Value: 9000,
},
},
},
})
env.Build()

if env.TaskEnv["NOMAD_PORT_just_some_dashes"] != "9000" {
t.Fatalf("Expected NOMAD_PORT_just_some_dashes=9000 in TaskEnv; found:\n%#v", env.TaskEnv)
}
}
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rm -rf pkg/*
mkdir -p bin/

targets=$TARGETS
if [[ ! -z TARGETS ]]; then
if [ -z TARGETS ]; then
if [[ $(uname) == "Linux" ]]; then
targets="linux_386 linux_amd64 linux_amd64-lxc linux_arm linux_arm64 windows_386 windows_amd64"
elif [[ $(uname) == "Darwin" ]]; then
Expand Down

0 comments on commit a9e3f2c

Please sign in to comment.