github.com/hernad/nomad@v1.6.112/helper/args/args_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package args
     5  
     6  import (
     7  	"fmt"
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  const (
    13  	ipKey     = "NOMAD_IP"
    14  	ipVal     = "127.0.0.1"
    15  	portKey   = "NOMAD_PORT_WEB"
    16  	portVal   = ":80"
    17  	periodKey = "NOMAD.PERIOD"
    18  	periodVal = "period"
    19  	dashKey   = "NOMAD-DASH"
    20  	dashVal   = "dash"
    21  )
    22  
    23  var (
    24  	envVars = map[string]string{
    25  		ipKey:     ipVal,
    26  		portKey:   portVal,
    27  		periodKey: periodVal,
    28  		dashKey:   dashVal,
    29  	}
    30  )
    31  
    32  func TestArgs_ReplaceEnv_Invalid(t *testing.T) {
    33  	input := "${FOO}"
    34  	exp := input
    35  	act := ReplaceEnv(input, envVars)
    36  
    37  	if !reflect.DeepEqual(act, exp) {
    38  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    39  	}
    40  }
    41  
    42  func TestArgs_ReplaceEnv_Valid(t *testing.T) {
    43  	input := fmt.Sprintf(`"${%v}"!`, ipKey)
    44  	exp := fmt.Sprintf("\"%s\"!", ipVal)
    45  	act := ReplaceEnv(input, envVars)
    46  
    47  	if !reflect.DeepEqual(act, exp) {
    48  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    49  	}
    50  }
    51  
    52  func TestArgs_ReplaceEnv_Period(t *testing.T) {
    53  	input := fmt.Sprintf(`"${%v}"!`, periodKey)
    54  	exp := fmt.Sprintf("\"%s\"!", periodVal)
    55  	act := ReplaceEnv(input, envVars)
    56  
    57  	if !reflect.DeepEqual(act, exp) {
    58  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    59  	}
    60  }
    61  
    62  func TestArgs_ReplaceEnv_Dash(t *testing.T) {
    63  	input := fmt.Sprintf(`"${%v}"!`, dashKey)
    64  	exp := fmt.Sprintf("\"%s\"!", dashVal)
    65  	act := ReplaceEnv(input, envVars)
    66  
    67  	if !reflect.DeepEqual(act, exp) {
    68  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    69  	}
    70  }
    71  
    72  func TestArgs_ReplaceEnv_Chained(t *testing.T) {
    73  	input := fmt.Sprintf("${%s}${%s}", ipKey, portKey)
    74  	exp := fmt.Sprintf("%s%s", ipVal, portVal)
    75  	act := ReplaceEnv(input, envVars)
    76  
    77  	if !reflect.DeepEqual(act, exp) {
    78  		t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
    79  	}
    80  }
    81  
    82  func TestArgs_ContainsEnv(t *testing.T) {
    83  	positiveCases := []string{
    84  		"test-${env_var}",
    85  	}
    86  	for _, c := range positiveCases {
    87  		t.Run(fmt.Sprintf("positive case: %v", c), func(t *testing.T) {
    88  			if !ContainsEnv(c) {
    89  				t.Fatalf("ContainsEnv(%v) returned false; want true", c)
    90  			}
    91  		})
    92  	}
    93  
    94  	negativeCases := []string{
    95  		"test",
    96  		"test-$",
    97  		"test-${asdf",
    98  		"test-{asdf}",
    99  		"$test",
   100  	}
   101  	for _, c := range negativeCases {
   102  		t.Run(fmt.Sprintf("negative case: %v", c), func(t *testing.T) {
   103  			if ContainsEnv(c) {
   104  				t.Fatalf("ContainsEnv(%v) returned true; want false", c)
   105  			}
   106  		})
   107  	}
   108  
   109  }