-
-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathtest_ansible.py
More file actions
95 lines (82 loc) · 3.21 KB
/
test_ansible.py
File metadata and controls
95 lines (82 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pytest
from testinfra.backend import parse_hostspec
from testinfra.utils.ansible_runner import expand_pattern, get_hosts, Inventory
@pytest.fixture
def inventory() -> Inventory:
"""Hosts are always under a group, the default is "ungrouped" if using the
ini file format. The "all" meta-group always contains all hosts when
expanded."""
return {
"_meta": {
"hostvars": {
"a": None,
"b": None,
"c": None,
}
},
"all": {
"children": ["nested"],
},
"left": {
"hosts": ["a", "b"],
},
"right": {
"hosts": ["b", "c"],
},
"combined": {
"children": ["left", "right"],
},
"nested": {
"children": ["combined"],
}
}
def test_expand_pattern_simple(inventory: Inventory):
"""Simple names are matched, recurring into groups if needed."""
# direct hostname
assert expand_pattern("a", inventory) == {"a"}
# group
assert expand_pattern("left", inventory) == {"a", "b"}
# meta-group
assert expand_pattern("combined", inventory) == {"a", "b", "c"}
# meta-meta-group
assert expand_pattern("nested", inventory) == {"a", "b", "c"}
def test_expand_pattern_fnmatch(inventory: Inventory):
"""Simple names are matched, recurring into groups if needed."""
# l->left
assert expand_pattern("l*", inventory) == {"a", "b"}
# any single letter name
assert expand_pattern("?", inventory) == {"a", "b", "c"}
def test_expand_pattern_regex(inventory: Inventory):
"""Simple names are matched, recurring into groups if needed."""
# simple character matching - "l" matches "left" but not "all"
assert expand_pattern("~l", inventory) == {"a", "b"}
# "b" matches an exact host, not any group
assert expand_pattern("~b", inventory) == {"b"}
# "a" will match all
assert expand_pattern("~a", inventory) == {"a", "b", "c"}
def test_get_hosts(inventory: Inventory):
"""Multiple names/patterns can be combined."""
assert get_hosts("a", inventory) == ["a"]
# the two pattern separators are handled
assert get_hosts("a:b", inventory) == ["a", "b"]
assert get_hosts("a,b", inventory) == ["a", "b"]
# difference works
assert get_hosts("left:!right", inventory) == ["a"]
# intersection works
assert get_hosts("left:&right", inventory) == ["b"]
# intersection is taken with the intersection of the intersection groups
assert get_hosts("all:&left:&right", inventory) == ["b"]
# when the intersections ends up empty, so does the result
assert get_hosts("all:&a:&c", inventory) == []
# negation is taken with the union of negation groups
assert get_hosts("all:!a:!c", inventory) == ["b"]
@pytest.mark.parametrize("left", ["h1", "!h1", "&h1", "~h1", "*h1"])
@pytest.mark.parametrize("sep", [":", ","])
@pytest.mark.parametrize("right", ["h2", "!h2", "&h2", "~h2", "*h2", ""])
def test_parse_hostspec(left: str, sep: str, right: str):
"""Ansible's host patterns are parsed without issue."""
if right:
pattern = f"{left}{sep}{right}"
else:
pattern = left
assert parse_hostspec(pattern) == (pattern, {})