Skip to content

Commit

Permalink
Updated consul template requests
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed May 27, 2016
1 parent 4b65b8b commit 70d76f0
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
env:
- VERSION=1.0
- VERSION=1.0.1

language: go

Expand Down
3 changes: 2 additions & 1 deletion flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ func (m Flow) Proxy(opts Opts, proxy Proxy) error {
opts.ServiceName,
color,
opts.ServicePath,
opts.ConsulTemplatePath,
opts.ConsulTemplateFePath,
opts.ConsulTemplateBePath,
); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ func (s FlowTestSuite) Test_Proxy_InvokesReconfigure_WhenDeploy() {
s.opts.NextColor,
s.opts.ServicePath,
"",
"",
)
}

Expand All @@ -530,6 +531,7 @@ func (s FlowTestSuite) Test_Proxy_InvokesReconfigure_WhenScale() {
s.opts.CurrentColor,
s.opts.ServicePath,
"",
"",
)
}

Expand All @@ -546,6 +548,7 @@ func (s FlowTestSuite) Test_Proxy_ReturnsError_WhenReconfigureFails() {
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(fmt.Errorf("This is an error"))

actual := Flow{}.Proxy(s.opts, mockObj)
Expand Down
34 changes: 22 additions & 12 deletions ha_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func (m HaProxy) Provision(dockerHost, reconfPort, certPath, scAddress string) e
func (m HaProxy) Reconfigure(
dockerHost, dockerCertPath, host, reconfPort, serviceName, serviceColor string,
servicePath []string,
consulTemplatePath string,
consulTemplateFePath string, consulTemplateBePath string,
) error {
if len(consulTemplatePath) > 0 {
if err := m.sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName, serviceColor); err != nil {
if len(consulTemplateFePath) > 0 {
if err := m.sendConsulTemplatesToTheProxy(dockerHost, dockerCertPath, consulTemplateFePath, consulTemplateBePath, serviceName, serviceColor); err != nil {
return err
}
} else if len(servicePath) == 0 {
Expand All @@ -78,7 +78,7 @@ func (m HaProxy) Reconfigure(
if len(reconfPort) == 0 && !strings.Contains(host, ":") {
return fmt.Errorf("Reconfigure port is mandatory.")
}
if err := m.sendReconfigureRequest(host, reconfPort, serviceName, serviceColor, servicePath, consulTemplatePath); err != nil {
if err := m.sendReconfigureRequest(host, reconfPort, serviceName, serviceColor, servicePath, consulTemplateFePath, consulTemplateBePath); err != nil {
return err
}
return nil
Expand All @@ -87,7 +87,7 @@ func (m HaProxy) Reconfigure(
func (m HaProxy) sendReconfigureRequest(
host, reconfPort, serviceName, serviceColor string,
servicePath []string,
consulTemplatePath string,
consulTemplateFePath, consulTemplateBePath string,
) error {
address := host
if len(reconfPort) > 0 {
Expand All @@ -101,8 +101,8 @@ func (m HaProxy) sendReconfigureRequest(
address,
serviceName,
)
if len(consulTemplatePath) > 0 {
proxyUrl = fmt.Sprintf("%s&consulTemplatePath=%s/%s.tmpl", proxyUrl, ConsulTemplatesDir, serviceName)
if len(consulTemplateFePath) > 0 {
proxyUrl = fmt.Sprintf("%s&consulTemplateFePath=%s/%s-fe.tmpl&consulTemplateBePath=%s/%s-be.tmpl", proxyUrl, ConsulTemplatesDir, serviceName, ConsulTemplatesDir, serviceName)
} else {
if len(serviceColor) > 0 {
proxyUrl = fmt.Sprintf("%s&serviceColor=%s", proxyUrl, serviceColor)
Expand All @@ -121,19 +121,29 @@ func (m HaProxy) sendReconfigureRequest(
return nil
}

func (m HaProxy) sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName, color string) error {
func (m HaProxy) sendConsulTemplatesToTheProxy(dockerHost, dockerCertPath, consulTemplateFePath, consulTemplateBePath, serviceName, color string) error {
if err := m.sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplateFePath, serviceName, color, "fe"); err != nil {
return err
}
if err := m.sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplateBePath, serviceName, color, "be"); err != nil {
return err
}
return nil
}

func (m HaProxy) sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName, color, templateType string) error {
if err := m.createTempConsulTemplate(consulTemplatePath, serviceName, color); err != nil {
return err
}
if err := m.copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName); err != nil {
file := fmt.Sprintf("%s-%s.tmpl", serviceName, templateType)
if err := m.copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, file); err != nil {
return err
}
removeFile(fmt.Sprintf("%s.tmp", consulTemplatePath))

return nil
}

func (m HaProxy) copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName string) error {
func (m HaProxy) copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, templateName string) error {
SetDockerHost(dockerHost, dockerCertPath)
args := []string{"exec", "-i", "docker-flow-proxy", "mkdir", "-p", ConsulTemplatesDir}
execCmd := exec.Command("docker", args...)
Expand All @@ -146,7 +156,7 @@ func (m HaProxy) copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consul
args = []string{
"cp",
fmt.Sprintf("%s.tmp", consulTemplatePath),
fmt.Sprintf("docker-flow-proxy:%s/%s.tmpl", ConsulTemplatesDir, serviceName),
fmt.Sprintf("docker-flow-proxy:%s/%s", ConsulTemplatesDir, templateName),
}
cpCmd := exec.Command("docker", args...)
cpCmd.Stdout = os.Stdout
Expand Down
86 changes: 51 additions & 35 deletions ha_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,25 +230,25 @@ func (s HaProxyTestSuite) Test_Provision_ReturnsError_WhenStartFailure() {
// Reconfigure

func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenProxyHostIsEmpty() {
err := HaProxy{}.Reconfigure("", "", "", s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "")
err := HaProxy{}.Reconfigure("", "", "", s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "", "")

s.Error(err)
}

func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenProjectIsEmpty() {
err := HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, "", s.Color, s.ServicePath, "")
err := HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, "", s.Color, s.ServicePath, "", "")

s.Error(err)
}

func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenServicePathAndConsulTemplatePathAreEmpty() {
err := HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, []string{""}, "")
err := HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, []string{""}, "", "")

s.Error(err)
}

func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenReconfPortIsEmpty() {
err := HaProxy{}.Reconfigure("", "", s.Host, "", s.ServiceName, s.Color, s.ServicePath, "")
err := HaProxy{}.Reconfigure("", "", s.Host, "", s.ServiceName, s.Color, s.ServicePath, "", "")

s.Error(err)
}
Expand All @@ -270,7 +270,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequest() {
return nil, fmt.Errorf("This is an HTTP error")
}

HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "")
HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "", "")

s.Equal(expected, actual)
}
Expand All @@ -291,7 +291,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequestWithOutColor_WhenNotB
return nil, fmt.Errorf("This is an HTTP error")
}

HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, "", s.ServicePath, "")
HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, "", s.ServicePath, "", "")

