From 3b6895be6799b27a4047e22418b13a801e4d932e Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Thu, 11 Aug 2016 16:27:33 -0400 Subject: [PATCH] Move network_entry function to top level This change adds some minor testing around the build process for a network's dictionary. This function may be further refactored in the future, but this serves as a start for that work. Change-Id: I63506e863386bd91a59d10530f3e81a444692cf7 --- playbooks/inventory/dynamic_inventory.py | 63 +++++++++++++++--------- tests/test_inventory.py | 15 ++++++ 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/playbooks/inventory/dynamic_inventory.py b/playbooks/inventory/dynamic_inventory.py index 0f5def1325..06c1b9f9b4 100755 --- a/playbooks/inventory/dynamic_inventory.py +++ b/playbooks/inventory/dynamic_inventory.py @@ -526,6 +526,31 @@ def _load_optional_q(config, cidr_name): return ip_q +def network_entry(is_metal, interface, + bridge=None, net_type=None, net_mtu=None): + """Return a network entry for a container.""" + + # TODO(cloudnull) After a few releases this conditional should be + # simplified. The container address checking that is ssh address + # is only being done to support old inventory. + + if is_metal: + _network = dict() + else: + _network = {'interface': interface} + + if bridge: + _network['bridge'] = bridge + + if net_type: + _network['type'] = net_type + + if net_mtu: + _network['mtu'] = net_mtu + + return _network + + def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface, bridge, net_type, net_mtu, user_config, is_ssh_address, is_container_address, @@ -547,28 +572,6 @@ def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface, :param is_container_address: ``bol`` set this address to container_address. :param static_routes: ``list`` List containing static route dicts. """ - def network_entry(): - """Return a network entry for a container.""" - - # TODO(cloudnull) After a few releases this conditional should be - # simplified. The container address checking that is ssh address - # is only being done to support old inventory. - - if is_metal: - _network = dict() - else: - _network = {'interface': interface} - - if bridge: - _network['bridge'] = bridge - - if net_type: - _network['type'] = net_type - - if net_mtu: - _network['mtu'] = net_mtu - - return _network base_hosts = inventory['_meta']['hostvars'] lookup = inventory.get(key, list()) @@ -625,7 +628,13 @@ def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface, # This should convert found addresses based on q_name + "_address" # and then build the network if its not found. if not is_metal and old_address not in networks: - network = networks[old_address] = network_entry() + network = networks[old_address] = network_entry( + is_metal, + interface, + bridge, + net_type, + net_mtu + ) if old_address in container and container[old_address]: network['address'] = container.pop(old_address) elif not is_metal: @@ -635,7 +644,13 @@ def _add_additional_networks(key, inventory, ip_q, q_name, netmask, interface, network['netmask'] = netmask elif is_metal: - network = networks[old_address] = network_entry() + network = networks[old_address] = network_entry( + is_metal, + interface, + bridge, + net_type, + net_mtu + ) network['netmask'] = netmask if is_ssh_address or is_container_address: # Container physical host group diff --git a/tests/test_inventory.py b/tests/test_inventory.py index 38efaef137..a91c8fa6ef 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -1029,5 +1029,20 @@ class TestConfigCheckFunctional(TestConfigCheckBase): self.assertEqual(context.exception.ip, '172.29.236.100') +class TestNetworkEntry(unittest.TestCase): + def test_all_args_filled(self): + entry = di.network_entry(True, 'eth1', 'br-mgmt', 'my_type', '1700') + + self.assertNotIn('interface', entry.keys()) + self.assertEqual(entry['bridge'], 'br-mgmt') + self.assertEqual(entry['type'], 'my_type') + self.assertEqual(entry['mtu'], '1700') + + def test_container_dict(self): + entry = di.network_entry(False, 'eth1', 'br-mgmt', 'my_type', '1700') + + self.assertEqual(entry['interface'], 'eth1') + + if __name__ == '__main__': unittest.main()