Skip to content

Commit 70d76f0

Browse files
committed
Updated consul template requests
1 parent 4b65b8b commit 70d76f0

13 files changed

+150
-87
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
env:
2-
- VERSION=1.0
2+
- VERSION=1.0.1
33

44
language: go
55

flow.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ func (m Flow) Proxy(opts Opts, proxy Proxy) error {
109109
opts.ServiceName,
110110
color,
111111
opts.ServicePath,
112-
opts.ConsulTemplatePath,
112+
opts.ConsulTemplateFePath,
113+
opts.ConsulTemplateBePath,
113114
); err != nil {
114115
return err
115116
}

flow_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ func (s FlowTestSuite) Test_Proxy_InvokesReconfigure_WhenDeploy() {
510510
s.opts.NextColor,
511511
s.opts.ServicePath,
512512
"",
513+
"",
513514
)
514515
}
515516

@@ -530,6 +531,7 @@ func (s FlowTestSuite) Test_Proxy_InvokesReconfigure_WhenScale() {
530531
s.opts.CurrentColor,
531532
s.opts.ServicePath,
532533
"",
534+
"",
533535
)
534536
}
535537

@@ -546,6 +548,7 @@ func (s FlowTestSuite) Test_Proxy_ReturnsError_WhenReconfigureFails() {
546548
mock.Anything,
547549
mock.Anything,
548550
mock.Anything,
551+
mock.Anything,
549552
).Return(fmt.Errorf("This is an error"))
550553

551554
actual := Flow{}.Proxy(s.opts, mockObj)

ha_proxy.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func (m HaProxy) Provision(dockerHost, reconfPort, certPath, scAddress string) e
6060
func (m HaProxy) Reconfigure(
6161
dockerHost, dockerCertPath, host, reconfPort, serviceName, serviceColor string,
6262
servicePath []string,
63-
consulTemplatePath string,
63+
consulTemplateFePath string, consulTemplateBePath string,
6464
) error {
65-
if len(consulTemplatePath) > 0 {
66-
if err := m.sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName, serviceColor); err != nil {
65+
if len(consulTemplateFePath) > 0 {
66+
if err := m.sendConsulTemplatesToTheProxy(dockerHost, dockerCertPath, consulTemplateFePath, consulTemplateBePath, serviceName, serviceColor); err != nil {
6767
return err
6868
}
6969
} else if len(servicePath) == 0 {
@@ -78,7 +78,7 @@ func (m HaProxy) Reconfigure(
7878
if len(reconfPort) == 0 && !strings.Contains(host, ":") {
7979
return fmt.Errorf("Reconfigure port is mandatory.")
8080
}
81-
if err := m.sendReconfigureRequest(host, reconfPort, serviceName, serviceColor, servicePath, consulTemplatePath); err != nil {
81+
if err := m.sendReconfigureRequest(host, reconfPort, serviceName, serviceColor, servicePath, consulTemplateFePath, consulTemplateBePath); err != nil {
8282
return err
8383
}
8484
return nil
@@ -87,7 +87,7 @@ func (m HaProxy) Reconfigure(
8787
func (m HaProxy) sendReconfigureRequest(
8888
host, reconfPort, serviceName, serviceColor string,
8989
servicePath []string,
90-
consulTemplatePath string,
90+
consulTemplateFePath, consulTemplateBePath string,
9191
) error {
9292
address := host
9393
if len(reconfPort) > 0 {
@@ -101,8 +101,8 @@ func (m HaProxy) sendReconfigureRequest(
101101
address,
102102
serviceName,
103103
)
104-
if len(consulTemplatePath) > 0 {
105-
proxyUrl = fmt.Sprintf("%s&consulTemplatePath=%s/%s.tmpl", proxyUrl, ConsulTemplatesDir, serviceName)
104+
if len(consulTemplateFePath) > 0 {
105+
proxyUrl = fmt.Sprintf("%s&consulTemplateFePath=%s/%s-fe.tmpl&consulTemplateBePath=%s/%s-be.tmpl", proxyUrl, ConsulTemplatesDir, serviceName, ConsulTemplatesDir, serviceName)
106106
} else {
107107
if len(serviceColor) > 0 {
108108
proxyUrl = fmt.Sprintf("%s&serviceColor=%s", proxyUrl, serviceColor)
@@ -121,19 +121,29 @@ func (m HaProxy) sendReconfigureRequest(
121121
return nil
122122
}
123123

124-
func (m HaProxy) sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName, color string) error {
124+
func (m HaProxy) sendConsulTemplatesToTheProxy(dockerHost, dockerCertPath, consulTemplateFePath, consulTemplateBePath, serviceName, color string) error {
125+
if err := m.sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplateFePath, serviceName, color, "fe"); err != nil {
126+
return err
127+
}
128+
if err := m.sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplateBePath, serviceName, color, "be"); err != nil {
129+
return err
130+
}
131+
return nil
132+
}
133+
134+
func (m HaProxy) sendConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName, color, templateType string) error {
125135
if err := m.createTempConsulTemplate(consulTemplatePath, serviceName, color); err != nil {
126136
return err
127137
}
128-
if err := m.copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName); err != nil {
138+
file := fmt.Sprintf("%s-%s.tmpl", serviceName, templateType)
139+
if err := m.copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, file); err != nil {
129140
return err
130141
}
131142
removeFile(fmt.Sprintf("%s.tmp", consulTemplatePath))
132-
133143
return nil
134144
}
135145

136-
func (m HaProxy) copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, serviceName string) error {
146+
func (m HaProxy) copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consulTemplatePath, templateName string) error {
137147
SetDockerHost(dockerHost, dockerCertPath)
138148
args := []string{"exec", "-i", "docker-flow-proxy", "mkdir", "-p", ConsulTemplatesDir}
139149
execCmd := exec.Command("docker", args...)
@@ -146,7 +156,7 @@ func (m HaProxy) copyConsulTemplateToTheProxy(dockerHost, dockerCertPath, consul
146156
args = []string{
147157
"cp",
148158
fmt.Sprintf("%s.tmp", consulTemplatePath),
149-
fmt.Sprintf("docker-flow-proxy:%s/%s.tmpl", ConsulTemplatesDir, serviceName),
159+
fmt.Sprintf("docker-flow-proxy:%s/%s", ConsulTemplatesDir, templateName),
150160
}
151161
cpCmd := exec.Command("docker", args...)
152162
cpCmd.Stdout = os.Stdout

ha_proxy_test.go

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,25 @@ func (s HaProxyTestSuite) Test_Provision_ReturnsError_WhenStartFailure() {
230230
// Reconfigure
231231

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

235235
s.Error(err)
236236
}
237237

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

241241
s.Error(err)
242242
}
243243

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

247247
s.Error(err)
248248
}
249249

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

253253
s.Error(err)
254254
}
@@ -270,7 +270,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequest() {
270270
return nil, fmt.Errorf("This is an HTTP error")
271271
}
272272

273-
HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "")
273+
HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "", "")
274274

275275
s.Equal(expected, actual)
276276
}
@@ -291,7 +291,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequestWithOutColor_WhenNotB
291291
return nil, fmt.Errorf("This is an HTTP error")
292292
}
293293

294-
HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, "", s.ServicePath, "")
294+
HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, "", s.ServicePath, "", "")
295295

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

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

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

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

