From c70d25eaeac62fb07fadb13d95dec7126b992581 Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Thu, 12 Jul 2018 07:43:51 -0400 Subject: [PATCH] Add some tests for GenerateEnv --- api/api_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/api/api_test.go b/api/api_test.go index f06bc1b30..407d9994a 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -15,6 +15,7 @@ import ( "time" "github.com/hashicorp/consul/testutil" + "github.com/stretchr/testify/require" ) type configCallback func(c *Config) @@ -548,3 +549,73 @@ func TestAPI_IsRetryableError(t *testing.T) { t.Fatal("should be a retryable error") } } + +func TestAPI_GenerateEnv(t *testing.T) { + t.Parallel() + + c := &Config{ + Address: "127.0.0.1:8500", + Token: "test", + Scheme: "http", + TLSConfig: TLSConfig{ + CAFile: "", + CAPath: "", + CertFile: "", + KeyFile: "", + Address: "", + InsecureSkipVerify: true, + }, + } + + expected := []string{ + "CONSUL_HTTP_ADDR=127.0.0.1:8500", + "CONSUL_HTTP_TOKEN=test", + "CONSUL_HTTP_SSL=false", + "CONSUL_CACERT=", + "CONSUL_CAPATH=", + "CONSUL_CLIENT_CERT=", + "CONSUL_CLIENT_KEY=", + "CONSUL_TLS_SERVER_NAME=", + "CONSUL_HTTP_SSL_VERIFY=false", + "CONSUL_HTTP_AUTH=", + } + + require.Equal(t, expected, c.GenerateEnv()) +} + +func TestAPI_GenerateEnvHTTPS(t *testing.T) { + t.Parallel() + + c := &Config{ + Address: "127.0.0.1:8500", + Token: "test", + Scheme: "https", + TLSConfig: TLSConfig{ + CAFile: "/var/consul/ca.crt", + CAPath: "/var/consul/ca.dir", + CertFile: "/var/consul/server.crt", + KeyFile: "/var/consul/ssl/server.key", + Address: "127.0.0.1:8500", + InsecureSkipVerify: false, + }, + HttpAuth: &HttpBasicAuth{ + Username: "user", + Password: "password", + }, + } + + expected := []string{ + "CONSUL_HTTP_ADDR=127.0.0.1:8500", + "CONSUL_HTTP_TOKEN=test", + "CONSUL_HTTP_SSL=true", + "CONSUL_CACERT=/var/consul/ca.crt", + "CONSUL_CAPATH=/var/consul/ca.dir", + "CONSUL_CLIENT_CERT=/var/consul/server.crt", + "CONSUL_CLIENT_KEY=/var/consul/ssl/server.key", + "CONSUL_TLS_SERVER_NAME=127.0.0.1:8500", + "CONSUL_HTTP_SSL_VERIFY=true", + "CONSUL_HTTP_AUTH=user:password", + } + + require.Equal(t, expected, c.GenerateEnv()) +}