def test_process_router_2_rids_1_vrf(self):
        driver = self._mock_driver_and_hosting_device(self.routing_helper)
        router1, ports = self.prepare_router_data()
        ri1 = svc_helper.RouterInfo(router1['id'], router=router1)

        # Router #2 is like #1, except with some different IDs
        router2 = copy.deepcopy(router1)
        router2['id'] = _uuid()
        ri2 = svc_helper.RouterInfo(router2['id'], router=router2)
        h_info1 = create_hosting_info()
        h_info2 = copy.deepcopy(h_info1)
        ri1.router['hosting_info'] = h_info1
        ri2.router['hosting_info'] = h_info2

        driver._get_vrf_name = mock.Mock(
            return_value=ri1.router['hosting_info']['vrf_id'])
        self.routing_helper._process_router(ri1)

        vrf = ri1.router['hosting_info']['vrf_id']
        driver._get_vrf_name.assert_called_with(ri1)
        driver._do_create_vrf.assert_called_with(vrf)
        self.assertEqual(1, len(self.routing_helper._router_ids_by_vrf))
        self.assertEqual(1, len(self.routing_helper._router_ids_by_vrf[vrf]))

        driver._get_vrf_name.reset_mock()
        driver._do_create_vrf.reset_mock()

        self.routing_helper._process_router(ri2)

        driver._get_vrf_name.assert_called_with(ri2)
        driver._do_create_vrf.assert_not_called()
        self.assertEqual(1, len(self.routing_helper._router_ids_by_vrf))
        self.assertEqual(2, len(self.routing_helper._router_ids_by_vrf[vrf]))

        del ri1.router['gw_port']
        driver._get_vrf_name.reset_mock()

        self.routing_helper._process_router(ri1)
        driver._get_vrf_name.assert_called_with(ri1)
        driver._remove_vrf.assert_not_called()
        self.assertEqual(1, len(self.routing_helper._router_ids_by_vrf))
        self.assertEqual(1, len(self.routing_helper._router_ids_by_vrf[vrf]))

        del ri2.router['gw_port']
        driver._get_vrf_name.reset_mock()
        driver._remove_vrf.reset_mock()
        self.routing_helper._process_router(ri2)
        driver._get_vrf_name.assert_called_with(ri2)
        driver._remove_vrf.assert_called_with(ri2)
        self.assertEqual({}, self.routing_helper._router_ids_by_vrf)
 def test_process_router_throw_no_ip_address_on_subnet_error(self):
     router, ports = prepare_router_data()
     # change subnet_id to something that does not exist
     router['gw_port']['subnets'][0]['id'] = 'fake_uuid'
     ri = routing_svc_helper.RouterInfo(router['id'], router)
     self.assertRaises(routing_svc_helper.IPAddressMissingException,
                       self.routing_helper._process_router, ri)
Exemple #3
0
    def test_process_router_internal_network_removed_unexpected_error(self):
        router, ports = prepare_router_data()
        ri = routing_svc_helper.RouterInfo(router['id'], router=router)
        # add an internal port
        self.routing_helper._process_router(ri)

        # raise RuntimeError to simulate that an unexpected exception occurrs

        self.routing_helper._internal_network_removed.side_effect = mock.Mock(
            side_effect=RuntimeError)
        router[l3_constants.INTERFACE_KEY][0]['admin_state_up'] = False
        # The above port is set to down state, remove it.
        self.assertRaises(RuntimeError, self.routing_helper._process_router,
                          ri)
        self.assertIn(router[l3_constants.INTERFACE_KEY][0], ri.internal_ports)

        # The unexpected exception has been fixed manually
        self.routing_helper._internal_network_removed.side_effect = None

        # Failure will cause a retry next time,
        # We were able to add the port to ri.internal_ports
        self.routing_helper._process_router(ri)
        # We were able to remove the port from ri.internal_ports
        self.assertNotIn(router[l3_constants.INTERFACE_KEY][0],
                         ri.internal_ports)
Exemple #4
0
 def test_router_info_create_with_router(self):
     router_id = _uuid()
     ri = routing_svc_helper.RouterInfo(router_id, self.router)
     self.assertTrue(ri.router_name().endswith(router_id))
     self.assertEqual(ri.router, self.router)
     self.assertEqual(ri._router, self.router)
     self.assertTrue(ri.snat_enabled)
     self.assertIsNone(ri.ex_gw_port)
    def test_process_router_throw_session_close(self):
        class SessionCloseError(Exception):
            pass

        self.routing_helper._internal_network_added.side_effect = (
            SessionCloseError("Simulate SessionCloseError"))
        router, ports = prepare_router_data()
        ri = routing_svc_helper.RouterInfo(router['id'], router)
        self.assertRaises(SessionCloseError,
                          self.routing_helper._process_router, ri)
