def test_process_router_internal_network_removed_unexpected_error(self):
        router, ports = prepare_router_data()
        ri = 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)
        ri.internal_ports[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)
 def test_router_info_create_with_router(self):
     router_id = _uuid()
     ri = 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_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 = 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)
예제 #4
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 = 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)
 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 = RouterInfo(router['id'], router)
     self.assertRaises(cfg_exceptions.CSR1kvConfigException,
                       self.routing_helper._process_router, ri)
    def test_process_router_internal_network_added_unexpected_error(self):
        router, ports = prepare_router_data()
        ri = 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 _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 = 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, 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)
    def test_router_info_create(self):
        router_id = _uuid()
        fake_router = {}
        ri = RouterInfo(router_id, fake_router)

        self.assertTrue(ri.router_name().endswith(router_id))
    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 = 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_router_info_create_snat_disabled(self):
     router_id = _uuid()
     self.router['enable_snat'] = False
     ri = RouterInfo(router_id, self.router)
     self.assertFalse(ri.snat_enabled)