Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/module_utils/test__opennebula.py

raw
1 # Copyright (c) 2023, Michal Opala <[email protected]>
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 textwrap
8
9 import pytest
10
11 from ansible_collections.community.general.plugins.module_utils._opennebula import flatten, render
12
13 FLATTEN_VALID = [
14 ([[[1]], [2], 3], False, [1, 2, 3]),
15 ([[[1]], [2], 3], True, [1, 2, 3]),
16 ([[1]], False, [1]),
17 ([[1]], True, 1),
18 (1, False, [1]),
19 (1, True, 1),
20 ]
21
22 RENDER_VALID = [
23 (
24 {
25 "NIC": {"NAME": "NIC0", "NETWORK_ID": 0},
26 "CPU": 1,
27 "MEMORY": 1024,
28 },
29 textwrap.dedent("""
30 CPU="1"
31 MEMORY="1024"
32 NIC=[NAME="NIC0",NETWORK_ID="0"]
33 """).strip(),
34 ),
35 (
36 {
37 "NIC": [
38 {"NAME": "NIC0", "NETWORK_ID": 0},
39 {"NAME": "NIC1", "NETWORK_ID": 1},
40 ],
41 "CPU": 1,
42 "MEMORY": 1024,
43 },
44 textwrap.dedent("""
45 CPU="1"
46 MEMORY="1024"
47 NIC=[NAME="NIC0",NETWORK_ID="0"]
48 NIC=[NAME="NIC1",NETWORK_ID="1"]
49 """).strip(),
50 ),
51 (
52 {
53 "EMPTY_VALUE": None,
54 "SCHED_REQUIREMENTS": 'CLUSTER_ID="100"',
55 "BACKSLASH_ESCAPED": "this is escaped: \\n; this isn't: \"\nend",
56 },
57 textwrap.dedent("""
58 BACKSLASH_ESCAPED="this is escaped: \\\\n; this isn't: \\"
59 end"
60 SCHED_REQUIREMENTS="CLUSTER_ID=\\"100\\""
61 """).strip(),
62 ),
63 ]
64
65
66 @pytest.mark.parametrize("to_flatten,extract,expected_result", FLATTEN_VALID)
67 def test_flatten(to_flatten, extract, expected_result):
68 result = flatten(to_flatten, extract)
69 assert result == expected_result, repr(result)
70
71
72 @pytest.mark.parametrize("to_render,expected_result", RENDER_VALID)
73 def test_render(to_render, expected_result):
74 result = render(to_render)
75 assert result == expected_result, repr(result)