From 16fe09eef85a2c65ff46b3d9b49c1ca13507ac0c Mon Sep 17 00:00:00 2001 From: Richard C Isaacson Date: Thu, 6 Mar 2014 12:09:53 -0600 Subject: [PATCH 1/4] Fixes related to uncommenting test_dir_inventory in TestInventory. 0. Uncomment the test. 1. Test fails. 2. Make vars unique per file in test inventory files. 3. Modify token addition to not ast.literal_eval(v) a variable containing a hash. 4. Modify vars to have an escape in test inventory file. 5. Catch exceptions explicitly. Any unknown exceptions should be a bug. 6. Test passes. --- lib/ansible/inventory/ini.py | 22 +++++++++++----- test/units/TestInventory.py | 26 ++++++++++--------- .../inventory_test_data/inventory_dir/0hosts | 2 +- .../inventory_test_data/inventory_dir/2levels | 2 +- .../inventory_dir/3comments | 2 +- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/ansible/inventory/ini.py b/lib/ansible/inventory/ini.py index 024eb9a9a0e..718fee1338d 100644 --- a/lib/ansible/inventory/ini.py +++ b/lib/ansible/inventory/ini.py @@ -123,12 +123,22 @@ class InventoryParser(object): (k,v) = t.split("=", 1) except ValueError, e: raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e))) - try: - host.set_variable(k,ast.literal_eval(v)) - except: - # most likely a string that literal_eval - # doesn't like, so just set it - host.set_variable(k,v) + + # If there is a hash in the value don't pass it through to ast at ast will split at the hash. + if "#" in v: + host.set_variable(k, v) + else: + try: + host.set_variable(k,ast.literal_eval(v)) + # Using explicit exceptions. + # Likely a string that literal_eval does not like. We wil then just set it. + except ValueError: + # For some reason this was thought to be malformed. + host.set_variable(k, v) + except SyntaxError: + # Is this a hash with an equals at the end? + host.set_variable(k, v) + self.groups[active_group_name].add_host(host) # [southeast:children] diff --git a/test/units/TestInventory.py b/test/units/TestInventory.py index 2ae6256e62b..bd2f24c063b 100644 --- a/test/units/TestInventory.py +++ b/test/units/TestInventory.py @@ -417,15 +417,17 @@ class TestInventory(unittest.TestCase): auth = inventory.get_variables('neptun')['auth'] assert auth == 'YWRtaW46YWRtaW4=' - # test disabled as needs to be updated to model desired behavior - # - #def test_dir_inventory(self): - # inventory = self.dir_inventory() - # vars = inventory.get_variables('zeus') - # - # print "VARS=%s" % vars - # - # assert vars == {'inventory_hostname': 'zeus', - # 'inventory_hostname_short': 'zeus', - # 'group_names': ['greek', 'major-god', 'ungrouped'], - # 'var_a': '1#2'} + def test_dir_inventory(self): + inventory = self.dir_inventory() + + host_vars = inventory.get_variables('zeus') + + expected_vars = {'inventory_hostname': 'zeus', + 'inventory_hostname_short': 'zeus', + 'group_names': ['greek', 'major-god', 'ungrouped'], + 'var_a': '3#4'} + + print "HOST VARS=%s" % host_vars + print "EXPECTED VARS=%s" % expected_vars + + assert host_vars == expected_vars \ No newline at end of file diff --git a/test/units/inventory_test_data/inventory_dir/0hosts b/test/units/inventory_test_data/inventory_dir/0hosts index 27fc46e8530..6f78a33a228 100644 --- a/test/units/inventory_test_data/inventory_dir/0hosts +++ b/test/units/inventory_test_data/inventory_dir/0hosts @@ -1,3 +1,3 @@ -zeus var_a=2 +zeus var_a=0 morpheus thor diff --git a/test/units/inventory_test_data/inventory_dir/2levels b/test/units/inventory_test_data/inventory_dir/2levels index 22f06bcd436..363294923ef 100644 --- a/test/units/inventory_test_data/inventory_dir/2levels +++ b/test/units/inventory_test_data/inventory_dir/2levels @@ -1,5 +1,5 @@ [major-god] -zeus var_a=1 +zeus var_a=2 thor [minor-god] diff --git a/test/units/inventory_test_data/inventory_dir/3comments b/test/units/inventory_test_data/inventory_dir/3comments index 74642f13cc7..e11b5e416bd 100644 --- a/test/units/inventory_test_data/inventory_dir/3comments +++ b/test/units/inventory_test_data/inventory_dir/3comments @@ -1,5 +1,5 @@ [major-god] # group with inline comments -zeus var_a="1#2" # host with inline comments and "#" in the var string +zeus var_a="3\#4" # host with inline comments and "#" in the var string # A comment thor From 6626cb0b3d2bff379488d3bfb32e9b2499bbb11b Mon Sep 17 00:00:00 2001 From: g-k-r Date: Fri, 31 Jan 2014 12:46:00 +0100 Subject: [PATCH 2/4] add test same host in different files in dir tests issue #5749 same host defined in different groups which in turn are defined in different ini files in an inventory directory Conflicts: test/units/TestInventory.py --- test/units/TestInventory.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/units/TestInventory.py b/test/units/TestInventory.py index bd2f24c063b..4e188cd49bf 100644 --- a/test/units/TestInventory.py +++ b/test/units/TestInventory.py @@ -430,4 +430,12 @@ class TestInventory(unittest.TestCase): print "HOST VARS=%s" % host_vars print "EXPECTED VARS=%s" % expected_vars - assert host_vars == expected_vars \ No newline at end of file + assert host_vars == expected_vars + + def test_dir_inventory_multiple_groups(self): + inventory = self.dir_inventory() + group_greek = inventory.get_group('greek') + group_major_god = inventory.get_group('major-god') + actual_host_names = [host.name for host in group_greek.get_hosts()]; + print "%s : %s " % (group_greek.name, actual_host_names) + assert actual_host_names == ['zeus','morpheus'] \ No newline at end of file From dbad5d71c6af83c8995793f036896865e7bfec41 Mon Sep 17 00:00:00 2001 From: g-k-r Date: Fri, 31 Jan 2014 12:34:21 +0100 Subject: [PATCH 3/4] modifed test to use get_hosts instead of get_groups closes #5749 Conflicts: test/units/TestInventory.py --- test/units/TestInventory.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/units/TestInventory.py b/test/units/TestInventory.py index 4e188cd49bf..d7c27b38e00 100644 --- a/test/units/TestInventory.py +++ b/test/units/TestInventory.py @@ -434,8 +434,7 @@ class TestInventory(unittest.TestCase): def test_dir_inventory_multiple_groups(self): inventory = self.dir_inventory() - group_greek = inventory.get_group('greek') - group_major_god = inventory.get_group('major-god') - actual_host_names = [host.name for host in group_greek.get_hosts()]; - print "%s : %s " % (group_greek.name, actual_host_names) - assert actual_host_names == ['zeus','morpheus'] \ No newline at end of file + group_greek = inventory.get_hosts('greek') + actual_host_names = [host.name for host in group_greek]; + print "greek : %s " % (actual_host_names) + assert actual_host_names == ['zeus','morpheus'] From 913c855df492a509542980185fbbf0dffbba8b8c Mon Sep 17 00:00:00 2001 From: Richard C Isaacson Date: Thu, 6 Mar 2014 13:08:35 -0600 Subject: [PATCH 4/4] Formatting cleanup. Post merging of GH-5837 via GH-5749. --- test/units/TestInventory.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/units/TestInventory.py b/test/units/TestInventory.py index d7c27b38e00..f8e9232c540 100644 --- a/test/units/TestInventory.py +++ b/test/units/TestInventory.py @@ -433,8 +433,8 @@ class TestInventory(unittest.TestCase): assert host_vars == expected_vars def test_dir_inventory_multiple_groups(self): - inventory = self.dir_inventory() - group_greek = inventory.get_hosts('greek') - actual_host_names = [host.name for host in group_greek]; - print "greek : %s " % (actual_host_names) - assert actual_host_names == ['zeus','morpheus'] + inventory = self.dir_inventory() + group_greek = inventory.get_hosts('greek') + actual_host_names = [host.name for host in group_greek] + print "greek : %s " % actual_host_names + assert actual_host_names == ['zeus', 'morpheus']