Replace "-" in env var keys with "_"

Fixes #2405
This commit is contained in:
Michael Schurter
2017-03-06 17:09:44 -08:00
parent be1b4c44f8
commit a9e3f2cf3e
3 changed files with 32 additions and 1 deletions

View File

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

View File

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

View File

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