test_upstreams_rr.py (1477:b93d1acf81bd) test_upstreams_rr.py (1596:b7e2d4d92624)
1import os
2import re
3
4from unit.applications.lang.python import TestApplicationPython
1import os
2import re
3
4from unit.applications.lang.python import TestApplicationPython
5from conftest import option
5
6
7class TestUpstreamsRR(TestApplicationPython):
8 prerequisites = {'modules': {'python': 'any'}}
9
6
7
8class TestUpstreamsRR(TestApplicationPython):
9 prerequisites = {'modules': {'python': 'any'}}
10
10 def setUp(self):
11 super().setUp()
11 def setup_method(self):
12 super().setup_method()
12
13
13 self.assertIn(
14 'success',
15 self.conf(
16 {
17 "listeners": {
18 "*:7080": {"pass": "upstreams/one"},
19 "*:7090": {"pass": "upstreams/two"},
20 "*:7081": {"pass": "routes/one"},
21 "*:7082": {"pass": "routes/two"},
22 "*:7083": {"pass": "routes/three"},
14 assert 'success' in self.conf(
15 {
16 "listeners": {
17 "*:7080": {"pass": "upstreams/one"},
18 "*:7090": {"pass": "upstreams/two"},
19 "*:7081": {"pass": "routes/one"},
20 "*:7082": {"pass": "routes/two"},
21 "*:7083": {"pass": "routes/three"},
22 },
23 "upstreams": {
24 "one": {
25 "servers": {
26 "127.0.0.1:7081": {},
27 "127.0.0.1:7082": {},
28 },
23 },
29 },
24 "upstreams": {
25 "one": {
26 "servers": {
27 "127.0.0.1:7081": {},
28 "127.0.0.1:7082": {},
29 },
30 "two": {
31 "servers": {
32 "127.0.0.1:7081": {},
33 "127.0.0.1:7082": {},
30 },
34 },
31 "two": {
32 "servers": {
33 "127.0.0.1:7081": {},
34 "127.0.0.1:7082": {},
35 },
36 },
37 },
35 },
38 "routes": {
39 "one": [{"action": {"return": 200}}],
40 "two": [{"action": {"return": 201}}],
41 "three": [{"action": {"return": 202}}],
42 },
43 "applications": {},
44 },
36 },
45 ),
46 'upstreams initial configuration',
47 )
37 "routes": {
38 "one": [{"action": {"return": 200}}],
39 "two": [{"action": {"return": 201}}],
40 "three": [{"action": {"return": 202}}],
41 },
42 "applications": {},
43 },
44 ), 'upstreams initial configuration'
48
49 self.cpu_count = os.cpu_count()
50
51 def get_resps(self, req=100, port=7080):
52 resps = [0]
53
54 for _ in range(req):
55 status = self.get(port=port)['status']

--- 30 unchanged lines hidden (view full) ---

86 resps = [0] * (max(ups) + 1)
87 for i in range(len(ups)):
88 resps[ups[i]] += 1
89
90 return resps
91
92 def test_upstreams_rr_no_weight(self):
93 resps = self.get_resps()
45
46 self.cpu_count = os.cpu_count()
47
48 def get_resps(self, req=100, port=7080):
49 resps = [0]
50
51 for _ in range(req):
52 status = self.get(port=port)['status']

--- 30 unchanged lines hidden (view full) ---

