From 3ee0ae8e53a259a563a1cd42491468d56dbdd5ce Mon Sep 17 00:00:00 2001 From: Viktor Date: Sun, 4 Dec 2016 02:13:11 +0100 Subject: [PATCH] Fixes #31 --- Dockerfile | 2 ++ haproxy.tmpl | 2 +- integration_tests/integration_test.go | 18 +++++++------- proxy/ha_proxy.go | 34 +++++++++++++++++---------- proxy/ha_proxy_test.go | 31 ++++++++++++------------ proxy/test_configs/tmpl/haproxy.tmpl | 2 +- server_test.go | 2 +- 7 files changed, 52 insertions(+), 39 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2cd70ce5..f52a3428 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,6 +24,8 @@ ENV TIMEOUT_SERVER "20" ENV TIMEOUT_QUEUE "30" ENV TIMEOUT_HTTP_REQUEST "5" ENV TIMEOUT_HTTP_KEEP_ALIVE "15" +ENV STATS_USER "admin" +ENV STATS_PASS "admin" EXPOSE 80 EXPOSE 443 diff --git a/haproxy.tmpl b/haproxy.tmpl index 89186fc9..163d19d9 100644 --- a/haproxy.tmpl +++ b/haproxy.tmpl @@ -23,7 +23,7 @@ defaults stats enable stats refresh 30s stats realm Strictly\ Private - stats auth admin:admin + stats auth {{.StatsUser}}:{{.StatsPass}} stats uri /admin?stats frontend services diff --git a/integration_tests/integration_test.go b/integration_tests/integration_test.go index 40f8c6d1..07d4970a 100644 --- a/integration_tests/integration_test.go +++ b/integration_tests/integration_test.go @@ -188,15 +188,15 @@ func (s IntegrationTestSuite) Test_Certs() { s.Equal(200, resp.StatusCode) -// // HTTPS works -// url = fmt.Sprintf("https://%s:8080/v2/test", os.Getenv("DOCKER_IP")) -// req, _ = http.NewRequest("GET", url, nil) -// client = &http.Client{} -// -// resp, err := client.Do(req) -// -// s.NoError(err) -// s.Equal(200, resp.StatusCode) + // // HTTPS works + // url = fmt.Sprintf("https://%s:8080/v2/test", os.Getenv("DOCKER_IP")) + // req, _ = http.NewRequest("GET", url, nil) + // client = &http.Client{} + // + // resp, err := client.Do(req) + // + // s.NoError(err) + // s.Equal(200, resp.StatusCode) // Can retrieve certs url = fmt.Sprintf("http://%s:8080/v1/docker-flow-proxy/certs", os.Getenv("DOCKER_IP")) diff --git a/proxy/ha_proxy.go b/proxy/ha_proxy.go index dd29dec1..2363d68e 100644 --- a/proxy/ha_proxy.go +++ b/proxy/ha_proxy.go @@ -18,13 +18,15 @@ type HaProxy struct { var Instance Proxy type ConfigData struct { - CertsString string - TimeoutConnect string - TimeoutClient string - TimeoutServer string - TimeoutQueue string - TimeoutHttpRequest string + CertsString string + TimeoutConnect string + TimeoutClient string + TimeoutServer string + TimeoutQueue string + TimeoutHttpRequest string TimeoutHttpKeepAlive string + StatsUser string + StatsPass string } func NewHaProxy(templatesPath, configsPath string, certs map[string]bool) Proxy { @@ -147,13 +149,15 @@ func (m HaProxy) getConfigData() ConfigData { } } d := ConfigData{ - CertsString: strings.Join(certs, " "), - TimeoutConnect: "5", - TimeoutClient: "20", - TimeoutServer: "20", - TimeoutQueue: "30", - TimeoutHttpRequest: "5", + CertsString: strings.Join(certs, " "), + TimeoutConnect: "5", + TimeoutClient: "20", + TimeoutServer: "20", + TimeoutQueue: "30", + TimeoutHttpRequest: "5", TimeoutHttpKeepAlive: "15", + StatsUser: "admin", + StatsPass: "admin", } if len(os.Getenv("TIMEOUT_CONNECT")) > 0 { d.TimeoutConnect = os.Getenv("TIMEOUT_CONNECT") @@ -173,5 +177,11 @@ func (m HaProxy) getConfigData() ConfigData { if len(os.Getenv("TIMEOUT_HTTP_KEEP_ALIVE")) > 0 { d.TimeoutHttpKeepAlive = os.Getenv("TIMEOUT_HTTP_KEEP_ALIVE") } + if len(os.Getenv("STATS_USER")) > 0 { + d.StatsUser = os.Getenv("STATS_USER") + } + if len(os.Getenv("STATS_PASS")) > 0 { + d.StatsPass = os.Getenv("STATS_PASS") + } return d } diff --git a/proxy/ha_proxy_test.go b/proxy/ha_proxy_test.go index d2abe473..bd615277 100644 --- a/proxy/ha_proxy_test.go +++ b/proxy/ha_proxy_test.go @@ -7,17 +7,17 @@ import ( "github.com/stretchr/testify/suite" "os" "os/exec" - "testing" "strings" + "testing" ) // Setup type HaProxyTestSuite struct { suite.Suite - TemplatesPath string - ConfigsPath string - Pid string + TemplatesPath string + ConfigsPath string + Pid string TemplateContent string ServicesContent string } @@ -147,24 +147,26 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsCert() { s.Equal(expectedData, actualData) } -func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_ReplacesTimeoutsWithEnvVars() { +func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_ReplacesValuesWithEnvVars() { tests := []struct { envKey string before string - after string + after string + value string }{ - { "TIMEOUT_CONNECT", "timeout connect 5s", "timeout connect 999s" }, - { "TIMEOUT_CLIENT", "timeout client 20s", "timeout client 999s" }, - { "TIMEOUT_SERVER", "timeout server 20s", "timeout server 999s" }, - { "TIMEOUT_QUEUE", "timeout queue 30s", "timeout queue 999s" }, - { "TIMEOUT_HTTP_REQUEST", "timeout http-request 5s", "timeout http-request 999s" }, - { "TIMEOUT_HTTP_KEEP_ALIVE", "timeout http-keep-alive 15s", "timeout http-keep-alive 999s" }, + {"TIMEOUT_CONNECT", "timeout connect 5s", "timeout connect 999s", "999"}, + {"TIMEOUT_CLIENT", "timeout client 20s", "timeout client 999s", "999"}, + {"TIMEOUT_SERVER", "timeout server 20s", "timeout server 999s", "999"}, + {"TIMEOUT_QUEUE", "timeout queue 30s", "timeout queue 999s", "999"}, + {"TIMEOUT_HTTP_REQUEST", "timeout http-request 5s", "timeout http-request 999s", "999"}, + {"TIMEOUT_HTTP_KEEP_ALIVE", "timeout http-keep-alive 15s", "timeout http-keep-alive 999s", "999"}, + {"STATS_USER", "stats auth admin:admin", "stats auth my-user:admin", "my-user"}, + {"STATS_PASS", "stats auth admin:admin", "stats auth admin:my-pass", "my-pass"}, } for _, t := range tests { timeoutOrig := os.Getenv(t.envKey) - os.Setenv(t.envKey, "999") + os.Setenv(t.envKey, t.value) var actualFilename string - expectedFilename := fmt.Sprintf("%s/haproxy.cfg", s.ConfigsPath) var actualData string expectedData := fmt.Sprintf( "%s%s", @@ -179,7 +181,6 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_ReplacesTimeoutsWithEnv NewHaProxy(s.TemplatesPath, s.ConfigsPath, map[string]bool{}).CreateConfigFromTemplates() - s.Equal(expectedFilename, actualFilename) s.Equal(expectedData, actualData) os.Setenv(t.envKey, timeoutOrig) diff --git a/proxy/test_configs/tmpl/haproxy.tmpl b/proxy/test_configs/tmpl/haproxy.tmpl index 89186fc9..163d19d9 100644 --- a/proxy/test_configs/tmpl/haproxy.tmpl +++ b/proxy/test_configs/tmpl/haproxy.tmpl @@ -23,7 +23,7 @@ defaults stats enable stats refresh 30s stats realm Strictly\ Private - stats auth admin:admin + stats auth {{.StatsUser}}:{{.StatsPass}} stats uri /admin?stats frontend services diff --git a/server_test.go b/server_test.go index 67761d25..77152bf0 100644 --- a/server_test.go +++ b/server_test.go @@ -516,7 +516,7 @@ func (s *ServerTestSuite) Test_ServeHTTP_InvokesReconfigureExecute() { s.ServiceReconfigure.AclName = "my-acl" url := fmt.Sprintf("%s&aclName=my-acl", s.ReconfigureUrl) req, _ := http.NewRequest("GET", url, nil) -// s.RequestReconfigure.u + // s.RequestReconfigure.u s.invokesReconfigure(req, true) }