329329
s.Error(err)
330330
}
@@ -334,15 +334,15 @@ func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenResponseCodeIsNot2xx
334334
w.WriteHeader(http.StatusBadRequest)
335335
}))
336336

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

339339
s.Error(err)
340340
}
341341

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

345-
err := HaProxy{}.Reconfigure(s.DockerHost, s.DockerCertPath, s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, "/path/to/consul/template")
345+
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")
346346

347347
s.NoError(err)
348348
s.Equal(s.DockerHost, os.Getenv("DOCKER_HOST"))
@@ -357,7 +357,7 @@ func (s HaProxyTestSuite) Test_Reconfigure_CreatesConsulTemplatesDirectory_WhenC
357357
return nil
358358
}
359359

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

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

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

374374
s.Error(actual)
375375
}
376376

377-
func (s HaProxyTestSuite) Test_Reconfigure_CopiesTemplate_WhenConsulTemplatePathIsPresent() {
378-
consulTemplatePath := "/path/to/consul/template"
379-
var actual []string
380-
expected := []string{
377+
func (s HaProxyTestSuite) Test_Reconfigure_CopiesTemplates_WhenConsulTemplatePathIsPresent() {
378+
fePath := "/path/to/consul/fe/template"
379+
bePath := "/path/to/consul/be/template"
380+
var actual [][]string
381+
feExpected := []string{
382+
"docker",
383+
"cp",
384+
fmt.Sprintf("%s.tmp", fePath),
385+
fmt.Sprintf("docker-flow-proxy:/consul_templates/%s-fe.tmpl", s.ServiceName),
386+
}
387+
beExpected := []string{
381388
"docker",
382389
"cp",
383-
fmt.Sprintf("%s.tmp", consulTemplatePath),
384-
fmt.Sprintf("docker-flow-proxy:/consul_templates/%s.tmpl", s.ServiceName),
390+
fmt.Sprintf("%s.tmp", bePath),
391+
fmt.Sprintf("docker-flow-proxy:/consul_templates/%s-be.tmpl", s.ServiceName),
385392
}
386393
runHaProxyCpCmdOrig := runHaProxyCpCmd
387394
defer func() { runHaProxyCpCmd = runHaProxyCpCmdOrig }()
388395
runHaProxyCpCmd = func(cmd *exec.Cmd) error {
389-
actual = cmd.Args
396+
actual = append(actual, cmd.Args)
390397
return nil
391398
}
392399

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

395-
s.Equal(expected, actual)
402+
s.Equal(feExpected, actual[0])
403+
s.Equal(beExpected, actual[1])
396404
}
397405

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

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

407415
s.Error(actual)
408416
}
@@ -413,25 +421,28 @@ func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequestWithConsulTemplatePat
413421
actual = fmt.Sprintf("%s?%s", r.URL.Path, r.URL.RawQuery)
414422
}))
415423
expected := fmt.Sprintf(
416-
"/v1/docker-flow-proxy/reconfigure?serviceName=%s&consulTemplatePath=/consul_templates/%s.tmpl",
424+
"/v1/docker-flow-proxy/reconfigure?serviceName=%s&consulTemplateFePath=/consul_templates/%s-fe.tmpl&consulTemplateBePath=/consul_templates/%s-be.tmpl",
425+
s.ServiceName,
417426
s.ServiceName,
418427
s.ServiceName,
419428
)
420429

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

