forked from fboender/ansible-cmdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
186 lines (163 loc) · 6.64 KB
/
Copy pathtest.py
File metadata and controls
186 lines (163 loc) · 6.64 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import sys
import unittest
import importlib.util
import io
import os
sys.path.insert(0, os.path.realpath("../lib"))
sys.path.insert(0, os.path.realpath("../src"))
import ansiblecmdb
class ExtendTestCase(unittest.TestCase):
"""
Test the extending of facts.
"""
def testExtendOverrideParams(self):
"""
Test that we can override a native fact
"""
fact_dirs = ["f_extend/out_setup", "f_extend/extend"]
ansible = ansiblecmdb.Ansible(fact_dirs)
env_editor = ansible.hosts["debian.dev.local"]["ansible_facts"]["ansible_env"][
"EDITOR"
]
self.assertEqual(env_editor, "nano")
def testExtendAddParams(self):
"""
Test that we can add new facts
"""
fact_dirs = ["f_extend/out_setup", "f_extend/extend"]
ansible = ansiblecmdb.Ansible(fact_dirs)
software = ansible.hosts["debian.dev.local"]["software"]
self.assertIn("Apache2", software)
class HostParseTestCase(unittest.TestCase):
"""
Test specifics of the hosts inventory parser
"""
def testChildGroupHosts(self):
"""
Test that children groups contain all hosts they should.
"""
fact_dirs = ["f_hostparse/out"]
inventories = ["f_hostparse/hosts"]
ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
groups = ansible.hosts["db.dev.local"]["groups"]
self.assertIn("db", groups)
self.assertIn("dev", groups)
self.assertIn("dev_local", groups)
def testChildGroupVars(self):
"""
Test that all vars applied against a child group are set on the hosts.
"""
fact_dirs = ["f_hostparse/out"]
inventories = ["f_hostparse/hosts"]
ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
host_vars = ansible.hosts["db.dev.local"]["hostvars"]
self.assertEqual(host_vars["function"], "db")
self.assertEqual(host_vars["dtap"], "dev")
def testExpandHostDef(self):
"""
Verify that host ranges are properly expanded. E.g. db[01-03].local ->
db01.local, db02.local, db03.local.
"""
fact_dirs = ["f_hostparse/out"]
inventories = ["f_hostparse/hosts"]
ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
self.assertIn("web02.dev.local", ansible.hosts)
self.assertIn("fe03.dev02.local", ansible.hosts)
class InventoryTestCase(unittest.TestCase):
def testHostsDir(self):
"""
Verify that we can specify a directory as the hosts inventory file and
that all files are parsed.
"""
fact_dirs = ["f_inventory/out"]
inventories = ["f_inventory/hostsdir"]
ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
host_vars = ansible.hosts["db.dev.local"]["hostvars"]
groups = ansible.hosts["db.dev.local"]["groups"]
self.assertEqual(host_vars["function"], "db")
self.assertIn("db", groups)
def testDynInv(self):
"""
Verify that we can specify a path to a dynamic inventory as the
inventory file, and it will be executed, it's output parsed and added
as available hosts.
"""
fact_dirs = ["f_inventory/out"] # Reuse f_hostparse
inventories = ["f_inventory/dyninv.py"]
ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
self.assertIn("host5.example.com", ansible.hosts)
host_vars = ansible.hosts["host5.example.com"]["hostvars"]
groups = ansible.hosts["host5.example.com"]["groups"]
self.assertEqual(host_vars["b"], False)
self.assertIn("atlanta", groups)
def testMixedDir(self):
"""
Verify that a mixed dir of hosts files and dynamic inventory scripts is
parsed correctly.
"""
fact_dirs = ["f_inventory/out"]
inventories = ["f_inventory/mixeddir"]
ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
# results from dynamic inventory
self.assertIn("host4.example.com", ansible.hosts)
self.assertIn("moocow.example.com", ansible.hosts)
# results from normal hosts file.
self.assertIn("web03.dev.local", ansible.hosts)
# INI file ignored.
self.assertNotIn("ini_setting", ansible.hosts)
def testExecutableStaticInventory(self):
"""
Verify that an inventory file which has the executable bit set but
isn't actually runnable falls back to being read as a static
inventory, instead of being silently dropped.
"""
fact_dirs = ["f_inventory/out"]
inventories = ["f_inventory/hosts_executable"]
ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
self.assertIn("execfallback01.dev.local", ansible.hosts)
self.assertIn("execfallback02.dev.local", ansible.hosts)
host = ansible.hosts["execfallback01.dev.local"]
self.assertEqual(host["hostvars"]["dtap"], "dev")
self.assertIn("execfallback", host["groups"])
def testFailingDynInventory(self):
"""
Verify that a dynamic inventory script which exits non-zero is
reported rather than raising, and that its output is not reinterpreted
as a static inventory.
"""
fact_dirs = ["f_inventory/out"]
inventories = ["f_inventory/dyninv_failing.sh"]
stderr = sys.stderr
sys.stderr = io.StringIO()
try:
ansible = ansiblecmdb.Ansible(fact_dirs, inventories)
captured = sys.stderr.getvalue()
finally:
sys.stderr = stderr
# The script's own error message must reach the user.
self.assertIn("dyninv: something went wrong", captured)
self.assertIn("exitcode 1", captured)
# Nothing from the script should have been parsed as an inventory.
self.assertNotIn("dyninv", ansible.hosts)
class FactCacheTestCase(unittest.TestCase):
"""
Test that we properly read fact-cached output dirs.
"""
def testFactCache(self):
fact_dirs = ["f_factcache/out"]
inventories = ["f_factcache/hosts"]
ansible = ansiblecmdb.Ansible(fact_dirs, inventories, fact_cache=True)
host_vars = ansible.hosts["debian.dev.local"]["hostvars"]
groups = ansible.hosts["debian.dev.local"]["groups"]
ansible_facts = ansible.hosts["debian.dev.local"]["ansible_facts"]
self.assertIn("dev", groups)
self.assertEqual(host_vars["dtap"], "dev")
self.assertIn("ansible_env", ansible_facts)
if __name__ == "__main__":
unittest.main(exit=True)
try:
os.unlink(
"../src/ansible-cmdbc"
) # FIXME: Where is this coming from? Our weird import I assume.
except Exception:
pass