open-nomad/client/driver/environment/vars_test.go

73 lines
1.7 KiB
Go
Raw Normal View History

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-09-28 00:34:02 +00:00
input := []string{"foo=bar", "BAZ=baM"}
env, err := ParseFromList(input)
if err != nil {
t.Fatalf("ParseFromList(%#v) failed: %v", input, err)
}
exp := map[string]string{
2015-10-16 17:11:31 +00:00
"foo": "bar",
2015-09-28 00:34:02 +00:00
"BAZ": "baM",
}
2015-10-16 17:11:31 +00:00
if len(env) != len(exp) {
t.Fatalf("ParseFromList(%#v) has length %v; want %v", input, len(env), len(exp))
}
for k, v := range exp {
if actV, ok := env[k]; !ok {
t.Fatalf("ParseFromList(%#v) doesn't contain expected %v", input, k)
} else if actV != v {
t.Fatalf("ParseFromList(%#v) has incorrect value for %v; got %v; want %v", input, k, actV, v)
}
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)
}
}