Exemple #6
0
 def test_process_router_throw_config_error(self):
     snip_name = 'CREATE_SUBINTERFACE'
     e_type = 'Fake error'
     e_tag = 'Fake error tag'
     params = {'snippet': snip_name, 'type': e_type, 'tag': e_tag}
     self.routing_helper._internal_network_added.side_effect = (
         cfg_exceptions.CSR1kvConfigException(**params))
     router, ports = prepare_router_data()
     ri = routing_svc_helper.RouterInfo(router['id'], router)
     self.assertRaises(cfg_exceptions.CSR1kvConfigException,
                       self.routing_helper._process_router, ri)
    def setUp(self):
        super(TestCSR1kvRouting, self).setUp()

        device_params = {'management_ip_address': 'fake_ip',
                         'protocol_port': 22,
                         'credentials': {"username": "******",
                                         "password": "******"},
                         'timeout': None
                         }
        self.driver = csr_driver.CSR1kvRoutingDriver(
            **device_params)
        self.mock_conn = mock.MagicMock()
        self.driver._csr_conn = self.mock_conn
        self.driver._check_response = mock.MagicMock(return_value=True)

        self.vrf = ('nrouter-' + FAKE_ID)[:csr_driver.CSR1kvRoutingDriver.
                                          DEV_NAME_LEN]
        self.driver._get_vrfs = mock.Mock(return_value=[self.vrf])
        self.ex_gw_ip = '20.0.0.30'
        self.ex_gw_cidr = '20.0.0.30/24'
        self.ex_gw_vlan = 1000
        self.ex_gw_gateway_ip = '20.0.0.1'
        self.ex_gw_port = {'id': _uuid(),
                           'network_id': _uuid(),
                           'fixed_ips': [{'ip_address': self.ex_gw_ip,
                                          'subnet_id': _uuid()}],
                           'subnets': [{'cidr': self.ex_gw_cidr,
                                      'gateway_ip': self.ex_gw_gateway_ip}],
                           'ip_cidr': self.ex_gw_cidr,
                           'mac_address': 'ca:fe:de:ad:be:ef',
                           'hosting_info': {'segmentation_id': self.ex_gw_vlan,
                                          'hosting_port_name': 't2_p:0'}}
        self.vlan_no = 500
        self.gw_ip_cidr = '10.0.0.1/16'
        self.gw_ip = '10.0.0.1'
        self.hosting_port = 't1_p:0'
        self.port = {'id': PORT_ID,
                     'ip_cidr': self.gw_ip_cidr,
                     'fixed_ips': [{'ip_address': self.gw_ip}],
                     'hosting_info': {'segmentation_id': self.vlan_no,
                                      'hosting_port_name': self.hosting_port}}
        int_ports = [self.port]

        self.router = {
            'id': FAKE_ID,
            l3_constants.INTERFACE_KEY: int_ports,
            'enable_snat': True,
            'routes': [],
            'gw_port': self.ex_gw_port}

        self.ri = routing_svc_helper.RouterInfo(FAKE_ID, self.router)
        self.ri.internal_ports = int_ports
Exemple #8
0
    def _create_test_routers(self, is_user_visible=True):
        self.router, ports = self.prepare_router_data(
            is_user_visible=is_user_visible)
        self.ri = routing_svc_helper.RouterInfo(self.router['id'],
                                                self.router)
        self.ha_priority = self.router[ha.DETAILS][ha.PRIORITY]
        self.vrf = ('nrouter-' + self.router['id'])[:DEV_NAME_LEN]

        # router port on external network, i.e., gateway port
        self.ext_gw_port = self.router['gw_port']
        self.ext_gw_port['ip_info'] = {
            'subnet_id': self.ext_gw_port['subnets'][0]['id'],
            'is_primary': True,
            'ip_cidr': self.ext_gw_port['subnets'][0]['cidr']
        }
        self.ext_phy_infc = (
            self.ext_gw_port['hosting_info']['physical_interface'])
        self.vlan_ext = self.ext_gw_port['hosting_info']['segmentation_id']
        self.ext_gw_upstream_ip = self.ext_gw_port['subnets'][0]['gateway_ip']
        self.ext_gw_ip = self.ext_gw_port['fixed_ips'][0]['ip_address']
        self.ext_gw_ip_cidr = self.ext_gw_port['subnets'][0]['cidr']
        self.ext_gw_ip_mask = str(
            netaddr.IPNetwork(self.ext_gw_ip_cidr).netmask)
        port_ha_info = self.ext_gw_port['ha_info']
        self.ext_gw_ha_group = port_ha_info['group']

        # router port on internal network
        self.int_port = ports[0]
        self.int_port['ip_info'] = {
            'subnet_id': self.int_port['subnets'][0]['id'],
            'is_primary': True,
            'ip_cidr': self.int_port['subnets'][0]['cidr']
        }
        self.int_port['change_details'] = {
            'new_ports': [self.int_port],
            'current_ports': [self.int_port],
            'old_ports': [],
            'former_ports': []
        }
        self.int_phy_infc = self.int_port['hosting_info']['physical_interface']
        self.vlan_int = self.int_port['hosting_info']['segmentation_id']
        self.int_gw_ip = self.int_port['fixed_ips'][0]['ip_address']
        self.int_gw_ip_cidr = self.int_port['subnets'][0]['cidr']
        self.int_gw_ip_mask = str(
            netaddr.IPNetwork(self.int_gw_ip_cidr).netmask)
        port_ha_info = self.int_port['ha_info']
        self.int_gw_ip_vip = (
            port_ha_info['ha_port']['fixed_ips'][0]['ip_address'])
        self.int_gw_ha_group = port_ha_info['group']
        self.floating_ip = '19.4.0.6'
        self.fixed_ip = '35.4.0.20'
