Debian Codegraph @

ansible_collections/community/docker/tests/unit/plugins/module_utils/test__compose_v2.py

raw
1 # Copyright 2022 Red Hat | Ansible
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import pytest
8
9 from ansible_collections.community.docker.plugins.module_utils._compose_v2 import (
10 Event,
11 parse_events,
12 parse_json_events,
13 )
14
15 from .compose_v2_test_cases import EVENT_TEST_CASES
16
17 EXTRA_TEST_CASES: list[tuple[str, str, bool, bool, str, list[Event], list[str]]] = [
18 (
19 "2.24.2-manual-build-dry-run",
20 "2.24.2",
21 True,
22 False,
23 " DRY-RUN MODE - build service foobar \n"
24 " DRY-RUN MODE - ==> ==> writing image dryRun-8843d7f92416211de9ebb963ff4ce28125932878 \n"
25 " DRY-RUN MODE - ==> ==> naming to my-python \n"
26 " DRY-RUN MODE - Network compose_default Creating\n"
27 " DRY-RUN MODE - Network compose_default Created\n"
28 " DRY-RUN MODE - Container compose-foobar-1 Creating\n"
29 " DRY-RUN MODE - Container compose-foobar-1 Created\n"
30 " DRY-RUN MODE - Container ompose-foobar-1 Starting\n"
31 " DRY-RUN MODE - Container ompose-foobar-1 Started\n",
32 [
33 Event(
34 "service",
35 "foobar",
36 "Building",
37 None,
38 ),
39 Event(
40 "network",
41 "compose_default",
42 "Creating",
43 None,
44 ),
45 Event(
46 "network",
47 "compose_default",
48 "Created",
49 None,
50 ),
51 Event(
52 "container",
53 "compose-foobar-1",
54 "Creating",
55 None,
56 ),
57 Event(
58 "container",
59 "compose-foobar-1",
60 "Created",
61 None,
62 ),
63 Event(
64 "container",
65 "ompose-foobar-1",
66 "Starting",
67 None,
68 ),
69 Event(
70 "container",
71 "ompose-foobar-1",
72 "Started",
73 None,
74 ),
75 ],
76 [],
77 ),
78 (
79 # https://github.com/ansible-collections/community.docker/issues/785
80 "2.20.0-manual-pull",
81 "2.20.0",
82 False,
83 False,
84 "4f4fb700ef54 Waiting\n"
85 "238022553356 Downloading 541B/541B\n"
86 "972e292d3a60 Downloading 106kB/10.43MB\n"
87 "f2543dc9f0a9 Downloading 25.36kB/2.425MB\n"
88 "972e292d3a60 Downloading 5.925MB/10.43MB\n"
89 "f2543dc9f0a9 Downloading 2.219MB/2.425MB\n"
90 "f2543dc9f0a9 Extracting 32.77kB/2.425MB\n"
91 "4f4fb700ef54 Downloading 32B/32B\n"
92 "f2543dc9f0a9 Extracting 2.425MB/2.425MB\n"
93 "972e292d3a60 Extracting 131.1kB/10.43MB\n"
94 "972e292d3a60 Extracting 10.43MB/10.43MB\n"
95 "238022553356 Extracting 541B/541B\n"
96 "4f4fb700ef54 Extracting 32B/32B\n",
97 [
98 Event(
99 "image-layer",
100 "4f4fb700ef54",
101 "Waiting",
102 None,
103 ),
104 Event(
105 "image-layer",
106 "238022553356",
107 "Downloading",
108 None,
109 ),
110 Event(
111 "image-layer",
112 "972e292d3a60",
113 "Downloading",
114 None,
115 ),
116 Event(
117 "image-layer",
118 "f2543dc9f0a9",
119 "Downloading",
120 None,
121 ),
122 Event(
123 "image-layer",
124 "972e292d3a60",
125 "Downloading",
126 None,
127 ),
128 Event(
129 "image-layer",
130 "f2543dc9f0a9",
131 "Downloading",
132 None,
133 ),
134 Event(
135 "image-layer",
136 "f2543dc9f0a9",
137 "Extracting",
138 None,
139 ),
140 Event(
141 "image-layer",
142 "4f4fb700ef54",
143 "Downloading",
144 None,
145 ),
146 Event(
147 "image-layer",
148 "f2543dc9f0a9",
149 "Extracting",
150 None,
151 ),
152 Event(
153 "image-layer",
154 "972e292d3a60",
155 "Extracting",
156 None,
157 ),
158 Event(
159 "image-layer",
160 "972e292d3a60",
161 "Extracting",
162 None,
163 ),
164 Event(
165 "image-layer",
166 "238022553356",
167 "Extracting",
168 None,
169 ),
170 Event(
171 "image-layer",
172 "4f4fb700ef54",
173 "Extracting",
174 None,
175 ),
176 ],
177 [],
178 ),
179 (
180 # https://github.com/ansible-collections/community.docker/issues/787
181 "2.20.3-logrus-warn",
182 "2.20.3",
183 False,
184 False,
185 'time="2024-02-02T08:14:10+01:00" level=warning msg="a network with name influxNetwork exists but was not'
186 ' created for project \\"influxdb\\".\\nSet `external: true` to use an existing network"\n',
187 [],
188 [
189 'a network with name influxNetwork exists but was not created for project "influxdb".\nSet `external: true` to use an existing network',
190 ],
191 ),
192 (
193 # https://github.com/ansible-collections/community.docker/issues/807
194 "2.20.3-image-warning-error",
195 "2.20.3",
196 False,
197 True,
198 " dummy3 Warning \n"
199 " dummy2 Warning \n"
200 " dummy Error \n"
201 " dummy4 Warning Foo bar \n"
202 " dummy5 Error Bar baz bam \n",
203 [
204 Event(
205 "unknown",
206 "dummy",
207 "Error",
208 None,
209 ),
210 Event(
211 "unknown",
212 "dummy5",
213 "Error",
214 "Bar baz bam",
215 ),
216 ],
217 [
218 "Unspecified warning for dummy3",
219 "Unspecified warning for dummy2",
220 "dummy4: Foo bar",
221 ],
222 ),
223 (
224 # https://github.com/ansible-collections/community.docker/issues/911
225 "2.28.1-image-pull-skipped",
226 "2.28.1",
227 False,
228 False,
229 # fmt: off
230 " bash_1 Skipped \n bash_2 Pulling \n bash_2 Pulled \n",
231 # fmt: on
232 [
233 Event(
234 "unknown",
235 "bash_1",
236 "Skipped",
237 None,
238 ),
239 Event(
240 "service",
241 "bash_2",
242 "Pulling",
243 None,
244 ),
245 Event(
246 "service",
247 "bash_2",
248 "Pulled",
249 None,
250 ),
251 ],
252 [],
253 ),
254 (
255 # https://github.com/ansible-collections/community.docker/issues/948
256 "2.28.1-unknown", # TODO: find out actual version!
257 "2.28.1", # TODO: find out actual version!
258 False,
259 True,
260 " prometheus Pulling \n"
261 " prometheus Pulled \n"
262 'network internet-monitoring-front-tier was found but has incorrect label com.docker.compose.network set to "internet-monitoring-front-tier"\n',
263 [
264 Event(
265 "service",
266 "prometheus",
267 "Pulling",
268 None,
269 ),
270 Event(
271 "service",
272 "prometheus",
273 "Pulled",
274 None,
275 ),
276 Event(
277 "unknown",
278 "",
279 "Error",
280 'network internet-monitoring-front-tier was found but has incorrect label com.docker.compose.network set to "internet-monitoring-front-tier"',
281 ),
282 ],
283 [],
284 ),
285 (
286 # https://github.com/ansible-collections/community.docker/issues/978
287 "2.28.1-unknown", # TODO: find out actual version!
288 "2.28.1", # TODO: find out actual version!
289 False,
290 True,
291 " Network create_users_db_default Creating\n"
292 " Network create_users_db_default Created\n"
293 " Container create_users_db-init Creating\n"
294 " Container create_users_db-init Created\n"
295 " Container create_users_db-init Starting\n"
296 " Container create_users_db-init Started\n"
297 " Container create_users_db-init Waiting\n"
298 "container create_users_db-init exited (0)\n",
299 [
300 Event(
301 "network",
302 "create_users_db_default",
303 "Creating",
304 None,
305 ),
306 Event(
307 "network",
308 "create_users_db_default",
309 "Created",
310 None,
311 ),
312 Event(
313 "container",
314 "create_users_db-init",
315 "Creating",
316 None,
317 ),
318 Event(
319 "container",
320 "create_users_db-init",
321 "Created",
322 None,
323 ),
324 Event(
325 "container",
326 "create_users_db-init",
327 "Starting",
328 None,
329 ),
330 Event(
331 "container",
332 "create_users_db-init",
333 "Started",
334 None,
335 ),
336 Event(
337 "container",
338 "create_users_db-init",
339 "Waiting",
340 None,
341 ),
342 Event(
343 "unknown",
344 "",
345 "Error",
346 "container create_users_db-init exited (0)",
347 ),
348 ],
349 [],
350 ),
351 ]
352
353 _ALL_TEST_CASES = EVENT_TEST_CASES + EXTRA_TEST_CASES
354
355
356 @pytest.mark.parametrize(
357 "test_id, compose_version, dry_run, nonzero_rc, stderr, events, warnings",
358 _ALL_TEST_CASES,
359 ids=[tc[0] for tc in _ALL_TEST_CASES],
360 )
361 def test_parse_events(
362 test_id: str,
363 compose_version: str,
364 dry_run: bool,
365 nonzero_rc: bool,
366 stderr: str,
367 events: list[Event],
368 warnings: list[str],
369 ) -> None:
370 collected_warnings = []
371
372 def collect_warning(msg: str) -> None:
373 collected_warnings.append(msg)
374
375 collected_events = parse_events(
376 stderr.encode("utf-8"),
377 dry_run=dry_run,
378 warn_function=collect_warning,
379 nonzero_rc=nonzero_rc,
380 )
381
382 print(collected_events)
383 print(collected_warnings)
384
385 assert collected_events == events
386 assert collected_warnings == warnings
387
388
389 JSON_TEST_CASES: list[tuple[str, str, str, list[Event], list[str]]] = [
390 (
391 "pull-compose-2",
392 "2.40.3",
393 '{"level":"warning","msg":"/tmp/ansible.f9pcm_i3.test/ansible-docker-test-3c46cd06-pull/docker-compose.yml: the attribute `version`'
394 ' is obsolete, it will be ignored, please remove it to avoid potential confusion","time":"2025-12-06T13:16:30Z"}\n'
395 '{"id":"ansible-docker-test-3c46cd06-cont","text":"Pulling"}\n'
396 '{"id":"63a26ae4e8a8","parent_id":"ansible-docker-test-3c46cd06-cont","text":"Pulling fs layer"}\n'
397 '{"id":"63a26ae4e8a8","parent_id":"ansible-docker-test-3c46cd06-cont","text":"Downloading","status":"[\\u003e '
398 ' ] 6.89kB/599.9kB","current":6890,"total":599883,"percent":1}\n'
399 '{"id":"63a26ae4e8a8","parent_id":"ansible-docker-test-3c46cd06-cont","text":"Download complete","percent":100}\n'
400 '{"id":"63a26ae4e8a8","parent_id":"ansible-docker-test-3c46cd06-cont","text":"Extracting","status":"[==\\u003e '
401 ' ] 32.77kB/599.9kB","current":32768,"total":599883,"percent":5}\n'
402 '{"id":"63a26ae4e8a8","parent_id":"ansible-docker-test-3c46cd06-cont","text":"Extracting","status":"[============'
403 '======================================\\u003e] 599.9kB/599.9kB","current":599883,"total":599883,"percent":100}\n'
404 '{"id":"63a26ae4e8a8","parent_id":"ansible-docker-test-3c46cd06-cont","text":"Extracting","status":"[============'
405 '======================================\\u003e] 599.9kB/599.9kB","current":599883,"total":599883,"percent":100}\n'
406 '{"id":"63a26ae4e8a8","parent_id":"ansible-docker-test-3c46cd06-cont","text":"Pull complete","percent":100}\n'
407 '{"id":"ansible-docker-test-3c46cd06-cont","text":"Pulled"}\n',
408 [
409 Event(
410 "unknown",
411 None,
412 "Warning",
413 "/tmp/ansible.f9pcm_i3.test/ansible-docker-test-3c46cd06-pull/docker-compose.yml: the attribute `version` is obsolete,"
414 " it will be ignored, please remove it to avoid potential confusion",
415 ),
416 Event(
417 "image",
418 "ansible-docker-test-3c46cd06-cont",
419 "Pulling",
420 None,
421 ),
422 Event(
423 "image-layer",
424 "63a26ae4e8a8",
425 "Pulling fs layer",
426 None,
427 ),
428 Event(
429 "image-layer",
430 "63a26ae4e8a8",
431 "Downloading",
432 "[> ] 6.89kB/599.9kB",
433 ),
434 Event(
435 "image-layer",
436 "63a26ae4e8a8",
437 "Download complete",
438 None,
439 ),
440 Event(
441 "image-layer",
442 "63a26ae4e8a8",
443 "Extracting",
444 "[==> ] 32.77kB/599.9kB",
445 ),
446 Event(
447 "image-layer",
448 "63a26ae4e8a8",
449 "Extracting",
450 "[==================================================>] 599.9kB/599.9kB",
451 ),
452 Event(
453 "image-layer",
454 "63a26ae4e8a8",
455 "Extracting",
456 "[==================================================>] 599.9kB/599.9kB",
457 ),
458 Event(
459 "image-layer",
460 "63a26ae4e8a8",
461 "Pull complete",
462 None,
463 ),
464 Event(
465 "image",
466 "ansible-docker-test-3c46cd06-cont",
467 "Pulled",
468 None,
469 ),
470 ],
471 [],
472 ),
473 (
474 "pull-compose-5",
475 "5.0.0",
476 '{"level":"warning","msg":"/tmp/ansible.1n0q46aj.test/ansible-docker-test-b2fa9191-pull/docker-compose.yml: the attribute'
477 ' `version` is obsolete, it will be ignored, please remove it to avoid potential confusion","time":"2025-12-06T13:08:22Z"}\n'
478 '{"id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Working","text":"Pulling"}\n'
479 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Working"}\n'
480 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Working","text":"[\\u003e '
481 ' ] 6.89kB/599.9kB","current":6890,"total":599883,"percent":1}\n'
482 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Working","text":"[=============='
483 '====================================\\u003e] 599.9kB/599.9kB","current":599883,"total":599883,"percent":100}\n'
484 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Working"}\n'
485 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Done","percent":100}\n'
486 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Working","text":"[==\\u003e '
487 ' ] 32.77kB/599.9kB","current":32768,"total":599883,"percent":5}\n'
488 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Working","text":"[=============='
489 '====================================\\u003e] 599.9kB/599.9kB","current":599883,"total":599883,"percent":100}\n'
490 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Working","text":"[=============='
491 '====================================\\u003e] 599.9kB/599.9kB","current":599883,"total":599883,"percent":100}\n'
492 '{"id":"63a26ae4e8a8","parent_id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Done","percent":100}\n'
493 '{"id":"Image ghcr.io/ansible-collections/simple-1:tag","status":"Done","text":"Pulled"}\n',
494 [
495 Event(
496 "unknown",
497 None,
498 "Warning",
499 "/tmp/ansible.1n0q46aj.test/ansible-docker-test-b2fa9191-pull/docker-compose.yml: the attribute `version`"
500 " is obsolete, it will be ignored, please remove it to avoid potential confusion",
501 ),
502 Event(
503 "image",
504 "ghcr.io/ansible-collections/simple-1:tag",
505 "Pulling",
506 "Working",
507 ),
508 Event(
509 "image-layer",
510 "ghcr.io/ansible-collections/simple-1:tag",
511 "Working",
512 None,
513 ),
514 Event(
515 "image-layer",
516 "ghcr.io/ansible-collections/simple-1:tag",
517 "Working",
518 "[> ] 6.89kB/599.9kB",
519 ),
520 Event(
521 "image-layer",
522 "ghcr.io/ansible-collections/simple-1:tag",
523 "Working",
524 "[==================================================>] 599.9kB/599.9kB",
525 ),
526 Event(
527 "image-layer",
528 "ghcr.io/ansible-collections/simple-1:tag",
529 "Working",
530 None,
531 ),
532 Event(
533 "image-layer", "ghcr.io/ansible-collections/simple-1:tag", "Done", None
534 ),
535 Event(
536 "image-layer",
537 "ghcr.io/ansible-collections/simple-1:tag",
538 "Working",
539 "[==> ] 32.77kB/599.9kB",
540 ),
541 Event(
542 "image-layer",
543 "ghcr.io/ansible-collections/simple-1:tag",
544 "Working",
545 "[==================================================>] 599.9kB/599.9kB",
546 ),
547 Event(
548 "image-layer",
549 "ghcr.io/ansible-collections/simple-1:tag",
550 "Working",
551 "[==================================================>] 599.9kB/599.9kB",
552 ),
553 Event(
554 "image-layer", "ghcr.io/ansible-collections/simple-1:tag", "Done", None
555 ),
556 Event(
557 "image", "ghcr.io/ansible-collections/simple-1:tag", "Pulled", "Done"
558 ),
559 ],
560 [],
561 ),
562 ]
563
564
565 @pytest.mark.parametrize(
566 "test_id, compose_version, stderr, events, warnings",
567 JSON_TEST_CASES,
568 ids=[tc[0] for tc in JSON_TEST_CASES],
569 )
570 def test_parse_json_events(
571 test_id: str,
572 compose_version: str,
573 stderr: str,
574 events: list[Event],
575 warnings: list[str],
576 ) -> None:
577 collected_warnings = []
578
579 def collect_warning(msg: str) -> None:
580 collected_warnings.append(msg)
581
582 collected_events = parse_json_events(
583 stderr.encode("utf-8"),
584 warn_function=collect_warning,
585 )
586
587 print(collected_events)
588 print(collected_warnings)
589
590 assert collected_events == events
591 assert collected_warnings == warnings