github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/helper/args/args_test.go (about)

     1  package args
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  const (
    10  	ipKey     = "NOMAD_IP"
    11  	ipVal     = "127.0.0.1"
    12  	portKey   = "NOMAD_PORT_WEB"
    13  	portVal   = ":80"
    14  	periodKey = "NOMAD.PERIOD"
    15  	periodVal = "period"
    16  	dashKey   = "NOMAD-DASH"
    17  	dashVal   = "dash"
    18  )
    19  
    20  var (
    21  	envVars = map[string]string{
    22  		ipKey:     ipVal,
    23  		portKey:   portVal,
    24  		periodKey: periodVal,
    25  		dashKey:   dashVal,
    26  	}
    27  )
    28  
    29  func TestArgs_ReplaceEnv_Invalid(t *testing.T) {
    30  	input := "${FOO}"
    31  	exp := input
    32  	act := ReplaceEnv(input, envVars)
    33  
    34  	if !reflect.DeepEqual(act, exp) {
    35  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    36  	}
    37  }
    38  
    39  func TestArgs_ReplaceEnv_Valid(t *testing.T) {
    40  	input := fmt.Sprintf(`"${%v}"!`, ipKey)
    41  	exp := fmt.Sprintf("\"%s\"!", ipVal)
    42  	act := ReplaceEnv(input, envVars)
    43  
    44  	if !reflect.DeepEqual(act, exp) {
    45  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    46  	}
    47  }
    48  
    49  func TestArgs_ReplaceEnv_Period(t *testing.T) {
    50  	input := fmt.Sprintf(`"${%v}"!`, periodKey)
    51  	exp := fmt.Sprintf("\"%s\"!", periodVal)
    52  	act := ReplaceEnv(input, envVars)
    53  
    54  	if !reflect.DeepEqual(act, exp) {
    55  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    56  	}
    57  }
    58  
    59  func TestArgs_ReplaceEnv_Dash(t *testing.T) {
    60  	input := fmt.Sprintf(`"${%v}"!`, dashKey)
    61  	exp := fmt.Sprintf("\"%s\"!", dashVal)
    62  	act := ReplaceEnv(input, envVars)
    63  
    64  	if !reflect.DeepEqual(act, exp) {
    65  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    66  	}
    67  }
    68  
    69  func TestArgs_ReplaceEnv_Chained(t *testing.T) {
    70  	input := fmt.Sprintf("${%s}${%s}", ipKey, portKey)
    71  	exp := fmt.Sprintf("%s%s", ipVal, portVal)
    72  	act := ReplaceEnv(input, envVars)
    73  
    74  	if !reflect.DeepEqual(act, exp) {
    75  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    76  	}
    77  }