Debian Codegraph @

test/units/inventory/test_group.py

raw
1 # Copyright 2018 Alan Rominger <[email protected]>
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 import pytest
7
8 from ansible.inventory.group import Group
9 from ansible.inventory.host import Host
10 from ansible.errors import AnsibleError
11
12
13 def test_depth_update():
14 A = Group('A')
15 B = Group('B')
16 Z = Group('Z')
17 A.add_child_group(B)
18 A.add_child_group(Z)
19 assert A.depth == 0
20 assert Z.depth == 1
21 assert B.depth == 1
22
23
24 def test_depth_update_dual_branches():
25 alpha = Group('alpha')
26 A = Group('A')
27 alpha.add_child_group(A)
28 B = Group('B')
29 A.add_child_group(B)
30 Z = Group('Z')
31 alpha.add_child_group(Z)
32 beta = Group('beta')
33 B.add_child_group(beta)
34 Z.add_child_group(beta)
35
36 assert alpha.depth == 0 # apex
37 assert beta.depth == 3 # alpha -> A -> B -> beta
38
39 omega = Group('omega')
40 omega.add_child_group(alpha)
41
42 # verify that both paths are traversed to get the max depth value
43 assert B.depth == 3 # omega -> alpha -> A -> B
44 assert beta.depth == 4 # B -> beta
45
46
47 def test_depth_recursion():
48 A = Group('A')
49 B = Group('B')
50 A.add_child_group(B)
51 # hypothetical of adding B as child group to A
52 A.parent_groups.append(B)
53 B.child_groups.append(A)
54 # can't update depths of groups, because of loop
55 with pytest.raises(AnsibleError):
56 B._check_children_depth()
57
58
59 def test_loop_detection():
60 A = Group('A')
61 B = Group('B')
62 C = Group('C')
63 A.add_child_group(B)
64 B.add_child_group(C)
65 with pytest.raises(AnsibleError):
66 C.add_child_group(A)
67
68
69 def test_direct_host_ordering():
70 """Hosts are returned in order they are added
71 """
72 group = Group('A')
73 # host names not added in alphabetical order
74 host_name_list = ['z', 'b', 'c', 'a', 'p', 'q']
75 expected_hosts = []
76 for host_name in host_name_list:
77 h = Host(host_name)
78 group.add_host(h)
79 expected_hosts.append(h)
80 assert group.get_hosts() == expected_hosts
81
82
83 def test_sub_group_host_ordering():
84 """With multiple nested groups, asserts that hosts are returned
85 in deterministic order
86 """
87 top_group = Group('A')
88 expected_hosts = []
89 for name in ['z', 'b', 'c', 'a', 'p', 'q']:
90 child = Group('group_{0}'.format(name))
91 top_group.add_child_group(child)
92 host = Host('host_{0}'.format(name))
93 child.add_host(host)
94 expected_hosts.append(host)
95 assert top_group.get_hosts() == expected_hosts
96
97
98 def test_populates_descendant_hosts():
99 A = Group('A')
100 B = Group('B')
101 C = Group('C')
102 h = Host('h')
103 C.add_host(h)
104 A.add_child_group(B) # B is child of A
105 B.add_child_group(C) # C is descendant of A
106 A.add_child_group(B)
107 assert set(h.groups) == set([C, B, A])
108 h2 = Host('h2')
109 C.add_host(h2)
110 assert set(h2.groups) == set([C, B, A])
111
112
113 def test_ancestor_example():
114 # see docstring for Group._walk_relationship
115 groups = {}
116 for name in ['A', 'B', 'C', 'D', 'E', 'F']:
117 groups[name] = Group(name)
118 # first row
119 groups['A'].add_child_group(groups['D'])
120 groups['B'].add_child_group(groups['D'])
121 groups['B'].add_child_group(groups['E'])
122 groups['C'].add_child_group(groups['D'])
123 # second row
124 groups['D'].add_child_group(groups['E'])
125 groups['D'].add_child_group(groups['F'])
126 groups['E'].add_child_group(groups['F'])
127
128 assert (
129 set(groups['F'].get_ancestors()) ==
130 set([
131 groups['A'], groups['B'], groups['C'], groups['D'], groups['E']
132 ])
133 )
134
135
136 def test_ancestors_recursive_loop_safe():
137 """
138 The get_ancestors method may be referenced before circular parenting
139 checks, so the method is expected to be stable even with loops
140 """
141 A = Group('A')
142 B = Group('B')
143 A.parent_groups.append(B)
144 B.parent_groups.append(A)
145 # finishes in finite time
146 assert A.get_ancestors() == set([A, B])
147
148
149 @pytest.mark.parametrize("priority, expected", [
150 pytest.param(5, 5, id="int"),
151 pytest.param('10', 10, id="string"),
152 pytest.param(-1, -1, id="negative number"),
153 ])
154 def test_set_priority_valid_values(priority, expected):
155 """Test that valid priority value is set"""
156 group = Group('test_group')
157 group.set_priority(priority)
158 assert group.priority == expected
159
160
161 @pytest.mark.parametrize("priority, expected", [
162 pytest.param('invalid', 1, id="invalid string"),
163 pytest.param(None, 1, id="None"),
164 pytest.param({'key': 'value'}, 1, id="dict"),
165 pytest.param(['item'], 1, id="list")
166 ])
167 def test_set_priority_invalid_values(priority, expected):
168 """Test that invalid priority value is handled gracefully with warnings"""
169 group = Group('test_group')
170 group.set_priority(priority)
171 assert group.priority == expected