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_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(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)