Exemple #9
0
 def test_process_router_throw_session_close(self):
     # snip_name = 'CREATE_SUBINTERFACE'
     # e_type = 'Fake error'
     # e_tag = 'Fake error tag'
     # confstr = 'Fake conf str'
     # params = {'snippet': snip_name, 'type': e_type, 'tag': e_tag,
     #          'confstr': confstr, 'dev_id': 'FAKE_ID', 'ip': 'FAKE_IP'}
     params = {'in_buf': ''}
     self.routing_helper._internal_network_added.side_effect = (
         ncc_errors.SessionCloseError(**params))
     router, ports = prepare_router_data()
     ri = routing_svc_helper.RouterInfo(router['id'], router)
     self.assertRaises(ncc_errors.SessionCloseError,
                       self.routing_helper._process_router, ri)
Exemple #10
0
    def test_routing_table_update(self):
        router = self.router
        fake_route1 = {'destination': '135.207.0.0/16', 'nexthop': '1.2.3.4'}
        fake_route2 = {
            'destination': '135.207.111.111/32',
            'nexthop': '1.2.3.4'
        }

        # First we set the routes to fake_route1 and see if the
        # driver.routes_updated was called with 'replace'(==add or replace)
        # and fake_route1
        router['routes'] = [fake_route1]
        ri = routing_svc_helper.RouterInfo(router['id'], router)
        self.routing_helper._process_router(ri)

        self.driver.routes_updated.assert_called_with(ri, 'replace',
                                                      fake_route1)

        # Now we replace fake_route1 with fake_route2. This should cause driver
        # to be invoked to delete fake_route1 and 'replace'(==add or replace)
        self.driver.reset_mock()
        router['routes'] = [fake_route2]
        ri.router = router
        self.routing_helper._process_router(ri)

        self.driver.routes_updated.assert_called_with(ri, 'delete',
                                                      fake_route1)
        self.driver.routes_updated.assert_any_call(ri, 'replace', fake_route2)

        # Now we add back fake_route1 as a new route, this should cause driver
        # to be invoked to 'replace'(==add or replace) fake_route1
        self.driver.reset_mock()
        router['routes'] = [fake_route2, fake_route1]
        ri.router = router
        self.routing_helper._process_router(ri)

        self.driver.routes_updated.assert_any_call(ri, 'replace', fake_route1)

        # Now we delete all routes. This should cause driver
        # to be invoked to delete fake_route1 and fake-route2
        self.driver.reset_mock()
        router['routes'] = []
        ri.router = router
        self.routing_helper._process_router(ri)

        self.driver.routes_updated.assert_any_call(ri, 'delete', fake_route2)
        self.driver.routes_updated.assert_any_call(ri, 'delete', fake_route1)
Exemple #11
0
    def test_process_router_internal_network_added_unexpected_error(self):
        router, ports = prepare_router_data()
        ri = routing_svc_helper.RouterInfo(router['id'], router=router)
        # raise RuntimeError to simulate that an unexpected exception occurrs
        self.routing_helper._internal_network_added.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.routing_helper._process_router,
                          ri)
        self.assertNotIn(router[l3_constants.INTERFACE_KEY][0],
                         ri.internal_ports)

        # The unexpected exception has been fixed manually
        self.routing_helper._internal_network_added.side_effect = None

        # Failure will cause a retry next time, then were able to add the
        # port to ri.internal_ports
        self.routing_helper._process_router(ri)
        self.assertIn(router[l3_constants.INTERFACE_KEY][0], ri.internal_ports)
    def _test_process_global_msn_router(self, num_ext_subnets=3):
        def _verify_ip_info(the_mock):
            self.assertEqual(num_ext_subnets, len(the_mock.ip_infos))
            self.assertTrue(the_mock.ip_infos[0]['is_primary'])
            self.assertEqual('35.4.0.4/24', the_mock.ip_infos[0]['ip_cidr'])
            for i in range(1, num_ext_subnets):
                self.assertFalse(the_mock.ip_infos[i]['is_primary'])
                self.assertEqual('35.4.%s.4/24' % i,
                                 the_mock.ip_infos[i]['ip_cidr'])

        # need these helpers to verify that ip_info is correct
        def _int_nw_added(ri, port, ex_gw_port):
            self.assertIsNone(ex_gw_port)
            self.routing_helper._internal_network_added.ip_infos.append(
                copy.deepcopy(port['ip_info']))

        router, ports = prepare_router_data(num_subnets=num_ext_subnets,
                                            is_global=True)
        # Setup mock for call to process floating ips
        self.routing_helper._process_router_floating_ips = mock.Mock()
        ri = routing_svc_helper.RouterInfo(router['id'], router=router)
        self.routing_helper._internal_network_added.ip_infos = []
        self.routing_helper._internal_network_added.side_effect = _int_nw_added
        # Process with initial values
        self.routing_helper._process_router(ri)
        self.assertFalse(
            self.routing_helper._process_router_floating_ips.called)
        self.assertFalse(self.routing_helper._external_gateway_added.called)
        _verify_ip_info(self.routing_helper._internal_network_added)
        self._reset_mocks()

        # now no ports so state is torn down
        del router[bc.constants.INTERFACE_KEY]
        # Update router_info object
        ri.router = router

        # Process router and verify that internal and external network removed
        # were called and floating_ips_process was called
        self.routing_helper._process_router(ri)
        self.assertFalse(
            self.routing_helper._process_router_floating_ips.called)
        self.assertFalse(self.routing_helper._external_gateway_added.called)
        self.assertFalse(self.routing_helper._external_gateway_removed.called)
        self.assertFalse(self.routing_helper._internal_network_added.called)
        self.routing_helper._internal_network_removed.assert_called_once_with(
            ri, ports[0], None)