83 resps = [0] * (max(ups) + 1)
84 for i in range(len(ups)):
85 resps[ups[i]] += 1
86
87 return resps
88
89 def test_upstreams_rr_no_weight(self):
90 resps = self.get_resps()
94 self.assertEqual(sum(resps), 100, 'no weight sum')
95 self.assertLessEqual(
96 abs(resps[0] - resps[1]), self.cpu_count, 'no weight'
97 )
91 assert sum(resps) == 100, 'no weight sum'
92 assert abs(resps[0] - resps[1]) <= self.cpu_count, 'no weight'
98
93
99 self.assertIn(
100 'success',
101 self.conf_delete('upstreams/one/servers/127.0.0.1:7081'),
102 'no weight server remove',
103 )
94 assert 'success' in self.conf_delete(
95 'upstreams/one/servers/127.0.0.1:7081'
96 ), 'no weight server remove'
104
105 resps = self.get_resps(req=50)
97
98 resps = self.get_resps(req=50)
106 self.assertEqual(resps[1], 50, 'no weight 2')
99 assert resps[1] == 50, 'no weight 2'
107
100
108 self.assertIn(
109 'success',
110 self.conf({}, 'upstreams/one/servers/127.0.0.1:7081'),
111 'no weight server revert',
112 )
101 assert 'success' in self.conf(
102 {}, 'upstreams/one/servers/127.0.0.1:7081'
103 ), 'no weight server revert'
113
114 resps = self.get_resps()
104
105 resps = self.get_resps()
115 self.assertEqual(sum(resps), 100, 'no weight 3 sum')
116 self.assertLessEqual(
117 abs(resps[0] - resps[1]), self.cpu_count, 'no weight 3'
118 )
106 assert sum(resps) == 100, 'no weight 3 sum'
107 assert abs(resps[0] - resps[1]) <= self.cpu_count, 'no weight 3'
119
108
120 self.assertIn(
121 'success',
122 self.conf({}, 'upstreams/one/servers/127.0.0.1:7083'),
123 'no weight server new',
124 )
109 assert 'success' in self.conf(
110 {}, 'upstreams/one/servers/127.0.0.1:7083'
111 ), 'no weight server new'
125
126 resps = self.get_resps()
112
113 resps = self.get_resps()
127 self.assertEqual(sum(resps), 100, 'no weight 4 sum')
128 self.assertLessEqual(
129 max(resps) - min(resps), self.cpu_count, 'no weight 4'
130 )
114 assert sum(resps) == 100, 'no weight 4 sum'
115 assert max(resps) - min(resps) <= self.cpu_count, 'no weight 4'
131
132 resps = self.get_resps_sc(req=30)
116
117 resps = self.get_resps_sc(req=30)
133 self.assertEqual(resps[0], 10, 'no weight 4 0')
134 self.assertEqual(resps[1], 10, 'no weight 4 1')
135 self.assertEqual(resps[2], 10, 'no weight 4 2')
118 assert resps[0] == 10, 'no weight 4 0'
119 assert resps[1] == 10, 'no weight 4 1'
120 assert resps[2] == 10, 'no weight 4 2'
136
137 def test_upstreams_rr_weight(self):
121
122 def test_upstreams_rr_weight(self):
138 self.assertIn(
139 'success',
140 self.conf({"weight": 3}, 'upstreams/one/servers/127.0.0.1:7081'),
141 'configure weight',
142 )
123 assert 'success' in self.conf(
124 {"weight": 3}, 'upstreams/one/servers/127.0.0.1:7081'
125 ), 'configure weight'
143
144 resps = self.get_resps_sc()
126
127 resps = self.get_resps_sc()
145 self.assertEqual(resps[0], 75, 'weight 3 0')
146 self.assertEqual(resps[1], 25, 'weight 3 1')
128 assert resps[0] == 75, 'weight 3 0'
129 assert resps[1] == 25, 'weight 3 1'
147
130
148 self.assertIn(
149 'success',
150 self.conf_delete('upstreams/one/servers/127.0.0.1:7081/weight'),
151 'configure weight remove',
152 )
131 assert 'success' in self.conf_delete(
132 'upstreams/one/servers/127.0.0.1:7081/weight'
133 ), 'configure weight remove'
153 resps = self.get_resps_sc(req=10)
134 resps = self.get_resps_sc(req=10)
154 self.assertEqual(resps[0], 5, 'weight 0 0')
155 self.assertEqual(resps[1], 5, 'weight 0 1')
135 assert resps[0] == 5, 'weight 0 0'
136 assert resps[1] == 5, 'weight 0 1'
156
137
157 self.assertIn(
158 'success',
159 self.conf('1', 'upstreams/one/servers/127.0.0.1:7081/weight'),
160 'configure weight 1',
161 )
138 assert 'success' in self.conf(
139 '1', 'upstreams/one/servers/127.0.0.1:7081/weight'
140 ), 'configure weight 1'
162
163 resps = self.get_resps_sc()
141
142 resps = self.get_resps_sc()
164 self.assertEqual(resps[0], 50, 'weight 1 0')
165 self.assertEqual(resps[1], 50, 'weight 1 1')
143 assert resps[0] == 50, 'weight 1 0'
144 assert resps[1] == 50, 'weight 1 1'
166
145
167 self.assertIn(
168 'success',
169 self.conf(
170 {
171 "127.0.0.1:7081": {"weight": 3},
172 "127.0.0.1:7083": {"weight": 2},
173 },
174 'upstreams/one/servers',
175 ),
176 'configure weight 2',
177 )
146 assert 'success' in self.conf(
147 {
148 "127.0.0.1:7081": {"weight": 3},
149 "127.0.0.1:7083": {"weight": 2},
150 },
151 'upstreams/one/servers',
152 ), 'configure weight 2'
178
179 resps = self.get_resps_sc()
153
154 resps = self.get_resps_sc()
180 self.assertEqual(resps[0], 60, 'weight 2 0')
181 self.assertEqual(resps[2], 40, 'weight 2 1')
155 assert resps[0] == 60, 'weight 2 0'
156 assert resps[2] == 40, 'weight 2 1'
182
183 def test_upstreams_rr_weight_rational(self):
184 def set_weights(w1, w2):
157
158 def test_upstreams_rr_weight_rational(self):
159 def set_weights(w1, w2):
185 self.assertIn(
186 'success',
187 self.conf(
188 {
189 "127.0.0.1:7081": {"weight": w1},
190 "127.0.0.1:7082": {"weight": w2},
191 },
192 'upstreams/one/servers',
193 ),
194 'configure weights',
195 )
160 assert 'success' in self.conf(
161 {
162 "127.0.0.1:7081": {"weight": w1},
163 "127.0.0.1:7082": {"weight": w2},
164 },
165 'upstreams/one/servers',
166 ), 'configure weights'
196
197 def check_reqs(w1, w2, reqs=10):
198 resps = self.get_resps_sc(req=reqs)
167
168 def check_reqs(w1, w2, reqs=10):
169 resps = self.get_resps_sc(req=reqs)
199 self.assertEqual(resps[0], reqs * w1 / (w1 + w2), 'weight 1')
200 self.assertEqual(resps[1], reqs * w2 / (w1 + w2), 'weight 2')
170 assert resps[0] == reqs * w1 / (w1 + w2), 'weight 1'
171 assert resps[1] == reqs * w2 / (w1 + w2), 'weight 2'
201
202 def check_weights(w1, w2):
203 set_weights(w1, w2)
204 check_reqs(w1, w2)
205
206 check_weights(0, 1)
207 check_weights(0, 999999.0123456)
208 check_weights(1, 9)
209 check_weights(100000, 900000)
172
173 def check_weights(w1, w2):
174 set_weights(w1, w2)
175 check_reqs(w1, w2)
176
177 check_weights(0, 1)
178 check_weights(0, 999999.0123456)
179 check_weights(1, 9)
180 check_weights(100000, 900000)
210 check_weights(1, .25)
211 check_weights(1, 0.25)
181 check_weights(1, 0.25)
212 check_weights(0.2, .8)
182 check_weights(1, 0.25)
183 check_weights(0.2, 0.8)
213 check_weights(1, 1.5)
184 check_weights(1, 1.5)
214 check_weights(1e-3, 1E-3)
185 check_weights(1e-3, 1e-3)
215 check_weights(1e-20, 1e-20)
216 check_weights(1e4, 1e4)
217 check_weights(1000000, 1000000)
218
219 set_weights(0.25, 0.25)
186 check_weights(1e-20, 1e-20)
187 check_weights(1e4, 1e4)
188 check_weights(1000000, 1000000)
189
190 set_weights(0.25, 0.25)
220 self.assertIn(
221 'success',
222 self.conf_delete('upstreams/one/servers/127.0.0.1:7081/weight'),
223 'delete weight',
224 )
191 assert 'success' in self.conf_delete(
192 'upstreams/one/servers/127.0.0.1:7081/weight'
193 ), 'delete weight'
225 check_reqs(1, 0.25)
226
194 check_reqs(1, 0.25)
195
227 self.assertIn(
228 'success',
229 self.conf(
230 {
231 "127.0.0.1:7081": {"weight": 0.1},
232 "127.0.0.1:7082": {"weight": 1},
233 "127.0.0.1:7083": {"weight": 0.9},
234 },
235 'upstreams/one/servers',
236 ),
237 'configure weights',
238 )
196 assert 'success' in self.conf(
197 {
198 "127.0.0.1:7081": {"weight": 0.1},
199 "127.0.0.1:7082": {"weight": 1},
200 "127.0.0.1:7083": {"weight": 0.9},
201 },
202 'upstreams/one/servers',
203 ), 'configure weights'
239 resps = self.get_resps_sc(req=20)
204 resps = self.get_resps_sc(req=20)
240 self.assertEqual(resps[0], 1, 'weight 3 1')
241 self.assertEqual(resps[1], 10, 'weight 3 2')
242 self.assertEqual(resps[2], 9, 'weight 3 3')
205 assert resps[0] == 1, 'weight 3 1'
206 assert resps[1] == 10, 'weight 3 2'
207 assert resps[2] == 9, 'weight 3 3'
243
244 def test_upstreams_rr_independent(self):
245 def sum_resps(*args):
246 sum = [0] * len(args[0])
247 for arg in args:
248 sum = [x + y for x, y in zip(sum, arg)]
249
250 return sum
251
252 resps = self.get_resps_sc(req=30, port=7090)
208
209 def test_upstreams_rr_independent(self):
210 def sum_resps(*args):
211 sum = [0] * len(args[0])
212 for arg in args:
213 sum = [x + y for x, y in zip(sum, arg)]
214
215 return sum
216
217 resps = self.get_resps_sc(req=30, port=7090)
253 self.assertEqual(resps[0], 15, 'dep two before 0')
254 self.assertEqual(resps[1], 15, 'dep two before 1')
218 assert resps[0] == 15, 'dep two before 0'
219 assert resps[1] == 15, 'dep two before 1'
255
256 resps = self.get_resps_sc(req=30)
220
221 resps = self.get_resps_sc(req=30)
257 self.assertEqual(resps[0], 15, 'dep one before 0')
258 self.assertEqual(resps[1], 15, 'dep one before 1')
222 assert resps[0] == 15, 'dep one before 0'
223 assert resps[1] == 15, 'dep one before 1'
259
224
260 self.assertIn(
261 'success',
262 self.conf('2', 'upstreams/two/servers/127.0.0.1:7081/weight'),
263 'configure dep weight',
264 )
225 assert 'success' in self.conf(
226 '2', 'upstreams/two/servers/127.0.0.1:7081/weight'
227 ), 'configure dep weight'
265
266 resps = self.get_resps_sc(req=30, port=7090)
228
229 resps = self.get_resps_sc(req=30, port=7090)
267 self.assertEqual(resps[0], 20, 'dep two 0')
268 self.assertEqual(resps[1], 10, 'dep two 1')
230 assert resps[0] == 20, 'dep two 0'
231 assert resps[1] == 10, 'dep two 1'
269
270 resps = self.get_resps_sc(req=30)
232
233 resps = self.get_resps_sc(req=30)
271 self.assertEqual(resps[0], 15, 'dep one 0')
272 self.assertEqual(resps[1], 15, 'dep one 1')
234 assert resps[0] == 15, 'dep one 0'
235 assert resps[1] == 15, 'dep one 1'
273
236
274 self.assertIn(
275 'success',
276 self.conf('1', 'upstreams/two/servers/127.0.0.1:7081/weight'),
277 'configure dep weight 1',
278 )
237 assert 'success' in self.conf(
238 '1', 'upstreams/two/servers/127.0.0.1:7081/weight'
239 ), 'configure dep weight 1'
279
280 r_one, r_two = [0, 0], [0, 0]
281 for _ in range(10):
282 r_one = sum_resps(r_one, self.get_resps(req=10))
283 r_two = sum_resps(r_two, self.get_resps(req=10, port=7090))
284
240
241 r_one, r_two = [0, 0], [0, 0]
242 for _ in range(10):
243 r_one = sum_resps(r_one, self.get_resps(req=10))
244 r_two = sum_resps(r_two, self.get_resps(req=10, port=7090))
245
246 assert sum(r_one) == 100, 'dep one mix sum'
247 assert abs(r_one[0] - r_one[1]) <= self.cpu_count, 'dep one mix'
248 assert sum(r_two) == 100, 'dep two mix sum'
249 assert abs(r_two[0] - r_two[1]) <= self.cpu_count, 'dep two mix'
285
250
286 self.assertEqual(sum(r_one), 100, 'dep one mix sum')
287 self.assertLessEqual(
288 abs(r_one[0] - r_one[1]), self.cpu_count, 'dep one mix'
289 )
290 self.assertEqual(sum(r_two), 100, 'dep two mix sum')
291 self.assertLessEqual(
292 abs(r_two[0] - r_two[1]), self.cpu_count, 'dep two mix'
293 )
294
295 def test_upstreams_rr_delay(self):
251 def test_upstreams_rr_delay(self):
296 self.assertIn(
297 'success',
298 self.conf(
299 {
300 "listeners": {
301 "*:7080": {"pass": "upstreams/one"},
302 "*:7081": {"pass": "routes"},
303 "*:7082": {"pass": "routes"},
304 },
305 "upstreams": {
306 "one": {
307 "servers": {
308 "127.0.0.1:7081": {},
309 "127.0.0.1:7082": {},
310 },
252 assert 'success' in self.conf(
253 {
254 "listeners": {
255 "*:7080": {"pass": "upstreams/one"},
256 "*:7081": {"pass": "routes"},
257 "*:7082": {"pass": "routes"},
258 },
259 "upstreams": {
260 "one": {
261 "servers": {
262 "127.0.0.1:7081": {},
263 "127.0.0.1:7082": {},
311 },
312 },
264 },
265 },
313 "routes": [
314 {
315 "match": {"destination": "*:7081"},
316 "action": {"pass": "applications/delayed"},
317 },
318 {
319 "match": {"destination": "*:7082"},
320 "action": {"return": 201},
321 },
322 ],
323 "applications": {
324 "delayed": {
325 "type": "python",
326 "processes": {"spare": 0},
327 "path": self.current_dir + "/python/delayed",
328 "working_directory": self.current_dir
329 + "/python/delayed",
330 "module": "wsgi",
331 }
266 },
267 "routes": [
268 {
269 "match": {"destination": "*:7081"},
270 "action": {"pass": "applications/delayed"},
332 },
271 },
272 {
273 "match": {"destination": "*:7082"},
274 "action": {"return": 201},
275 },
276 ],
277 "applications": {
278 "delayed": {
279 "type": "python",
280 "processes": {"spare": 0},
281 "path": option.test_dir + "/python/delayed",
282 "working_directory": option.test_dir
283 + "/python/delayed",
284 "module": "wsgi",
285 }
333 },
286 },
334 ),
335 'upstreams initial configuration',
336 )
287 },
288 ), 'upstreams initial configuration'
337
338 req = 50
339
340 socks = []
341 for i in range(req):
342 delay = 1 if i % 5 == 0 else 0
343 _, sock = self.get(
344 headers={

--- 7 unchanged lines hidden (view full) ---

352 )
353 socks.append(sock)
354
355 resps = [0, 0]
356 for i in range(req):
357 resp = self.recvall(socks[i]).decode()
358 socks[i].close()
359
289
290 req = 50
291
292 socks = []
293 for i in range(req):
294 delay = 1 if i % 5 == 0 else 0
295 _, sock = self.get(
296 headers={

--- 7 unchanged lines hidden (view full) ---

304 )
305 socks.append(sock)
306
307 resps = [0, 0]
308 for i in range(req):
309 resp = self.recvall(socks[i]).decode()
310 socks[i].close()
311
360 m = re.search('HTTP/1.1 20(\d)', resp)
361 self.assertIsNotNone(m, 'status')
312 m = re.search(r'HTTP/1.1 20(\d)', resp)
313 assert m is not None, 'status'
362 resps[int(m.group(1))] += 1
363
314 resps[int(m.group(1))] += 1
315
364 self.assertEqual(sum(resps), req, 'delay sum')
365 self.assertLessEqual(abs(resps[0] - resps[1]), self.cpu_count, 'delay')
316 assert sum(resps) == req, 'delay sum'
317 assert abs(resps[0] - resps[1]) <= self.cpu_count, 'delay'
366
367 def test_upstreams_rr_active_req(self):
368 conns = 5
369 socks = []
370 socks2 = []
371
372 for _ in range(conns):
373 _, sock = self.get(start=True, no_recv=True)

--- 10 unchanged lines hidden (view full) ---

384 no_recv=True,
385 raw=True,
386 )
387 socks2.append(sock2)
388
389 # Send one more request and read response to make sure that previous
390 # requests had enough time to reach server.
391
318
319 def test_upstreams_rr_active_req(self):
320 conns = 5
321 socks = []
322 socks2 = []
323
324 for _ in range(conns):
325 _, sock = self.get(start=True, no_recv=True)

--- 10 unchanged lines hidden (view full) ---

336 no_recv=True,
337 raw=True,
338 )
339 socks2.append(sock2)
340
341 # Send one more request and read response to make sure that previous
342 # requests had enough time to reach server.
343
392 self.assertEqual(self.get()['body'], '')
344 assert self.get()['body'] == ''
393
345
394 self.assertIn(
395 'success',
396 self.conf(
397 {"127.0.0.1:7083": {"weight": 2}}, 'upstreams/one/servers',
398 ),
399 'active req new server',
400 )
401 self.assertIn(
402 'success',
403 self.conf_delete('upstreams/one/servers/127.0.0.1:7083'),
404 'active req server remove',
405 )
406 self.assertIn(
407 'success', self.conf_delete('listeners/*:7080'), 'delete listener'
408 )
409 self.assertIn(
410 'success',
411 self.conf_delete('upstreams/one'),
412 'active req upstream remove',
413 )
346 assert 'success' in self.conf(
347 {"127.0.0.1:7083": {"weight": 2}}, 'upstreams/one/servers',
348 ), 'active req new server'
349 assert 'success' in self.conf_delete(
350 'upstreams/one/servers/127.0.0.1:7083'
351 ), 'active req server remove'
352 assert 'success' in self.conf_delete(
353 'listeners/*:7080'
354 ), 'delete listener'
355 assert 'success' in self.conf_delete(
356 'upstreams/one'
357 ), 'active req upstream remove'
414
415 for i in range(conns):
358
359 for i in range(conns):
416 self.assertEqual(
417 self.http(b'', sock=socks[i], raw=True)['body'],
418 '',
419 'active req GET',
420 )
360 assert (
361 self.http(b'', sock=socks[i], raw=True)['body'] == ''
362 ), 'active req GET'
421
363
422 self.assertEqual(
423 self.http(b"""0123456789""", sock=socks2[i], raw=True)['body'],
424 '',
425 'active req POST',
426 )
364 assert (
365 self.http(b"""0123456789""", sock=socks2[i], raw=True)['body']
366 == ''
367 ), 'active req POST'
427
428 def test_upstreams_rr_bad_server(self):
368
369 def test_upstreams_rr_bad_server(self):
429 self.assertIn(
430 'success',
431 self.conf({"weight": 1}, 'upstreams/one/servers/127.0.0.1:7084'),
432 'configure bad server',
433 )
370 assert 'success' in self.conf(
371 {"weight": 1}, 'upstreams/one/servers/127.0.0.1:7084'
372 ), 'configure bad server'
434
435 resps = self.get_resps_sc(req=30)
373
374 resps = self.get_resps_sc(req=30)
436 self.assertEqual(resps[0], 10, 'bad server 0')
437 self.assertEqual(resps[1], 10, 'bad server 1')
438 self.assertEqual(sum(resps), 20, 'bad server sum')
375 assert resps[0] == 10, 'bad server 0'
376 assert resps[1] == 10, 'bad server 1'
377 assert sum(resps) == 20, 'bad server sum'
439
440 def test_upstreams_rr_pipeline(self):
441 resps = self.get_resps_sc()
442
378
379 def test_upstreams_rr_pipeline(self):
380 resps = self.get_resps_sc()
381
443 self.assertEqual(resps[0], 50, 'pipeline 0')
444 self.assertEqual(resps[1], 50, 'pipeline 1')
382 assert resps[0] == 50, 'pipeline 0'
383 assert resps[1] == 50, 'pipeline 1'
445
446 def test_upstreams_rr_post(self):
447 resps = [0, 0]
448 for _ in range(50):
449 resps[self.get()['status'] % 10] += 1
450 resps[self.post(body='0123456789')['status'] % 10] += 1
451
384
385 def test_upstreams_rr_post(self):
386 resps = [0, 0]
387 for _ in range(50):
388 resps[self.get()['status'] % 10] += 1
389 resps[self.post(body='0123456789')['status'] % 10] += 1
390
452 self.assertEqual(sum(resps), 100, 'post sum')
453 self.assertLessEqual(abs(resps[0] - resps[1]), self.cpu_count, 'post')
391 assert sum(resps) == 100, 'post sum'
392 assert abs(resps[0] - resps[1]) <= self.cpu_count, 'post'
454
455 def test_upstreams_rr_unix(self):
393
394 def test_upstreams_rr_unix(self):
456 addr_0 = self.testdir + '/sock_0'
457 addr_1 = self.testdir + '/sock_1'
395 addr_0 = self.temp_dir + '/sock_0'
396 addr_1 = self.temp_dir + '/sock_1'
458
397
459 self.assertIn(
460 'success',
461 self.conf(
462 {
463 "*:7080": {"pass": "upstreams/one"},
464 "unix:" + addr_0: {"pass": "routes/one"},
465 "unix:" + addr_1: {"pass": "routes/two"},
466 },
467 'listeners',
468 ),
469 'configure listeners unix',
470 )
398 assert 'success' in self.conf(
399 {
400 "*:7080": {"pass": "upstreams/one"},
401 "unix:" + addr_0: {"pass": "routes/one"},
402 "unix:" + addr_1: {"pass": "routes/two"},
403 },
404 'listeners',
405 ), 'configure listeners unix'
471
406
472 self.assertIn(
473 'success',
474 self.conf(
475 {"unix:" + addr_0: {}, "unix:" + addr_1: {}},
476 'upstreams/one/servers',
477 ),
478 'configure servers unix',
479 )
407 assert 'success' in self.conf(
408 {"unix:" + addr_0: {}, "unix:" + addr_1: {}},
409 'upstreams/one/servers',
410 ), 'configure servers unix'
480
481 resps = self.get_resps_sc()
482
411
412 resps = self.get_resps_sc()
413
483 self.assertEqual(resps[0], 50, 'unix 0')
484 self.assertEqual(resps[1], 50, 'unix 1')
414 assert resps[0] == 50, 'unix 0'
415 assert resps[1] == 50, 'unix 1'
485
486 def test_upstreams_rr_ipv6(self):
416
417 def test_upstreams_rr_ipv6(self):
487 self.assertIn(
488 'success',
489 self.conf(
490 {
491 "*:7080": {"pass": "upstreams/one"},
492 "[::1]:7081": {"pass": "routes/one"},
493 "[::1]:7082": {"pass": "routes/two"},
494 },
495 'listeners',
496 ),
497 'configure listeners ipv6',
498 )
418 assert 'success' in self.conf(
419 {
420 "*:7080": {"pass": "upstreams/one"},
421 "[::1]:7081": {"pass": "routes/one"},
422 "[::1]:7082": {"pass": "routes/two"},
423 },
424 'listeners',
425 ), 'configure listeners ipv6'
499
426
500 self.assertIn(
501 'success',
502 self.conf(
503 {"[::1]:7081": {}, "[::1]:7082": {}}, 'upstreams/one/servers'
504 ),
505 'configure servers ipv6',
506 )
427 assert 'success' in self.conf(
428 {"[::1]:7081": {}, "[::1]:7082": {}}, 'upstreams/one/servers'
429 ), 'configure servers ipv6'
507
508 resps = self.get_resps_sc()
509
430
431 resps = self.get_resps_sc()
432
510 self.assertEqual(resps[0], 50, 'ipv6 0')
511 self.assertEqual(resps[1], 50, 'ipv6 1')
433 assert resps[0] == 50, 'ipv6 0'
434 assert resps[1] == 50, 'ipv6 1'
512
513 def test_upstreams_rr_servers_empty(self):
435
436 def test_upstreams_rr_servers_empty(self):
514 self.assertIn(
515 'success',
516 self.conf({}, 'upstreams/one/servers'),
517 'configure servers empty',
518 )
519 self.assertEqual(self.get()['status'], 502, 'servers empty')
437 assert 'success' in self.conf(
438 {}, 'upstreams/one/servers'
439 ), 'configure servers empty'
440 assert self.get()['status'] == 502, 'servers empty'
520
441
521 self.assertIn(
522 'success',
523 self.conf(
524 {"127.0.0.1:7081": {"weight": 0}}, 'upstreams/one/servers'
525 ),
526 'configure servers empty one',
527 )
528 self.assertEqual(self.get()['status'], 502, 'servers empty one')
529 self.assertIn(
530 'success',
531 self.conf(
532 {
533 "127.0.0.1:7081": {"weight": 0},
534 "127.0.0.1:7082": {"weight": 0},
535 },
536 'upstreams/one/servers',
537 ),
538 'configure servers empty two',
539 )
540 self.assertEqual(self.get()['status'], 502, 'servers empty two')
442 assert 'success' in self.conf(
443 {"127.0.0.1:7081": {"weight": 0}}, 'upstreams/one/servers'
444 ), 'configure servers empty one'
445 assert self.get()['status'] == 502, 'servers empty one'
446 assert 'success' in self.conf(
447 {
448 "127.0.0.1:7081": {"weight": 0},
449 "127.0.0.1:7082": {"weight": 0},
450 },
451 'upstreams/one/servers',
452 ), 'configure servers empty two'
453 assert self.get()['status'] == 502, 'servers empty two'
541
542 def test_upstreams_rr_invalid(self):
454
455 def test_upstreams_rr_invalid(self):
543 self.assertIn(
544 'error', self.conf({}, 'upstreams'), 'upstreams empty',
545 )
546 self.assertIn(
547 'error', self.conf({}, 'upstreams/one'), 'named upstreams empty',
548 )
549 self.assertIn(
550 'error',
551 self.conf({}, 'upstreams/one/servers/127.0.0.1'),
552 'invalid address',
553 )
554 self.assertIn(
555 'error',
556 self.conf({}, 'upstreams/one/servers/127.0.0.1:7081/blah'),
557 'invalid server option',
558 )
456 assert 'error' in self.conf({}, 'upstreams'), 'upstreams empty'
457 assert 'error' in self.conf(
458 {}, 'upstreams/one'
459 ), 'named upstreams empty'
460 assert 'error' in self.conf(
461 {}, 'upstreams/one/servers/127.0.0.1'
462 ), 'invalid address'
463 assert 'error' in self.conf(
464 {}, 'upstreams/one/servers/127.0.0.1:7081/blah'
465 ), 'invalid server option'
559
560 def check_weight(w):
466
467 def check_weight(w):
561 self.assertIn(
562 'error',
563 self.conf(w, 'upstreams/one/servers/127.0.0.1:7081/weight'),
564 'invalid weight option',
565 )
468 assert 'error' in self.conf(
469 w, 'upstreams/one/servers/127.0.0.1:7081/weight'
470 ), 'invalid weight option'
471
566 check_weight({})
567 check_weight('-1')
568 check_weight('1.')
569 check_weight('1.1.')
570 check_weight('.')
571 check_weight('.01234567890123')
572 check_weight('1000001')
573 check_weight('2e6')
472 check_weight({})
473 check_weight('-1')
474 check_weight('1.')
475 check_weight('1.1.')
476 check_weight('.')
477 check_weight('.01234567890123')
478 check_weight('1000001')
479 check_weight('2e6')
574
575
576if __name__ == '__main__':
577 TestUpstreamsRR.main()