github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/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  	"github.com/hashicorp/nomad/nomad/structs"
    11  )
    12  
    13  const (
    14  	// Node values that tests can rely on
    15  	metaKey   = "instance"
    16  	metaVal   = "t2-micro"
    17  	attrKey   = "arch"
    18  	attrVal   = "amd64"
    19  	nodeName  = "test node"
    20  	nodeClass = "test class"
    21  
    22  	// Environment variable values that tests can rely on
    23  	envOneKey = "NOMAD_IP"
    24  	envOneVal = "127.0.0.1"
    25  	envTwoKey = "NOMAD_PORT_WEB"
    26  	envTwoVal = ":80"
    27  )
    28  
    29  var (
    30  	// Networks that tests can rely on
    31  	networks = []*structs.NetworkResource{
    32  		&structs.NetworkResource{
    33  			IP:            "127.0.0.1",
    34  			ReservedPorts: []structs.Port{{"http", 80}},
    35  			DynamicPorts:  []structs.Port{{"https", 8080}},
    36  		},
    37  	}
    38  	portMap = map[string]int{
    39  		"https": 443,
    40  	}
    41  )
    42  
    43  func testTaskEnvironment() *TaskEnvironment {
    44  	n := mock.Node()
    45  	n.Attributes = map[string]string{
    46  		attrKey: attrVal,
    47  	}
    48  	n.Meta = map[string]string{
    49  		metaKey: metaVal,
    50  	}
    51  	n.Name = nodeName
    52  	n.NodeClass = nodeClass
    53  
    54  	envVars := map[string]string{
    55  		envOneKey: envOneVal,
    56  		envTwoKey: envTwoVal,
    57  	}
    58  	return NewTaskEnvironment(n).SetEnvvars(envVars).Build()
    59  }
    60  
    61  func TestEnvironment_ParseAndReplace_Env(t *testing.T) {
    62  	env := testTaskEnvironment()
    63  
    64  	input := []string{fmt.Sprintf(`"${%v}"!`, envOneKey), fmt.Sprintf("${%s}${%s}", envOneKey, envTwoKey)}
    65  	act := env.ParseAndReplace(input)
    66  	exp := []string{fmt.Sprintf(`"%s"!`, envOneVal), fmt.Sprintf("%s%s", envOneVal, envTwoVal)}
    67  
    68  	if !reflect.DeepEqual(act, exp) {
    69  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
    70  	}
    71  }
    72  
    73  func TestEnvironment_ParseAndReplace_Meta(t *testing.T) {
    74  	input := []string{fmt.Sprintf("${%v%v}", nodeMetaPrefix, metaKey)}
    75  	exp := []string{metaVal}
    76  	env := testTaskEnvironment()
    77  	act := env.ParseAndReplace(input)
    78  
    79  	if !reflect.DeepEqual(act, exp) {
    80  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
    81  	}
    82  }
    83  
    84  func TestEnvironment_ParseAndReplace_Attr(t *testing.T) {
    85  	input := []string{fmt.Sprintf("${%v%v}", nodeAttributePrefix, attrKey)}
    86  	exp := []string{attrVal}
    87  	env := testTaskEnvironment()
    88  	act := env.ParseAndReplace(input)
    89  
    90  	if !reflect.DeepEqual(act, exp) {
    91  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
    92  	}
    93  }
    94  
    95  func TestEnvironment_ParseAndReplace_Node(t *testing.T) {
    96  	input := []string{fmt.Sprintf("${%v}", nodeNameKey), fmt.Sprintf("${%v}", nodeClassKey)}
    97  	exp := []string{nodeName, nodeClass}
    98  	env := testTaskEnvironment()
    99  	act := env.ParseAndReplace(input)
   100  
   101  	if !reflect.DeepEqual(act, exp) {
   102  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
   103  	}
   104  }
   105  
   106  func TestEnvironment_ParseAndReplace_Mixed(t *testing.T) {
   107  	input := []string{
   108  		fmt.Sprintf("${%v}${%v%v}", nodeNameKey, nodeAttributePrefix, attrKey),
   109  		fmt.Sprintf("${%v}${%v%v}", nodeClassKey, nodeMetaPrefix, metaKey),
   110  		fmt.Sprintf("${%v}${%v}", envTwoKey, nodeClassKey),
   111  	}
   112  	exp := []string{
   113  		fmt.Sprintf("%v%v", nodeName, attrVal),
   114  		fmt.Sprintf("%v%v", nodeClass, metaVal),
   115  		fmt.Sprintf("%v%v", envTwoVal, nodeClass),
   116  	}
   117  	env := testTaskEnvironment()
   118  	act := env.ParseAndReplace(input)
   119  
   120  	if !reflect.DeepEqual(act, exp) {
   121  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
   122  	}
   123  }
   124  
   125  func TestEnvironment_ReplaceEnv_Mixed(t *testing.T) {
   126  	input := fmt.Sprintf("${%v}${%v%v}", nodeNameKey, nodeAttributePrefix, attrKey)
   127  	exp := fmt.Sprintf("%v%v", nodeName, attrVal)
   128  	env := testTaskEnvironment()
   129  	act := env.ReplaceEnv(input)
   130  
   131  	if act != exp {
   132  		t.Fatalf("ParseAndReplace(%v) returned %#v; want %#v", input, act, exp)
   133  	}
   134  }
   135  
   136  func TestEnvironment_AsList(t *testing.T) {
   137  	n := mock.Node()
   138  	env := NewTaskEnvironment(n).
   139  		SetNetworks(networks).
   140  		SetPortMap(portMap).
   141  		SetMeta(map[string]string{"foo": "baz"}).Build()
   142  
   143  	act := env.EnvList()
   144  	exp := []string{
   145  		"NOMAD_ADDR_http=127.0.0.1:80",
   146  		"NOMAD_ADDR_https=127.0.0.1:443",
   147  		"NOMAD_HOST_PORT_https=443",
   148  		"NOMAD_META_FOO=baz",
   149  	}
   150  	sort.Strings(act)
   151  	sort.Strings(exp)
   152  	if !reflect.DeepEqual(act, exp) {
   153  		t.Fatalf("env.List() returned %v; want %v", act, exp)
   154  	}
   155  }
   156  
   157  func TestEnvironment_ClearEnvvars(t *testing.T) {
   158  	n := mock.Node()
   159  	env := NewTaskEnvironment(n).
   160  		SetNetworks(networks).
   161  		SetPortMap(portMap).
   162  		SetEnvvars(map[string]string{"foo": "baz", "bar": "bang"}).Build()
   163  
   164  	act := env.EnvList()
   165  	exp := []string{
   166  		"NOMAD_ADDR_http=127.0.0.1:80",
   167  		"NOMAD_ADDR_https=127.0.0.1:443",
   168  		"NOMAD_HOST_PORT_https=443",
   169  		"bar=bang",
   170  		"foo=baz",
   171  	}
   172  	sort.Strings(act)
   173  	sort.Strings(exp)
   174  	if !reflect.DeepEqual(act, exp) {
   175  		t.Fatalf("env.List() returned %v; want %v", act, exp)
   176  	}
   177  
   178  	// Clear the environent variables.
   179  	env.ClearEnvvars().Build()
   180  
   181  	act = env.EnvList()
   182  	exp = []string{
   183  		"NOMAD_ADDR_http=127.0.0.1:80",
   184  		"NOMAD_ADDR_https=127.0.0.1:443",
   185  		"NOMAD_HOST_PORT_https=443",
   186  	}
   187  	sort.Strings(act)
   188  	sort.Strings(exp)
   189  	if !reflect.DeepEqual(act, exp) {
   190  		t.Fatalf("env.List() returned %v; want %v", act, exp)
   191  	}
   192  }
   193  
   194  func TestEnvironment_Interprolate(t *testing.T) {
   195  	env := testTaskEnvironment().
   196  		SetEnvvars(map[string]string{"test": "${node.class}", "test2": "${attr.arch}"}).
   197  		Build()
   198  
   199  	act := env.EnvList()
   200  	exp := []string{fmt.Sprintf("test=%s", nodeClass), fmt.Sprintf("test2=%s", attrVal)}
   201  	sort.Strings(act)
   202  	sort.Strings(exp)
   203  	if !reflect.DeepEqual(act, exp) {
   204  		t.Fatalf("env.List() returned %v; want %v", act, exp)
   205  	}
   206  }