This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathconfig_test.go
264 lines (227 loc) · 7 KB
/
config_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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var (
badJSON = `"name":"www.example.com","backends":["127.0.0.1:80","127.0.0.1:80","127.0.0.1:80"],"weights":[5,1],"ratio":0.3,"paths":["/"],"methods":["GET"]}`
badConfig = `{"name":"www.example.com","backends":["127.0.0.1:80","127.0.0.1:80","127.0.0.1:80"],"weights":[5,1],"ratio":0.3,"paths":["/"],"methods":["GET"]}`
goodConfig = `{"name":"www.example.com","backends":["127.0.0.1:80","127.0.0.1:80","127.0.0.1:80"],"weights":[5,1,1],"ratio":0.3,"paths":["/"],"methods":["GET"]}`
)
func TestUpdateConfig(t *testing.T) {
fakeServer := httptest.NewServer(
http.HandlerFunc(appHandler),
)
defer fakeServer.Close()
url := fakeServer.URL + "/app"
// try get
resp, err := http.Get(url)
if err != nil || resp.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("should return 405, but got: %d", resp.StatusCode)
}
// post bad json
resp, err = http.Post(url, "application/json", bytes.NewBuffer([]byte(badJSON)))
if err != nil || resp.StatusCode != http.StatusBadRequest {
t.Errorf("should return 400, but got: %d", resp.StatusCode)
}
// post bad config
resp, err = http.Post(url, "application/json", bytes.NewBuffer([]byte(badConfig)))
if err != nil || resp.StatusCode != http.StatusBadRequest {
t.Errorf("should return 400, but got: %d", resp.StatusCode)
}
// post bad config
resp, err = http.Post(url, "application/json", bytes.NewBuffer([]byte(goodConfig)))
if err != nil || resp.StatusCode != http.StatusOK {
t.Errorf("should return 200, but got: %d", resp.StatusCode)
}
}
func TestCheckAPPConfig(t *testing.T) {
config := &appConfig{}
// not not exist
if err := checkAppConfig(config); err == nil {
t.Errorf("should return error but not")
}
config.Name = "www.example.com"
// len(backends) != len(weights)
config.Backends = []string{"192.168.1.1:80"}
if err := checkAppConfig(config); err == nil {
t.Errorf("should return error but not")
}
// len(path) != len(methods)
config.Weights = []int{1}
config.Paths = []string{"/"}
if err := checkAppConfig(config); err == nil {
t.Errorf("should return error but not")
}
config.Methods = []string{"get"}
// load balancer algorithm not exist
config.LoadBalanceMethod = "what"
if err := checkAppConfig(config); err == nil {
t.Errorf("should return error but not")
}
// check fallback
config.Name = "www.example.com"
config.Backends = []string{"192.168.1.1:80"}
config.Weights = []int{1}
config.Ratio = 0.3
config.DisableTSR = false
config.LoadBalanceMethod = LBMWRR
config.Paths = []string{"/"}
config.Methods = []string{"get"}
// "" or text
config.FallbackType = ""
content := "too many requests"
if err := checkAppConfig(config); err != nil {
t.Errorf("should not return error but got: %s", err)
}
if config.FallbackType != fallbackTEXT || config.FallbackContent != content {
t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent)
}
config.FallbackType = fallbackTEXT
if err := checkAppConfig(config); err != nil {
t.Errorf("should not return error but got: %s", err)
}
if config.FallbackType != fallbackTEXT || config.FallbackContent != content {
t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent)
}
// "json"
config.FallbackType = fallbackJSON
content = "{}"
config.FallbackContent = content
if err := checkAppConfig(config); err != nil {
t.Errorf("should not return error but got: %s", err)
}
if config.FallbackType != fallbackJSON || config.FallbackContent != content {
t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent)
}
// "html"
config.FallbackType = fallbackHTML
content = "<html></html>"
config.FallbackContent = content
if err := checkAppConfig(config); err != nil {
t.Errorf("should not return error but got: %s", err)
}
if config.FallbackType != fallbackHTML || config.FallbackContent != content {
t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent)
}
// "html_file"
// file not exist
filePath := "/tmp/test_guard_html_file_path.html"
os.Remove(filePath)
config.FallbackType = fallbackHTMLFile
config.FallbackContent = filePath
if err := checkAppConfig(config); err == nil {
t.Errorf("should return error but not")
}
// file exists
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
t.Errorf("failed to open %s: %s", filePath, err)
}
defer f.Close()
content = "<html></html>"
f.Write([]byte(content))
if err := checkAppConfig(config); err != nil {
t.Errorf("should not return error, but got: %s", err)
}
if config.FallbackType != fallbackHTML || config.FallbackContent != content {
t.Errorf("fallback options error: %s, %s", config.FallbackType, config.FallbackContent)
}
// bad type
config.FallbackType = "what"
if err := checkAppConfig(config); err == nil {
t.Errorf("should return error but not")
}
}
func TestGetBalancer(t *testing.T) {
getBalancer(LBMWRR)
getBalancer(LBMRR)
getBalancer(LBMRandom)
defer shouldPanic()
getBalancer("what")
}
func TestReadFromFile(t *testing.T) {
os.Remove(*configPath)
defer os.Remove(*configPath)
f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
t.Errorf("failed to open config file: %s", err)
}
defer f.Close()
content := []byte("hello world")
f.Write(content)
readContent, _ := readFromFile(*configPath)
if !bytes.Equal(readContent, content) {
t.Errorf("read from file return bad content: %s", string(readContent))
}
}
func TestConfigKeeper(t *testing.T) {
os.Remove(*configPath)
defer os.Remove(*configPath)
config := appConfig{
"www.example.com",
[]string{"192.168.1.1:80"},
[]int{1},
0.3,
false,
LBMWRR,
[]string{"/"},
[]string{"GET"},
"",
"too many requests",
}
go configKeeper()
configSync <- config
}
func TestConfigKeeperBadJSON(t *testing.T) {
os.Remove(*configPath)
defer os.Remove(*configPath)
f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
t.Errorf("failed to open config file: %s", err)
}
defer f.Close()
content := []byte("hello world")
f.Write(content)
go configKeeper()
}
func TestConfigKeeperGoodJSON(t *testing.T) {
os.Remove(*configPath)
//defer os.Remove(*configPath)
f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
t.Errorf("failed to open config file: %s", err)
}
f.Truncate(0)
f.Seek(0, 0)
f.Write([]byte(goodConfig))
f.Close()
go configKeeper()
close(configSync)
}
func TestConfigIndex(t *testing.T) {
fakeServer := httptest.NewServer(
http.HandlerFunc(configIndexHandler),
)
defer fakeServer.Close()
url := fakeServer.URL + "/"
// test 200
f, err := os.OpenFile(*configPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
t.Errorf("failed to open config file: %s", err)
}
f.Truncate(0)
f.Seek(0, 0)
f.Write([]byte(goodConfig))
f.Close()
resp, err := http.Get(url)
if err != nil {
t.Errorf("failed to get config index page")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("should got 200, but: %d", resp.StatusCode)
}
}