Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace "-" in port labels with "_" for env vars #2406

Merged
merged 3 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace "-" in env var keys with "_"
Fixes #2405
  • Loading branch information
schmichael committed Mar 7, 2017
commit a9e3f2cf3edd4963ee8d0aa6ed7c4d5242cb9a27
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