-
Notifications
You must be signed in to change notification settings - Fork 31
/
ha_proxy_test.go
535 lines (442 loc) · 15.5 KB
/
ha_proxy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package main
import (
"fmt"
"github.com/stretchr/testify/suite"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
"./util"
)
type HaProxyTestSuite struct {
suite.Suite
ScAddress string
CertPath string
ExitedMessage string
Host string
ServiceName string
Color string
ServicePath []string
ReconfPort string
DockerHost string
DockerCertPath string
Server *httptest.Server
}
func (s *HaProxyTestSuite) SetupTest() {
s.ScAddress = "1.2.3.4:1234"
s.ServiceName = "my-service"
s.Color = "purpurina"
s.ServicePath = []string{"/path/to/my/service", "/path/to/my/other/service"}
s.ExitedMessage = "Exited (2) 15 seconds ago"
s.ReconfPort = "5362"
s.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reconfigureUrl := fmt.Sprintf(
"/v1/docker-flow-proxy/reconfigure",
s.ServiceName,
strings.Join(s.ServicePath, ","),
)
actualUrl := fmt.Sprintf("%s?%s", r.URL.Path, r.URL.RawQuery)
if r.Method == "GET" {
if strings.HasPrefix(actualUrl, reconfigureUrl) {
w.WriteHeader(http.StatusOK)
}
}
}))
s.DockerHost = "tcp://my-docker-proxy-host"
s.DockerCertPath = "/path/to/pem"
s.Host = "http://my-docker-proxy-host.com"
runHaProxyRunCmd = func(cmd *exec.Cmd) error {
return nil
}
runHaProxyPsCmd = func(cmd *exec.Cmd) error {
return nil
}
runHaProxyStartCmd = func(cmd *exec.Cmd) error {
return nil
}
httpGetOrig := httpGet
defer func() { httpGet = httpGetOrig }()
httpGet = func(url string) (resp *http.Response, err error) {
return nil, nil
}
}
// Provision
func (s HaProxyTestSuite) Test_Provision_SetsDockerHost() {
actual := ""
SetDockerHostOrig := util.SetDockerHost
defer func() { util.SetDockerHost = SetDockerHostOrig }()
util.SetDockerHost = func(host, certPath string) {
actual = host
}
HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.Equal(s.Host, actual)
}
func (s HaProxyTestSuite) Test_Provision_ReturnsError_WhenProxyHostIsEmpty() {
err := HaProxy{}.Provision("", s.ReconfPort, s.CertPath, s.ScAddress)
s.Error(err)
}
func (s HaProxyTestSuite) Test_Provision_ReturnsError_WhenScAddressIsEmpty() {
err := HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, "")
s.Error(err)
}
func (s HaProxyTestSuite) Test_Provision_RunsDockerFlowProxyContainer() {
var actual []string
expected := []string{
"docker", "run", "-d",
"--name", "docker-flow-proxy",
"-e", fmt.Sprintf("%s=%s", "CONSUL_ADDRESS", s.ScAddress),
"-p", "80:80", "-p", fmt.Sprintf("%s:8080", s.ReconfPort),
"vfarcic/docker-flow-proxy",
}
runHaProxyRunCmd = func(cmd *exec.Cmd) error {
actual = cmd.Args
return nil
}
HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.Equal(expected, actual)
}
func (s HaProxyTestSuite) Test_Provision_ReturnsError_WhenFailure() {
runHaProxyRunCmd = func(cmd *exec.Cmd) error {
return fmt.Errorf("This is an error")
}
err := HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.Error(err)
}
func (s HaProxyTestSuite) Test_Provision_RunsDockerPs() {
var actual []string
expected := []string{
"docker", "ps", "-a",
"--filter", "name=docker-flow-proxy",
"--format", "{{.Status}}",
}
runHaProxyPsCmd = func(cmd *exec.Cmd) error {
actual = cmd.Args
return nil
}
HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.Equal(expected, actual)
}
func (s HaProxyTestSuite) Test_Provision_ReturnsError_WhenPsFailure() {
runHaProxyPsCmd = func(cmd *exec.Cmd) error {
return fmt.Errorf("This is an docker ps error")
}
err := HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.Error(err)
}
func (s HaProxyTestSuite) Test_Provision_ReturnsError_WhenProxyFails() {
runHaProxyPsCmd = func(cmd *exec.Cmd) error {
return fmt.Errorf("This is an docker ps error")
}
err := HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.Error(err)
}
func (s HaProxyTestSuite) Test_Provision_DoesNotRun_WhenProxyExists() {
actual := false
runHaProxyRunCmd = func(cmd *exec.Cmd) error {
actual = true
return nil
}
runHaProxyPsCmd = func(cmd *exec.Cmd) error {
cmd.Stdout.Write([]byte("Up 3 hours"))
return nil
}
HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.False(actual)
}
func (s HaProxyTestSuite) Test_Provision_StartsAndDoesNotRun_WhenProxyIsExited() {
start := false
run := false
runHaProxyStartCmd = func(cmd *exec.Cmd) error {
start = true
return nil
}
runHaProxyRunCmd = func(cmd *exec.Cmd) error {
run = true
return nil
}
runHaProxyPsCmd = func(cmd *exec.Cmd) error {
cmd.Stdout.Write([]byte(s.ExitedMessage))
return nil
}
HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.True(start)
s.False(run)
}
func (s HaProxyTestSuite) Test_Provision_StartsDockerFlowProxyContainer_WhenProxyIsExited() {
var actual []string
expected := []string{"docker", "start", "docker-flow-proxy"}
runHaProxyPsCmd = func(cmd *exec.Cmd) error {
cmd.Stdout.Write([]byte(s.ExitedMessage))
return nil
}
runHaProxyStartCmd = func(cmd *exec.Cmd) error {
actual = cmd.Args
return nil
}
HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.Equal(expected, actual)
}
func (s HaProxyTestSuite) Test_Provision_ReturnsError_WhenStartFailure() {
runHaProxyPsCmd = func(cmd *exec.Cmd) error {
cmd.Stdout.Write([]byte(s.ExitedMessage))
return nil
}
runHaProxyStartCmd = func(cmd *exec.Cmd) error {
return fmt.Errorf("This is an docker start error")
}
err := HaProxy{}.Provision(s.Host, s.ReconfPort, s.CertPath, s.ScAddress)
s.Error(err)
}
// Reconfigure
func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenProxyHostIsEmpty() {
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, "", "")
s.Error(err)
}
func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenServicePathAndConsulTemplatePathAreEmpty() {
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, "", "")
s.Error(err)
}
func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequest() {
actual := ""
expected := fmt.Sprintf(
"%s:%s/v1/docker-flow-proxy/reconfigure?serviceName=%s&serviceColor=%s&servicePath=%s",
s.Host,
s.ReconfPort,
s.ServiceName,
s.Color,
strings.Join(s.ServicePath, ","),
)
httpGetOrig := httpGet
defer func() { httpGet = httpGetOrig }()
httpGet = func(url string) (resp *http.Response, err error) {
actual = url
return nil, fmt.Errorf("This is an HTTP error")
}
HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "", "")
s.Equal(expected, actual)
}
func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequestWithOutColor_WhenNotBlueGreen() {
actual := ""
expected := fmt.Sprintf(
"%s:%s/v1/docker-flow-proxy/reconfigure?serviceName=%s&servicePath=%s",
s.Host,
s.ReconfPort,
s.ServiceName,
strings.Join(s.ServicePath, ","),
)
httpGetOrig := httpGet
defer func() { httpGet = httpGetOrig }()
httpGet = func(url string) (resp *http.Response, err error) {
actual = url
return nil, fmt.Errorf("This is an HTTP error")
}
HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, "", s.ServicePath, "", "")
s.Equal(expected, actual)
}
func (s HaProxyTestSuite) Test_Reconfigure_SendsHttpRequestWithPrependedHttp() {
actual := ""
expected := fmt.Sprintf(
"%s:%s/v1/docker-flow-proxy/reconfigure?serviceName=%s&servicePath=%s",
s.Host,
s.ReconfPort,
s.ServiceName,
strings.Join(s.ServicePath, ","),
)
httpGetOrig := httpGet
defer func() { httpGet = httpGetOrig }()
httpGet = func(url string) (resp *http.Response, err error) {
actual = url
return nil, fmt.Errorf("This is an HTTP error")
}
HaProxy{}.Reconfigure("", "", "my-docker-proxy-host.com", s.ReconfPort, s.ServiceName, "", s.ServicePath, "", "")
s.Equal(expected, actual)
}
func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenRequestFails() {
httpGetOrig := httpGet
defer func() { httpGet = httpGetOrig }()
httpGet = func(url string) (resp *http.Response, err error) {
return nil, fmt.Errorf("This is an HTTP error")
}
err := HaProxy{}.Reconfigure("", "", s.Host, s.ReconfPort, s.ServiceName, s.Color, s.ServicePath, "", "")
s.Error(err)
}
func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenResponseCodeIsNot2xx() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}))
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/fe/template", "/path/to/consul/be/template")
s.NoError(err)
s.Equal(s.DockerHost, os.Getenv("DOCKER_HOST"))
s.Equal(s.DockerCertPath, os.Getenv("DOCKER_CERT_PATH"))
}
func (s HaProxyTestSuite) Test_Reconfigure_CreatesConsulTemplatesDirectory_WhenConsulTemplatePathIsPresent() {
var actual []string
expected := []string{"docker", "exec", "-i", "docker-flow-proxy", "mkdir", "-p", "/consul_templates"}
runHaProxyExecCmd = func(cmd *exec.Cmd) error {
actual = cmd.Args
return nil
}
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)
}
func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenDirectoryCreationFails() {
runHaProxyExecCmdOrig := runHaProxyExecCmd
defer func() { runHaProxyExecCmd = runHaProxyExecCmdOrig }()
runHaProxyExecCmd = func(cmd *exec.Cmd) error {
return fmt.Errorf("This is an docker exec error")
}
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_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", 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 = append(actual, cmd.Args)
return nil
}
HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, fePath, bePath)
s.Equal(feExpected, actual[0])
s.Equal(beExpected, actual[1])
}
func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenTemplateCopyFails() {
runHaProxyCpCmdOrig := runHaProxyCpCmd
defer func() { runHaProxyCpCmd = runHaProxyCpCmdOrig }()
runHaProxyCpCmd = func(cmd *exec.Cmd) error {
return fmt.Errorf("This is an docker cp error")
}
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_SendsHttpRequestWithConsulTemplatePath_WhenSpecified() {
actual := ""
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
actual = fmt.Sprintf("%s?%s", r.URL.Path, r.URL.RawQuery)
}))
expected := fmt.Sprintf(
"/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/fe/template", "/path/to/consul/be/template")
s.Equal(expected, actual)
}
func (s HaProxyTestSuite) Test_Reconfigure_CreatesTempTemplateFile() {
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 := util.WriteFile
defer func() { util.WriteFile = writeFileOrig }()
util.WriteFile = func(filename string, data []byte, perm os.FileMode) error {
actualFilenames = append(actualFilenames, filename)
actualData = string(data)
return nil
}
readFileOrig := util.ReadFile
defer func() { util.ReadFile = readFileOrig }()
util.ReadFile = func(fileName string) ([]byte, error) {
return []byte(fmt.Sprintf(data, "SERVICE_NAME")), nil
}
HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, fePath, bePath)
s.Equal(fePath+".tmp", actualFilenames[0])
s.Equal(bePath+".tmp", actualFilenames[1])
s.Equal(expectedData, actualData)
}
func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenTemplateFileReadFails() {
readFileOrig := util.ReadFile
defer func() { util.ReadFile = readFileOrig }()
util.ReadFile = func(fileName string) ([]byte, error) {
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/fe/template", "/path/to/consul/be/template")
s.Error(err)
}
func (s HaProxyTestSuite) Test_Reconfigure_ReturnsError_WhenTempTemplateFileCreationFails() {
writeFileOrig := util.WriteFile
defer func() { util.WriteFile = writeFileOrig }()
util.WriteFile = func(filename string, data []byte, perm os.FileMode) error {
return fmt.Errorf("This is an write file error")
}
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() {
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 := util.RemoveFile
defer func() { util.RemoveFile = removeFileOrig }()
util.RemoveFile = func(name string) error {
actual = append(actual, name)
return nil
}
HaProxy{}.Reconfigure("", "", s.Server.URL, "", s.ServiceName, s.Color, s.ServicePath, fePath, bePath)
s.Equal(expected, actual)
}
// Suite
func TestHaProxyTestSuite(t *testing.T) {
logPrintln = func(v ...interface{}) {}
logPrintf = func(format string, v ...interface{}) {}
util.Sleep = func(d time.Duration) {}
dockerHost := os.Getenv("DOCKER_HOST")
dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
runHaProxyExecCmd = func(cmd *exec.Cmd) error {
return nil
}
runHaProxyCpCmd = func(cmd *exec.Cmd) error {
return nil
}
util.WriteFile = func(fileName string, data []byte, perm os.FileMode) error {
return nil
}
util.ReadFile = func(fileName string) ([]byte, error) {
return []byte(""), nil
}
util.RemoveFile = func(name string) error {
return nil
}
defer func() {
os.Setenv("DOCKER_HOST", dockerHost)
os.Setenv("DOCKER_CERT_PATH", dockerCertPath)
}()
suite.Run(t, new(HaProxyTestSuite))
}