For AC-332. Added support to inventory script view, inventory script and task engine to include hostvars inline when using Ansible >= 1.3.

This commit is contained in:
Chris Church
2013-08-27 00:08:54 -04:00
parent 2bb5374685
commit 0129036b40
7 changed files with 164 additions and 4 deletions

View File

@@ -663,7 +663,60 @@ class InventoryTest(BaseTest):
# on a group resource, I can see related resources for variables, inventories, and children
# and these work
def test_get_inventory_tree(self):
def test_get_inventory_script_view(self):
i_a = self.inventory_a
i_a.variables = json.dumps({'i-vars': 123})
i_a.save()
# Group A is parent of B, B is parent of C, C is parent of D.
g_a = i_a.groups.create(name='A', variables=json.dumps({'A-vars': 'AAA'}))
g_b = i_a.groups.create(name='B', variables=json.dumps({'B-vars': 'BBB'}))
g_b.parents.add(g_a)
g_c = i_a.groups.create(name='C', variables=json.dumps({'C-vars': 'CCC'}))
g_c.parents.add(g_b)
g_d = i_a.groups.create(name='D', variables=json.dumps({'D-vars': 'DDD'}))
g_d.parents.add(g_c)
# Each group "X" contains one host "x".
h_a = i_a.hosts.create(name='a', variables=json.dumps({'a-vars': 'aaa'}))
h_a.groups.add(g_a)
h_b = i_a.hosts.create(name='b', variables=json.dumps({'b-vars': 'bbb'}))
h_b.groups.add(g_b)
h_c = i_a.hosts.create(name='c', variables=json.dumps({'c-vars': 'ccc'}))
h_c.groups.add(g_c)
h_d = i_a.hosts.create(name='d', variables=json.dumps({'d-vars': 'ddd'}))
h_d.groups.add(g_d)
# Old, slow 1.2 way.
url = reverse('main:inventory_script_view', args=(i_a.pk,))
with self.current_user(self.super_django_user):
response = self.get(url, expect=200)
self.assertTrue('all' in response)
self.assertEqual(response['all']['vars'], i_a.variables_dict)
for g in i_a.groups.all():
self.assertTrue(g.name in response)
self.assertEqual(response[g.name]['vars'], g.variables_dict)
self.assertEqual(set(response[g.name]['children']),
set(g.children.values_list('name', flat=True)))
self.assertEqual(set(response[g.name]['hosts']),
set(g.hosts.values_list('name', flat=True)))
self.assertFalse('_meta' in response)
for h in i_a.hosts.all():
h_url = '%s?host=%s' % (url, h.name)
with self.current_user(self.super_django_user):
response = self.get(h_url, expect=200)
self.assertEqual(response, h.variables_dict)
# New 1.3 way.
url = reverse('main:inventory_script_view', args=(i_a.pk,))
url = '%s?hostvars=1' % url
with self.current_user(self.super_django_user):
response = self.get(url, expect=200)
self.assertTrue('_meta' in response)
self.assertTrue('hostvars' in response['_meta'])
for h in i_a.hosts.all():
self.assertEqual(response['_meta']['hostvars'][h.name],
h.variables_dict)
def test_get_inventory_tree_view(self):
# Group A is parent of B, B is parent of C, C is parent of D.
g_a = self.inventory_a.groups.create(name='A')
g_b = self.inventory_a.groups.create(name='B')

View File

@@ -208,6 +208,55 @@ class InventoryScriptTest(BaseScriptTest):
else:
self.assertTrue(len(v['children']) == 0)
def test_list_with_hostvars_inline(self):
inventory = self.inventories[1]
self.assertTrue(inventory.active)
rc, stdout, stderr = self.run_inventory_script(list=True,
inventory=inventory.pk,
hostvars=True)
self.assertEqual(rc, 0, stderr)
data = json.loads(stdout)
groups = inventory.groups.filter(active=True)
groupnames = list(groups.values_list('name', flat=True))
groupnames.extend(['all', '_meta'])
self.assertEqual(set(data.keys()), set(groupnames))
all_hostnames = set()
# Groups for this inventory should have hosts, variable data, and one
# parent/child relationship.
for k,v in data.items():
self.assertTrue(isinstance(v, dict))
if k == 'all':
self.assertEqual(v.get('vars', {}), inventory.variables_dict)
continue
if k == '_meta':
continue
group = inventory.groups.get(active=True, name=k)
hosts = group.hosts.filter(active=True)
hostnames = hosts.values_list('name', flat=True)
all_hostnames.update(hostnames)
self.assertEqual(set(v.get('hosts', [])), set(hostnames))
if group.variables:
self.assertEqual(v.get('vars', {}), group.variables_dict)
if k == 'group-3':
children = group.children.filter(active=True)
childnames = children.values_list('name', flat=True)
self.assertEqual(set(v.get('children', [])), set(childnames))
else:
self.assertTrue(len(v['children']) == 0)
# Check hostvars in ['_meta']['hostvars'] dict.
for hostname in all_hostnames:
self.assertTrue(hostname in data['_meta']['hostvars'])
host = inventory.hosts.get(name=hostname)
self.assertEqual(data['_meta']['hostvars'][hostname],
host.variables_dict)
# Hostvars can also be requested via environment variable.
os.environ['INVENTORY_HOSTVARS'] = str(True)
rc, stdout, stderr = self.run_inventory_script(list=True,
inventory=inventory.pk)
self.assertEqual(rc, 0, stderr)
data = json.loads(stdout)
self.assertTrue('_meta' in data)
def test_valid_host(self):
# Host without variable data.
inventory = self.inventories[0]
@@ -280,11 +329,10 @@ class InventoryScriptTest(BaseScriptTest):
self.assertNotEqual(rc, 0, stderr)
self.assertEqual(json.loads(stdout), {})
def _test_with_both_list_and_host_arguments(self):
def test_with_both_list_and_host_arguments(self):
inventory = self.inventories[0]
self.assertTrue(inventory.active)
os.environ['INVENTORY_ID'] = str(inventory.pk)
rc, stdout, stderr = self.run_inventory_script(list=True, host='blah')
self.assertNotEqual(rc, 0, stderr)
self.assertEqual(json.loads(stdout), {})