Merge pull request #1396 from hashicorp/b-task-host-port

Fixed the host port environment variable
This commit is contained in:
Diptanu Choudhury 2016-07-08 22:14:49 -07:00 committed by GitHub
commit 92deab0f3a
4 changed files with 19 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime/debug" "runtime/debug"
"strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -709,7 +710,7 @@ func TestDockerPortsMapping(t *testing.T) {
expectedEnvironment := map[string]string{ expectedEnvironment := map[string]string{
"NOMAD_ADDR_main": "127.0.0.1:8080", "NOMAD_ADDR_main": "127.0.0.1:8080",
"NOMAD_ADDR_REDIS": "127.0.0.1:6379", "NOMAD_ADDR_REDIS": "127.0.0.1:6379",
"NOMAD_HOST_PORT_main": "8080", "NOMAD_HOST_PORT_main": strconv.Itoa(docker_reserved),
} }
for key, val := range expectedEnvironment { for key, val := range expectedEnvironment {

View File

@ -129,15 +129,19 @@ func TestDriver_GetTaskEnv(t *testing.T) {
"NOMAD_ADDR_one": "1.2.3.4:80", "NOMAD_ADDR_one": "1.2.3.4:80",
"NOMAD_IP_one": "1.2.3.4", "NOMAD_IP_one": "1.2.3.4",
"NOMAD_PORT_one": "80", "NOMAD_PORT_one": "80",
"NOMAD_HOST_PORT_one": "80",
"NOMAD_ADDR_two": "1.2.3.4:443", "NOMAD_ADDR_two": "1.2.3.4:443",
"NOMAD_IP_two": "1.2.3.4", "NOMAD_IP_two": "1.2.3.4",
"NOMAD_PORT_two": "443", "NOMAD_PORT_two": "443",
"NOMAD_HOST_PORT_two": "443",
"NOMAD_ADDR_admin": "1.2.3.4:8081", "NOMAD_ADDR_admin": "1.2.3.4:8081",
"NOMAD_IP_admin": "1.2.3.4", "NOMAD_IP_admin": "1.2.3.4",
"NOMAD_PORT_admin": "8081", "NOMAD_PORT_admin": "8081",
"NOMAD_HOST_PORT_admin": "8081",
"NOMAD_ADDR_web": "1.2.3.4:8086", "NOMAD_ADDR_web": "1.2.3.4:8086",
"NOMAD_IP_web": "1.2.3.4", "NOMAD_IP_web": "1.2.3.4",
"NOMAD_PORT_web": "8086", "NOMAD_PORT_web": "8086",
"NOMAD_HOST_PORT_web": "8086",
"NOMAD_META_CHOCOLATE": "cake", "NOMAD_META_CHOCOLATE": "cake",
"NOMAD_META_STRAWBERRY": "icecream", "NOMAD_META_STRAWBERRY": "icecream",
"NOMAD_META_ELB_CHECK_INTERVAL": "30s", "NOMAD_META_ELB_CHECK_INTERVAL": "30s",

View File

@ -133,16 +133,16 @@ func (t *TaskEnvironment) Build() *TaskEnvironment {
// Build the ports // Build the ports
for _, network := range t.Networks { for _, network := range t.Networks {
for label, value := range network.MapLabelToValues(t.PortMap) { for label, value := range network.MapLabelToValues(nil) {
t.TaskEnv[fmt.Sprintf("%s%s", IpPrefix, label)] = network.IP
t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(value)
if forwardedPort, ok := t.PortMap[label]; ok {
value = forwardedPort
}
t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value)
IPPort := fmt.Sprintf("%s:%d", network.IP, value) IPPort := fmt.Sprintf("%s:%d", network.IP, value)
t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort
t.TaskEnv[fmt.Sprintf("%s%s", IpPrefix, label)] = network.IP
t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value)
// Pass an explicit port mapping to the environment
if port, ok := t.PortMap[label]; ok {
t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(port)
}
} }
} }

View File

@ -151,7 +151,8 @@ func TestEnvironment_AsList(t *testing.T) {
"NOMAD_ADDR_https=127.0.0.1:443", "NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_PORT_https=443", "NOMAD_PORT_https=443",
"NOMAD_IP_https=127.0.0.1", "NOMAD_IP_https=127.0.0.1",
"NOMAD_HOST_PORT_https=443", "NOMAD_HOST_PORT_http=80",
"NOMAD_HOST_PORT_https=8080",
"NOMAD_META_FOO=baz", "NOMAD_META_FOO=baz",
"NOMAD_META_BAZ=bam", "NOMAD_META_BAZ=bam",
} }
@ -177,7 +178,8 @@ func TestEnvironment_ClearEnvvars(t *testing.T) {
"NOMAD_ADDR_https=127.0.0.1:443", "NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_PORT_https=443", "NOMAD_PORT_https=443",
"NOMAD_IP_https=127.0.0.1", "NOMAD_IP_https=127.0.0.1",
"NOMAD_HOST_PORT_https=443", "NOMAD_HOST_PORT_http=80",
"NOMAD_HOST_PORT_https=8080",
"bar=bang", "bar=bang",
"foo=baz", "foo=baz",
} }
@ -198,7 +200,8 @@ func TestEnvironment_ClearEnvvars(t *testing.T) {
"NOMAD_ADDR_https=127.0.0.1:443", "NOMAD_ADDR_https=127.0.0.1:443",
"NOMAD_PORT_https=443", "NOMAD_PORT_https=443",
"NOMAD_IP_https=127.0.0.1", "NOMAD_IP_https=127.0.0.1",
"NOMAD_HOST_PORT_https=443", "NOMAD_HOST_PORT_https=8080",
"NOMAD_HOST_PORT_http=80",
} }
sort.Strings(act) sort.Strings(act)
sort.Strings(exp) sort.Strings(exp)