s.Equal(expected, actual)
}
Expand All @@ -312,7 +312,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequestWithPrependedHttp() {
return nil, fmt.Errorf("This is an HTTP error")
}

HaProxy{}.Reconfigure("", "", "my-docker-proxy-host.com", s.ReconfPort, s.ServiceName, "", s.ServicePath, "")
HaProxy{}.Reconfigure("", "", "my-docker-proxy-host.com", s.ReconfPort, s.ServiceName, "", s.ServicePath, "", "")

s.Equal(expected, actual)
}
Expand All @@ -324,7 +324,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenRequestFails() {
return nil, fmt.Errorf("This is an HTTP error")
}

err := HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "")
err := HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "", "")

s.Error(err)
}
Expand All @@ -334,15 +334,15 @@ func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenResponseCodeIsNot2xx
w.WriteHeader(http.StatusBadRequest)
}))

err := HaProxy{}.Reconfigure("", "", server.URL, "", s.ServiceName, s.Color, s.ServicePath, "")
err := HaProxy{}.Reconfigure("", "", server.URL, "", s.ServiceName, s.Color, s.ServicePath, "", "")

s.Error(err)
}

func (s HaProxyTestSuite) Test_Reconfigure_SetsDockerHost_WhenConsulTemplatePathIsPresent() {
os.Unsetenv("DOCKER_HOST")

err := HaProxy{}.Reconfigure(s.DockerHost, s.DockerCertPath, s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
err := HaProxy{}.Reconfigure(s.DockerHost, s.DockerCertPath, s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/fe/template", "/path/to/consul/be/template")

s.NoError(err)
s.Equal(s.DockerHost, os.Getenv("DOCKER_HOST"))
Expand All @@ -357,7 +357,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_CreatesConsulTemplatesDirectory_WhenC
return nil
}

HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/fe/template", "/path/to/consul/be/template")

s.Equal(expected, actual)
}
Expand All @@ -369,30 +369,38 @@ func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenDirectoryCreationFai
return fmt.Errorf("This is an docker exec error")
}

actual := HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
actual := HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/fe/template", "/path/to/consul/be/template")

s.Error(actual)
}

