-
Notifications
You must be signed in to change notification settings - Fork 7
/
loadbalancer_test.go
238 lines (223 loc) · 7.29 KB
/
loadbalancer_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
package gsclient
import (
"encoding/json"
"fmt"
"net/http"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_CreateLoadBalancer(t *testing.T) {
server, client, mux := setupTestClient(true)
defer server.Close()
var isFailed bool
lb := getMockLoadbalancer("active").Properties
labelSlices := [][]string{nil, lb.Labels}
uri := path.Join(apiLoadBalancerBase)
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodPost)
w.Header().Set(requestUUIDHeader, dummyRequestUUID)
if isFailed {
w.WriteHeader(400)
} else {
fmt.Fprint(w, prepareLoadBalancerHTTPCreateResponse())
}
})
for _, testSuccessFail := range commonSuccessFailTestCases {
isFailed = testSuccessFail.isFailed
for _, testLabel := range labelSlices {
lbRequest := LoadBalancerCreateRequest{
Name: lb.Name,
Algorithm: LoadbalancerLeastConnAlg,
ListenIPv6UUID: lb.ListenIPv6UUID,
ListenIPv4UUID: lb.ListenIPv4UUID,
RedirectHTTPToHTTPS: lb.RedirectHTTPToHTTPS,
ForwardingRules: lb.ForwardingRules,
BackendServers: lb.BackendServers,
Labels: testLabel,
}
response, err := client.CreateLoadBalancer(emptyCtx, lbRequest)
if testSuccessFail.isFailed {
assert.NotNil(t, err)
} else {
assert.Nil(t, err, "CreateLoadBalancer returned error: %v", err)
assert.Equal(t, fmt.Sprintf("%s", prepareLoadBalancerObjectCreateResponse()), fmt.Sprintf("%s", response))
}
}
}
}
func TestClient_GetLoadBalancer(t *testing.T) {
server, client, mux := setupTestClient(true)
uri := path.Join(apiLoadBalancerBase, dummyUUID)
expectedObject := getMockLoadbalancer("active")
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodGet)
w.Header().Set(requestUUIDHeader, dummyRequestUUID)
fmt.Fprint(w, prepareLoadBalancerHTTPGetResponse("active"))
})
for _, test := range uuidCommonTestCases {
loadbalancer, err := client.GetLoadBalancer(emptyCtx, test.testUUID)
if test.isFailed {
assert.NotNil(t, err)
} else {
assert.Nil(t, err, "GetLoadBalancer returned error: %v", err)
assert.Equal(t, fmt.Sprintf("%v", expectedObject.Properties), fmt.Sprintf("%v", loadbalancer.Properties))
}
}
server.Close()
}
func TestClient_GetLoadBalancerList(t *testing.T) {
server, client, mux := setupTestClient(true)
defer server.Close()
uri := path.Join(apiLoadBalancerBase)
expectedObjects := getMockLoadbalancer("active")
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodGet)
w.Header().Set(requestUUIDHeader, dummyRequestUUID)
fmt.Fprint(w, prepareLoadBalancerHTTPListResponse("active"))
})
loadbalancers, err := client.GetLoadBalancerList(emptyCtx)
assert.Nil(t, err, "GetLoadBalancerList returned error: %v", err)
assert.Equal(t, 1, len(loadbalancers))
assert.Equal(t, fmt.Sprintf("[%v]", expectedObjects), fmt.Sprintf("%v", loadbalancers))
}
func TestClient_UpdateLoadBalancer(t *testing.T) {
server, client, mux := setupTestClient(true)
defer server.Close()
var isFailed bool
uri := path.Join(apiLoadBalancerBase, dummyUUID)
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(requestUUIDHeader, dummyRequestUUID)
if isFailed {
w.WriteHeader(400)
} else {
if r.Method == http.MethodPatch {
fmt.Fprintf(w, "")
} else if r.Method == http.MethodGet {
fmt.Fprint(w, prepareLoadBalancerHTTPGetResponse("active"))
}
}
})
for _, serverTest := range commonSuccessFailTestCases {
isFailed = serverTest.isFailed
for _, test := range uuidCommonTestCases {
err := client.UpdateLoadBalancer(
emptyCtx,
test.testUUID,
LoadBalancerUpdateRequest{
Name: "test",
ListenIPv6UUID: dummyUUID,
ListenIPv4UUID: dummyUUID,
RedirectHTTPToHTTPS: false,
Status: "inactive",
})
if test.isFailed || isFailed {
assert.NotNil(t, err)
} else {
assert.Nil(t, err, "UpdateLoadBalancer returned an error %v", err)
}
}
}
}
func TestClient_DeleteLoadBalancer(t *testing.T) {
server, client, mux := setupTestClient(true)
defer server.Close()
var isFailed bool
uri := path.Join(apiLoadBalancerBase, dummyUUID)
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(requestUUIDHeader, dummyRequestUUID)
if isFailed {
w.WriteHeader(400)
} else {
if r.Method == http.MethodDelete {
fmt.Fprintf(w, "")
} else if r.Method == http.MethodGet {
w.WriteHeader(404)
}
}
})
for _, serverTest := range commonSuccessFailTestCases {
isFailed = serverTest.isFailed
for _, test := range uuidCommonTestCases {
err := client.DeleteLoadBalancer(emptyCtx, test.testUUID)
if test.isFailed || isFailed {
assert.NotNil(t, err)
} else {
assert.Nil(t, err, "DeleteLoadBalancer returned an error %v", err)
}
}
}
}
func TestClient_GetLoadBalancerEventList(t *testing.T) {
server, client, mux := setupTestClient(true)
defer server.Close()
uri := path.Join(apiLoadBalancerBase, dummyUUID, "events")
expectedObjects := getMockEvent()
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodGet)
w.Header().Set(requestUUIDHeader, dummyRequestUUID)
fmt.Fprint(w, prepareEventListHTTPGet())
})
for _, test := range uuidCommonTestCases {
response, err := client.GetLoadBalancerEventList(emptyCtx, test.testUUID)
if test.isFailed {
assert.NotNil(t, err)
} else {
assert.Nil(t, err, "GetLoadBalancerEventList returned error: %v", err)
assert.Equal(t, 1, len(response))
assert.Equal(t, fmt.Sprintf("[%v]", expectedObjects), fmt.Sprintf("%v", response))
}
}
}
func getMockLoadbalancer(status string) LoadBalancer {
labels := make([]string, 0)
labels = append(labels, "nice")
lb := LoadBalancer{
Properties: LoadBalancerProperties{
ObjectUUID: dummyUUID,
Name: "go-client-lb",
Status: status,
Algorithm: "leastconn",
LocationUUID: "45ed677b-3702-4b36-be2a-a2eab9827950",
ListenIPv6UUID: "880b7f98-3702-4b36-be2a-a2eab9827950",
ListenIPv4UUID: "880b7f98-3702-4b36-be2a-a2eab9827950",
RedirectHTTPToHTTPS: false,
ForwardingRules: []ForwardingRule{
{
LetsencryptSSL: nil,
ListenPort: 8080,
Mode: "http",
TargetPort: 8000,
},
},
BackendServers: []BackendServer{
{
Weight: 100,
Host: "185.201.147.176",
ProxyProtocol: nil,
},
},
Labels: labels,
},
}
return lb
}
func prepareLoadBalancerHTTPGetResponse(status string) string {
lb := getMockLoadbalancer(status)
res, _ := json.Marshal(lb.Properties)
return fmt.Sprintf(`{"loadbalancer": %s}`, string(res))
}
func prepareLoadBalancerHTTPListResponse(status string) string {
lb := getMockLoadbalancer(status)
res, _ := json.Marshal(lb.Properties)
return fmt.Sprintf(`{"loadbalancers": {"%s": %s}}`, dummyUUID, string(res))
}
func prepareLoadBalancerHTTPCreateResponse() string {
return fmt.Sprintf(`{"request_uuid": "%s","object_uuid": "%s"}`, dummyRequestUUID, dummyUUID)
}
func prepareLoadBalancerObjectCreateResponse() LoadBalancerCreateResponse {
return LoadBalancerCreateResponse{
RequestUUID: dummyRequestUUID,
ObjectUUID: dummyUUID,
}
}