Exemple #13
0
    def test_process_router_internal_network_added_raises_HAMissingError(self):
        router, ports = prepare_router_data()
        router[ha.ENABLED] = True
        ri = routing_svc_helper.RouterInfo(router['id'], router=router)
        # raise RuntimeError to simulate that a HAParamsMissingException
        params = {'r_id': FAKE_ID, 'p_id': FAKE_ID, 'port': ports[0]}
        self.routing_helper._internal_network_added.side_effect = (
            cfg_exceptions.HAParamsMissingException(**params))
        self.routing_helper._process_router(ri)
        self.assertIn(ri.router_id, self.routing_helper.updated_routers)
        self.assertNotIn(router[l3_constants.INTERFACE_KEY][0],
                         ri.internal_ports)
        # The unexpected exception has been fixed manually
        self.routing_helper._internal_network_added.side_effect = None

        # Failure will cause a retry next time, then were able to add the
        # port to ri.internal_ports
        self.routing_helper._process_router(ri)
        self.assertIn(router[l3_constants.INTERFACE_KEY][0], ri.internal_ports)
Exemple #14
0
    def _process_routers_floatingips(self, action='add'):
        router, port = prepare_router_data()
        driver = self._mock_driver_and_hosting_device(self.routing_helper)
        ex_gw_port = router['gw_port']
        floating_ip_address = '19.4.4.10'
        fixed_ip_address = '35.4.1.10'
        fixed_ip_address_2 = '35.4.1.15'
        port_id = 'fake_port_id'
        floating_ip = {
            'fixed_ip_address': fixed_ip_address,
            'floating_ip_address': floating_ip_address,
            'id': 'floating_ip_id',
            'port_id': port_id,
            'status': 'ACTIVE',
        }
        router[l3_constants.FLOATINGIP_KEY] = [floating_ip]
        ri = routing_svc_helper.RouterInfo(router['id'], router=router)

        # Default add action
        self.routing_helper._process_router_floating_ips(ri, ex_gw_port)
        driver.floating_ip_added.assert_called_with(ri, ex_gw_port,
                                                    floating_ip_address,
                                                    fixed_ip_address)

        if action == 'remove':
            router[l3_constants.FLOATINGIP_KEY] = []
            self.routing_helper._process_router_floating_ips(ri, ex_gw_port)
            driver.floating_ip_removed.assert_called_with(
                ri, ri.ex_gw_port, floating_ip_address, fixed_ip_address)

        if action == 'remap':
            driver.reset_mock()
            floating_ip_2 = copy.deepcopy(floating_ip)
            floating_ip_2['fixed_ip_address'] = fixed_ip_address_2
            ri.router[l3_constants.FLOATINGIP_KEY] = [floating_ip_2]

            self.routing_helper._process_router_floating_ips(ri, ex_gw_port)
            driver.floating_ip_added.assert_called_with(
                ri, ex_gw_port, floating_ip_address, fixed_ip_address_2)

            driver.floating_ip_removed.assert_called_with(
                ri, ri.ex_gw_port, floating_ip_address, fixed_ip_address)
Exemple #15
0
 def _create_test_global_routers(self, num_ext_subnets=1, subnet_index=0):
     # global router and its ports
     self.global_router, gl_ports = self.prepare_router_data(
         is_global=True, num_ext_subnets=num_ext_subnets)
     self.ha_priority = self.global_router[ha.DETAILS][ha.PRIORITY]
     self.ri_global = routing_svc_helper.RouterInfo(
         self.global_router['id'], self.global_router)
     self.gl_port = gl_ports[0]
     self.gl_port['ip_info'] = {
         'subnet_id': self.gl_port['subnets'][0]['id'],
         'is_primary': True,
         'ip_cidr': self.gl_port['subnets'][0]['cidr']
     }
     self.ext_phy_infc = self.gl_port['hosting_info']['physical_interface']
     self.vlan_ext = self.gl_port['hosting_info']['segmentation_id']
     self.gl_port_ip = self.gl_port['fixed_ips'][subnet_index]['ip_address']
     self.gl_port_ip_cidr = self.gl_port['subnets'][subnet_index]['cidr']
     self.gl_port_ip_mask = str(
         netaddr.IPNetwork(self.gl_port_ip_cidr).netmask)
     port_ha_info = self.gl_port['ha_info']
     self.gl_port_vip = (
         port_ha_info['ha_port']['fixed_ips'][subnet_index]['ip_address'])
     self.gl_port_ha_group = port_ha_info['group']
 def _test_process_router_throw_multiple_ipv4_subnets_error(self):
     router, ports = prepare_router_data(num_subnets=2, is_global=True)
     # make router a regular one which triggers the error condition
     router[routerrole.ROUTER_ROLE_ATTR] = None
     ri = routing_svc_helper.RouterInfo(router['id'], router)
     return ri, router
