open-nomad/helper/args/args_test.go

107 lines
2.3 KiB
Go
Raw Normal View History

2015-09-26 22:37:48 +00:00
package args
import (
"fmt"
"reflect"
"testing"
)
const (
2016-01-11 17:58:26 +00:00
ipKey = "NOMAD_IP"
ipVal = "127.0.0.1"
portKey = "NOMAD_PORT_WEB"
portVal = ":80"
periodKey = "NOMAD.PERIOD"
periodVal = "period"
2016-02-27 16:42:33 +00:00
dashKey = "NOMAD-DASH"
dashVal = "dash"
2015-09-26 22:37:48 +00:00
)
var (
envVars = map[string]string{
2016-01-11 17:58:26 +00:00
ipKey: ipVal,
portKey: portVal,
periodKey: periodVal,
2016-02-27 16:42:33 +00:00
dashKey: dashVal,
2015-09-26 22:37:48 +00:00
}
)
2016-01-11 17:58:26 +00:00
func TestArgs_ReplaceEnv_Invalid(t *testing.T) {
2016-02-05 00:50:20 +00:00
input := "${FOO}"
exp := input
2016-01-11 17:58:26 +00:00
act := ReplaceEnv(input, envVars)
2015-09-26 22:37:48 +00:00
if !reflect.DeepEqual(act, exp) {
2016-01-11 17:58:26 +00:00
t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
2015-09-26 22:37:48 +00:00
}
}
2016-01-11 17:58:26 +00:00
func TestArgs_ReplaceEnv_Valid(t *testing.T) {
2016-02-05 00:50:20 +00:00
input := fmt.Sprintf(`"${%v}"!`, ipKey)
2016-01-11 17:58:26 +00:00
exp := fmt.Sprintf("\"%s\"!", ipVal)
act := ReplaceEnv(input, envVars)
2015-09-26 22:37:48 +00:00
if !reflect.DeepEqual(act, exp) {
2016-01-11 17:58:26 +00:00
t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
2015-09-26 22:37:48 +00:00
}
}
2016-01-11 17:58:26 +00:00
func TestArgs_ReplaceEnv_Period(t *testing.T) {
2016-02-05 00:50:20 +00:00
input := fmt.Sprintf(`"${%v}"!`, periodKey)
2016-01-11 17:58:26 +00:00
exp := fmt.Sprintf("\"%s\"!", periodVal)
act := ReplaceEnv(input, envVars)
2015-09-26 22:37:48 +00:00
if !reflect.DeepEqual(act, exp) {
2016-01-11 17:58:26 +00:00
t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
}
}
2016-02-27 16:42:33 +00:00
func TestArgs_ReplaceEnv_Dash(t *testing.T) {
input := fmt.Sprintf(`"${%v}"!`, dashKey)
exp := fmt.Sprintf("\"%s\"!", dashVal)
act := ReplaceEnv(input, envVars)
if !reflect.DeepEqual(act, exp) {
t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
}
}
2016-01-11 17:58:26 +00:00
func TestArgs_ReplaceEnv_Chained(t *testing.T) {
2016-02-05 00:50:20 +00:00
input := fmt.Sprintf("${%s}${%s}", ipKey, portKey)
2016-01-11 17:58:26 +00:00
exp := fmt.Sprintf("%s%s", ipVal, portVal)
act := ReplaceEnv(input, envVars)
if !reflect.DeepEqual(act, exp) {
t.Fatalf("ReplaceEnv(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
2015-09-26 22:37:48 +00:00
}
}
func TestArgs_ContainsEnv(t *testing.T) {
positiveCases := []string{
"test-${env_var}",
}
for _, c := range positiveCases {
t.Run(fmt.Sprintf("positive case: %v", c), func(t *testing.T) {
if !ContainsEnv(c) {
t.Fatalf("ContainsEnv(%v) returned false; want true", c)
}
})
}
negativeCases := []string{
"test",
"test-$",
"test-${asdf",
2018-10-30 17:58:52 +00:00
"test-{asdf}",
"$test",
}
for _, c := range negativeCases {
2018-10-30 17:58:52 +00:00
t.Run(fmt.Sprintf("negative case: %v", c), func(t *testing.T) {
if ContainsEnv(c) {
t.Fatalf("ContainsEnv(%v) returned true; want false", c)
}
})
}
}