From 86fbc79f6d6da92e17a1972fc47fc3b266601f31 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Thu, 19 May 2016 16:33:35 -0400 Subject: [PATCH] Add tests for the _net_address_search function The _net_address_search function looks through the provider networks and sets a key to True if it's the main network. This commit adds tests around the conditionals found in the function. In practice, the keys that are used (is_ssh_address and is_container_address) are specified in the gate user_config files, but this function seems to be there to catch and correct errors. The 'else' clause from the 'for' loop was moved up, since logically it was the action that happened when the target key was not present. The 'else' clause made it re-loop, which was unnecessary. Change-Id: Ic5920f19c8e21e324aee9c5e11831fd2c2e2b21d --- playbooks/inventory/dynamic_inventory.py | 7 ++---- tests/test_inventory.py | 27 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/playbooks/inventory/dynamic_inventory.py b/playbooks/inventory/dynamic_inventory.py index 96aa32472c..b0b7f48912 100755 --- a/playbooks/inventory/dynamic_inventory.py +++ b/playbooks/inventory/dynamic_inventory.py @@ -667,7 +667,7 @@ def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface, def _net_address_search(provider_networks, main_network, key): - """Set the key netwokr type to the main network if not specified. + """Set the key network type to the main network if not specified. :param provider_networks: ``list`` Network list of ``dict``s :param main_network: ``str`` The name of the main network bridge. @@ -680,10 +680,7 @@ def _net_address_search(provider_networks, main_network, key): # Check for the key if p_net.get(key): break - else: - for pn in provider_networks: - p_net = pn.get('network') - if p_net: + else: if p_net.get('container_bridge') == main_network: p_net[key] = True diff --git a/tests/test_inventory.py b/tests/test_inventory.py index 4e15c3941b..f6ed904698 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -610,6 +610,33 @@ class TestStaticRouteConfig(TestConfigChecks): self.assertEqual(exception.message, self.expectedMsg) +class TestNetAddressSearch(unittest.TestCase): + def test_net_address_search_key_not_found(self): + pns = [ + {'network': {'container_bridge': 'br-mgmt'}} + ] + new_pns = di._net_address_search(pns, 'br-mgmt', 'is_ssh_address') + + self.assertTrue(new_pns[0]['network']['is_ssh_address']) + + def test_net_address_search_key_not_found_bridge_doesnt_match(self): + pns = [ + {'network': {'container_bridge': 'lxcbr0'}} + ] + new_pns = di._net_address_search(pns, 'br-mgmt', 'is_ssh_address') + + self.assertNotIn('is_ssh_address', new_pns[0]['network']) + + def test_net_address_search_key_found(self): + pns = [ + {'network': {'container_bridge': 'br-mgmt', + 'is_ssh_address': True}} + ] + new_pns = di._net_address_search(pns, 'br-mgmt', 'is_ssh_address') + + self.assertEqual(pns, new_pns) + + class TestMultipleRuns(unittest.TestCase): def test_creating_backup_file(self): # Generate the initial inventory files