Exemple #17
0
    def test_process_router(self):
        router, ports = prepare_router_data()
        #Setup mock for call to proceess floating ips
        self.routing_helper._process_router_floating_ips = mock.Mock()
        fake_floatingips1 = {
            'floatingips': [{
                'id': _uuid(),
                'floating_ip_address': '8.8.8.8',
                'fixed_ip_address': '7.7.7.7',
                'port_id': _uuid()
            }]
        }
        ri = routing_svc_helper.RouterInfo(router['id'], router=router)
        # Process with initial values
        self.routing_helper._process_router(ri)
        ex_gw_port = ri.router.get('gw_port')
        # Assert that process_floating_ips, internal_network & external network
        # added were all called with the right params
        self.routing_helper._process_router_floating_ips.assert_called_with(
            ri, ex_gw_port)
        self.routing_helper._internal_network_added.assert_called_with(
            ri, ports[0], ex_gw_port)
        self.routing_helper._external_gateway_added.assert_called_with(
            ri, ex_gw_port)
        self._reset_mocks()
        # remap floating IP to a new fixed ip
        fake_floatingips2 = copy.deepcopy(fake_floatingips1)
        fake_floatingips2['floatingips'][0]['fixed_ip_address'] = '7.7.7.8'
        router[l3_constants.FLOATINGIP_KEY] = fake_floatingips2['floatingips']

        # Process again and check that this time only the process_floating_ips
        # was only called.
        self.routing_helper._process_router(ri)
        ex_gw_port = ri.router.get('gw_port')
        self.routing_helper._process_router_floating_ips.assert_called_with(
            ri, ex_gw_port)
        self.assertFalse(self.routing_helper._internal_network_added.called)
        self.assertFalse(self.routing_helper._external_gateway_added.called)
        self._reset_mocks()
        # remove just the floating ips
        del router[l3_constants.FLOATINGIP_KEY]
        # Process again and check that this time also only the
        # process_floating_ips and external_network remove was called
        self.routing_helper._process_router(ri)
        ex_gw_port = ri.router.get('gw_port')
        self.routing_helper._process_router_floating_ips.assert_called_with(
            ri, ex_gw_port)
        self.assertFalse(self.routing_helper._internal_network_added.called)
        self.assertFalse(self.routing_helper._external_gateway_added.called)
        self._reset_mocks()
        # now no ports so state is torn down
        del router[l3_constants.INTERFACE_KEY]
        del router['gw_port']
        # Update router_info object
        ri.router = router
        # Keep a copy of the ex_gw_port before its gone after processing.
        ex_gw_port = ri.ex_gw_port
        # Process router and verify that internal and external network removed
        # were called and floating_ips_process was called
        self.routing_helper._process_router(ri)
        self.assertFalse(
            self.routing_helper._process_router_floating_ips.called)
        self.assertFalse(self.routing_helper._external_gateway_added.called)
        self.assertTrue(self.routing_helper._internal_network_removed.called)
        self.assertTrue(self.routing_helper._external_gateway_removed.called)
        self.routing_helper._internal_network_removed.assert_called_with(
            ri, ports[0], ex_gw_port)
        self.routing_helper._external_gateway_removed.assert_called_with(
            ri, ex_gw_port)
    def test_process_router_2_rids_2_vrfs_2_networks(self):
        driver = _mock_driver_and_hosting_device(self.routing_helper)
        self._set_driver_port_mocks(driver)

        router1, ports = self.prepare_router_data()
        ri1 = svc_helper.RouterInfo(router1['id'], router=router1)

        # Router #2 is like #1, except with different IDs and host info
        router2 = copy.deepcopy(router1)
        router2['id'] = _uuid()
        ri2 = svc_helper.RouterInfo(router2['id'], router=router2)
        h_info1 = create_hosting_info()
        h_info2 = create_hosting_info(net_name=TEST_NET2,
                                      vlan=TEST_VLAN2,
                                      gw_ip=TEST_GW_IP2,
                                      cidr=TEST_CIDR2)
        ri1.router['hosting_info'] = h_info1
        ri2.router['hosting_info'] = h_info2

        ex_gw_port1 = ri1.router.get('gw_port')
        ex_gw_port2 = ri2.router.get('gw_port')
        ex_gw_port1['hosting_info'] = h_info1
        ex_gw_port2['hosting_info'] = h_info2
        vrf1 = ri1.router['hosting_info']['vrf_id']
        vrf2 = ri2.router['hosting_info']['vrf_id']
        network_name1 = h_info1['network_name']
        network_name2 = h_info2['network_name']
        driver._get_vrf_name = mock.Mock(return_value=vrf1)
        self.routing_helper._process_router(ri1)

        driver.internal_network_added.assert_called_with(ri1, ports[0])
        driver.enable_internal_network_NAT.assert_called_with(
            ri1, ports[0], ex_gw_port1)

        v_n_r_dict = self.routing_helper._router_ids_by_vrf_and_ext_net
        self.assertEqual(1, len(v_n_r_dict))
        self.assertEqual(1, len(v_n_r_dict[vrf1]))
        self.assertEqual(1, len(v_n_r_dict[vrf1][network_name1]))

        driver.internal_network_added.reset_mock()
        driver.enable_internal_network_NAT.reset_mock()
        driver._get_vrf_name = mock.Mock(return_value=vrf2)

        self.routing_helper._process_router(ri2)
        # remove change_details as they are recursive and prevent comparison
        del ports[0]['change_details']
        del ri2.internal_ports[0]['change_details']
        driver.internal_network_added.assert_called_with(ri2, ports[0])
        driver.enable_internal_network_NAT.assert_called_with(
            ri2, ports[0], ex_gw_port2)
        self.assertEqual(2, len(v_n_r_dict))
        self.assertEqual(1, len(v_n_r_dict[vrf1]))
        self.assertEqual(1, len(v_n_r_dict[vrf2]))
        self.assertEqual(1, len(v_n_r_dict[vrf1][network_name1]))
        self.assertEqual(1, len(v_n_r_dict[vrf2][network_name2]))

        del ri1.router[bc.constants.INTERFACE_KEY]
        driver._get_vrf_name = mock.Mock(return_value=vrf1)
        self.routing_helper._process_router(ri1)

        driver.internal_network_removed.assert_called_with(ri1,
                                                           ports[0],
                                                           itfc_deleted=True)
        driver.disable_internal_network_NAT.assert_called_with(
            ri1, ports[0], ex_gw_port1, itfc_deleted=True)
        self.assertEqual(1, len(v_n_r_dict))
        self.assertEqual(1, len(v_n_r_dict[vrf2]))
        self.assertFalse(v_n_r_dict.get(vrf1))
        self.assertEqual(1, len(v_n_r_dict[vrf2][network_name2]))

        driver.internal_network_removed.reset_mock()
        driver.disable_internal_network_NAT.reset_mock()

        del ri2.router[bc.constants.INTERFACE_KEY]
        driver._get_vrf_name = mock.Mock(return_value=vrf2)
        self.routing_helper._process_router(ri2)

        # remove change_details as they are recursive and prevent comparison
        del ports[0]['change_details']
        del driver.internal_network_removed.call_args[0][1]['change_details']
        driver.internal_network_removed.assert_called_with(ri2,
                                                           ports[0],
                                                           itfc_deleted=True)
        driver.disable_internal_network_NAT.assert_called_with(
            ri2, ports[0], ex_gw_port2, itfc_deleted=True)
        self.assertEqual({}, v_n_r_dict)
