summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/reverseproxy/selectionpolicies_test.go
blob: 8006fb1bef10507b33e328a5daa179db2d6e29ed (plain)
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
// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package reverseproxy

// TODO: finish migrating these

// import (
// 	"net/http"
// 	"net/http/httptest"
// 	"os"
// 	"testing"
// )

// var workableServer *httptest.Server

// func TestMain(m *testing.M) {
// 	workableServer = httptest.NewServer(http.HandlerFunc(
// 		func(w http.ResponseWriter, r *http.Request) {
// 			// do nothing
// 		}))
// 	r := m.Run()
// 	workableServer.Close()
// 	os.Exit(r)
// }

// type customPolicy struct{}

// func (customPolicy) Select(pool HostPool, _ *http.Request) Host {
// 	return pool[0]
// }

// func testPool() HostPool {
// 	pool := []*UpstreamHost{
// 		{
// 			Name: workableServer.URL, // this should resolve (healthcheck test)
// 		},
// 		{
// 			Name: "http://localhost:99998", // this shouldn't
// 		},
// 		{
// 			Name: "http://C",
// 		},
// 	}
// 	return HostPool(pool)
// }

// func TestRoundRobinPolicy(t *testing.T) {
// 	pool := testPool()
// 	rrPolicy := &RoundRobin{}
// 	request, _ := http.NewRequest("GET", "/", nil)

// 	h := rrPolicy.Select(pool, request)
// 	// First selected host is 1, because counter starts at 0
// 	// and increments before host is selected
// 	if h != pool[1] {
// 		t.Error("Expected first round robin host to be second host in the pool.")
// 	}
// 	h = rrPolicy.Select(pool, request)
// 	if h != pool[2] {
// 		t.Error("Expected second round robin host to be third host in the pool.")
// 	}
// 	h = rrPolicy.Select(pool, request)
// 	if h != pool[0] {
// 		t.Error("Expected third round robin host to be first host in the pool.")
// 	}
// 	// mark host as down
// 	pool[1].Unhealthy = 1
// 	h = rrPolicy.Select(pool, request)
// 	if h != pool[2] {
// 		t.Error("Expected to skip down host.")
// 	}
// 	// mark host as up
// 	pool[1].Unhealthy = 0

// 	h = rrPolicy.Select(pool, request)
// 	if h == pool[2] {
// 		t.Error("Expected to balance evenly among healthy hosts")
// 	}
// 	// mark host as full
// 	pool[1].Conns = 1
// 	pool[1].MaxConns = 1
// 	h = rrPolicy.Select(pool, request)
// 	if h != pool[2] {
// 		t.Error("Expected to skip full host.")
// 	}
// }

// func TestLeastConnPolicy(t *testing.T) {
// 	pool := testPool()
// 	lcPolicy := &LeastConn{}
// 	request, _ := http.NewRequest("GET", "/", nil)

// 	pool[0].Conns = 10
// 	pool[1].Conns = 10
// 	h := lcPolicy.Select(pool, request)
// 	if h != pool[2] {
// 		t.Error("Expected least connection host to be third host.")
// 	}
// 	pool[2].Conns = 100
// 	h = lcPolicy.Select(pool, request)
// 	if h != pool[0] && h != pool[1] {
// 		t.Error("Expected least connection host to be first or second host.")
// 	}
// }

// func TestCustomPolicy(t *testing.T) {
// 	pool := testPool()
// 	customPolicy := &customPolicy{}
// 	request, _ := http.NewRequest("GET", "/", nil)

// 	h := customPolicy.Select(pool, request)
// 	if h != pool[0] {
// 		t.Error("Expected custom policy host to be the first host.")
// 	}
// }

// func TestIPHashPolicy(t *testing.T) {
// 	pool := testPool()
// 	ipHash := &IPHash{}
// 	request, _ := http.NewRequest("GET", "/", nil)
// 	// We should be able to predict where every request is routed.
// 	request.RemoteAddr = "172.0.0.1:80"
// 	h := ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}
// 	request.RemoteAddr = "172.0.0.2:80"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}
// 	request.RemoteAddr = "172.0.0.3:80"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[2] {
// 		t.Error("Expected ip hash policy host to be the third host.")
// 	}
// 	request.RemoteAddr = "172.0.0.4:80"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}

// 	// we should get the same results without a port
// 	request.RemoteAddr = "172.0.0.1"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}
// 	request.RemoteAddr = "172.0.0.2"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}
// 	request.RemoteAddr = "172.0.0.3"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[2] {
// 		t.Error("Expected ip hash policy host to be the third host.")
// 	}
// 	request.RemoteAddr = "172.0.0.4"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}

// 	// we should get a healthy host if the original host is unhealthy and a
// 	// healthy host is available
// 	request.RemoteAddr = "172.0.0.1"
// 	pool[1].Unhealthy = 1
// 	h = ipHash.Select(pool, request)
// 	if h != pool[2] {
// 		t.Error("Expected ip hash policy host to be the third host.")
// 	}

// 	request.RemoteAddr = "172.0.0.2"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[2] {
// 		t.Error("Expected ip hash policy host to be the third host.")
// 	}
// 	pool[1].Unhealthy = 0

