github.com/mailgun/holster/v4@v4.20.0/consul/config_test.go (about) 1 package consul_test 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/consul/api" 7 "github.com/mailgun/holster/v4/consul" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestNewClientTLS(t *testing.T) { 13 t.Skip("TODO: https://github.com/mailgun/holster/issues/151") 14 15 t.Setenv("CONSUL_HTTP_ADDR", "https://127.0.0.1:8501") 16 t.Setenv("CONSUL_CLIENT_CERT", "config/dc1-server-consul-0.pem") 17 t.Setenv("CONSUL_CLIENT_KEY", "config/dc1-server-consul-0-key.pem") 18 t.Setenv("CONSUL_CACERT", "config/consul-agent-ca.pem") 19 20 client, err := consul.NewClient(nil) 21 require.NoError(t, err) 22 23 kv := api.KVPair{ 24 Key: "test-key-tls", 25 Value: []byte("test-value-tls"), 26 } 27 _, err = client.KV().Put(&kv, nil) 28 require.NoError(t, err) 29 resp, _, err := client.KV().Get("test-key-tls", nil) 30 require.NoError(t, err) 31 assert.Equal(t, resp.Key, "test-key-tls") 32 assert.Equal(t, resp.Value, []byte("test-value-tls")) 33 } 34 35 func TestNewClient(t *testing.T) { 36 client, err := consul.NewClient(nil) 37 require.NoError(t, err) 38 39 kv := api.KVPair{ 40 Key: "test-key", 41 Value: []byte("test-value"), 42 } 43 _, err = client.KV().Put(&kv, nil) 44 require.NoError(t, err) 45 resp, _, err := client.KV().Get("test-key", nil) 46 require.NoError(t, err) 47 assert.Equal(t, resp.Key, "test-key") 48 assert.Equal(t, resp.Value, []byte("test-value")) 49 } 50 51 func TestEnvHasConsulConfig(t *testing.T) { 52 t.Setenv("CONSUL_HTTP_ADDR", "127.0.0.1:8500") 53 assert.True(t, consul.EnvHasConsulConfig()) 54 } 55 56 func TestNewConfig(t *testing.T) { 57 t.Setenv("CONSUL_HTTP_AUTH", "username:password") 58 t.Setenv("CONSUL_HTTP_SSL_VERIFY", "true") 59 60 cfg := api.DefaultConfig() 61 cfg, err := consul.NewConfig(cfg) 62 require.NoError(t, err) 63 64 assert.Equal(t, "username", cfg.HttpAuth.Username) 65 assert.Equal(t, "password", cfg.HttpAuth.Password) 66 assert.Equal(t, true, cfg.TLSConfig.InsecureSkipVerify) 67 }