github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/client/driver/env/env_test.go (about)

     1  package env
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/nomad/nomad/mock"
    10  )
    11  
    12  const (
    13  	// Node values that tests can rely on
    14  	metaKey   = "instance"
    15  	metaVal   = "t2-micro"
    16  	attrKey   = "arch"
    17  	attrVal   = "amd64"
    18  	nodeName  = "test node"
    19  	nodeClass = "test class"
    20  
    21  	// Environment variable values that tests can rely on
    22  	envOneKey = "NOMAD_IP"
    23  	envOneVal = "127.0.0.1"
    24  	envTwoKey = "NOMAD_PORT_WEB"
    25  	envTwoVal = ":80"
    26  )
    27  
    28  func testTaskEnvironment() *TaskEnvironment {
    29  	n := mock.Node()
    30  	n.Attributes = map[string]string{
    31  		attrKey: attrVal,
    32  	}
    33  	n.Meta = map[string]string{
    34  		metaKey: metaVal,
    35  	}
    36  	n.Name = nodeName
    37  	n.NodeClass = nodeClass
    38  
    39  	envVars := map[string]string{
    40  		envOneKey: envOneVal,
    41  		envTwoKey: envTwoVal,
    42  	}
    43  	return NewTaskEnvironment(n).SetEnvvars(envVars).Build()
    44  }
    45  
    46  func TestEnvironment_ParseAndReplace_Env(t *testing.T) {
    47  	env := testTaskEnvironment()
    48  
    49  	input := []string{fmt.Sprintf(`"$%v"!`, envOneKey), fmt.Sprintf("$%s$%s", envOneKey, envTwoKey)}
    50  	act := env.ParseAndReplace(input)
    51  	exp := []string{fmt.Sprintf(`"%s"!`, envOneVal), fmt.Sprintf("%s%s", envOneVal, envTwoVal)}
    52  
    53  	if !reflect.DeepEqual(act, exp) {
    54  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
    55  	}
    56  }
    57  
    58  func TestEnvironment_ParseAndReplace_Meta(t *testing.T) {
    59  	input := []string{fmt.Sprintf("$%v%v", nodeMetaPrefix, metaKey)}
    60  	exp := []string{metaVal}
    61  	env := testTaskEnvironment()
    62  	act := env.ParseAndReplace(input)
    63  
    64  	if !reflect.DeepEqual(act, exp) {
    65  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
    66  	}
    67  }
    68  
    69  func TestEnvironment_ParseAndReplace_Attr(t *testing.T) {
    70  	input := []string{fmt.Sprintf("$%v%v", nodeAttributePrefix, attrKey)}
    71  	exp := []string{attrVal}
    72  	env := testTaskEnvironment()
    73  	act := env.ParseAndReplace(input)
    74  
    75  	if !reflect.DeepEqual(act, exp) {
    76  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
    77  	}
    78  }
    79  
    80  func TestEnvironment_ParseAndReplace_Node(t *testing.T) {
    81  	input := []string{fmt.Sprintf("$%v", nodeNameKey), fmt.Sprintf("$%v", nodeClassKey)}
    82  	exp := []string{nodeName, nodeClass}
    83  	env := testTaskEnvironment()
    84  	act := env.ParseAndReplace(input)
    85  
    86  	if !reflect.DeepEqual(act, exp) {
    87  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
    88  	}
    89  }
    90  
    91  func TestEnvironment_ParseAndReplace_Mixed(t *testing.T) {
    92  	input := []string{
    93  		fmt.Sprintf("$%v$%v%v", nodeNameKey, nodeAttributePrefix, attrKey),
    94  		fmt.Sprintf("$%v$%v%v", nodeClassKey, nodeMetaPrefix, metaKey),
    95  		fmt.Sprintf("$%v$%v", envTwoKey, nodeClassKey),
    96  	}
    97  	exp := []string{
    98  		fmt.Sprintf("%v%v", nodeName, attrVal),
    99  		fmt.Sprintf("%v%v", nodeClass, metaVal),
   100  		fmt.Sprintf("%v%v", envTwoVal, nodeClass),
   101  	}
   102  	env := testTaskEnvironment()
   103  	act := env.ParseAndReplace(input)
   104  
   105  	if !reflect.DeepEqual(act, exp) {
   106  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
   107  	}
   108  }
   109  
   110  func TestEnvironment_ReplaceEnv_Mixed(t *testing.T) {
   111  	input := fmt.Sprintf("$%v$%v%v", nodeNameKey, nodeAttributePrefix, attrKey)
   112  	exp := fmt.Sprintf("%v%v", nodeName, attrVal)
   113  	env := testTaskEnvironment()
   114  	act := env.ReplaceEnv(input)
   115  
   116  	if act != exp {
   117  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
   118  	}
   119  }
   120  
   121  func TestEnvironment_AsList(t *testing.T) {
   122  	n := mock.Node()
   123  	env := NewTaskEnvironment(n).
   124  		SetTaskIp("127.0.0.1").SetPorts(map[string]int{"http": 80}).
   125  		SetMeta(map[string]string{"foo": "baz"}).Build()
   126  
   127  	act := env.EnvList()
   128  	exp := []string{"NOMAD_IP=127.0.0.1", "NOMAD_PORT_http=80", "NOMAD_META_FOO=baz"}
   129  	sort.Strings(act)
   130  	sort.Strings(exp)
   131  	if !reflect.DeepEqual(act, exp) {
   132  		t.Fatalf("env.List() returned %v; want %v", act, exp)
   133  	}
   134  }
   135  
   136  func TestEnvironment_ClearEnvvars(t *testing.T) {
   137  	n := mock.Node()
   138  	env := NewTaskEnvironment(n).
   139  		SetTaskIp("127.0.0.1").
   140  		SetEnvvars(map[string]string{"foo": "baz", "bar": "bang"}).Build()
   141  
   142  	act := env.EnvList()
   143  	exp := []string{"NOMAD_IP=127.0.0.1", "bar=bang", "foo=baz"}
   144  	sort.Strings(act)
   145  	sort.Strings(exp)
   146  	if !reflect.DeepEqual(act, exp) {
   147  		t.Fatalf("env.List() returned %v; want %v", act, exp)
   148  	}
   149  
   150  	// Clear the environent variables.
   151  	env.ClearEnvvars().Build()
   152  
   153  	act = env.EnvList()
   154  	exp = []string{"NOMAD_IP=127.0.0.1"}
   155  	sort.Strings(act)
   156  	sort.Strings(exp)
   157  	if !reflect.DeepEqual(act, exp) {
   158  		t.Fatalf("env.List() returned %v; want %v", act, exp)
   159  	}
   160  }
   161  
   162  func TestEnvironment_Interprolate(t *testing.T) {
   163  	env := testTaskEnvironment().
   164  		SetEnvvars(map[string]string{"test": "$node.class", "test2": "$attr.arch"}).
   165  		Build()
   166  
   167  	act := env.EnvList()
   168  	exp := []string{fmt.Sprintf("test=%s", nodeClass), fmt.Sprintf("test2=%s", attrVal)}
   169  	sort.Strings(act)
   170  	sort.Strings(exp)
   171  	if !reflect.DeepEqual(act, exp) {
   172  		t.Fatalf("env.List() returned %v; want %v", act, exp)
   173  	}
   174  }