github.com/manicqin/nomad@v0.9.5/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  }
    78  
    79  func TestArgs_ContainsEnv(t *testing.T) {
    80  	positiveCases := []string{
    81  		"test-${env_var}",
    82  	}
    83  	for _, c := range positiveCases {
    84  		t.Run(fmt.Sprintf("positive case: %v", c), func(t *testing.T) {
    85  			if !ContainsEnv(c) {
    86  				t.Fatalf("ContainsEnv(%v) returned false; want true", c)
    87  			}
    88  		})
    89  	}
    90  
    91  	negativeCases := []string{
    92  		"test",
    93  		"test-$",
    94  		"test-${asdf",
    95  		"test-{asdf}",
    96  		"$test",
    97  	}
    98  	for _, c := range negativeCases {
    99  		t.Run(fmt.Sprintf("negative case: %v", c), func(t *testing.T) {
   100  			if ContainsEnv(c) {
   101  				t.Fatalf("ContainsEnv(%v) returned true; want false", c)
   102  			}
   103  		})
   104  	}
   105  
   106  }