-
Notifications
You must be signed in to change notification settings - Fork 3
/
haproxy_test.go
200 lines (174 loc) · 6.16 KB
/
haproxy_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
package main
import (
"os"
"reflect"
"testing"
"text/template"
)
const (
testConfigPath = "test-fixtures/haproxy.cfg"
testTemplate = `global
maxconn 128
defaults
timeout connect 1000ms
{{range .Frontends}}
frontend {{.Name}}{{if .Bind}}
bind {{.Bind}}{{end}}{{if .Mode}}
mode {{.Mode}}{{end}}{{if .DefaultBackend}}
default_backend {{.DefaultBackend}}{{end}}{{if .Option}}
option {{.Option}}{{end}}
{{end}}
{{range .Backends}}
backend {{.Name}}{{if .Mode}}
mode {{.Mode}}{{end}}{{if .Balance}}
balance {{.Balance}}{{end}}{{range .Members}}
server {{.Name}} {{.Host}}:{{.Port}} check inter 2000{{end}}
{{end}}
`
)
// ----------------------------------------------
// NewHAProxy TESTS
// ----------------------------------------------
// Tests the "happy path" for the NewHAProxy() function.
func Test_NewHAProxy(t *testing.T) {
tmpl, _ := template.New("test").Parse(testTemplate)
h := NewHAProxy(testConfigPath, tmpl, "")
assert.EnsureNotNil(t, h, "NewHAProxy() returned a nil value")
assert.Equal(t, reflect.TypeOf(h), reflect.TypeOf(&haProxyImpl{}), "NewHAProxy() returned an unexpected object type")
}
// Tests that the NewHAProxy() function assigns a default template if none is passed it.
func Test_NewHAProxy_NilTemplate(t *testing.T) {
h := NewHAProxy(testConfigPath, nil, "")
assert.EnsureNotNil(t, h, "NewHAProxy() returned a nil value")
assert.Equal(t, reflect.TypeOf(h), reflect.TypeOf(&haProxyImpl{}), "NewHAProxy() returned an unexpected object type")
expTmpl, _ := template.New("test").Parse(string(defaultTemplate))
assert.Equal(t, h.Template(), expTmpl, "NewHAProxy() did not use the default template when none was provided")
}
// ----------------------------------------------
// haProxyImpl.Template TESTS
// ----------------------------------------------
func Test_haProxyImpl_Template(t *testing.T) {
tmpl, _ := template.New("test").Parse("testing")
h := &haProxyImpl{
template: tmpl,
}
assert.Equal(t, h.Template(), tmpl, "haProxyImpl.Template() returned an unexpected value")
}
// ----------------------------------------------
// haProxyImpl.SetTemplate TESTS
// ----------------------------------------------
func Test_haProxyImpl_SetTemplate(t *testing.T) {
h := &haProxyImpl{}
tmpl, _ := template.New("test").Parse("testing")
h.SetTemplate(tmpl)
assert.Equal(t, h.template, tmpl, "haProxyImpl.SetTemplate() did not assign the template to the expected value")
}
// ----------------------------------------------
// haProxyImpl.GetConfig TESTS
// ----------------------------------------------
// Tests the "happy path" for the haProxyImpl.GetConfig() function.
func Test_haProxyImpl_GetConfig(t *testing.T) {
tmpl, _ := template.New("test").Parse(testTemplate)
h := &haProxyImpl{
configPath: testConfigPath,
template: tmpl,
}
s, err := h.GetConfig()
assert.EnsureNil(t, err, "haProxyImpl.GetConfig() returned an unexpected error: %v", err)
assert.NotEmpty(t, s, "haProxyImpl.GetConfig() returned no data; expected contents of text-fixtures/haproxy.cfg")
}
// ----------------------------------------------
// haProxyImpl.GetFrontends TESTS
// ----------------------------------------------
// Tests the "happy path" for the haProxyImpl.GetFrontends() function.
func Test_haProxyImpl_GetFrontends(t *testing.T) {
tmpl, _ := template.New("test").Parse(testTemplate)
h := &haProxyImpl{
configPath: testConfigPath,
template: tmpl,
}
f, err := h.GetFrontends()
assert.EnsureNil(t, err, "haProxyImpl.GetFrontends() returned an unexpected error: %v", err)
assert.EnsureEqual(t, len(f), 1, "haProxyImpl.GetFrontends() returned unexpected number of objects")
expFrontend := &Frontend{
Name: "app",
Bind: "*:80",
Mode: "http",
DefaultBackend: "app-1",
Option: "httplog",
}
assert.Equal(t, f[0], expFrontend, "haProxyImpl.GetFrontends() returned unexpected object")
}
// ----------------------------------------------
// haProxyImpl.GetBackends TESTS
// ----------------------------------------------
// Tests the "happy path" for the haProxyImpl.GetBackends() function.
func Test_haProxyImpl_GetBackends(t *testing.T) {
tmpl, _ := template.New("test").Parse(testTemplate)
h := &haProxyImpl{
configPath: testConfigPath,
template: tmpl,
}
b, err := h.GetBackends()
assert.EnsureNil(t, err, "haProxyImpl.GetBackends() returned an unexpected error: %v", err)
assert.EnsureEqual(t, len(b), 2, "haProxyImpl.GetBackends() returned unexpected number of objects")
expBackend := &Backend{
Name: "app-2",
Mode: "http",
Members: BackendMembers{
BackendMember{
Name: "app2_node1",
Host: "10.2.2.10",
Port: 8080,
},
BackendMember{
Name: "app2_node2",
Host: "10.2.2.20",
Port: 8080,
},
},
}
assert.Equal(t, b[1], expBackend, "haProxyImpl.GetBackends() returned unexpected object")
}
// ----------------------------------------------
// haProxyImpl.WriteConfig TESTS
// ----------------------------------------------
// Tests the "happy path" for the haProxyImpl.WriteConfig() function.
func Test_haProxyImpl_WriteConfig(t *testing.T) {
testFile := "test-fixtures/test.cfg"
defer os.Remove(testFile)
tmpl, _ := template.New("test").Parse(testTemplate)
h := &haProxyImpl{
configPath: testFile,
template: tmpl,
}
frontends := Frontends{
&Frontend{
Name: "test-app",
Bind: "*:80",
Mode: "http",
DefaultBackend: "test-app-1",
},
}
backends := Backends{
&Backend{
Name: "test-app-1",
Mode: "http",
Members: BackendMembers{
BackendMember{
Name: "testapp1_node1",
Host: "10.2.2.10",
Port: 8080,
},
},
},
}
err := h.WriteConfig(frontends, backends)
assert.EnsureNil(t, err, "haProxyImpl.WriteConfig() returned an unexpected error: %v", err)
f, _ := h.GetFrontends()
assert.EnsureEqual(t, len(f), 1, "haProxyImpl.GetFrontends() returned unexptected number of objects")
b, _ := h.GetBackends()
assert.EnsureEqual(t, len(b), 1, "haProxyImpl.GetBackends() returned unexptected number of objects")
assert.Equal(t, f[0], frontends[0], "haProxyImpl.GetFrontends() returned unexpected object")
assert.Equal(t, b[0], backends[0], "haProxyImpl.GetBackends() returned unexpected object")
}