-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathtest_ansible_api.py
More file actions
139 lines (123 loc) · 5.05 KB
/
Copy pathtest_ansible_api.py
File metadata and controls
139 lines (123 loc) · 5.05 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
import sys
import unittest
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.AnsibleViaAPI(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.AnsibleViaAPI(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.AnsibleViaAPI(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.AnsibleViaAPI(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.AnsibleViaAPI(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.AnsibleViaAPI(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.AnsibleViaAPI(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.AnsibleViaAPI(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)
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.AnsibleViaAPI(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