Debian Codegraph @

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

raw
1 # Copyright (c) Ansible Project
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 typing as t
8
9 import pytest
10
11 from ansible_collections.community.docker.plugins.module_utils._util import (
12 compare_dict_allow_more_present,
13 compare_generic,
14 convert_duration_to_nanosecond,
15 parse_healthcheck,
16 )
17
18 if t.TYPE_CHECKING:
19
20 class DAMSpec(t.TypedDict):
21 av: dict[str, t.Any]
22 bv: dict[str, t.Any]
23 result: bool
24
25 class Spec(t.TypedDict):
26 a: t.Any
27 b: t.Any
28 method: t.Literal["strict", "ignore", "allow_more_present"]
29 type: t.Literal["value", "list", "set", "set(dict)", "dict"]
30 result: bool
31
32
33 DICT_ALLOW_MORE_PRESENT: list[DAMSpec] = [
34 {"av": {}, "bv": {"a": 1}, "result": True},
35 {"av": {"a": 1}, "bv": {"a": 1, "b": 2}, "result": True},
36 {"av": {"a": 1}, "bv": {"b": 2}, "result": False},
37 {"av": {"a": 1}, "bv": {"a": None, "b": 1}, "result": False},
38 {"av": {"a": None}, "bv": {"b": 1}, "result": False},
39 ]
40
41 DICT_ALLOW_MORE_PRESENT_SPECS: list[Spec] = [
42 {
43 "a": entry["av"],
44 "b": entry["bv"],
45 "method": "allow_more_present",
46 "type": "dict",
47 "result": entry["result"],
48 }
49 for entry in DICT_ALLOW_MORE_PRESENT
50 ]
51
52 COMPARE_GENERIC: list[Spec] = [
53 ########################################################################################
54 # value
55 {"a": 1, "b": 2, "method": "strict", "type": "value", "result": False},
56 {"a": "hello", "b": "hello", "method": "strict", "type": "value", "result": True},
57 {"a": None, "b": "hello", "method": "strict", "type": "value", "result": False},
58 {"a": None, "b": None, "method": "strict", "type": "value", "result": True},
59 {"a": 1, "b": 2, "method": "ignore", "type": "value", "result": True},
60 {"a": None, "b": 2, "method": "ignore", "type": "value", "result": True},
61 ########################################################################################
62 # list
63 {
64 "a": [
65 "x",
66 ],
67 "b": [
68 "y",
69 ],
70 "method": "strict",
71 "type": "list",
72 "result": False,
73 },
74 {
75 "a": [
76 "x",
77 ],
78 "b": [
79 "x",
80 "x",
81 ],
82 "method": "strict",
83 "type": "list",
84 "result": False,
85 },
86 {
87 "a": [
88 "x",
89 "y",
90 ],
91 "b": [
92 "x",
93 "y",
94 ],
95 "method": "strict",
96 "type": "list",
97 "result": True,
98 },
99 {
100 "a": [
101 "x",
102 "y",
103 ],
104 "b": [
105 "y",
106 "x",
107 ],
108 "method": "strict",
109 "type": "list",
110 "result": False,
111 },
112 {
113 "a": [
114 "x",
115 "y",
116 ],
117 "b": [
118 "x",
119 ],
120 "method": "allow_more_present",
121 "type": "list",
122 "result": False,
123 },
124 {
125 "a": [
126 "x",
127 ],
128 "b": [
129 "x",
130 "y",
131 ],
132 "method": "allow_more_present",
133 "type": "list",
134 "result": True,
135 },
136 {
137 "a": [
138 "x",
139 "x",
140 "y",
141 ],
142 "b": [
143 "x",
144 "y",
145 ],
146 "method": "allow_more_present",
147 "type": "list",
148 "result": False,
149 },
150 {
151 "a": [
152 "x",
153 "z",
154 ],
155 "b": [
156 "x",
157 "y",
158 "x",
159 "z",
160 ],
161 "method": "allow_more_present",
162 "type": "list",
163 "result": True,
164 },
165 {
166 "a": [
167 "x",
168 "y",
169 ],
170 "b": [
171 "y",
172 "x",
173 ],
174 "method": "ignore",
175 "type": "list",
176 "result": True,
177 },
178 ########################################################################################
179 # set
180 {
181 "a": [
182 "x",
183 ],
184 "b": [
185 "y",
186 ],
187 "method": "strict",
188 "type": "set",
189 "result": False,
190 },
191 {
192 "a": [
193 "x",
194 ],
195 "b": [
196 "x",
197 "x",
198 ],
199 "method": "strict",
200 "type": "set",
201 "result": True,
202 },
203 {
204 "a": [
205 "x",
206 "y",
207 ],
208 "b": [
209 "x",
210 "y",
211 ],
212 "method": "strict",
213 "type": "set",
214 "result": True,
215 },
216 {
217 "a": [
218 "x",
219 "y",
220 ],
221 "b": [
222 "y",
223 "x",
224 ],
225 "method": "strict",
226 "type": "set",
227 "result": True,
228 },
229 {
230 "a": [
231 "x",
232 "y",
233 ],
234 "b": [
235 "x",
236 ],
237 "method": "allow_more_present",
238 "type": "set",
239 "result": False,
240 },
241 {
242 "a": [
243 "x",
244 ],
245 "b": [
246 "x",
247 "y",
248 ],
249 "method": "allow_more_present",
250 "type": "set",
251 "result": True,
252 },
253 {
254 "a": [
255 "x",
256 "x",
257 "y",
258 ],
259 "b": [
260 "x",
261 "y",
262 ],
263 "method": "allow_more_present",
264 "type": "set",
265 "result": True,
266 },
267 {
268 "a": [
269 "x",
270 "z",
271 ],
272 "b": [
273 "x",
274 "y",
275 "x",
276 "z",
277 ],
278 "method": "allow_more_present",
279 "type": "set",
280 "result": True,
281 },
282 {
283 "a": [
284 "x",
285 "a",
286 ],
287 "b": [
288 "y",
289 "z",
290 ],
291 "method": "ignore",
292 "type": "set",
293 "result": True,
294 },
295 ########################################################################################
296 # set(dict)
297 {
298 "a": [
299 {"x": 1},
300 ],
301 "b": [
302 {"y": 1},
303 ],
304 "method": "strict",
305 "type": "set(dict)",
306 "result": False,
307 },
308 {
309 "a": [
310 {"x": 1},
311 ],
312 "b": [
313 {"x": 1},
314 ],
315 "method": "strict",
316 "type": "set(dict)",
317 "result": True,
318 },
319 {
320 "a": [
321 {"x": 1},
322 ],
323 "b": [
324 {"x": 1, "y": 2},
325 ],
326 "method": "strict",
327 "type": "set(dict)",
328 "result": True,
329 },
330 {
331 "a": [
332 {"x": 1},
333 {"x": 2, "y": 3},
334 ],
335 "b": [
336 {"x": 1},
337 {"x": 2, "y": 3},
338 ],
339 "method": "strict",
340 "type": "set(dict)",
341 "result": True,
342 },
343 {
344 "a": [
345 {"x": 1},
346 ],
347 "b": [
348 {"x": 1, "z": 2},
349 {"x": 2, "y": 3},
350 ],
351 "method": "allow_more_present",
352 "type": "set(dict)",
353 "result": True,
354 },
355 {
356 "a": [
357 {"x": 1, "y": 2},
358 ],
359 "b": [
360 {"x": 1},
361 {"x": 2, "y": 3},
362 ],
363 "method": "allow_more_present",
364 "type": "set(dict)",
365 "result": False,
366 },
367 {
368 "a": [
369 {"x": 1, "y": 3},
370 ],
371 "b": [
372 {"x": 1},
373 {"x": 1, "y": 3, "z": 4},
374 ],
375 "method": "allow_more_present",
376 "type": "set(dict)",
377 "result": True,
378 },
379 {
380 "a": [
381 {"x": 1},
382 {"x": 2, "y": 3},
383 ],
384 "b": [
385 {"x": 1},
386 ],
387 "method": "ignore",
388 "type": "set(dict)",
389 "result": True,
390 },
391 ########################################################################################
392 # dict
393 {"a": {"x": 1}, "b": {"y": 1}, "method": "strict", "type": "dict", "result": False},
394 {
395 "a": {"x": 1},
396 "b": {"x": 1, "y": 2},
397 "method": "strict",
398 "type": "dict",
399 "result": False,
400 },
401 {"a": {"x": 1}, "b": {"x": 1}, "method": "strict", "type": "dict", "result": True},
402 {
403 "a": {"x": 1, "z": 2},
404 "b": {"x": 1, "y": 2},
405 "method": "strict",
406 "type": "dict",
407 "result": False,
408 },
409 {
410 "a": {"x": 1, "z": 2},
411 "b": {"x": 1, "y": 2},
412 "method": "ignore",
413 "type": "dict",
414 "result": True,
415 },
416 ]
417
418
419 @pytest.mark.parametrize("entry", DICT_ALLOW_MORE_PRESENT)
420 def test_dict_allow_more_present(entry: DAMSpec) -> None:
421 assert compare_dict_allow_more_present(entry["av"], entry["bv"]) == entry["result"]
422
423
424 @pytest.mark.parametrize("entry", COMPARE_GENERIC + DICT_ALLOW_MORE_PRESENT_SPECS)
425 def test_compare_generic(entry: Spec) -> None:
426 assert (
427 compare_generic(entry["a"], entry["b"], entry["method"], entry["type"])
428 == entry["result"]
429 )
430
431
432 def test_convert_duration_to_nanosecond() -> None:
433 nanoseconds = convert_duration_to_nanosecond("5s")
434 assert nanoseconds == 5000000000
435 nanoseconds = convert_duration_to_nanosecond("1m5s")
436 assert nanoseconds == 65000000000
437 with pytest.raises(ValueError):
438 convert_duration_to_nanosecond([1, 2, 3]) # type: ignore
439 with pytest.raises(ValueError):
440 convert_duration_to_nanosecond("10x")
441
442
443 def test_parse_healthcheck() -> None:
444 result, disabled = parse_healthcheck(
445 {
446 "test": "sleep 1",
447 "interval": "1s",
448 }
449 )
450 assert disabled is False
451 assert result == {"test": ["CMD-SHELL", "sleep 1"], "interval": 1000000000}
452
453 result, disabled = parse_healthcheck(
454 {
455 "test": ["NONE"],
456 }
457 )
458 assert result is None
459 assert disabled
460
461 result, disabled = parse_healthcheck({"test": "sleep 1", "interval": "1s423ms"})
462 assert result == {"test": ["CMD-SHELL", "sleep 1"], "interval": 1423000000}
463 assert disabled is False
464
465 result, disabled = parse_healthcheck(
466 {"test": "sleep 1", "interval": "1h1m2s3ms4us"}
467 )
468 assert result == {"test": ["CMD-SHELL", "sleep 1"], "interval": 3662003004000}
469 assert disabled is False