From 3403334a7c1758bd81dafb3fad77470d91691580 Mon Sep 17 00:00:00 2001 From: Dan Radez Date: Tue, 23 Aug 2016 20:22:26 -0400 Subject: adding more network_environment.py tests - increasing required test coverage to 90% - also consolidating some repeated code into functions Change-Id: I6b582478c509c24fce43224a516414e1ead6c8e4 Signed-off-by: Dan Radez --- build/Makefile | 2 +- lib/python/apex/network_environment.py | 61 +++++++++++---------- tests/test_apex_network_environment.py | 98 ++++++++++++++++++++++++++++++++-- 3 files changed, 126 insertions(+), 35 deletions(-) diff --git a/build/Makefile b/build/Makefile index 85f7f8df..69c66f9f 100644 --- a/build/Makefile +++ b/build/Makefile @@ -70,7 +70,7 @@ python-tests: rm -rf ../tests/.coverage rm -rf ../tests/htmlcov # run nose tests - cd ../tests && PYTHONPATH=../lib/python/ nosetests-3.4 . --with-coverage --cover-package apex --cover-package apex_python_utils --cover-html --cover-min-percentage 85 + cd ../tests && PYTHONPATH=../lib/python/ nosetests-3.4 . --with-coverage --cover-package apex --cover-package apex_python_utils --cover-html --cover-min-percentage 90 # generate reports cd ../tests && coverage3 report --include '*lib/python/*' -m diff --git a/lib/python/apex/network_environment.py b/lib/python/apex/network_environment.py index d96fa44e..175f408f 100644 --- a/lib/python/apex/network_environment.py +++ b/lib/python/apex/network_environment.py @@ -42,6 +42,9 @@ API_RESOURCES = {'OS::TripleO::Network::InternalApi': None, IPV6_FLAGS = ["NovaIPv6", "MongoDbIPv6", "CorosyncIPv6", "CephIPv6", "RabbitIPv6", "MemcachedIPv6"] +reg = 'resource_registry' +param_def = 'parameter_defaults' + class NetworkEnvironment(dict): """ @@ -63,24 +66,10 @@ class NetworkEnvironment(dict): enabled_networks = net_settings.enabled_network_list except: raise NetworkEnvException('Invalid Network Setting object') - param_def = self['parameter_defaults'] - reg = self['resource_registry'] - if not net_settings: - raise NetworkEnvException("Network Settings does not exist") + self._set_tht_dir() enabled_networks = net_settings.get_enabled_networks() - param_def = 'parameter_defaults' - reg = 'resource_registry' - for key, prefix in TENANT_RESOURCES.items(): - if prefix is None: - prefix = '' - m = re.split('%s/\w+\.yaml' % prefix, self[reg][key]) - if m is not None: - tht_dir = m[0] - break - if not tht_dir: - raise NetworkEnvException('Unable to parse THT Directory') admin_cidr = net_settings[ADMIN_NETWORK]['cidr'] admin_prefix = str(admin_cidr.prefixlen) @@ -111,10 +100,8 @@ class NetworkEnvironment(dict): else: postfix = '/external.yaml' - for key, prefix in EXTERNAL_RESOURCES.items(): - if prefix is None: - prefix = '' - self[reg][key] = tht_dir + prefix + postfix + # apply resource registry update for EXTERNAL_RESOURCES + self._config_resource_reg(EXTERNAL_RESOURCES, postfix) if PRIVATE_NETWORK in enabled_networks: priv_range = net_settings[PRIVATE_NETWORK][ @@ -136,10 +123,8 @@ class NetworkEnvironment(dict): else: postfix = '/noop.yaml' - for key, prefix in TENANT_RESOURCES.items(): - if prefix is None: - prefix = '' - self[reg][key] = tht_dir + prefix + postfix + # apply resource registry update for TENANT_RESOURCES + self._config_resource_reg(TENANT_RESOURCES, postfix) if STORAGE_NETWORK in enabled_networks: storage_range = net_settings[STORAGE_NETWORK][ @@ -162,10 +147,8 @@ class NetworkEnvironment(dict): else: postfix = '/noop.yaml' - for key, prefix in STORAGE_RESOURCES.items(): - if prefix is None: - prefix = '' - self[reg][key] = tht_dir + prefix + postfix + # apply resource registry update for STORAGE_RESOURCES + self._config_resource_reg(STORAGE_RESOURCES, postfix) if API_NETWORK in enabled_networks: api_range = net_settings[API_NETWORK][ @@ -186,10 +169,8 @@ class NetworkEnvironment(dict): else: postfix = '/noop.yaml' - for key, prefix in API_RESOURCES.items(): - if prefix is None: - prefix = '' - self[reg][key] = tht_dir + prefix + postfix + # apply resource registry update for API_RESOURCES + self._config_resource_reg(API_RESOURCES, postfix) if compute_pre_config: self[reg][COMPUTE_PRE] = PRE_CONFIG_DIR + "compute/numa.yaml" @@ -203,6 +184,24 @@ class NetworkEnvironment(dict): for flag in IPV6_FLAGS: self[param_def][flag] = True + def _set_tht_dir(self): + self.tht_dir = None + for key, prefix in TENANT_RESOURCES.items(): + if prefix is None: + prefix = '' + m = re.split('%s/\w+\.yaml' % prefix, self[reg][key]) + if m is not None: + self.tht_dir = m[0] + break + if not self.tht_dir: + raise NetworkEnvException('Unable to parse THT Directory') + + def _config_resource_reg(self, resources, postfix): + for key, prefix in resources.items(): + if prefix is None: + prefix = '' + self[reg][key] = self.tht_dir + prefix + postfix + class NetworkEnvException(Exception): def __init__(self, value): diff --git a/tests/test_apex_network_environment.py b/tests/test_apex_network_environment.py index 9dcaffc0..673368e8 100644 --- a/tests/test_apex_network_environment.py +++ b/tests/test_apex_network_environment.py @@ -7,9 +7,21 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import ipaddress + +from apex.common.constants import ( + PUBLIC_NETWORK, + PRIVATE_NETWORK, + STORAGE_NETWORK, + API_NETWORK) from apex.network_settings import NetworkSettings -from apex.network_environment import NetworkEnvironment -from apex.network_environment import NetworkEnvException +from apex.network_environment import ( + NetworkEnvironment, + NetworkEnvException, + EXTERNAL_RESOURCES, + TENANT_RESOURCES, + STORAGE_RESOURCES, + API_RESOURCES) from nose.tools import assert_equal from nose.tools import assert_raises @@ -36,10 +48,90 @@ class TestNetworkEnvironment(object): assert_raises(NetworkEnvException, NetworkEnvironment, None, '../build/network-environment.yaml') - def test_get_netenv_settings(self): + def test_netenv_settings_public_network(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + # test vlans + ns[PUBLIC_NETWORK]['vlan'] = 100 + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + assert_equal(ne['parameter_defaults']['NeutronExternalNetworkBridge'], + '""') + assert_equal(ne['parameter_defaults']['ExternalNetworkVlanID'], 100) + + # Test IPv6 + ns[PUBLIC_NETWORK]['cidr'] = ipaddress.ip_network('::1/128') + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + regstr = ne['resource_registry'][next(iter(EXTERNAL_RESOURCES.keys()))] + assert_equal(regstr.split('/')[-1], 'external_v6.yaml') + + def test_netenv_settings_private_network(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + # test vlans + ns[PRIVATE_NETWORK]['vlan'] = 100 + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + assert_equal(ne['parameter_defaults']['TenantNetworkVlanID'], 100) + + # Test IPv6 + ns[PRIVATE_NETWORK]['cidr'] = ipaddress.ip_network('::1/128') + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + regstr = ne['resource_registry'][next(iter(TENANT_RESOURCES.keys()))] + assert_equal(regstr.split('/')[-1], 'tenant_v6.yaml') + + # Test removing PRIVATE_NETWORK + ns.enabled_network_list.remove(PRIVATE_NETWORK) + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + regstr = ne['resource_registry'][next(iter(TENANT_RESOURCES.keys()))] + assert_equal(regstr.split('/')[-1], 'noop.yaml') + + def test_netenv_settings_storage_network(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + # test vlans + ns[STORAGE_NETWORK]['vlan'] = 100 + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + assert_equal(ne['parameter_defaults']['StorageNetworkVlanID'], 100) + + # Test IPv6 + ns[STORAGE_NETWORK]['cidr'] = ipaddress.ip_network('::1/128') + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + regstr = ne['resource_registry'][next(iter(STORAGE_RESOURCES.keys()))] + assert_equal(regstr.split('/')[-1], 'storage_v6.yaml') + + # Test removing STORAGE_NETWORK + ns.enabled_network_list.remove(STORAGE_NETWORK) + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + regstr = ne['resource_registry'][next(iter(STORAGE_RESOURCES.keys()))] + assert_equal(regstr.split('/')[-1], 'noop.yaml') + + def test_netenv_settings_api_network(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + # test vlans + ns.enabled_network_list.append(API_NETWORK) + ns[API_NETWORK] = {'vlan': 100, + 'cidr': ipaddress.ip_network('10.10.10.0/24'), + 'usable_ip_range': '10.10.10.10,10.10.10.100'} + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + assert_equal(ne['parameter_defaults']['InternalApiNetworkVlanID'], 100) + + # Test IPv6 + ns[API_NETWORK]['cidr'] = ipaddress.ip_network('::1/128') + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + regstr = ne['resource_registry'][next(iter(API_RESOURCES.keys()))] + assert_equal(regstr.split('/')[-1], 'internal_api_v6.yaml') + + # Test removing API_NETWORK + ns.enabled_network_list.remove(API_NETWORK) + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + regstr = ne['resource_registry'][next(iter(API_RESOURCES.keys()))] + assert_equal(regstr.split('/')[-1], 'noop.yaml') + + def test_numa_configs(self): ns = NetworkSettings('../config/network/network_settings.yaml', True) ne = NetworkEnvironment(ns, '../build/network-environment.yaml', compute_pre_config=True, controller_pre_config=True) assert_is_instance(ne, dict) assert_not_equal(ne, {}) + + def test_exception(self): + e = NetworkEnvException("test") + print(e) + assert_is_instance(e, NetworkEnvException) -- cgit 1.2.3-korg