2015-09-28 00:34:02 +00:00
|
|
|
package environment
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEnvironment_AsList(t *testing.T) {
|
|
|
|
env := NewTaskEnivornment()
|
|
|
|
env.SetTaskIp("127.0.0.1")
|
|
|
|
env.SetPorts(map[string]int{"http": 80})
|
|
|
|
env.SetMeta(map[string]string{"foo": "baz"})
|
|
|
|
|
|
|
|
act := env.List()
|
|
|
|
exp := []string{"NOMAD_IP=127.0.0.1", "NOMAD_PORT_http=80", "NOMAD_META_FOO=baz"}
|
|
|
|
sort.Strings(act)
|
|
|
|
sort.Strings(exp)
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("env.List() returned %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 01:13:05 +00:00
|
|
|
func TestEnvironment_ParseFromList(t *testing.T) {
|
2015-11-19 02:17:22 +00:00
|
|
|
input := []string{
|
|
|
|
"foo=bar",
|
|
|
|
"BAZ=baM",
|
|
|
|
"bar=emb=edded", // This can be done in multiple OSes.
|
|
|
|
"=ExitCode=00000000", // A Windows cmd.exe annoyance
|
|
|
|
}
|
2015-09-28 00:34:02 +00:00
|
|
|
env, err := ParseFromList(input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ParseFromList(%#v) failed: %v", input, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := map[string]string{
|
2015-11-19 02:17:22 +00:00
|
|
|
"foo": "bar",
|
|
|
|
"BAZ": "baM",
|
|
|
|
"bar": "emb=edded",
|
|
|
|
"=ExitCode": "00000000",
|
2015-09-28 00:34:02 +00:00
|
|
|
}
|
2015-10-16 17:11:31 +00:00
|
|
|
|
|
|
|
if len(env) != len(exp) {
|
2015-11-19 02:17:22 +00:00
|
|
|
t.Errorf("ParseFromList(%#v) has length %v; want %v", input, len(env), len(exp))
|
2015-10-16 17:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range exp {
|
|
|
|
if actV, ok := env[k]; !ok {
|
2015-11-19 02:17:22 +00:00
|
|
|
t.Errorf("ParseFromList(%#v) doesn't contain expected %v", input, k)
|
2015-10-16 17:11:31 +00:00
|
|
|
} else if actV != v {
|
2015-11-19 02:17:22 +00:00
|
|
|
t.Errorf("ParseFromList(%#v) has incorrect value for %v; got %v; want %v", input, k, actV, v)
|
2015-10-16 17:11:31 +00:00
|
|
|
}
|
2015-09-28 00:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-16 01:13:05 +00:00
|
|
|
|
|
|
|
func TestEnvironment_ClearEnvvars(t *testing.T) {
|
|
|
|
env := NewTaskEnivornment()
|
|
|
|
env.SetTaskIp("127.0.0.1")
|
|
|
|
env.SetEnvvars(map[string]string{"foo": "baz", "bar": "bang"})
|
|
|
|
|
|
|
|
act := env.List()
|
|
|
|
exp := []string{"NOMAD_IP=127.0.0.1", "bar=bang", "foo=baz"}
|
|
|
|
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()
|
|
|
|
|
|
|
|
act = env.List()
|
|
|
|
exp = []string{"NOMAD_IP=127.0.0.1"}
|
|
|
|
sort.Strings(act)
|
|
|
|
sort.Strings(exp)
|
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("env.List() returned %v; want %v", act, exp)
|
|
|
|
}
|
|
|
|
}
|