github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/nomad/structs/config/consul_test.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "os/exec" 8 "testing" 9 "time" 10 11 consulapi "github.com/hashicorp/consul/api" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestMain(m *testing.M) { 17 if os.Getenv("NOMAD_ENV_TEST") != "1" { 18 os.Exit(m.Run()) 19 } 20 21 // Encode the default config as json to stdout for testing env var 22 // handling. 23 if err := json.NewEncoder(os.Stdout).Encode(DefaultConsulConfig()); err != nil { 24 fmt.Fprintf(os.Stderr, "error encoding config: %v", err) 25 os.Exit(2) 26 } 27 28 os.Exit(0) 29 } 30 31 func TestConsulConfig_Merge(t *testing.T) { 32 yes, no := true, false 33 34 c1 := &ConsulConfig{ 35 ServerServiceName: "1", 36 ServerHTTPCheckName: "1", 37 ServerSerfCheckName: "1", 38 ServerRPCCheckName: "1", 39 ClientServiceName: "1", 40 ClientHTTPCheckName: "1", 41 Tags: []string{"a", "1"}, 42 AutoAdvertise: &no, 43 ChecksUseAdvertise: &no, 44 Addr: "1", 45 GRPCAddr: "1", 46 Timeout: time.Duration(1), 47 TimeoutHCL: "1", 48 Token: "1", 49 AllowUnauthenticated: &no, 50 Auth: "1", 51 EnableSSL: &no, 52 VerifySSL: &no, 53 CAFile: "1", 54 CertFile: "1", 55 KeyFile: "1", 56 ServerAutoJoin: &no, 57 ClientAutoJoin: &no, 58 ExtraKeysHCL: []string{"a", "1"}, 59 } 60 61 c2 := &ConsulConfig{ 62 ServerServiceName: "2", 63 ServerHTTPCheckName: "2", 64 ServerSerfCheckName: "2", 65 ServerRPCCheckName: "2", 66 ClientServiceName: "2", 67 ClientHTTPCheckName: "2", 68 Tags: []string{"b", "2"}, 69 AutoAdvertise: &yes, 70 ChecksUseAdvertise: &yes, 71 Addr: "2", 72 GRPCAddr: "2", 73 Timeout: time.Duration(2), 74 TimeoutHCL: "2", 75 Token: "2", 76 AllowUnauthenticated: &yes, 77 Auth: "2", 78 EnableSSL: &yes, 79 VerifySSL: &yes, 80 CAFile: "2", 81 CertFile: "2", 82 KeyFile: "2", 83 ServerAutoJoin: &yes, 84 ClientAutoJoin: &yes, 85 ExtraKeysHCL: []string{"b", "2"}, 86 } 87 88 exp := &ConsulConfig{ 89 ServerServiceName: "2", 90 ServerHTTPCheckName: "2", 91 ServerSerfCheckName: "2", 92 ServerRPCCheckName: "2", 93 ClientServiceName: "2", 94 ClientHTTPCheckName: "2", 95 Tags: []string{"a", "1", "b", "2"}, 96 AutoAdvertise: &yes, 97 ChecksUseAdvertise: &yes, 98 Addr: "2", 99 GRPCAddr: "2", 100 Timeout: time.Duration(2), 101 TimeoutHCL: "2", 102 Token: "2", 103 AllowUnauthenticated: &yes, 104 Auth: "2", 105 EnableSSL: &yes, 106 VerifySSL: &yes, 107 CAFile: "2", 108 CertFile: "2", 109 KeyFile: "2", 110 ServerAutoJoin: &yes, 111 ClientAutoJoin: &yes, 112 ExtraKeysHCL: []string{"a", "1"}, // not merged 113 } 114 115 result := c1.Merge(c2) 116 require.Equal(t, exp, result) 117 } 118 119 // TestConsulConfig_Defaults asserts Consul defaults are copied from their 120 // upstream API package defaults. 121 func TestConsulConfig_Defaults(t *testing.T) { 122 t.Parallel() 123 124 nomadDef := DefaultConsulConfig() 125 consulDef := consulapi.DefaultConfig() 126 127 require.Equal(t, consulDef.Address, nomadDef.Addr) 128 require.NotZero(t, nomadDef.Addr) 129 require.Equal(t, consulDef.Scheme == "https", *nomadDef.EnableSSL) 130 require.Equal(t, !consulDef.TLSConfig.InsecureSkipVerify, *nomadDef.VerifySSL) 131 require.Equal(t, consulDef.TLSConfig.CAFile, nomadDef.CAFile) 132 } 133 134 // TestConsulConfig_Exec asserts Consul defaults use env vars when they are 135 // set by forking a subprocess. 136 func TestConsulConfig_Exec(t *testing.T) { 137 t.Parallel() 138 139 self, err := os.Executable() 140 if err != nil { 141 t.Fatalf("error finding test binary: %v", err) 142 } 143 144 cmd := exec.Command(self) 145 cmd.Env = []string{ 146 "NOMAD_ENV_TEST=1", 147 "CONSUL_CACERT=cacert", 148 "CONSUL_HTTP_ADDR=addr", 149 "CONSUL_HTTP_SSL=1", 150 "CONSUL_HTTP_SSL_VERIFY=1", 151 } 152 153 out, err := cmd.Output() 154 if err != nil { 155 if eerr, ok := err.(*exec.ExitError); ok { 156 t.Fatalf("exit error code %d; output:\n%s", eerr.ExitCode(), string(eerr.Stderr)) 157 } 158 t.Fatalf("error running command %q: %v", self, err) 159 } 160 161 conf := ConsulConfig{} 162 require.NoError(t, json.Unmarshal(out, &conf)) 163 assert.Equal(t, "cacert", conf.CAFile) 164 assert.Equal(t, "addr", conf.Addr) 165 require.NotNil(t, conf.EnableSSL) 166 assert.True(t, *conf.EnableSSL) 167 require.NotNil(t, conf.VerifySSL) 168 assert.True(t, *conf.VerifySSL) 169 }