Exemple #19
0
    def test_router_info_create(self):
        router_id = _uuid()
        fake_router = {}
        ri = routing_svc_helper.RouterInfo(router_id, fake_router)

        self.assertTrue(ri.router_name().endswith(router_id))
    def setUp(self):
        super(ASR1kRoutingDriver, self).setUp()

        cfg.CONF.set_override('enable_multi_region', False, 'multi_region')

        device_params = {
            'management_ip_address': 'fake_ip',
            'protocol_port': 22,
            'credentials': {
                "user_name": "stack",
                "password": "******"
            },
            'timeout': None,
            'id': '0000-1',
            'device_id': 'ASR-1'
        }
        self.driver = driver.ASR1kRoutingDriver(**device_params)
        self.driver._ncc_connection = mock.MagicMock()
        self.driver._check_response = mock.MagicMock(return_value=True)
        self.driver._check_acl = mock.MagicMock(return_value=False)

        self.vrf = ('nrouter-' +
                    FAKE_ID)[:iosxe_driver.IosXeRoutingDriver.DEV_NAME_LEN]
        self.driver._get_vrfs = mock.Mock(return_value=[self.vrf])
        self.ex_gw_ip = '20.0.0.31'
        # VIP is same as gw_ip for user visible router
        self.ex_gw_ip_vip = self.ex_gw_ip
        self.ex_gw_prefixlen = 24
        self.ex_gw_cidr = '20.0.0.30/24'
        self.ex_gw_ip_mask = '255.255.255.0'
        self.ex_gw_ha_group = 1500
        self.ex_gw_ha_info = {
            'group': self.ex_gw_ha_group,
            'ha_port': {
                'fixed_ips': [{
                    'ip_address': self.ex_gw_ip_vip,
                    'prefixlen': self.ex_gw_prefixlen
                }]
            }
        }
        self.ex_gw_gateway_ip = '20.0.0.1'
        self.vlan_ext = 317
        self.phy_infc = 'GigabitEthernet0/0/0'

        self.ex_gw_port = {
            'id':
            _uuid(),
            'network_id':
            _uuid(),
            'fixed_ips': [{
                'ip_address': self.ex_gw_ip,
                'prefixlen': self.ex_gw_prefixlen,
                'subnet_id': _uuid()
            }],
            'subnets': [{
                'cidr': self.ex_gw_cidr,
                'gateway_ip': self.ex_gw_gateway_ip
            }],
            'device_owner':
            l3_constants.DEVICE_OWNER_ROUTER_GW,
            'mac_address':
            'ca:fe:de:ad:be:ef',
            'admin_state_up':
            True,
            'hosting_info': {
                'physical_interface': self.phy_infc,
                'segmentation_id': self.vlan_ext
            },
            HA_INFO:
            self.ex_gw_ha_info
        }

        self.vlan_int = 314
        self.hosting_info = {
            'physical_interface': self.phy_infc,
            'segmentation_id': self.vlan_ext
        }
        self.gw_ip_cidr = '10.0.3.0/24'
        self.gw_prefixlen = 24
        self.gw_ip = '10.0.3.3'
        self.gw_ip_vip = '10.0.3.1'
        self.gw_ip_mask = '255.255.255.0'
        self.gw_ha_group = 1621
        self.gw_ha_info = {
            'group': self.gw_ha_group,
            'ha_port': {
                'fixed_ips': [{
                    'ip_address': self.gw_ip_vip,
                    'prefixlen': self.gw_prefixlen
                }]
            }
        }
        self.port = {
            'id': PORT_ID,
            'ip_cidr': self.gw_ip_cidr,
            'fixed_ips': [{
                'ip_address': self.gw_ip
            }],
            'subnets': [{
                'cidr': self.gw_ip_cidr,
                'gateway_ip': self.gw_ip
            }],
            'hosting_info': {
                'physical_interface': self.phy_infc,
                'segmentation_id': self.vlan_int
            },
            HA_INFO: self.gw_ha_info
        }
        int_ports = [self.port]
        self.floating_ip = '20.0.0.35'
        self.fixed_ip = '10.0.3.5'
        self.ha_priority = 10
        self.cisco_ha_details = {
            'priority':
            self.ha_priority,
            'redundancy_level':
            1,
            'redundancy_routers': [{
                'id': _uuid(),
                'priority': 20,
                'state': 'STANDBY'
            }],
            'state':
            'ACTIVE',
            'type':
            'HSRP'
        }
        self.router = {
            'id': FAKE_ID,
            l3_constants.INTERFACE_KEY: int_ports,
            'enable_snat': True,
            'admin_state_up': True,
            'routes': [],
            routerrole.ROUTER_ROLE_ATTR: 'Logical',
            ha.ENABLED: True,
            ha.DETAILS: self.cisco_ha_details,
            'gw_port': self.ex_gw_port
        }

        self.ri = routing_svc_helper.RouterInfo(FAKE_ID, self.router)
        self.ri.internal_ports = int_ports
        # Global router
        self.global_router = copy.deepcopy(self.router)
        self.global_router[routerrole.ROUTER_ROLE_ATTR] = (
            cisco_constants.ROUTER_ROLE_GLOBAL)
        self.cisco_ha_details_global = {
            'priority':
            self.ha_priority,
            'redundancy_level':
            2,
            'redundancy_routers': [{
                'priority': 10,
                'state': 'STANDBY',
                'id': FAKE_ID
            }, {
                'id': _uuid(),
                'priority': 20,
                'state': 'STANDBY'
            }],
            'state':
            'ACTIVE',
            'type':
            'HSRP'
        }
        self.global_router[ha.DETAILS] = self.cisco_ha_details_global
        self.global_router['gw_port'][HA_INFO]['ha_port']['fixed_ips'][0][
            'ip_address'] = self.ex_gw_ip_vip
        self.ri_global = routing_svc_helper.RouterInfo(FAKE_ID,
                                                       self.global_router)
        self.ri_global.internal_ports = int_ports