func (s HaProxyTestSuite) Test_Reconfigure_CopiesTemplate_WhenConsulTemplatePathIsPresent() {
consulTemplatePath := "/path/to/consul/template"
var actual []string
expected := []string{
func (s HaProxyTestSuite) Test_Reconfigure_CopiesTemplates_WhenConsulTemplatePathIsPresent() {
fePath := "/path/to/consul/fe/template"
bePath := "/path/to/consul/be/template"
var actual [][]string
feExpected := []string{
"docker",
"cp",
fmt.Sprintf("%s.tmp", fePath),
fmt.Sprintf("docker-flow-proxy:/consul_templates/%s-fe.tmpl", s.ServiceName),
}
beExpected := []string{
"docker",
"cp",
fmt.Sprintf("%s.tmp", consulTemplatePath),
fmt.Sprintf("docker-flow-proxy:/consul_templates/%s.tmpl", s.ServiceName),
fmt.Sprintf("%s.tmp", bePath),
fmt.Sprintf("docker-flow-proxy:/consul_templates/%s-be.tmpl", s.ServiceName),
}
runHaProxyCpCmdOrig := runHaProxyCpCmd
defer func() { runHaProxyCpCmd = runHaProxyCpCmdOrig }()
runHaProxyCpCmd = func(cmd *exec.Cmd) error {
actual = cmd.Args
actual = append(actual, cmd.Args)
return nil
}

HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, fePath, bePath)

s.Equal(expected, actual)
s.Equal(feExpected, actual[0])
s.Equal(beExpected, actual[1])
}

func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenTemplateCopyFails() {
Expand All @@ -402,7 +410,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenTemplateCopyFails()
return fmt.Errorf("This is an docker cp error")
}

actual := HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
actual := HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/fe/template", "/path/to/consul/be/template")

s.Error(actual)
}
Expand All @@ -413,25 +421,28 @@ func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequestWithConsulTemplatePat
actual = fmt.Sprintf("%s?%s", r.URL.Path, r.URL.RawQuery)
}))
expected := fmt.Sprintf(
"/v1/docker-flow-proxy/reconfigure?serviceName=%s&consulTemplatePath=/consul_templates/%s.tmpl",
"/v1/docker-flow-proxy/reconfigure?serviceName=%s&consulTemplateFePath=/consul_templates/%s-fe.tmpl&consulTemplateBePath=/consul_templates/%s-be.tmpl",
s.ServiceName,
s.ServiceName,
s.ServiceName,
)

HaProxy{}.Reconfigure("", "", server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
HaProxy{}.Reconfigure("", "", server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/fe/template", "/path/to/consul/be/template")

s.Equal(expected, actual)
}

func (s HaProxyTestSuite) Test_Reconfigure_CreatesTempTemplateFile() {
actualFilename := ""
fePath := "/path/to/consul/fe/template"
bePath := "/path/to/consul/be/template"
var actualFilenames []string
actualData := ""
data := "This is a %s template"
expectedData := fmt.Sprintf(data, s.ServiceName+"-"+s.Color)
writeFileOrig := writeFile
defer func() { writeFile = writeFileOrig }()
writeFile = func(filename string, data []byte, perm os.FileMode) error {
actualFilename = filename
actualFilenames = append(actualFilenames, filename)
actualData = string(data)
return nil
}
Expand All @@ -441,9 +452,10 @@ func (s HaProxyTestSuite) Test_Reconfigure_CreatesTempTemplateFile() {
return []byte(fmt.Sprintf(data, "SERVICE_NAME")), nil
}

HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, fePath, bePath)

s.Equal("/path/to/consul/template.tmp", actualFilename)
s.Equal(fePath+".tmp", actualFilenames[0])
s.Equal(bePath+".tmp", actualFilenames[1])
s.Equal(expectedData, actualData)
}

Expand All @@ -454,7 +466,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenTemplateFileReadFail
return []byte(""), fmt.Errorf("This is an read file error")
}

err := HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
err := HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/fe/template", "/path/to/consul/be/template")

s.Error(err)
}
Expand All @@ -466,23 +478,27 @@ func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenTempTemplateFileCrea
return fmt.Errorf("This is an write file error")
}

err := HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
err := HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/fe/template", "/path/to/consul/be/template")

s.Error(err)
}

func (s HaProxyTestSuite) Test_Reconfigure_RemovesTempTemplateFile() {
path := "/path/to/consul/template"
expected := fmt.Sprintf("%s.tmp", path)
actual := ""
fePath := "/path/to/consul/fe/template"
bePath := "/path/to/consul/be/template"
expected := []string{
fmt.Sprintf("%s.tmp", fePath),
fmt.Sprintf("%s.tmp", bePath),
}
var actual []string
removeFileOrig := removeFile
defer func() { removeFile = removeFileOrig }()
removeFile = func(name string) error {
actual = name
actual = append(actual, name)
return nil
}

HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, fePath, bePath)

s.Equal(expected, actual)
}
Expand Down
3 changes: 2 additions & 1 deletion integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ func (s IntegrationTestSuite) Test_Proxy_Templates() {
"--proxy-docker-host", s.ProxyDockerHost,
"--proxy-docker-cert-path", s.ProxyDockerCertPath,
"--service-path", "INCORRECT",
"--consul-template-path", "test_configs/tmpl/go-demo-app.tmpl",
"--consul-template-fe-path", "test_configs/tmpl/go-demo-app-fe.tmpl",
"--consul-template-be-path", "test_configs/tmpl/go-demo-app-be.tmpl",
"--flow", "deploy", "--flow", "proxy",
)
s.verifyContainer([]ContainerStatus{
Expand Down
Loading

0 comments on commit 70d76f0

Please sign in to comment.