1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #6319 from risaacson/fix_test_dir_inventory

Fix TestInventory Unit Tests

Tested clean.
This commit is contained in:
Richard Isaacson 2014-03-06 15:43:45 -06:00
commit 119434d079
5 changed files with 40 additions and 21 deletions

View file

@ -123,12 +123,22 @@ class InventoryParser(object):
(k,v) = t.split("=", 1) (k,v) = t.split("=", 1)
except ValueError, e: except ValueError, e:
raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e))) raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e)))
try:
host.set_variable(k,ast.literal_eval(v)) # If there is a hash in the value don't pass it through to ast at ast will split at the hash.
except: if "#" in v:
# most likely a string that literal_eval host.set_variable(k, v)
# doesn't like, so just set it else:
host.set_variable(k,v) 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) self.groups[active_group_name].add_host(host)
# [southeast:children] # [southeast:children]

View file

@ -417,15 +417,24 @@ class TestInventory(unittest.TestCase):
auth = inventory.get_variables('neptun')['auth'] auth = inventory.get_variables('neptun')['auth']
assert auth == 'YWRtaW46YWRtaW4=' assert auth == 'YWRtaW46YWRtaW4='
# test disabled as needs to be updated to model desired behavior def test_dir_inventory(self):
# inventory = self.dir_inventory()
#def test_dir_inventory(self):
# inventory = self.dir_inventory() host_vars = inventory.get_variables('zeus')
# vars = inventory.get_variables('zeus')
# expected_vars = {'inventory_hostname': 'zeus',
# print "VARS=%s" % vars 'inventory_hostname_short': 'zeus',
# 'group_names': ['greek', 'major-god', 'ungrouped'],
# assert vars == {'inventory_hostname': 'zeus', 'var_a': '3#4'}
# 'inventory_hostname_short': 'zeus',
# 'group_names': ['greek', 'major-god', 'ungrouped'], print "HOST VARS=%s" % host_vars
# 'var_a': '1#2'} print "EXPECTED VARS=%s" % expected_vars
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']

View file

@ -1,3 +1,3 @@
zeus var_a=2 zeus var_a=0
morpheus morpheus
thor thor

View file

@ -1,5 +1,5 @@
[major-god] [major-god]
zeus var_a=1 zeus var_a=2
thor thor
[minor-god] [minor-god]

View file

@ -1,5 +1,5 @@
[major-god] # group with inline comments [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 # A comment
thor thor