From a7b9bf958d41c11b15b35994f535c85a4e178a41 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sun, 6 May 2012 18:01:11 -0400 Subject: [PATCH] Add tests for new advanced inventory features (groups of groups, group variables) in the default INI format file. --- lib/ansible/host.py | 2 +- lib/ansible/inventory.py | 2 +- test/TestInventory.py | 42 +++++++++++++++++++++++------ test/complex_hosts | 58 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 test/complex_hosts diff --git a/lib/ansible/host.py b/lib/ansible/host.py index 5b7efea1509..ad385c0e0e5 100644 --- a/lib/ansible/host.py +++ b/lib/ansible/host.py @@ -57,6 +57,6 @@ class Host(object): results.update(self.vars) results['inventory_hostname'] = self.name groups = self.get_groups() - results['group_names'] = [ g.name for g in groups if g.name != 'all'] + results['group_names'] = sorted([ g.name for g in groups if g.name != 'all']) return results diff --git a/lib/ansible/inventory.py b/lib/ansible/inventory.py index 7372c8b9b0d..21e11a1b47c 100644 --- a/lib/ansible/inventory.py +++ b/lib/ansible/inventory.py @@ -119,7 +119,7 @@ class Inventory(object): results = utils.parse_json(out) results['inventory_hostname'] = hostname groups = [ g.name for g in host.get_groups() if g.name != 'all' ] - results['group_names'] = groups + results['group_names'] = sorted(groups) return results host = self.get_host(hostname) diff --git a/test/TestInventory.py b/test/TestInventory.py index 58650cecd17..2a8930277c7 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -11,9 +11,10 @@ class TestInventory(unittest.TestCase): self.cwd = os.getcwd() self.test_dir = os.path.join(self.cwd, 'test') - self.inventory_file = os.path.join(self.test_dir, 'simple_hosts') - self.inventory_script = os.path.join(self.test_dir, 'inventory_api.py') - self.inventory_yaml = os.path.join(self.test_dir, 'yaml_hosts') + self.inventory_file = os.path.join(self.test_dir, 'simple_hosts') + self.complex_inventory_file = os.path.join(self.test_dir, 'complex_hosts') + self.inventory_script = os.path.join(self.test_dir, 'inventory_api.py') + self.inventory_yaml = os.path.join(self.test_dir, 'yaml_hosts') os.chmod(self.inventory_script, 0755) @@ -28,16 +29,20 @@ class TestInventory(unittest.TestCase): assert left == right - ### Simple inventory format tests - def simple_inventory(self): - return Inventory( self.inventory_file ) + return Inventory(self.inventory_file) def script_inventory(self): - return Inventory( self.inventory_script ) + return Inventory(self.inventory_script) def yaml_inventory(self): - return Inventory( self.inventory_yaml ) + return Inventory(self.inventory_yaml) + + def complex_inventory(self): + return Inventory(self.complex_inventory_file) + + ##################################### + ### Simple inventory format tests def test_simple(self): inventory = self.simple_inventory() @@ -113,6 +118,27 @@ class TestInventory(unittest.TestCase): print expected assert vars == expected + ################################################### + ### INI file advanced tests + + def test_complex_vars(self): + inventory = self.complex_inventory() + + vars = inventory.get_variables('rtp_a') + print vars + + expected = dict( + a='1', b='2', c='3', d='100002', + inventory_hostname='rtp_a', + group_names=[ 'eastcoast', 'nc', 'rtp', 'us' ] + ) + print vars + print expected + assert vars == expected + + + + ################################################### ### Inventory API tests def test_script(self): diff --git a/test/complex_hosts b/test/complex_hosts new file mode 100644 index 00000000000..24bdbf1b966 --- /dev/null +++ b/test/complex_hosts @@ -0,0 +1,58 @@ +# order of groups, children, and vars is not signficant +# so this example mixes them up for maximum testing + +[nc:children] +rtp +triangle + +[eastcoast:children] +nc +florida + +[us:children] +eastcoast + +[nc:vars] +b=10000 +c=10001 +d=10002 + +[rtp] +rtp_a +rtp_b +rtb_c + +[rtp:vars] +a=1 +b=2 +c=3 + +[triangle] +tri_a +tri_b +tri_c + +[triangle:vars] +a=11 +b=12 +c=13 + +[florida] +orlando +miami + +[florida:vars] +a=100 +b=101 +c=102 + + +[eastcoast:vars] +b=100000 +c=100001 +d=100002 + +[us:vars] +c=1000000 + +