Exemple #21
0
 def test_router_info_create_snat_disabled(self):
     router_id = _uuid()
     self.router['enable_snat'] = False
     ri = routing_svc_helper.RouterInfo(router_id, self.router)
     self.assertFalse(ri.snat_enabled)
    def test_process_router_2_rids_2_vrfs_1_network(self):
        driver = _mock_driver_and_hosting_device(self.routing_helper)
        self._set_driver_port_mocks(driver)

        router1, ports = helper.prepare_router_data()
        ri1 = svc_helper.RouterInfo(router1['id'], router=router1)

        # Router #2 is like #1, except with some different IDs
        router2 = copy.deepcopy(router1)
        router2['id'] = _uuid()
        ri2 = svc_helper.RouterInfo(router2['id'], router=router2)
        h_info1 = create_hosting_info()
        h_info2 = copy.deepcopy(h_info1)
        h_info2['vrf_id'] = _uuid()
        ri1.router['hosting_info'] = h_info1
        ri2.router['hosting_info'] = h_info2

        ex_gw_port1 = ri1.router.get('gw_port')
        ex_gw_port2 = ri2.router.get('gw_port')
        ex_gw_port1['hosting_info'] = h_info1
        ex_gw_port2['hosting_info'] = h_info2
        vrf1 = ri1.router['hosting_info']['vrf_id']
        vrf2 = ri2.router['hosting_info']['vrf_id']
        driver._get_vrf_name = mock.Mock(return_value=vrf1)
        self.routing_helper._process_router(ri1)

        driver.internal_network_added.assert_called_with(
            ri1, ports[0])
        driver.enable_internal_network_NAT.assert_called_with(
            ri1, ports[0], ex_gw_port1)

        v_n_r_dict = self.routing_helper._router_ids_by_vrf_and_ext_net
        network_name = h_info1['network_name']
        self.assertEqual(1, len(v_n_r_dict))
        self.assertEqual(1, len(v_n_r_dict[vrf1]))
        self.assertEqual(1, len(v_n_r_dict[vrf1][network_name]))

        driver.internal_network_added.reset_mock()
        driver.enable_internal_network_NAT.reset_mock()

        driver._get_vrf_name = mock.Mock(return_value=vrf2)
        self.routing_helper._process_router(ri2)
        driver.internal_network_added.assert_called_with(
            ri2, ports[0])
        driver.enable_internal_network_NAT.assert_called_with(
            ri2, ports[0], ex_gw_port2)
        network_name = h_info1['network_name']
        self.assertEqual(2, len(v_n_r_dict))
        self.assertEqual(1, len(v_n_r_dict[vrf1]))
        self.assertEqual(1, len(v_n_r_dict[vrf2]))
        self.assertEqual(1, len(v_n_r_dict[vrf1][network_name]))
        self.assertEqual(1, len(v_n_r_dict[vrf2][network_name]))

        del ri1.router[bc.constants.INTERFACE_KEY]
        driver._get_vrf_name = mock.Mock(return_value=vrf1)
        self.routing_helper._process_router(ri1)

        driver.internal_network_removed.assert_called_with(
            ri1, ports[0], itfc_deleted=True)
        driver.disable_internal_network_NAT.assert_called_with(
            ri1, ports[0], ex_gw_port1, itfc_deleted=True)
        self.assertEqual(1, len(v_n_r_dict))
        self.assertFalse(v_n_r_dict.get(vrf1))
        self.assertEqual(1, len(v_n_r_dict[vrf2]))
        self.assertEqual(1, len(v_n_r_dict[vrf2][network_name]))

        driver.internal_network_removed.reset_mock()
        driver.disable_internal_network_NAT.reset_mock()

        del ri2.router[bc.constants.INTERFACE_KEY]
        driver._get_vrf_name = mock.Mock(return_value=vrf2)
        self.routing_helper._process_router(ri2)

        driver.internal_network_removed.assert_called_with(
            ri2, ports[0], itfc_deleted=True)
        driver.disable_internal_network_NAT.assert_called_with(
            ri2, ports[0], ex_gw_port2, itfc_deleted=True)
        self.assertEqual({}, v_n_r_dict)
    def _test_process_msn_router(self,
                                 num_ext_subnets=3,
                                 test_admin_state=True):
        def _verify_ip_info(the_mock):
            self.assertEqual(num_ext_subnets, len(the_mock.ip_infos))
            self.assertTrue(the_mock.ip_infos[0]['is_primary'])
            self.assertEqual('19.4.0.4/28', the_mock.ip_infos[0]['ip_cidr'])
            for i in range(1, num_ext_subnets):
                self.assertFalse(the_mock.ip_infos[i]['is_primary'])
                self.assertEqual('19.4.%s.4/27' % i,
                                 the_mock.ip_infos[i]['ip_cidr'])

        # need these helpers to verify that ip_info is correct
        def _ext_gw_added(ri, ex_gw_port):
            self.routing_helper._external_gateway_added.ip_infos.append(
                copy.deepcopy(ex_gw_port['ip_info']))

        def _ext_gw_removed(ri, ex_gw_port):
            self.routing_helper._external_gateway_removed.ip_infos.append(
                copy.deepcopy(ex_gw_port['ip_info']))

        router, ports = prepare_router_data(num_subnets=num_ext_subnets)
        # Setup mock for call to process floating ips
        self.routing_helper._process_router_floating_ips = mock.Mock()
        fake_floatingips1 = {
            'floatingips': [{
                'id': _uuid(),
                'floating_ip_address': '8.8.8.8',
                'fixed_ip_address': '7.7.7.7',
                'port_id': _uuid()
            }]
        }
        ri = routing_svc_helper.RouterInfo(router['id'], router=router)
        self.routing_helper._external_gateway_added.ip_infos = []
        self.routing_helper._external_gateway_added.side_effect = _ext_gw_added
        # Process with initial values
        self.routing_helper._process_router(ri)
        ex_gw_port = ri.router.get('gw_port')
        # Assert that process_floating_ips, internal_network & external network
        # added were all called with the right params
        (self.routing_helper._process_router_floating_ips.
         assert_called_once_with(ri, ex_gw_port))
        self.routing_helper._internal_network_added.assert_called_once_with(
            ri, ports[0], ex_gw_port)
        _verify_ip_info(self.routing_helper._external_gateway_added)
        self._reset_mocks()
        # remap floating IP to a new fixed ip
        fake_floatingips2 = copy.deepcopy(fake_floatingips1)
        fake_floatingips2['floatingips'][0]['fixed_ip_address'] = '7.7.7.8'
        router[bc.constants.FLOATINGIP_KEY] = fake_floatingips2['floatingips']

        # Process again and check that this time only the process_floating_ips
        # was only called.
        self.routing_helper._process_router(ri)
        ex_gw_port = ri.router.get('gw_port')
        (self.routing_helper._process_router_floating_ips.
         assert_called_once_with(ri, ex_gw_port))
        self.assertFalse(self.routing_helper._internal_network_added.called)
        self.assertFalse(self.routing_helper._external_gateway_added.called)
        self._reset_mocks()
        # remove just the floating ips
        del router[bc.constants.FLOATINGIP_KEY]
        # Process again and check that this time also only the
        # process_floating_ips and external_network remove was called
        self.routing_helper._process_router(ri)
        ex_gw_port = ri.router.get('gw_port')
        (self.routing_helper._process_router_floating_ips.
         assert_called_once_with(ri, ex_gw_port))
        self.assertFalse(self.routing_helper._internal_network_added.called)
        self.assertFalse(self.routing_helper._external_gateway_added.called)
        self._reset_mocks()

        if test_admin_state:
            self._test_router_admin_port_state(router, ri, ex_gw_port)

        # now no ports so state is torn down
        del router[bc.constants.INTERFACE_KEY]
        del router['gw_port']
        # Update router_info object
        ri.router = router
        # Keep a copy of the ex_gw_port before its gone after processing.
        ex_gw_port = ri.ex_gw_port
        self.routing_helper._external_gateway_removed.ip_infos = []
        self.routing_helper._external_gateway_removed.side_effect = (
            _ext_gw_removed)
        # Process router and verify that internal and external network removed
        # were called and floating_ips_process was called
        self.routing_helper._process_router(ri)
        self.assertFalse(
            self.routing_helper._process_router_floating_ips.called)
        self.assertFalse(self.routing_helper._external_gateway_added.called)
        self.routing_helper._internal_network_removed.assert_called_once_with(
            ri, ports[0], ex_gw_port)
        _verify_ip_info(self.routing_helper._external_gateway_removed)