423432
s.Equal(expected, actual)
424433
}
425434

426435
func (s HaProxyTestSuite) Test_Reconfigure_CreatesTempTemplateFile() {
427-
actualFilename := ""
436+
fePath := "/path/to/consul/fe/template"
437+
bePath := "/path/to/consul/be/template"
438+
var actualFilenames []string
428439
actualData := ""
429440
data := "This is a %s template"
430441
expectedData := fmt.Sprintf(data, s.ServiceName+"-"+s.Color)
431442
writeFileOrig := writeFile
432443
defer func() { writeFile = writeFileOrig }()
433444
writeFile = func(filename string, data []byte, perm os.FileMode) error {
434-
actualFilename = filename
445+
actualFilenames = append(actualFilenames, filename)
435446
actualData = string(data)
436447
return nil
437448
}
@@ -441,9 +452,10 @@ func (s HaProxyTestSuite) Test_Reconfigure_CreatesTempTemplateFile() {
441452
return []byte(fmt.Sprintf(data, "SERVICE_NAME")), nil
442453
}
443454

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

446-
s.Equal("/path/to/consul/template.tmp", actualFilename)
457+
s.Equal(fePath+".tmp", actualFilenames[0])
458+
s.Equal(bePath+".tmp", actualFilenames[1])
447459
s.Equal(expectedData, actualData)
448460
}
449461

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

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

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

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

471483
s.Error(err)
472484
}
473485

474486
func (s HaProxyTestSuite) Test_Reconfigure_RemovesTempTemplateFile() {
475-
path := "/path/to/consul/template"
476-
expected := fmt.Sprintf("%s.tmp", path)
477-
actual := ""
487+
fePath := "/path/to/consul/fe/template"
488+
bePath := "/path/to/consul/be/template"
489+
expected := []string{
490+
fmt.Sprintf("%s.tmp", fePath),
491+
fmt.Sprintf("%s.tmp", bePath),
492+
}
493+
var actual []string
478494
removeFileOrig := removeFile
479495
defer func() { removeFile = removeFileOrig }()
480496
removeFile = func(name string) error {
481-
actual = name
497+
actual = append(actual, name)
482498
return nil
483499
}
484500

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

487503
s.Equal(expected, actual)
488504
}

integration_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ func (s IntegrationTestSuite) Test_Proxy_Templates() {
229229
"--proxy-docker-host", s.ProxyDockerHost,
230230
"--proxy-docker-cert-path", s.ProxyDockerCertPath,
231231
"--service-path", "INCORRECT",
232-
"--consul-template-path", "test_configs/tmpl/go-demo-app.tmpl",
232+
"--consul-template-fe-path", "test_configs/tmpl/go-demo-app-fe.tmpl",
233+
"--consul-template-be-path", "test_configs/tmpl/go-demo-app-be.tmpl",
233234
"--flow", "deploy", "--flow", "proxy",
234235
)
235236
s.verifyContainer([]ContainerStatus{

0 commit comments

Comments
 (0)