From 24d27fce83c4b2d706a89dc7dbe5fa510183c67f Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Tue, 28 Mar 2017 10:16:27 -0700 Subject: [PATCH 1/2] Don't take a reference a var in a loop Fixes #2491 --- client/driver/env/env.go | 6 +++--- client/driver/env/env_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index c2aacd383..dcc2858dc 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -203,12 +203,12 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { continue } for _, nw := range resources.Networks { - ports := make([]*structs.Port, 0, len(nw.ReservedPorts)+len(nw.DynamicPorts)) + ports := make([]structs.Port, 0, len(nw.ReservedPorts)+len(nw.DynamicPorts)) for _, port := range nw.ReservedPorts { - ports = append(ports, &port) + ports = append(ports, port) } for _, port := range nw.DynamicPorts { - ports = append(ports, &port) + ports = append(ports, port) } for _, p := range ports { key := fmt.Sprintf("%s%s_%s", AddrPrefix, taskName, p.Label) diff --git a/client/driver/env/env_test.go b/client/driver/env/env_test.go index dd8eddfb1..3e672a79a 100644 --- a/client/driver/env/env_test.go +++ b/client/driver/env/env_test.go @@ -139,6 +139,16 @@ func TestEnvironment_AsList(t *testing.T) { n := mock.Node() a := mock.Alloc() a.TaskResources["web"].Networks[0].DynamicPorts[0].Value = 2000 + a.TaskResources["ssh"] = &structs.Resources{ + Networks: []*structs.NetworkResource{ + { + ReservedPorts: []structs.Port{ + {Label: "ssh", Value: 22}, + {Label: "other", Value: 1234}, + }, + }, + }, + } env := NewTaskEnvironment(n). SetNetworks(networks). SetPortMap(portMap). @@ -165,6 +175,12 @@ func TestEnvironment_AsList(t *testing.T) { "NOMAD_IP_web_main=192.168.0.100", "NOMAD_IP_web_http=192.168.0.100", "NOMAD_TASK_NAME=taskA", + "NOMAD_ADDR_ssh_other=:1234", + "NOMAD_ADDR_ssh_ssh=:22", + "NOMAD_IP_ssh_other=", + "NOMAD_IP_ssh_ssh=", + "NOMAD_PORT_ssh_other=1234", + "NOMAD_PORT_ssh_ssh=22", } allocID := fmt.Sprintf("NOMAD_ALLOC_ID=%s", a.ID) exp = append(exp, allocID) From 1ead3cae6e388b993a54352900942e307c338974 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Tue, 28 Mar 2017 10:53:26 -0700 Subject: [PATCH 2/2] Make test more accurate and add changelog entry --- CHANGELOG.md | 1 + client/driver/env/env_test.go | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9a73d665..1ef91c7b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ BUG FIXES: * api: Fix TLS in logs and fs commands/APIs [GH-2290] * cli/plan: Fix diff alignment and remove no change DC output [GH-2465] * client: Fix panic when restarting non-running tasks [GH-2480] + * client: Fix env vars when multiple tasks and ports present [GH-2491] ## 0.5.5 (March 14, 2017) diff --git a/client/driver/env/env_test.go b/client/driver/env/env_test.go index 3e672a79a..c520b0b9d 100644 --- a/client/driver/env/env_test.go +++ b/client/driver/env/env_test.go @@ -138,10 +138,17 @@ func TestEnvironment_ReplaceEnv_Mixed(t *testing.T) { func TestEnvironment_AsList(t *testing.T) { n := mock.Node() a := mock.Alloc() + a.Resources.Networks[0].ReservedPorts = append(a.Resources.Networks[0].ReservedPorts, + structs.Port{Label: "ssh", Value: 22}, + structs.Port{Label: "other", Value: 1234}, + ) a.TaskResources["web"].Networks[0].DynamicPorts[0].Value = 2000 a.TaskResources["ssh"] = &structs.Resources{ Networks: []*structs.NetworkResource{ { + Device: "eth0", + IP: "192.168.0.100", + MBits: 50, ReservedPorts: []structs.Port{ {Label: "ssh", Value: 22}, {Label: "other", Value: 1234}, @@ -175,19 +182,23 @@ func TestEnvironment_AsList(t *testing.T) { "NOMAD_IP_web_main=192.168.0.100", "NOMAD_IP_web_http=192.168.0.100", "NOMAD_TASK_NAME=taskA", - "NOMAD_ADDR_ssh_other=:1234", - "NOMAD_ADDR_ssh_ssh=:22", - "NOMAD_IP_ssh_other=", - "NOMAD_IP_ssh_ssh=", + "NOMAD_ADDR_ssh_other=192.168.0.100:1234", + "NOMAD_ADDR_ssh_ssh=192.168.0.100:22", + "NOMAD_IP_ssh_other=192.168.0.100", + "NOMAD_IP_ssh_ssh=192.168.0.100", "NOMAD_PORT_ssh_other=1234", "NOMAD_PORT_ssh_ssh=22", + fmt.Sprintf("NOMAD_ALLOC_ID=%s", a.ID), } - allocID := fmt.Sprintf("NOMAD_ALLOC_ID=%s", a.ID) - exp = append(exp, allocID) sort.Strings(act) sort.Strings(exp) - if !reflect.DeepEqual(act, exp) { - t.Fatalf("env.List() returned %v;\n want:\n%v", strings.Join(act, "\n"), strings.Join(exp, "\n")) + if len(act) != len(exp) { + t.Fatalf("wat: %d != %d", len(act), len(exp)) + } + for i := range act { + if act[i] != exp[i] { + t.Errorf("%d %q != %q", i, act[i], exp[i]) + } } }