Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/inventory/test_lxd.py

raw
1 # Copyright (c) 2021, Frank Dornheim <[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
6 from __future__ import annotations
7
8 import pytest
9 from ansible.inventory.data import InventoryData
10
11 from ansible_collections.community.general.plugins.inventory.lxd import InventoryModule
12
13 HOST_COMPARATIVE_DATA = {
14 "ansible_connection": "ssh",
15 "ansible_host": "10.98.143.199",
16 "ansible_lxd_os": "ubuntu",
17 "ansible_lxd_release": "focal",
18 "ansible_lxd_profile": ["default"],
19 "ansible_lxd_state": "running",
20 "ansible_lxd_location": "Berlin",
21 "ansible_lxd_vlan_ids": {"my-macvlan": 666},
22 "inventory_hostname": "vlantest",
23 "inventory_hostname_short": "vlantest",
24 }
25 GROUP_COMPARATIVE_DATA = {
26 "all": [],
27 "ungrouped": [],
28 "testpattern": ["vlantest"],
29 "vlan666": ["vlantest"],
30 "locationBerlin": ["vlantest"],
31 "osUbuntu": ["vlantest"],
32 "releaseFocal": ["vlantest"],
33 "releaseBionic": [],
34 "profileDefault": ["vlantest"],
35 "profileX11": [],
36 "netRangeIPv4": ["vlantest"],
37 "netRangeIPv6": ["vlantest"],
38 }
39 GROUP_Config = {
40 "testpattern": {"type": "pattern", "attribute": "test"},
41 "vlan666": {"type": "vlanid", "attribute": 666},
42 "locationBerlin": {"type": "location", "attribute": "Berlin"},
43 "osUbuntu": {"type": "os", "attribute": "ubuntu"},
44 "releaseFocal": {"type": "release", "attribute": "focal"},
45 "releaseBionic": {"type": "release", "attribute": "bionic"},
46 "profileDefault": {"type": "profile", "attribute": "default"},
47 "profileX11": {"type": "profile", "attribute": "x11"},
48 "netRangeIPv4": {"type": "network_range", "attribute": "10.98.143.0/24"},
49 "netRangeIPv6": {"type": "network_range", "attribute": "fd42:bd00:7b11:2167:216:3eff::/96"},
50 }
51
52
53 @pytest.fixture
54 def inventory():
55 inv = InventoryModule()
56 inv.inventory = InventoryData()
57
58 # Test Values
59 inv.data = inv.load_json_data("tests/unit/plugins/inventory/fixtures/lxd_inventory.atd") # Load Test Data
60 inv.groupby = GROUP_Config
61 inv.prefered_instance_network_interface = "eth"
62 inv.prefered_instance_network_family = "inet"
63 inv.filter = "running"
64 inv.dump_data = False
65 inv.type_filter = "both"
66
67 return inv
68
69
70 def test_verify_file(tmp_path, inventory):
71 file = tmp_path / "foobar.lxd.yml"
72 file.touch()
73 assert inventory.verify_file(str(file)) is True
74
75
76 def test_verify_file_bad_config(inventory):
77 assert inventory.verify_file("foobar.lxd.yml") is False
78
79
80 def test_build_inventory_hosts(inventory):
81 """Load example data and start the inventoryto test the host generation.
82
83 After the inventory plugin has run with the test data, the result of the host is checked."""
84 inventory._populate()
85 generated_data = inventory.inventory.get_host("vlantest").get_vars()
86
87 eq = True
88 for key, value in HOST_COMPARATIVE_DATA.items():
89 if generated_data[key] != value:
90 eq = False
91 assert eq
92
93
94 def test_build_inventory_groups(inventory):
95 """Load example data and start the inventory to test the group generation.
96
97 After the inventory plugin has run with the test data, the result of the host is checked."""
98 inventory._populate()
99 generated_data = inventory.inventory.get_groups_dict()
100
101 eq = True
102 for key, value in GROUP_COMPARATIVE_DATA.items():
103 if generated_data[key] != value:
104 eq = False
105 assert eq
106
107
108 def test_build_inventory_groups_with_no_groupselection(inventory):
109 """Load example data and start the inventory to test the group generation with groupby is none.
110
111 After the inventory plugin has run with the test data, the result of the host is checked."""
112 inventory.groupby = None
113 inventory._populate()
114 generated_data = inventory.inventory.get_groups_dict()
115 group_comparative_data = {"all": [], "ungrouped": []}
116
117 eq = True
118 print(f"data: {generated_data}")
119 for key, value in group_comparative_data.items():
120 if generated_data[key] != value:
121 eq = False
122 assert eq