From: spisarski Date: Thu, 13 Jul 2017 20:34:01 +0000 (-0600) Subject: Created domain classes for networks and subnets. X-Git-Tag: opnfv-5.0.0~68 X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F39%2F37439%2F1;p=snaps.git Created domain classes for networks and subnets. Created Network and Subnet domain classes so neutron_utils.py functions returning these types of objects will not be leaking out implementation details as each API version can change these data structures which is now being handled by the SNAPS neutron utility. JIRA: SNAPS-113 Change-Id: Id95dd0f8c2618c20a1a73a428abb95686dfa8251 Signed-off-by: spisarski --- diff --git a/snaps/domain/network.py b/snaps/domain/network.py index 0ba9e99..68889f1 100644 --- a/snaps/domain/network.py +++ b/snaps/domain/network.py @@ -14,6 +14,42 @@ # limitations under the License. +class Network: + """ + SNAPS domain object for interface routers. Should contain attributes that + are shared amongst cloud providers + """ + def __init__(self, **kwargs): + """ + Constructor + """ + self.name = kwargs.get('name') + self.id = kwargs.get('id') + self.type = kwargs.get('provider:network_type') + + def __eq__(self, other): + return (self.name == other.name and self.id == other.id and + self.type == other.type) + + +class Subnet: + """ + SNAPS domain object for interface routers. Should contain attributes that + are shared amongst cloud providers + """ + def __init__(self, **kwargs): + """ + Constructor + """ + self.name = kwargs.get('name') + self.id = kwargs.get('id') + self.cidr = kwargs.get('cidr') + + def __eq__(self, other): + return (self.name == other.name and self.id == other.id and + self.cidr == other.cidr) + + class Port: """ SNAPS domain object for ports. Should contain attributes that diff --git a/snaps/domain/test/network_tests.py b/snaps/domain/test/network_tests.py index c394b4a..592090b 100644 --- a/snaps/domain/test/network_tests.py +++ b/snaps/domain/test/network_tests.py @@ -15,7 +15,46 @@ import unittest from snaps.domain.network import ( - Port, SecurityGroup, SecurityGroupRule, Router, InterfaceRouter) + Port, SecurityGroup, SecurityGroupRule, Router, InterfaceRouter, Network, + Subnet) + + +class NetworkObjectTests(unittest.TestCase): + """ + Tests the construction of the snaps.domain.network.Network class + """ + + def test_construction_kwargs(self): + network = Network( + **{'name': 'name', 'id': 'id', 'provider:network_type': 'flat'}) + self.assertEqual('name', network.name) + self.assertEqual('id', network.id) + self.assertEqual('flat', network.type) + + def test_construction_named(self): + network = Network(id='id', name='name') + self.assertEqual('name', network.name) + self.assertEqual('id', network.id) + self.assertIsNone(network.type) + + +class SubnetObjectTests(unittest.TestCase): + """ + Tests the construction of the snaps.domain.network.Subnet class + """ + + def test_construction_kwargs(self): + subnet = Subnet( + **{'name': 'name', 'id': 'id', 'cidr': '10.0.0.0/24'}) + self.assertEqual('name', subnet.name) + self.assertEqual('id', subnet.id) + self.assertEqual('10.0.0.0/24', subnet.cidr) + + def test_construction_named(self): + subnet = Subnet(cidr='10.0.0.0/24', id='id', name='name') + self.assertEqual('name', subnet.name) + self.assertEqual('id', subnet.id) + self.assertEqual('10.0.0.0/24', subnet.cidr) class PortDomainObjectTests(unittest.TestCase): @@ -45,27 +84,27 @@ class RouterDomainObjectTests(unittest.TestCase): """ def test_construction_kwargs(self): - sec_grp = Router( + router = Router( **{'name': 'name', 'id': 'id', 'status': 'hello', 'tenant_id': '1234', 'admin_state_up': 'yes', 'external_gateway_info': 'no'}) - self.assertEqual('name', sec_grp.name) - self.assertEqual('id', sec_grp.id) - self.assertEqual('hello', sec_grp.status) - self.assertEqual('1234', sec_grp.tenant_id) - self.assertEqual('yes', sec_grp.admin_state_up) - self.assertEqual('no', sec_grp.external_gateway_info) + self.assertEqual('name', router.name) + self.assertEqual('id', router.id) + self.assertEqual('hello', router.status) + self.assertEqual('1234', router.tenant_id) + self.assertEqual('yes', router.admin_state_up) + self.assertEqual('no', router.external_gateway_info) def test_construction_named(self): - sec_grp = Router( + router = Router( external_gateway_info='no', admin_state_up='yes', tenant_id='1234', status='hello', id='id', name='name') - self.assertEqual('name', sec_grp.name) - self.assertEqual('id', sec_grp.id) - self.assertEqual('hello', sec_grp.status) - self.assertEqual('1234', sec_grp.tenant_id) - self.assertEqual('yes', sec_grp.admin_state_up) - self.assertEqual('no', sec_grp.external_gateway_info) + self.assertEqual('name', router.name) + self.assertEqual('id', router.id) + self.assertEqual('hello', router.status) + self.assertEqual('1234', router.tenant_id) + self.assertEqual('yes', router.admin_state_up) + self.assertEqual('no', router.external_gateway_info) class InterfaceRouterDomainObjectTests(unittest.TestCase): @@ -74,17 +113,17 @@ class InterfaceRouterDomainObjectTests(unittest.TestCase): """ def test_construction_kwargs(self): - sec_grp = InterfaceRouter( + intf_router = InterfaceRouter( **{'id': 'id', 'subnet_id': 'foo', 'port_id': 'bar'}) - self.assertEqual('id', sec_grp.id) - self.assertEqual('foo', sec_grp.subnet_id) - self.assertEqual('bar', sec_grp.port_id) + self.assertEqual('id', intf_router.id) + self.assertEqual('foo', intf_router.subnet_id) + self.assertEqual('bar', intf_router.port_id) def test_construction_named(self): - sec_grp = InterfaceRouter(port_id='bar', subnet_id='foo', id='id') - self.assertEqual('id', sec_grp.id) - self.assertEqual('foo', sec_grp.subnet_id) - self.assertEqual('bar', sec_grp.port_id) + intf_router = InterfaceRouter(port_id='bar', subnet_id='foo', id='id') + self.assertEqual('id', intf_router.id) + self.assertEqual('foo', intf_router.subnet_id) + self.assertEqual('bar', intf_router.port_id) class SecurityGroupDomainObjectTests(unittest.TestCase): diff --git a/snaps/openstack/create_instance.py b/snaps/openstack/create_instance.py index 08e90c1..9aaf127 100644 --- a/snaps/openstack/create_instance.py +++ b/snaps/openstack/create_instance.py @@ -195,7 +195,7 @@ class OpenStackVmInstance: self.__neutron, router.external_gateway_info['network_id']) if network: - return network['network']['name'] + return network.name return None def clean(self): @@ -263,27 +263,16 @@ class OpenStackVmInstance: ports = list() for port_setting in port_settings: - # First check to see if network already has this port - # TODO/FIXME - this could potentially cause problems if another - # port with the same name exists - # VM has the same network/port name pair - found = False - - # TODO/FIXME - should we not be iterating on ports for the specific - # network in question as unique port names - # seem to only be important by network - existing_ports = self.__neutron.list_ports()['ports'] - for existing_port in existing_ports: - if existing_port['name'] == port_setting.name: - ports.append((port_setting.name, {'port': existing_port})) - found = True - break - - if not found and not cleanup: - ports.append((port_setting.name, - neutron_utils.create_port(self.__neutron, - self.__os_creds, - port_setting))) + port = neutron_utils.get_port_by_name(self.__neutron, + port_setting.name) + if port: + ports.append((port_setting.name, {'port': port})) + elif not cleanup: + # Exception will be raised when port with same name already + # exists + ports.append( + (port_setting.name, neutron_utils.create_port( + self.__neutron, self.__os_creds, port_setting))) return ports @@ -372,7 +361,7 @@ class OpenStackVmInstance: subnet_name) return None for fixed_ip in port.ips: - if fixed_ip['subnet_id'] == subnet['subnet']['id']: + if fixed_ip['subnet_id'] == subnet.id: return fixed_ip['ip_address'] else: if port.ips and len(port.ips) > 0: diff --git a/snaps/openstack/create_network.py b/snaps/openstack/create_network.py index 96fca77..ad91a52 100644 --- a/snaps/openstack/create_network.py +++ b/snaps/openstack/create_network.py @@ -67,25 +67,22 @@ class OpenStackNetwork: ' mode') return logger.debug( - "Network '%s' created successfully" % self.__network['network'][ - 'id']) + "Network '%s' created successfully" % self.__network.id) logger.debug('Creating Subnets....') for subnet_setting in self.network_settings.subnet_settings: - sub_inst = neutron_utils.get_subnet_by_name(self.__neutron, - subnet_setting.name) + sub_inst = neutron_utils.get_subnet_by_name( + self.__neutron, subnet_setting.name) if sub_inst: self.__subnets.append(sub_inst) logger.debug( - "Subnet '%s' created successfully" % sub_inst['subnet'][ - 'id']) + "Subnet '%s' created successfully" % sub_inst.id) else: if not cleanup: self.__subnets.append( - neutron_utils.create_subnet(self.__neutron, - subnet_setting, - self.__os_creds, - self.__network)) + neutron_utils.create_subnet( + self.__neutron, subnet_setting, self.__os_creds, + self.__network)) return self.__network @@ -96,7 +93,7 @@ class OpenStackNetwork: for subnet in self.__subnets: try: logger.info( - 'Deleting subnet with name ' + subnet['subnet']['name']) + 'Deleting subnet with name ' + subnet.name) neutron_utils.delete_subnet(self.__neutron, subnet) except NotFound as e: logger.warning( @@ -335,7 +332,7 @@ class SubnetSettings: } if network: - out['network_id'] = network['network']['id'] + out['network_id'] = network.id if self.name: out['name'] = self.name if self.project_name: @@ -457,8 +454,7 @@ class PortSettings: 'subnet_name']) if subnet: self.fixed_ips.append({'ip_address': ip_addr_dict['ip'], - 'subnet_id': subnet['subnet'][ - 'id']}) + 'subnet_id': subnet.id}) else: raise Exception( 'Invalid port configuration, subnet does not exist ' @@ -494,7 +490,7 @@ class PortSettings: raise Exception( 'Cannot locate network with name - ' + self.network_name) - out['network_id'] = self.network['network']['id'] + out['network_id'] = self.network.id if self.admin_state_up is not None: out['admin_state_up'] = self.admin_state_up diff --git a/snaps/openstack/create_router.py b/snaps/openstack/create_router.py index 89c3431..db6ffe3 100644 --- a/snaps/openstack/create_router.py +++ b/snaps/openstack/create_router.py @@ -131,7 +131,7 @@ class OpenStackRouter: for internal_subnet in self.__internal_subnets: logger.info( 'Removing router interface from router %s and subnet %s', - self.router_settings.name, internal_subnet['subnet']['name']) + self.router_settings.name, internal_subnet.name) try: neutron_utils.remove_interface_router(self.__neutron, self.__router, @@ -248,7 +248,7 @@ class RouterSettings: ext_net = neutron_utils.get_network(neutron, self.external_gateway, project_id) if ext_net: - ext_gw['network_id'] = ext_net['network']['id'] + ext_gw['network_id'] = ext_net.id out['external_gateway_info'] = ext_gw else: raise Exception( diff --git a/snaps/openstack/tests/create_network_tests.py b/snaps/openstack/tests/create_network_tests.py index 1bb1369..e22f8bf 100644 --- a/snaps/openstack/tests/create_network_tests.py +++ b/snaps/openstack/tests/create_network_tests.py @@ -458,8 +458,8 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): self.net_config.network_settings) self.net_creator2.create() - self.assertEqual(self.net_creator.get_network()['network']['id'], - self.net_creator2.get_network()['network']['id']) + self.assertEqual(self.net_creator.get_network().id, + self.net_creator2.get_network().id) class CreateNetworkTypeTests(OSComponentTestCase): @@ -521,8 +521,7 @@ class CreateNetworkTypeTests(OSComponentTestCase): neutron_utils_tests.validate_network( self.neutron, net_settings.name, True) - self.assertEquals( - network_type, network['network']['provider:network_type']) + self.assertEquals(network_type, network.type) def test_create_network_type_vxlan(self): """ @@ -543,8 +542,7 @@ class CreateNetworkTypeTests(OSComponentTestCase): neutron_utils_tests.validate_network(self.neutron, net_settings.name, True) - self.assertEqual(network_type, - network['network']['provider:network_type']) + self.assertEqual(network_type, network.type) def test_create_network_type_flat(self): """ @@ -566,8 +564,7 @@ class CreateNetworkTypeTests(OSComponentTestCase): neutron_utils_tests.validate_network( self.neutron, net_settings.name, True) - self.assertEquals(network_type, - network['network']['provider:network_type']) + self.assertEquals(network_type, network.type) def test_create_network_type_foo(self): """ diff --git a/snaps/openstack/utils/neutron_utils.py b/snaps/openstack/utils/neutron_utils.py index aaa0903..dfae98b 100644 --- a/snaps/openstack/utils/neutron_utils.py +++ b/snaps/openstack/utils/neutron_utils.py @@ -18,7 +18,8 @@ from neutronclient.common.exceptions import NotFound from neutronclient.neutron.client import Client from snaps.domain.network import ( - Port, SecurityGroup, SecurityGroupRule, Router, InterfaceRouter) + Port, SecurityGroup, SecurityGroupRule, Router, InterfaceRouter, Subnet, + Network) from snaps.domain.vm_inst import FloatingIp from snaps.openstack.utils import keystone_utils @@ -50,12 +51,13 @@ def create_network(neutron, os_creds, network_settings): :param network_settings: A dictionary containing the network configuration and is responsible for creating the network request JSON body - :return: the network object + :return: a SNAPS-OO Network domain object """ if neutron and network_settings: logger.info('Creating network with name ' + network_settings.name) json_body = network_settings.dict_for_neutron(os_creds) - return neutron.create_network(body=json_body) + os_network = neutron.create_network(body=json_body) + return Network(**os_network['network']) else: logger.error("Failed to create network") raise Exception @@ -65,11 +67,11 @@ def delete_network(neutron, network): """ Deletes a network for OpenStack :param neutron: the client - :param network: the network object + :param network: a SNAPS-OO Network domain object """ if neutron and network: - logger.info('Deleting network with name ' + network['network']['name']) - neutron.delete_network(network['network']['id']) + logger.info('Deleting network with name ' + network.name) + neutron.delete_network(network.id) def get_network(neutron, network_name, project_id=None): @@ -79,7 +81,7 @@ def get_network(neutron, network_name, project_id=None): :param neutron: the client :param network_name: the name of the network to retrieve :param project_id: the id of the network's project - :return: + :return: a SNAPS-OO Network domain object """ net_filter = dict() if network_name: @@ -94,7 +96,7 @@ def get_network(neutron, network_name, project_id=None): if project_id and inst.get('project_id') == project_id: return {'network': inst} else: - return {'network': inst} + return Network(**inst) return None @@ -103,13 +105,13 @@ def get_network_by_id(neutron, network_id): Returns the network object (dictionary) with the given ID :param neutron: the client :param network_id: the id of the network to retrieve - :return: + :return: a SNAPS-OO Network domain object """ networks = neutron.list_networks(**{'id': network_id}) for network, netInsts in networks.items(): for inst in netInsts: if inst.get('id') == network_id: - return {'network': inst} + return Network(**inst) return None @@ -122,14 +124,14 @@ def create_subnet(neutron, subnet_settings, os_creds, network=None): and is responsible for creating the subnet request JSON body :param os_creds: the OpenStack credentials - :return: the subnet object + :return: a SNAPS-OO Subnet domain object """ if neutron and network and subnet_settings: json_body = {'subnets': [subnet_settings.dict_for_neutron( os_creds, network=network)]} logger.info('Creating subnet with name ' + subnet_settings.name) subnets = neutron.create_subnet(body=json_body) - return {'subnet': subnets['subnets'][0]} + return Subnet(**subnets['subnets'][0]) else: logger.error("Failed to create subnet.") raise Exception @@ -139,11 +141,11 @@ def delete_subnet(neutron, subnet): """ Deletes a network subnet for OpenStack :param neutron: the client - :param subnet: the subnet object + :param subnet: a SNAPS-OO Subnet domain object """ if neutron and subnet: - logger.info('Deleting subnet with name ' + subnet['subnet']['name']) - neutron.delete_subnet(subnet['subnet']['id']) + logger.info('Deleting subnet with name ' + subnet.name) + neutron.delete_subnet(subnet.id) def get_subnet_by_name(neutron, subnet_name): @@ -151,13 +153,13 @@ def get_subnet_by_name(neutron, subnet_name): Returns the first subnet object (dictionary) found with a given name :param neutron: the client :param subnet_name: the name of the network to retrieve - :return: + :return: a SNAPS-OO Subnet domain object """ subnets = neutron.list_subnets(**{'name': subnet_name}) for subnet, subnetInst in subnets.items(): for inst in subnetInst: - if inst.get('name') == subnet_name: - return {'subnet': inst} + if inst['name'] == subnet_name: + return Subnet(**inst) return None @@ -269,7 +271,7 @@ def __create_port_json_body(subnet=None, port=None): raise Exception('Cannot create JSON body without subnet or port') if subnet: - return {"subnet_id": subnet['subnet']['id']} + return {"subnet_id": subnet.id} else: return {"port_id": port.id} @@ -480,7 +482,7 @@ def create_floating_ip(neutron, ext_net_name): if ext_net: fip = neutron.create_floatingip( body={'floatingip': - {'floating_network_id': ext_net['network']['id']}}) + {'floating_network_id': ext_net.id}}) return FloatingIp(inst_id=fip['floatingip']['id'], ip=fip['floatingip']['floating_ip_address']) @@ -499,13 +501,13 @@ def get_floating_ip(neutron, floating_ip): """ logger.debug('Attempting to retrieve existing floating ip with IP - %s', floating_ip.ip) - os_fip = get_os_floating_ip(neutron, floating_ip) + os_fip = __get_os_floating_ip(neutron, floating_ip) if os_fip: return FloatingIp( inst_id=os_fip['id'], ip=os_fip['floating_ip_address']) -def get_os_floating_ip(neutron, floating_ip): +def __get_os_floating_ip(neutron, floating_ip): """ Returns an OpenStack floating IP object parameter diff --git a/snaps/openstack/utils/tests/neutron_utils_tests.py b/snaps/openstack/utils/tests/neutron_utils_tests.py index f3cacd5..3c4a24f 100644 --- a/snaps/openstack/utils/tests/neutron_utils_tests.py +++ b/snaps/openstack/utils/tests/neutron_utils_tests.py @@ -98,8 +98,7 @@ class NeutronUtilsNetworkTests(OSComponentTestCase): """ if self.network: neutron_utils.delete_network(self.neutron, self.network) - validate_network(self.neutron, self.network['network']['name'], - False) + validate_network(self.neutron, self.network.name, False) def test_create_network(self): """ @@ -108,10 +107,9 @@ class NeutronUtilsNetworkTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) def test_create_network_empty_name(self): """ @@ -155,14 +153,13 @@ class NeutronUtilsSubnetTests(OSComponentTestCase): """ if self.subnet: neutron_utils.delete_subnet(self.neutron, self.subnet) - validate_subnet(self.neutron, self.subnet.get('name'), + validate_subnet(self.neutron, self.subnet.name, self.net_config.network_settings.subnet_settings[ 0].cidr, False) if self.network: neutron_utils.delete_network(self.neutron, self.network) - validate_network(self.neutron, self.network['network']['name'], - False) + validate_network(self.neutron, self.network.name, False) def test_create_subnet(self): """ @@ -171,19 +168,15 @@ class NeutronUtilsSubnetTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( - self.neutron, subnet_setting, - self.os_creds, network=self.network) + self.neutron, subnet_setting, self.os_creds, network=self.network) validate_subnet( - self.neutron, - subnet_setting.name, - subnet_setting.cidr, True) + self.neutron, subnet_setting.name, subnet_setting.cidr, True) def test_create_subnet_null_name(self): """ @@ -193,10 +186,9 @@ class NeutronUtilsSubnetTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) with self.assertRaises(Exception): SubnetSettings(cidr=self.net_config.subnet_cidr) @@ -209,17 +201,14 @@ class NeutronUtilsSubnetTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] neutron_utils.create_subnet( - self.neutron, subnet_setting, - self.os_creds, network=self.network) - validate_subnet(self.neutron, '', - subnet_setting.cidr, True) + self.neutron, subnet_setting, self.os_creds, network=self.network) + validate_subnet(self.neutron, '', subnet_setting.cidr, True) def test_create_subnet_null_cidr(self): """ @@ -229,16 +218,15 @@ class NeutronUtilsSubnetTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) with self.assertRaises(Exception): - sub_sets = SubnetSettings(cidr=None, - name=self.net_config.subnet_name) - neutron_utils.create_subnet(self.neutron, sub_sets, self.os_creds, - network=self.network) + sub_sets = SubnetSettings( + cidr=None, name=self.net_config.subnet_name) + neutron_utils.create_subnet( + self.neutron, sub_sets, self.os_creds, network=self.network) def test_create_subnet_empty_cidr(self): """ @@ -248,14 +236,13 @@ class NeutronUtilsSubnetTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) with self.assertRaises(Exception): - sub_sets = SubnetSettings(cidr='', - name=self.net_config.subnet_name) + sub_sets = SubnetSettings( + cidr='', name=self.net_config.subnet_name) neutron_utils.create_subnet(self.neutron, sub_sets, self.os_creds, network=self.network) @@ -295,14 +282,14 @@ class NeutronUtilsRouterTests(OSComponentTestCase): if self.subnet: neutron_utils.delete_subnet(self.neutron, self.subnet) - validate_subnet(self.neutron, self.subnet.get('name'), - self.net_config.network_settings.subnet_settings[ - 0].cidr, False) + validate_subnet( + self.neutron, self.subnet.name, + self.net_config.network_settings.subnet_settings[0].cidr, + False) if self.network: neutron_utils.delete_network(self.neutron, self.network) - validate_network(self.neutron, self.network['network']['name'], - False) + validate_network(self.neutron, self.network.name, False) def test_create_router_simple(self): """ @@ -361,10 +348,9 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( @@ -393,19 +379,16 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( self.neutron, subnet_setting, self.os_creds, self.network) validate_subnet( - self.neutron, - subnet_setting.name, - subnet_setting.cidr, True) + self.neutron, subnet_setting.name, subnet_setting.cidr, True) with self.assertRaises(Exception): self.interface_router = neutron_utils.add_interface_router( @@ -419,10 +402,9 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) self.router = neutron_utils.create_router( self.neutron, self.os_creds, self.net_config.router_settings) @@ -440,10 +422,9 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( @@ -467,10 +448,9 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( @@ -495,10 +475,9 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( @@ -543,10 +522,9 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( @@ -575,16 +553,13 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( - self.neutron, - subnet_setting, - self.os_creds, self.network) + self.neutron, subnet_setting, self.os_creds, self.network) validate_subnet(self.neutron, subnet_setting.name, subnet_setting.cidr, True) @@ -607,19 +582,15 @@ class NeutronUtilsRouterTests(OSComponentTestCase): self.network = neutron_utils.create_network( self.neutron, self.os_creds, self.net_config.network_settings) self.assertEqual(self.net_config.network_settings.name, - self.network['network']['name']) - self.assertTrue(validate_network(self.neutron, - self.net_config.network_settings.name, - True)) + self.network.name) + self.assertTrue(validate_network( + self.neutron, self.net_config.network_settings.name, True)) subnet_setting = self.net_config.network_settings.subnet_settings[0] self.subnet = neutron_utils.create_subnet( - self.neutron, - subnet_setting, - self.os_creds, self.network) - validate_subnet(self.neutron, - subnet_setting.name, - subnet_setting.cidr, True) + self.neutron, subnet_setting, self.os_creds, self.network) + validate_subnet( + self.neutron, subnet_setting.name, subnet_setting.cidr, True) with self.assertRaises(Exception): self.port = neutron_utils.create_port( @@ -851,7 +822,7 @@ def validate_subnet(neutron, name, cidr, exists): """ subnet = neutron_utils.get_subnet_by_name(neutron, name) if exists and subnet: - return subnet.get('cidr') == cidr + return subnet.cidr == cidr if not exists and not subnet: return True return False @@ -885,7 +856,7 @@ def validate_interface_router(interface_router, router, subnet): subnet_id = interface_router.subnet_id router_id = interface_router.port_id - return subnet.get('id') == subnet_id and router.get('id') == router_id + return subnet.id == subnet_id and router.id == router_id def validate_port(neutron, port_obj, this_port_name): @@ -898,9 +869,9 @@ def validate_port(neutron, port_obj, this_port_name): :param this_port_name: The expected router name :return: True/False """ - ports = neutron.list_ports() - for port, port_insts in ports.items(): - for inst in port_insts: - if inst['id'] == port_obj.id: - return inst['name'] == this_port_name + os_ports = neutron.list_ports() + for os_port, os_port_insts in os_ports.items(): + for os_inst in os_port_insts: + if os_inst['id'] == port_obj.id: + return os_inst['name'] == this_port_name return False diff --git a/snaps/test_suite_builder.py b/snaps/test_suite_builder.py index ec9d17b..79e974c 100644 --- a/snaps/test_suite_builder.py +++ b/snaps/test_suite_builder.py @@ -22,7 +22,7 @@ from snaps.domain.test.keypair_tests import KeypairDomainObjectTests from snaps.domain.test.network_tests import ( SecurityGroupDomainObjectTests, SecurityGroupRuleDomainObjectTests, PortDomainObjectTests, RouterDomainObjectTests, - InterfaceRouterDomainObjectTests) + InterfaceRouterDomainObjectTests, NetworkObjectTests, SubnetObjectTests) from snaps.domain.test.project_tests import ProjectDomainObjectTests from snaps.domain.test.role_tests import RoleDomainObjectTests from snaps.domain.test.stack_tests import StackDomainObjectTests @@ -126,8 +126,12 @@ def add_unit_tests(suite): RoleDomainObjectTests)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( NetworkSettingsUnitTests)) + suite.addTest(unittest.TestLoader().loadTestsFromTestCase( + NetworkObjectTests)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( SubnetSettingsUnitTests)) + suite.addTest(unittest.TestLoader().loadTestsFromTestCase( + SubnetObjectTests)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( PortSettingsUnitTests)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase(