From f36c6c8c5b419865939c7e2b0b26f6c97255fdc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag=20Wie=C3=ABrs?= Date: Thu, 12 Jul 2012 16:42:00 +0200 Subject: [PATCH] Added unit tests for host-groups patch For the unit test I chose to keep the original yaml file in place as a reference. This patch also includes a fix. --- lib/ansible/inventory/yaml.py | 2 +- test/TestInventory.py | 111 ++++++++++++++++++++++++++++++++++ test/yaml2_hosts | 40 ++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 test/yaml2_hosts diff --git a/lib/ansible/inventory/yaml.py b/lib/ansible/inventory/yaml.py index c63a975d098..5ee379339ea 100644 --- a/lib/ansible/inventory/yaml.py +++ b/lib/ansible/inventory/yaml.py @@ -133,4 +133,4 @@ class InventoryParserYaml(object): ungrouped.add_host(host) # make sure ungrouped_hosts is the complement of grouped_hosts - ungrouped_hosts = [host if host not in grouped_hosts for host in ungrouped_hosts] + ungrouped_hosts = [host for host in ungrouped.hosts if host not in grouped_hosts] diff --git a/test/TestInventory.py b/test/TestInventory.py index ef551e5816a..043519f26a0 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -15,6 +15,7 @@ class TestInventory(unittest.TestCase): 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') + self.inventory_yaml2 = os.path.join(self.test_dir, 'yaml2_hosts') os.chmod(self.inventory_script, 0755) @@ -38,6 +39,9 @@ class TestInventory(unittest.TestCase): def yaml_inventory(self): return Inventory(self.inventory_yaml) + def yaml2_inventory(self): + return Inventory(self.inventory_yaml2) + def complex_inventory(self): return Inventory(self.complex_inventory_file) @@ -323,3 +327,110 @@ class TestInventory(unittest.TestCase): assert 'group_names' in vars assert sorted(vars['group_names']) == [ 'norse', 'ruler' ] + ### Tests for yaml2 inventory file + + def test_yaml2(self): + inventory = self.yaml2_inventory() + hosts = inventory.list_hosts() + print hosts + expected_hosts=['jupiter', 'saturn', 'mars', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki'] + self.compare(hosts, expected_hosts) + + def test_yaml2_all(self): + inventory = self.yaml2_inventory() + hosts = inventory.list_hosts('all') + + expected_hosts=['jupiter', 'saturn', 'mars', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki'] + self.compare(hosts, expected_hosts) + + def test_yaml2_norse(self): + inventory = self.yaml2_inventory() + hosts = inventory.list_hosts("norse") + + expected_hosts=['thor', 'odin', 'loki'] + self.compare(hosts, expected_hosts) + + def test_yaml2_ungrouped(self): + inventory = self.yaml2_inventory() + hosts = inventory.list_hosts("ungrouped") + + expected_hosts=['jupiter', 'mars'] + self.compare(hosts, expected_hosts) + + def test_yaml2_combined(self): + inventory = self.yaml2_inventory() + hosts = inventory.list_hosts("norse:greek") + + expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki'] + self.compare(hosts, expected_hosts) + + def test_yaml2_restrict(self): + inventory = self.yaml2_inventory() + + restricted_hosts = ['hera', 'poseidon', 'thor'] + expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki'] + + inventory.restrict_to(restricted_hosts) + hosts = inventory.list_hosts("norse:greek") + + self.compare(hosts, restricted_hosts) + + inventory.lift_restriction() + hosts = inventory.list_hosts("norse:greek") + + self.compare(hosts, expected_hosts) + + def test_yaml2_vars(self): + inventory = self.yaml2_inventory() + vars = inventory.get_variables('thor') + assert vars == {'group_names': ['norse'], + 'hammer':True, + 'inventory_hostname': 'thor'} + + def test_yaml2_list_vars(self): + inventory = self.yaml2_inventory() + vars = inventory.get_variables('zeus') + assert vars == {'ansible_ssh_port': 3001, + 'group_names': ['greek', 'ruler'], + 'inventory_hostname': 'zeus', + 'ntp_server': 'olympus.example.com'} + + def test_yaml2_change_vars(self): + inventory = self.yaml2_inventory() + vars = inventory.get_variables('thor') + + vars["hammer"] = False + + vars = inventory.get_variables('thor') + print vars + assert vars == {'hammer':True, + 'inventory_hostname': 'thor', + 'group_names': ['norse']} + + def test_yaml2_host_vars(self): + inventory = self.yaml2_inventory() + vars = inventory.get_variables('saturn') + + print vars + assert vars == {'inventory_hostname': 'saturn', + 'moon': 'titan', + 'moon2': 'enceladus', + 'group_names': ['multiple']} + + def test_yaml2_port(self): + inventory = self.yaml2_inventory() + vars = inventory.get_variables('hera') + + print vars + assert vars == {'ansible_ssh_port': 3000, + 'inventory_hostname': 'hera', + 'ntp_server': 'olympus.example.com', + 'group_names': ['greek']} + + def test_yaml2_multiple_groups(self): + inventory = self.yaml2_inventory() + vars = inventory.get_variables('odin') + + assert 'group_names' in vars + assert sorted(vars['group_names']) == [ 'norse', 'ruler' ] + diff --git a/test/yaml2_hosts b/test/yaml2_hosts new file mode 100644 index 00000000000..62cecdbddf9 --- /dev/null +++ b/test/yaml2_hosts @@ -0,0 +1,40 @@ +--- + +- jupiter + +- host: saturn + groups: [ multiple ] + vars: + moon: titan + moon2: enceladus + +- host: hera + groups: [ greek ] + +- host: poseidon + groups: greek + +- host: mars + +- host: zeus + groups: [ greek, ruler ] + vars: + - ansible_ssh_port: 3001 + +- host: odin + groups: + - norse + - ruler + +- host: loki + groups: [ norse ] + +- host: thor + groups: [ norse ] + vars: + - hammer: True + +- group: greek + vars: + - ansible_ssh_port: 3000 + - ntp_server: olympus.example.com