2016-01-05 22:50:25 +00:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-23 18:45:03 +00:00
|
|
|
"os"
|
2016-01-05 22:50:25 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2016-03-23 18:45:03 +00:00
|
|
|
"strings"
|
2016-01-05 22:50:25 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2016-01-24 09:31:03 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-01-05 22:50:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Node values that tests can rely on
|
|
|
|
metaKey = "instance"
|
|
|
|
metaVal = "t2-micro"
|
|
|
|
attrKey = "arch"
|
|
|
|
attrVal = "amd64"
|
|
|
|
nodeName = "test node"
|
|
|
|
nodeClass = "test class"
|
|
|
|
|
|
|
|
// Environment variable values that tests can rely on
|
|
|
|
envOneKey = "NOMAD_IP"
|
|
|
|
envOneVal = "127.0.0.1"
|
|
|
|
envTwoKey = "NOMAD_PORT_WEB"
|
|
|
|
envTwoVal = ":80"
|
|
|
|
)
|
|
|
|
|
2016-01-24 09:31:03 +00:00
|
|
|
var (
|
|
|
|
// Networks that tests can rely on
|
|
|
|
networks = []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
|
|
|
IP: "127.0.0.1",
|
|
|
|
ReservedPorts: []structs.Port{{"http", 80}},
|
|
|
|
DynamicPorts: []structs.Port{{"https", 8080}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
portMap = map[string]int{
|
|
|
|
"https": 443,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-01-05 22:50:25 +00:00
|
|
|
func testTaskEnvironment() *TaskEnvironment {
|
|
|
|
n := mock.Node()
|
|
|
|
n.Attributes = map[string]string{
|
|
|
|
attrKey: attrVal,
|
|
|
|
}
|
|
|
|
n.Meta = map[string]string{
|
|
|
|
metaKey: metaVal,
|
|
|
|
}
|
|
|
|
n.Name = nodeName
|
|
|
|
n.NodeClass = nodeClass
|
|
|
|
|
|
|
|
envVars := map[string]string{
|
|
|
|
envOneKey: envOneVal,
|
|
|
|
envTwoKey: envTwoVal,
|
|
|
|
}
|
|
|
|
return NewTaskEnvironment(n).SetEnvvars(envVars).Build()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ParseAndReplace_Env(t *testing.T) {
|
|
|
|
env := testTaskEnvironment()
|
|
|
|
|
2016-02-05 01:21:00 +00:00
|
|
|
input := []string{fmt.Sprintf(`"${%v}"!`, envOneKey), fmt.Sprintf("${%s}${%s}", envOneKey, envTwoKey)}
|
2016-01-05 22:50:25 +00:00
|
|
|
act := env.ParseAndReplace(input)
|
|
|
|
exp := []string{fmt.Sprintf(`"%s"!`, envOneVal), fmt.Sprintf("%s%s", envOneVal, envTwoVal)}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ParseAndReplace_Meta(t *testing.T) {
|
2016-02-05 01:21:00 +00:00
|
|
|
input := []string{fmt.Sprintf("${%v%v}", nodeMetaPrefix, metaKey)}
|
2016-01-05 22:50:25 +00:00
|
|
|
exp := []string{metaVal}
|
|
|
|
env := testTaskEnvironment()
|
|
|
|
act := env.ParseAndReplace(input)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ParseAndReplace_Attr(t *testing.T) {
|
2016-02-05 01:21:00 +00:00
|
|
|
input := []string{fmt.Sprintf("${%v%v}", nodeAttributePrefix, attrKey)}
|
2016-01-05 22:50:25 +00:00
|
|
|
exp := []string{attrVal}
|
|
|
|
env := testTaskEnvironment()
|
|
|
|
act := env.ParseAndReplace(input)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ParseAndReplace_Node(t *testing.T) {
|
2016-02-05 01:21:00 +00:00
|
|
|
input := []string{fmt.Sprintf("${%v}", nodeNameKey), fmt.Sprintf("${%v}", nodeClassKey)}
|
2016-01-05 22:50:25 +00:00
|
|
|
exp := []string{nodeName, nodeClass}
|
|
|
|
env := testTaskEnvironment()
|
|
|
|
act := env.ParseAndReplace(input)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ParseAndReplace_Mixed(t *testing.T) {
|
|
|
|
input := []string{
|
2016-02-05 01:21:00 +00:00
|
|
|
fmt.Sprintf("${%v}${%v%v}", nodeNameKey, nodeAttributePrefix, attrKey),
|
|
|
|
fmt.Sprintf("${%v}${%v%v}", nodeClassKey, nodeMetaPrefix, metaKey),
|
|
|
|
fmt.Sprintf("${%v}${%v}", envTwoKey, nodeClassKey),
|
2016-01-05 22:50:25 +00:00
|
|
|
}
|
|
|
|
exp := []string{
|
|
|
|
fmt.Sprintf("%v%v", nodeName, attrVal),
|
|
|
|
fmt.Sprintf("%v%v", nodeClass, metaVal),
|
|
|
|
fmt.Sprintf("%v%v", envTwoVal, nodeClass),
|
|
|
|
}
|
|
|
|
env := testTaskEnvironment()
|
|
|
|
act := env.ParseAndReplace(input)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ReplaceEnv_Mixed(t *testing.T) {
|
2016-02-05 01:21:00 +00:00
|
|
|
input := fmt.Sprintf("${%v}${%v%v}", nodeNameKey, nodeAttributePrefix, attrKey)
|
2016-01-05 22:50:25 +00:00
|
|
|
exp := fmt.Sprintf("%v%v", nodeName, attrVal)
|
|
|
|
env := testTaskEnvironment()
|
|
|
|
act := env.ReplaceEnv(input)
|
|
|
|
|
|
|
|
if act != exp {
|
|
|
|
t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_AsList(t *testing.T) {
|
|
|
|
n := mock.Node()
|
|
|
|
env := NewTaskEnvironment(n).
|
2016-01-24 09:31:03 +00:00
|
|
|
SetNetworks(networks).
|
|
|
|
SetPortMap(portMap).
|
2016-03-25 17:16:04 +00:00
|
|
|
SetTaskGroupMeta(map[string]string{"foo": "bar", "baz": "bam"}).
|
|
|
|
SetTaskMeta(map[string]string{"foo": "baz"}).Build()
|
2016-01-05 22:50:25 +00:00
|
|
|
|
|
|
|
act := env.EnvList()
|
2016-01-24 09:31:03 +00:00
|
|
|
exp := []string{
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_ADDR_http=127.0.0.1:80",
|
2016-04-15 17:27:51 +00:00
|
|
|
"NOMAD_PORT_http=80",
|
|
|
|
"NOMAD_IP_http=127.0.0.1",
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_ADDR_https=127.0.0.1:443",
|
2016-04-15 17:27:51 +00:00
|
|
|
"NOMAD_PORT_https=443",
|
|
|
|
"NOMAD_IP_https=127.0.0.1",
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_HOST_PORT_https=443",
|
2016-01-24 09:31:03 +00:00
|
|
|
"NOMAD_META_FOO=baz",
|
2016-03-25 17:16:04 +00:00
|
|
|
"NOMAD_META_BAZ=bam",
|
2016-01-24 09:31:03 +00:00
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
sort.Strings(act)
|
|
|
|
sort.Strings(exp)
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("env.List() returned %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ClearEnvvars(t *testing.T) {
|
|
|
|
n := mock.Node()
|
|
|
|
env := NewTaskEnvironment(n).
|
2016-01-24 09:31:03 +00:00
|
|
|
SetNetworks(networks).
|
|
|
|
SetPortMap(portMap).
|
2016-01-05 22:50:25 +00:00
|
|
|
SetEnvvars(map[string]string{"foo": "baz", "bar": "bang"}).Build()
|
|
|
|
|
|
|
|
act := env.EnvList()
|
2016-01-24 09:31:03 +00:00
|
|
|
exp := []string{
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_ADDR_http=127.0.0.1:80",
|
2016-04-15 17:27:51 +00:00
|
|
|
"NOMAD_PORT_http=80",
|
|
|
|
"NOMAD_IP_http=127.0.0.1",
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_ADDR_https=127.0.0.1:443",
|
2016-04-15 17:27:51 +00:00
|
|
|
"NOMAD_PORT_https=443",
|
|
|
|
"NOMAD_IP_https=127.0.0.1",
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_HOST_PORT_https=443",
|
2016-01-24 09:31:03 +00:00
|
|
|
"bar=bang",
|
|
|
|
"foo=baz",
|
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
sort.Strings(act)
|
|
|
|
sort.Strings(exp)
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("env.List() returned %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the environent variables.
|
|
|
|
env.ClearEnvvars().Build()
|
|
|
|
|
|
|
|
act = env.EnvList()
|
2016-01-24 09:31:03 +00:00
|
|
|
exp = []string{
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_ADDR_http=127.0.0.1:80",
|
2016-04-15 17:27:51 +00:00
|
|
|
"NOMAD_PORT_http=80",
|
|
|
|
"NOMAD_IP_http=127.0.0.1",
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_ADDR_https=127.0.0.1:443",
|
2016-04-15 17:27:51 +00:00
|
|
|
"NOMAD_PORT_https=443",
|
|
|
|
"NOMAD_IP_https=127.0.0.1",
|
2016-01-25 19:46:01 +00:00
|
|
|
"NOMAD_HOST_PORT_https=443",
|
2016-01-24 09:31:03 +00:00
|
|
|
}
|
2016-01-05 22:50:25 +00:00
|
|
|
sort.Strings(act)
|
|
|
|
sort.Strings(exp)
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("env.List() returned %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_Interprolate(t *testing.T) {
|
|
|
|
env := testTaskEnvironment().
|
2016-02-05 01:21:00 +00:00
|
|
|
SetEnvvars(map[string]string{"test": "${node.class}", "test2": "${attr.arch}"}).
|
2016-01-05 22:50:25 +00:00
|
|
|
Build()
|
|
|
|
|
|
|
|
act := env.EnvList()
|
|
|
|
exp := []string{fmt.Sprintf("test=%s", nodeClass), fmt.Sprintf("test2=%s", attrVal)}
|
|
|
|
sort.Strings(act)
|
|
|
|
sort.Strings(exp)
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("env.List() returned %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 18:45:03 +00:00
|
|
|
|
|
|
|
func TestEnvironment_AppendHostEnvVars(t *testing.T) {
|
|
|
|
host := os.Environ()
|
|
|
|
if len(host) < 2 {
|
|
|
|
t.Skip("No host environment variables. Can't test")
|
|
|
|
}
|
|
|
|
skip := strings.Split(host[0], "=")[0]
|
|
|
|
env := testTaskEnvironment().
|
|
|
|
AppendHostEnvvars([]string{skip}).
|
|
|
|
Build()
|
|
|
|
|
|
|
|
act := env.EnvMap()
|
|
|
|
if len(act) < 1 {
|
|
|
|
t.Fatalf("Host environment variables not properly set")
|
|
|
|
}
|
|
|
|
if _, ok := act[skip]; ok {
|
|
|
|
t.Fatalf("Didn't filter environment variable %q", skip)
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 17:16:04 +00:00
|
|
|
|
|
|
|
func TestEnvironment_MetaPrecedence(t *testing.T) {
|
|
|
|
n := mock.Node()
|
|
|
|
env := NewTaskEnvironment(n).
|
|
|
|
SetJobMeta(map[string]string{"foo": "job", "bar": "job", "baz": "job"}).
|
|
|
|
SetTaskGroupMeta(map[string]string{"foo": "tg", "bar": "tg"}).
|
|
|
|
SetTaskMeta(map[string]string{"foo": "task"}).Build()
|
|
|
|
|
|
|
|
act := env.EnvList()
|
|
|
|
exp := []string{
|
|
|
|
"NOMAD_META_FOO=task",
|
|
|
|
"NOMAD_META_BAR=tg",
|
|
|
|
"NOMAD_META_BAZ=job",
|
|
|
|
}
|
|
|
|
sort.Strings(act)
|
|
|
|
sort.Strings(exp)
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("env.List() returned %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
}
|