1import logging
2import sys
3from typing import Optional
4
5import pytest
6
7logging.basicConfig(
8 level=logging.INFO,
9 format="%(asctime)s test %(levelname)s: %(message)s",
10 datefmt="%Y-%m-%d %H:%M:%S",
11)
12
13logger = logging.getLogger("ambassador")
14
15from ambassador import IR, Config
16from ambassador.fetch import ResourceFetcher
17from ambassador.ir.irbasemapping import normalize_service_name
18from ambassador.utils import NullSecretHandler
19
20yaml = """
21---
22apiVersion: getambassador.io/v3alpha1
23kind: Module
24name: ambassador
25config: {}
26"""
27
28
29def qualify_service_name(
30 ir: "IR", service: str, namespace: Optional[str], rkey: Optional[str] = None
31) -> str:
32 return normalize_service_name(ir, service, namespace, "KubernetesTestResolver", rkey=rkey)
33
34
35def test_qualify_service():
36 """
37 Note: This has a Go equivalent in github.com/datawire/ambassador/v2/pkg/emissaryutil. Please
38 keep them in-sync.
39 """
40 aconf = Config()
41
42 fetcher = ResourceFetcher(logger, aconf)
43 fetcher.parse_yaml(yaml)
44
45 aconf.load_all(fetcher.sorted())
46
47 secret_handler = NullSecretHandler(logger, None, None, "0")
48
49 ir = IR(aconf, file_checker=lambda path: True, secret_handler=secret_handler)
50
51 assert ir, "could not create an IR"
52
53 assert qualify_service_name(ir, "backoffice", None) == "backoffice"
54 assert qualify_service_name(ir, "backoffice", "default") == "backoffice"
55 assert qualify_service_name(ir, "backoffice", "otherns") == "backoffice.otherns"
56 assert qualify_service_name(ir, "backoffice.otherns", None) == "backoffice.otherns"
57 assert qualify_service_name(ir, "backoffice.otherns", "default") == "backoffice.otherns"
58 assert qualify_service_name(ir, "backoffice.otherns", "otherns") == "backoffice.otherns"
59
60 assert normalize_service_name(ir, "backoffice", None, "ConsulResolver") == "backoffice"
61 assert normalize_service_name(ir, "backoffice", "default", "ConsulResolver") == "backoffice"
62 assert normalize_service_name(ir, "backoffice", "otherns", "ConsulResolver") == "backoffice"
63 assert (
64 normalize_service_name(ir, "backoffice.otherns", None, "ConsulResolver")
65 == "backoffice.otherns"
66 )
67 assert (
68 normalize_service_name(ir, "backoffice.otherns", "default", "ConsulResolver")
69 == "backoffice.otherns"
70 )
71 assert (
72 normalize_service_name(ir, "backoffice.otherns", "otherns", "ConsulResolver")
73 == "backoffice.otherns"
74 )
75
76 assert qualify_service_name(ir, "backoffice:80", None) == "backoffice:80"
77 assert qualify_service_name(ir, "backoffice:80", "default") == "backoffice:80"
78 assert qualify_service_name(ir, "backoffice:80", "otherns") == "backoffice.otherns:80"
79 assert qualify_service_name(ir, "backoffice.otherns:80", None) == "backoffice.otherns:80"
80 assert qualify_service_name(ir, "backoffice.otherns:80", "default") == "backoffice.otherns:80"
81 assert qualify_service_name(ir, "backoffice.otherns:80", "otherns") == "backoffice.otherns:80"
82
83 assert (
84 qualify_service_name(ir, "[fe80::e022:9cff:fecc:c7c4]", None)
85 == "[fe80::e022:9cff:fecc:c7c4]"
86 )
87 assert (
88 qualify_service_name(ir, "[fe80::e022:9cff:fecc:c7c4]", "default")
89 == "[fe80::e022:9cff:fecc:c7c4]"
90 )
91 assert (
92 qualify_service_name(ir, "[fe80::e022:9cff:fecc:c7c4]", "other")
93 == "[fe80::e022:9cff:fecc:c7c4]"
94 )
95 assert (
96 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4]", None)
97 == "https://[fe80::e022:9cff:fecc:c7c4]"
98 )
99 assert (
100 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4]", "default")
101 == "https://[fe80::e022:9cff:fecc:c7c4]"
102 )
103 assert (
104 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4]", "other")
105 == "https://[fe80::e022:9cff:fecc:c7c4]"
106 )
107 assert (
108 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4]:443", None)
109 == "https://[fe80::e022:9cff:fecc:c7c4]:443"
110 )
111 assert (
112 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4]:443", "default")
113 == "https://[fe80::e022:9cff:fecc:c7c4]:443"
114 )
115 assert (
116 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4]:443", "other")
117 == "https://[fe80::e022:9cff:fecc:c7c4]:443"
118 )
119 assert (
120 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4%25zone]:443", "other")
121 == "https://[fe80::e022:9cff:fecc:c7c4%25zone]:443"
122 )
123
124 assert normalize_service_name(ir, "backoffice:80", None, "ConsulResolver") == "backoffice:80"
125 assert (
126 normalize_service_name(ir, "backoffice:80", "default", "ConsulResolver") == "backoffice:80"
127 )
128 assert (
129 normalize_service_name(ir, "backoffice:80", "otherns", "ConsulResolver") == "backoffice:80"
130 )
131 assert (
132 normalize_service_name(ir, "backoffice.otherns:80", None, "ConsulResolver")
133 == "backoffice.otherns:80"
134 )
135 assert (
136 normalize_service_name(ir, "backoffice.otherns:80", "default", "ConsulResolver")
137 == "backoffice.otherns:80"
138 )
139 assert (
140 normalize_service_name(ir, "backoffice.otherns:80", "otherns", "ConsulResolver")
141 == "backoffice.otherns:80"
142 )
143
144 assert qualify_service_name(ir, "http://backoffice", None) == "http://backoffice"
145 assert qualify_service_name(ir, "http://backoffice", "default") == "http://backoffice"
146 assert qualify_service_name(ir, "http://backoffice", "otherns") == "http://backoffice.otherns"
147 assert (
148 qualify_service_name(ir, "http://backoffice.otherns", None) == "http://backoffice.otherns"
149 )
150 assert (
151 qualify_service_name(ir, "http://backoffice.otherns", "default")
152 == "http://backoffice.otherns"
153 )
154 assert (
155 qualify_service_name(ir, "http://backoffice.otherns", "otherns")
156 == "http://backoffice.otherns"
157 )
158
159 assert (
160 normalize_service_name(ir, "http://backoffice", None, "ConsulResolver")
161 == "http://backoffice"
162 )
163 assert (
164 normalize_service_name(ir, "http://backoffice", "default", "ConsulResolver")
165 == "http://backoffice"
166 )
167 assert (
168 normalize_service_name(ir, "http://backoffice", "otherns", "ConsulResolver")
169 == "http://backoffice"
170 )
171 assert (
172 normalize_service_name(ir, "http://backoffice.otherns", None, "ConsulResolver")
173 == "http://backoffice.otherns"
174 )
175 assert (
176 normalize_service_name(ir, "http://backoffice.otherns", "default", "ConsulResolver")
177 == "http://backoffice.otherns"
178 )
179 assert (
180 normalize_service_name(ir, "http://backoffice.otherns", "otherns", "ConsulResolver")
181 == "http://backoffice.otherns"
182 )
183
184 assert qualify_service_name(ir, "http://backoffice:80", None) == "http://backoffice:80"
185 assert qualify_service_name(ir, "http://backoffice:80", "default") == "http://backoffice:80"
186 assert (
187 qualify_service_name(ir, "http://backoffice:80", "otherns")
188 == "http://backoffice.otherns:80"
189 )
190 assert (
191 qualify_service_name(ir, "http://backoffice.otherns:80", None)
192 == "http://backoffice.otherns:80"
193 )
194 assert (
195 qualify_service_name(ir, "http://backoffice.otherns:80", "default")
196 == "http://backoffice.otherns:80"
197 )
198 assert (
199 qualify_service_name(ir, "http://backoffice.otherns:80", "otherns")
200 == "http://backoffice.otherns:80"
201 )
202
203 assert (
204 normalize_service_name(ir, "http://backoffice:80", None, "ConsulResolver")
205 == "http://backoffice:80"
206 )
207 assert (
208 normalize_service_name(ir, "http://backoffice:80", "default", "ConsulResolver")
209 == "http://backoffice:80"
210 )
211 assert (
212 normalize_service_name(ir, "http://backoffice:80", "otherns", "ConsulResolver")
213 == "http://backoffice:80"
214 )
215 assert (
216 normalize_service_name(ir, "http://backoffice.otherns:80", None, "ConsulResolver")
217 == "http://backoffice.otherns:80"
218 )
219 assert (
220 normalize_service_name(ir, "http://backoffice.otherns:80", "default", "ConsulResolver")
221 == "http://backoffice.otherns:80"
222 )
223 assert (
224 normalize_service_name(ir, "http://backoffice.otherns:80", "otherns", "ConsulResolver")
225 == "http://backoffice.otherns:80"
226 )
227
228 assert qualify_service_name(ir, "https://backoffice", None) == "https://backoffice"
229 assert qualify_service_name(ir, "https://backoffice", "default") == "https://backoffice"
230 assert qualify_service_name(ir, "https://backoffice", "otherns") == "https://backoffice.otherns"
231 assert (
232 qualify_service_name(ir, "https://backoffice.otherns", None) == "https://backoffice.otherns"
233 )
234 assert (
235 qualify_service_name(ir, "https://backoffice.otherns", "default")
236 == "https://backoffice.otherns"
237 )
238 assert (
239 qualify_service_name(ir, "https://backoffice.otherns", "otherns")
240 == "https://backoffice.otherns"
241 )
242
243 assert (
244 normalize_service_name(ir, "https://backoffice", None, "ConsulResolver")
245 == "https://backoffice"
246 )
247 assert (
248 normalize_service_name(ir, "https://backoffice", "default", "ConsulResolver")
249 == "https://backoffice"
250 )
251 assert (
252 normalize_service_name(ir, "https://backoffice", "otherns", "ConsulResolver")
253 == "https://backoffice"
254 )
255 assert (
256 normalize_service_name(ir, "https://backoffice.otherns", None, "ConsulResolver")
257 == "https://backoffice.otherns"
258 )
259 assert (
260 normalize_service_name(ir, "https://backoffice.otherns", "default", "ConsulResolver")
261 == "https://backoffice.otherns"
262 )
263 assert (
264 normalize_service_name(ir, "https://backoffice.otherns", "otherns", "ConsulResolver")
265 == "https://backoffice.otherns"
266 )
267
268 assert qualify_service_name(ir, "https://backoffice:443", None) == "https://backoffice:443"
269 assert qualify_service_name(ir, "https://backoffice:443", "default") == "https://backoffice:443"
270 assert (
271 qualify_service_name(ir, "https://backoffice:443", "otherns")
272 == "https://backoffice.otherns:443"
273 )
274 assert (
275 qualify_service_name(ir, "https://backoffice.otherns:443", None)
276 == "https://backoffice.otherns:443"
277 )
278 assert (
279 qualify_service_name(ir, "https://backoffice.otherns:443", "default")
280 == "https://backoffice.otherns:443"
281 )
282 assert (
283 qualify_service_name(ir, "https://backoffice.otherns:443", "otherns")
284 == "https://backoffice.otherns:443"
285 )
286
287 assert (
288 normalize_service_name(ir, "https://backoffice:443", None, "ConsulResolver")
289 == "https://backoffice:443"
290 )
291 assert (
292 normalize_service_name(ir, "https://backoffice:443", "default", "ConsulResolver")
293 == "https://backoffice:443"
294 )
295 assert (
296 normalize_service_name(ir, "https://backoffice:443", "otherns", "ConsulResolver")
297 == "https://backoffice:443"
298 )
299 assert (
300 normalize_service_name(ir, "https://backoffice.otherns:443", None, "ConsulResolver")
301 == "https://backoffice.otherns:443"
302 )
303 assert (
304 normalize_service_name(ir, "https://backoffice.otherns:443", "default", "ConsulResolver")
305 == "https://backoffice.otherns:443"
306 )
307 assert (
308 normalize_service_name(ir, "https://backoffice.otherns:443", "otherns", "ConsulResolver")
309 == "https://backoffice.otherns:443"
310 )
311
312 assert qualify_service_name(ir, "localhost", None) == "localhost"
313 assert qualify_service_name(ir, "localhost", "default") == "localhost"
314 assert qualify_service_name(ir, "localhost", "otherns") == "localhost"
315 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
316 assert qualify_service_name(ir, "localhost.otherns", None) == "localhost.otherns"
317 assert qualify_service_name(ir, "localhost.otherns", "default") == "localhost.otherns"
318 assert qualify_service_name(ir, "localhost.otherns", "otherns") == "localhost.otherns"
319
320 assert normalize_service_name(ir, "localhost", None, "ConsulResolver") == "localhost"
321 assert normalize_service_name(ir, "localhost", "default", "ConsulResolver") == "localhost"
322 assert normalize_service_name(ir, "localhost", "otherns", "ConsulResolver") == "localhost"
323 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
324 assert (
325 normalize_service_name(ir, "localhost.otherns", None, "ConsulResolver")
326 == "localhost.otherns"
327 )
328 assert (
329 normalize_service_name(ir, "localhost.otherns", "default", "ConsulResolver")
330 == "localhost.otherns"
331 )
332 assert (
333 normalize_service_name(ir, "localhost.otherns", "otherns", "ConsulResolver")
334 == "localhost.otherns"
335 )
336
337 assert qualify_service_name(ir, "localhost:80", None) == "localhost:80"
338 assert qualify_service_name(ir, "localhost:80", "default") == "localhost:80"
339 assert qualify_service_name(ir, "localhost:80", "otherns") == "localhost:80"
340 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
341 assert qualify_service_name(ir, "localhost.otherns:80", None) == "localhost.otherns:80"
342 assert qualify_service_name(ir, "localhost.otherns:80", "default") == "localhost.otherns:80"
343 assert qualify_service_name(ir, "localhost.otherns:80", "otherns") == "localhost.otherns:80"
344
345 assert normalize_service_name(ir, "localhost:80", None, "ConsulResolver") == "localhost:80"
346 assert normalize_service_name(ir, "localhost:80", "default", "ConsulResolver") == "localhost:80"
347 assert normalize_service_name(ir, "localhost:80", "otherns", "ConsulResolver") == "localhost:80"
348 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
349 assert (
350 normalize_service_name(ir, "localhost.otherns:80", None, "ConsulResolver")
351 == "localhost.otherns:80"
352 )
353 assert (
354 normalize_service_name(ir, "localhost.otherns:80", "default", "ConsulResolver")
355 == "localhost.otherns:80"
356 )
357 assert (
358 normalize_service_name(ir, "localhost.otherns:80", "otherns", "ConsulResolver")
359 == "localhost.otherns:80"
360 )
361
362 assert qualify_service_name(ir, "http://localhost", None) == "http://localhost"
363 assert qualify_service_name(ir, "http://localhost", "default") == "http://localhost"
364 assert qualify_service_name(ir, "http://localhost", "otherns") == "http://localhost"
365 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
366 assert qualify_service_name(ir, "http://localhost.otherns", None) == "http://localhost.otherns"
367 assert (
368 qualify_service_name(ir, "http://localhost.otherns", "default")
369 == "http://localhost.otherns"
370 )
371 assert (
372 qualify_service_name(ir, "http://localhost.otherns", "otherns")
373 == "http://localhost.otherns"
374 )
375
376 assert (
377 normalize_service_name(ir, "http://localhost", None, "ConsulResolver") == "http://localhost"
378 )
379 assert (
380 normalize_service_name(ir, "http://localhost", "default", "ConsulResolver")
381 == "http://localhost"
382 )
383 assert (
384 normalize_service_name(ir, "http://localhost", "otherns", "ConsulResolver")
385 == "http://localhost"
386 )
387 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
388 assert (
389 normalize_service_name(ir, "http://localhost.otherns", None, "ConsulResolver")
390 == "http://localhost.otherns"
391 )
392 assert (
393 normalize_service_name(ir, "http://localhost.otherns", "default", "ConsulResolver")
394 == "http://localhost.otherns"
395 )
396 assert (
397 normalize_service_name(ir, "http://localhost.otherns", "otherns", "ConsulResolver")
398 == "http://localhost.otherns"
399 )
400
401 assert qualify_service_name(ir, "http://localhost:80", None) == "http://localhost:80"
402 assert qualify_service_name(ir, "http://localhost:80", "default") == "http://localhost:80"
403 assert qualify_service_name(ir, "http://localhost:80", "otherns") == "http://localhost:80"
404 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
405 assert (
406 qualify_service_name(ir, "http://localhost.otherns:80", None)
407 == "http://localhost.otherns:80"
408 )
409 assert (
410 qualify_service_name(ir, "http://localhost.otherns:80", "default")
411 == "http://localhost.otherns:80"
412 )
413 assert (
414 qualify_service_name(ir, "http://localhost.otherns:80", "otherns")
415 == "http://localhost.otherns:80"
416 )
417
418 assert (
419 normalize_service_name(ir, "http://localhost:80", None, "ConsulResolver")
420 == "http://localhost:80"
421 )
422 assert (
423 normalize_service_name(ir, "http://localhost:80", "default", "ConsulResolver")
424 == "http://localhost:80"
425 )
426 assert (
427 normalize_service_name(ir, "http://localhost:80", "otherns", "ConsulResolver")
428 == "http://localhost:80"
429 )
430 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
431 assert (
432 normalize_service_name(ir, "http://localhost.otherns:80", None, "ConsulResolver")
433 == "http://localhost.otherns:80"
434 )
435 assert (
436 normalize_service_name(ir, "http://localhost.otherns:80", "default", "ConsulResolver")
437 == "http://localhost.otherns:80"
438 )
439 assert (
440 normalize_service_name(ir, "http://localhost.otherns:80", "otherns", "ConsulResolver")
441 == "http://localhost.otherns:80"
442 )
443
444 assert qualify_service_name(ir, "https://localhost", None) == "https://localhost"
445 assert qualify_service_name(ir, "https://localhost", "default") == "https://localhost"
446 assert qualify_service_name(ir, "https://localhost", "otherns") == "https://localhost"
447 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
448 assert (
449 qualify_service_name(ir, "https://localhost.otherns", None) == "https://localhost.otherns"
450 )
451 assert (
452 qualify_service_name(ir, "https://localhost.otherns", "default")
453 == "https://localhost.otherns"
454 )
455 assert (
456 qualify_service_name(ir, "https://localhost.otherns", "otherns")
457 == "https://localhost.otherns"
458 )
459
460 assert (
461 normalize_service_name(ir, "https://localhost", None, "ConsulResolver")
462 == "https://localhost"
463 )
464 assert (
465 normalize_service_name(ir, "https://localhost", "default", "ConsulResolver")
466 == "https://localhost"
467 )
468 assert (
469 normalize_service_name(ir, "https://localhost", "otherns", "ConsulResolver")
470 == "https://localhost"
471 )
472 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
473 assert (
474 normalize_service_name(ir, "https://localhost.otherns", None, "ConsulResolver")
475 == "https://localhost.otherns"
476 )
477 assert (
478 normalize_service_name(ir, "https://localhost.otherns", "default", "ConsulResolver")
479 == "https://localhost.otherns"
480 )
481 assert (
482 normalize_service_name(ir, "https://localhost.otherns", "otherns", "ConsulResolver")
483 == "https://localhost.otherns"
484 )
485
486 assert qualify_service_name(ir, "https://localhost:443", None) == "https://localhost:443"
487 assert qualify_service_name(ir, "https://localhost:443", "default") == "https://localhost:443"
488 assert qualify_service_name(ir, "https://localhost:443", "otherns") == "https://localhost:443"
489 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
490 assert (
491 qualify_service_name(ir, "https://localhost.otherns:443", None)
492 == "https://localhost.otherns:443"
493 )
494 assert (
495 qualify_service_name(ir, "https://localhost.otherns:443", "default")
496 == "https://localhost.otherns:443"
497 )
498 assert (
499 qualify_service_name(ir, "https://localhost.otherns:443", "otherns")
500 == "https://localhost.otherns:443"
501 )
502
503 assert (
504 normalize_service_name(ir, "https://localhost:443", None, "ConsulResolver")
505 == "https://localhost:443"
506 )
507 assert (
508 normalize_service_name(ir, "https://localhost:443", "default", "ConsulResolver")
509 == "https://localhost:443"
510 )
511 assert (
512 normalize_service_name(ir, "https://localhost:443", "otherns", "ConsulResolver")
513 == "https://localhost:443"
514 )
515 # It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
516 assert (
517 normalize_service_name(ir, "https://localhost.otherns:443", None, "ConsulResolver")
518 == "https://localhost.otherns:443"
519 )
520 assert (
521 normalize_service_name(ir, "https://localhost.otherns:443", "default", "ConsulResolver")
522 == "https://localhost.otherns:443"
523 )
524 assert (
525 normalize_service_name(ir, "https://localhost.otherns:443", "otherns", "ConsulResolver")
526 == "https://localhost.otherns:443"
527 )
528
529 assert (
530 qualify_service_name(ir, "ambassador://foo.ns", "otherns") == "ambassador://foo.ns"
531 ) # let's not introduce silly semantics
532 assert (
533 qualify_service_name(ir, "//foo.ns:1234", "otherns") == "foo.ns:1234"
534 ) # we tell people "URL-ish", actually support URL-ish
535 assert qualify_service_name(ir, "foo.ns:1234", "otherns") == "foo.ns:1234"
536
537 assert (
538 normalize_service_name(ir, "ambassador://foo.ns", "otherns", "ConsulResolver")
539 == "ambassador://foo.ns"
540 ) # let's not introduce silly semantics
541 assert (
542 normalize_service_name(ir, "//foo.ns:1234", "otherns", "ConsulResolver") == "foo.ns:1234"
543 ) # we tell people "URL-ish", actually support URL-ish
544 assert normalize_service_name(ir, "foo.ns:1234", "otherns", "ConsulResolver") == "foo.ns:1234"
545
546 errors = ir.aconf.errors
547 assert not errors
548
549 assert (
550 qualify_service_name(ir, "https://bad-service:443:443", "otherns")
551 == "https://bad-service:443:443"
552 )
553 assert (
554 qualify_service_name(ir, "https://bad-service:443:443", "otherns", rkey="test-rkey")
555 == "https://bad-service:443:443"
556 )
557 assert qualify_service_name(ir, "bad-service:443:443", "otherns") == "bad-service:443:443"
558 assert (
559 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4:443", "otherns")
560 == "https://[fe80::e022:9cff:fecc:c7c4:443"
561 )
562 assert (
563 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4", "otherns")
564 == "https://[fe80::e022:9cff:fecc:c7c4"
565 )
566 assert (
567 qualify_service_name(ir, "https://fe80::e022:9cff:fecc:c7c4", "otherns")
568 == "https://fe80::e022:9cff:fecc:c7c4"
569 )
570 assert qualify_service_name(ir, "https://bad-service:-1", "otherns") == "https://bad-service:-1"
571 assert (
572 qualify_service_name(ir, "https://bad-service:70000", "otherns")
573 == "https://bad-service:70000"
574 )
575
576 assert (
577 normalize_service_name(ir, "https://bad-service:443:443", "otherns", "ConsulResolver")
578 == "https://bad-service:443:443"
579 )
580 assert (
581 normalize_service_name(
582 ir, "https://bad-service:443:443", "otherns", "ConsulResolver", rkey="test-rkey"
583 )
584 == "https://bad-service:443:443"
585 )
586 assert (
587 normalize_service_name(ir, "bad-service:443:443", "otherns", "ConsulResolver")
588 == "bad-service:443:443"
589 )
590 assert (
591 normalize_service_name(
592 ir, "https://[fe80::e022:9cff:fecc:c7c4:443", "otherns", "ConsulResolver"
593 )
594 == "https://[fe80::e022:9cff:fecc:c7c4:443"
595 )
596 assert (
597 normalize_service_name(
598 ir, "https://[fe80::e022:9cff:fecc:c7c4", "otherns", "ConsulResolver"
599 )
600 == "https://[fe80::e022:9cff:fecc:c7c4"
601 )
602 assert (
603 normalize_service_name(ir, "https://fe80::e022:9cff:fecc:c7c4", "otherns", "ConsulResolver")
604 == "https://fe80::e022:9cff:fecc:c7c4"
605 )
606 assert (
607 normalize_service_name(ir, "https://bad-service:-1", "otherns", "ConsulResolver")
608 == "https://bad-service:-1"
609 )
610 assert (
611 normalize_service_name(ir, "https://bad-service:70000", "otherns", "ConsulResolver")
612 == "https://bad-service:70000"
613 )
614 assert (
615 qualify_service_name(ir, "https://[fe80::e022:9cff:fecc:c7c4%zone]:443", "other")
616 == "https://[fe80::e022:9cff:fecc:c7c4%zone]:443"
617 )
618
619 errors = ir.aconf.errors
620 assert "-global-" in errors
621 errors = errors["-global-"]
622
623 assert len(errors) == 17
624
625 # Ugg, different versions of Python have different error messages. Let's recognize the "Port could not be cast to
626 # integer value as" to keep pytest working on peoples up-to-date laptops with Python 3.8, and let's recognize
627 # "invalid literal for int() with base 10:" for the Python 3.7 in the builder container.
628 assert not errors[0]["ok"]
629 assert (
630 errors[0]["error"]
631 == "Malformed service 'https://bad-service:443:443': Port could not be cast to integer value as '443:443'"
632 or errors[0]["error"]
633 == "Malformed service 'https://bad-service:443:443': invalid literal for int() with base 10: '443:443'"
634 )
635
636 assert not errors[1]["ok"]
637 assert (
638 errors[1]["error"]
639 == "test-rkey: Malformed service 'https://bad-service:443:443': Port could not be cast to integer value as '443:443'"
640 or errors[1]["error"]
641 == "test-rkey: Malformed service 'https://bad-service:443:443': invalid literal for int() with base 10: '443:443'"
642 )
643
644 assert not errors[2]["ok"]
645 assert (
646 errors[2]["error"]
647 == "Malformed service 'bad-service:443:443': Port could not be cast to integer value as '443:443'"
648 or errors[2]["error"]
649 == "Malformed service 'bad-service:443:443': invalid literal for int() with base 10: '443:443'"
650 )
651
652 assert not errors[3]["ok"]
653 assert (
654 errors[3]["error"]
655 == "Malformed service 'https://[fe80::e022:9cff:fecc:c7c4:443': Invalid IPv6 URL"
656 )
657
658 assert not errors[4]["ok"]
659 assert (
660 errors[4]["error"]
661 == "Malformed service 'https://[fe80::e022:9cff:fecc:c7c4': Invalid IPv6 URL"
662 )
663
664 assert not errors[5]["ok"]
665 assert (
666 errors[5]["error"]
667 == "Malformed service 'https://fe80::e022:9cff:fecc:c7c4': Port could not be cast to integer value as ':e022:9cff:fecc:c7c4'"
668 or errors[5]["error"]
669 == "Malformed service 'https://fe80::e022:9cff:fecc:c7c4': invalid literal for int() with base 10: ':e022:9cff:fecc:c7c4'"
670 )
671
672 assert not errors[6]["ok"]
673 assert (
674 errors[6]["error"]
675 == "Malformed service 'https://bad-service:-1': Port out of range 0-65535"
676 )
677
678 assert not errors[7]["ok"]
679 assert (
680 errors[7]["error"]
681 == "Malformed service 'https://bad-service:70000': Port out of range 0-65535"
682 )
683
684 assert not errors[8]["ok"]
685 assert (
686 errors[8]["error"]
687 == "Malformed service 'https://bad-service:443:443': Port could not be cast to integer value as '443:443'"
688 or errors[8]["error"]
689 == "Malformed service 'https://bad-service:443:443': invalid literal for int() with base 10: '443:443'"
690 )
691
692 assert not errors[9]["ok"]
693 assert (
694 errors[9]["error"]
695 == "test-rkey: Malformed service 'https://bad-service:443:443': Port could not be cast to integer value as '443:443'"
696 or errors[9]["error"]
697 == "test-rkey: Malformed service 'https://bad-service:443:443': invalid literal for int() with base 10: '443:443'"
698 )
699
700 assert not errors[10]["ok"]
701 assert (
702 errors[10]["error"]
703 == "Malformed service 'bad-service:443:443': Port could not be cast to integer value as '443:443'"
704 or errors[10]["error"]
705 == "Malformed service 'bad-service:443:443': invalid literal for int() with base 10: '443:443'"
706 )
707
708 assert not errors[11]["ok"]
709 assert (
710 errors[11]["error"]
711 == "Malformed service 'https://[fe80::e022:9cff:fecc:c7c4:443': Invalid IPv6 URL"
712 )
713
714 assert not errors[12]["ok"]
715 assert (
716 errors[12]["error"]
717 == "Malformed service 'https://[fe80::e022:9cff:fecc:c7c4': Invalid IPv6 URL"
718 )
719
720 assert not errors[13]["ok"]
721 assert (
722 errors[13]["error"]
723 == "Malformed service 'https://fe80::e022:9cff:fecc:c7c4': Port could not be cast to integer value as ':e022:9cff:fecc:c7c4'"
724 or errors[13]["error"]
725 == "Malformed service 'https://fe80::e022:9cff:fecc:c7c4': invalid literal for int() with base 10: ':e022:9cff:fecc:c7c4'"
726 )
727
728 assert not errors[14]["ok"]
729 assert (
730 errors[14]["error"]
731 == "Malformed service 'https://bad-service:-1': Port out of range 0-65535"
732 )
733
734 assert not errors[15]["ok"]
735 assert (
736 errors[15]["error"]
737 == "Malformed service 'https://bad-service:70000': Port out of range 0-65535"
738 )
739
740 assert not errors[16]["ok"]
741 assert (
742 errors[16]["error"]
743 == "Malformed service 'https://[fe80::e022:9cff:fecc:c7c4%zone]:443': Invalid percent-escape in hostname: %zo"
744 )
745
746
747if __name__ == "__main__":
748 pytest.main(sys.argv)
View as plain text