github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/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  )
    17  
    18  var (
    19  	envVars = map[string]string{
    20  		ipKey:     ipVal,
    21  		portKey:   portVal,
    22  		periodKey: periodVal,
    23  	}
    24  )
    25  
    26  func TestArgs_ReplaceEnv_Invalid(t *testing.T) {
    27  	input := "$FOO"
    28  	exp := "$FOO"
    29  	act := ReplaceEnv(input, envVars)
    30  
    31  	if !reflect.DeepEqual(act, exp) {
    32  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    33  	}
    34  }
    35  
    36  func TestArgs_ReplaceEnv_Valid(t *testing.T) {
    37  	input := fmt.Sprintf(`"$%v"!`, ipKey)
    38  	exp := fmt.Sprintf("\"%s\"!", ipVal)
    39  	act := ReplaceEnv(input, envVars)
    40  
    41  	if !reflect.DeepEqual(act, exp) {
    42  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    43  	}
    44  }
    45  
    46  func TestArgs_ReplaceEnv_Period(t *testing.T) {
    47  	input := fmt.Sprintf(`"$%v"!`, periodKey)
    48  	exp := fmt.Sprintf("\"%s\"!", periodVal)
    49  	act := ReplaceEnv(input, envVars)
    50  
    51  	if !reflect.DeepEqual(act, exp) {
    52  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    53  	}
    54  }
    55  
    56  func TestArgs_ReplaceEnv_Chained(t *testing.T) {
    57  	input := fmt.Sprintf("$%s$%s", ipKey, portKey)
    58  	exp := fmt.Sprintf("%s%s", ipVal, portVal)
    59  	act := ReplaceEnv(input, envVars)
    60  
    61  	if !reflect.DeepEqual(act, exp) {
    62  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    63  	}
    64  }