// 	request.RemoteAddr = "172.0.0.3"
// 	pool[2].Unhealthy = 1
// 	h = ipHash.Select(pool, request)
// 	if h != pool[0] {
// 		t.Error("Expected ip hash policy host to be the first host.")
// 	}
// 	request.RemoteAddr = "172.0.0.4"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}

// 	// We should be able to resize the host pool and still be able to predict
// 	// where a request will be routed with the same IP's used above
// 	pool = []*UpstreamHost{
// 		{
// 			Name: workableServer.URL, // this should resolve (healthcheck test)
// 		},
// 		{
// 			Name: "http://localhost:99998", // this shouldn't
// 		},
// 	}
// 	pool = HostPool(pool)
// 	request.RemoteAddr = "172.0.0.1:80"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[0] {
// 		t.Error("Expected ip hash policy host to be the first host.")
// 	}
// 	request.RemoteAddr = "172.0.0.2:80"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}
// 	request.RemoteAddr = "172.0.0.3:80"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[0] {
// 		t.Error("Expected ip hash policy host to be the first host.")
// 	}
// 	request.RemoteAddr = "172.0.0.4:80"
// 	h = ipHash.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected ip hash policy host to be the second host.")
// 	}

// 	// We should get nil when there are no healthy hosts
// 	pool[0].Unhealthy = 1
// 	pool[1].Unhealthy = 1
// 	h = ipHash.Select(pool, request)
// 	if h != nil {
// 		t.Error("Expected ip hash policy host to be nil.")
// 	}
// }

// func TestFirstPolicy(t *testing.T) {
// 	pool := testPool()
// 	firstPolicy := &First{}
// 	req := httptest.NewRequest(http.MethodGet, "/", nil)

// 	h := firstPolicy.Select(pool, req)
// 	if h != pool[0] {
// 		t.Error("Expected first policy host to be the first host.")
// 	}

// 	pool[0].Unhealthy = 1
// 	h = firstPolicy.Select(pool, req)
// 	if h != pool[1] {
// 		t.Error("Expected first policy host to be the second host.")
// 	}
// }

// func TestUriPolicy(t *testing.T) {
// 	pool := testPool()
// 	uriPolicy := &URIHash{}

// 	request := httptest.NewRequest(http.MethodGet, "/test", nil)
// 	h := uriPolicy.Select(pool, request)
// 	if h != pool[0] {
// 		t.Error("Expected uri policy host to be the first host.")
// 	}

// 	pool[0].Unhealthy = 1
// 	h = uriPolicy.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected uri policy host to be the first host.")
// 	}

// 	request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
// 	h = uriPolicy.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected uri policy host to be the second host.")
// 	}

// 	// We should be able to resize the host pool and still be able to predict
// 	// where a request will be routed with the same URI's used above
// 	pool = []*UpstreamHost{
// 		{
// 			Name: workableServer.URL, // this should resolve (healthcheck test)
// 		},
// 		{
// 			Name: "http://localhost:99998", // this shouldn't
// 		},
// 	}

// 	request = httptest.NewRequest(http.MethodGet, "/test", nil)
// 	h = uriPolicy.Select(pool, request)
// 	if h != pool[0] {
// 		t.Error("Expected uri policy host to be the first host.")
// 	}

// 	pool[0].Unhealthy = 1
// 	h = uriPolicy.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected uri policy host to be the first host.")
// 	}

// 	request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
// 	h = uriPolicy.Select(pool, request)
// 	if h != pool[1] {
// 		t.Error("Expected uri policy host to be the second host.")
// 	}

// 	pool[0].Unhealthy = 1
// 	pool[1].Unhealthy = 1
// 	h = uriPolicy.Select(pool, request)
// 	if h != nil {
// 		t.Error("Expected uri policy policy host to be nil.")
// 	}
// }

// func TestHeaderPolicy(t *testing.T) {
// 	pool := testPool()
// 	tests := []struct {
// 		Name               string
// 		Policy             *Header
// 		RequestHeaderName  string
// 		RequestHeaderValue string
// 		NilHost            bool
// 		HostIndex          int
// 	}{
// 		{"empty config", &Header{""}, "", "", true, 0},
// 		{"empty config+header+value", &Header{""}, "Affinity", "somevalue", true, 0},
// 		{"empty config+header", &Header{""}, "Affinity", "", true, 0},

// 		{"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 1},
// 		{"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 2},
// 		{"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 0},

// 		{"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue", false, 1},
// 		{"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue2", false, 0},
// 		{"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue3", false, 2},
// 		{"hash route with empty value", &Header{"Affinity"}, "Affinity", "", false, 1},
// 	}

// 	for idx, test := range tests {
// 		request, _ := http.NewRequest("GET", "/", nil)
// 		if test.RequestHeaderName != "" {
// 			request.Header.Add(test.RequestHeaderName, test.RequestHeaderValue)
// 		}

// 		host := test.Policy.Select(pool, request)
// 		if test.NilHost && host != nil {
// 			t.Errorf("%d: Expected host to be nil", idx)
// 		}
// 		if !test.NilHost && host == nil {
// 			t.Errorf("%d: Did not expect host to be nil", idx)
// 		}
// 		if !test.NilHost && host != pool[test.HostIndex] {
// 			t.Errorf("%d: Expected Header policy to be host %d", idx, test.HostIndex)
// 		}
// 	}
// }