def test_external_gateway_removed_ext_gw_port_and_fip(self):
    agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
    agent.conf.agent_mode = lib_constants.L3_AGENT_MODE_DVR_SNAT
    router = l3_test_common.prepare_router_data(num_internal_ports=2)
    router['gw_port_host'] = HOSTNAME
    self.mock_driver.unplug.reset_mock()
    external_net_id = router['gw_port']['network_id']
    self._set_ri_kwargs(agent, router['id'], router)
    ri = dvr_edge_rtr.DvrEdgeRouter(HOSTNAME, **self.ri_kwargs)
    ri.remove_floating_ip = mock.Mock()
    agent._fetch_external_net_id = mock.Mock(return_value=external_net_id)
    ri.ex_gw_port = ri.router['gw_port']
    del ri.router['gw_port']
    ri.external_gateway_added(ri.ex_gw_port, ri.get_external_device_name(ri.ex_gw_port['id']))
    ri.fip_ns = None
    nat = ri.iptables_manager.ipv4['nat']
    nat.clear_rules_by_tag = mock.Mock()
    nat.add_rule = mock.Mock()
    ri.fip_ns = agent.get_fip_ns(external_net_id)
    subnet_id = _uuid()
    ri.fip_ns.agent_gateway_port = {'fixed_ips': [{'ip_address': '0.0.0.0', 'prefixlen': 24, 'subnet_id': subnet_id}], 'subnets': [{'id': subnet_id, 'cidr': '20.0.0.0/24', 'gateway_ip': '0.0.0.0'}], 'id': _uuid(), 'network_id': external_net_id, 'mac_address': 'ca:fe:de:ad:be:ef'}
    vm_floating_ip = '0.0.0.0'
    ri.floating_ips_dict[vm_floating_ip] = FIP_PRI
    ri.rtr_fip_subnet = ri.fip_ns.local_subnets.allocate(ri.router_id)
    (_, fip_to_rtr) = ri.rtr_fip_subnet.get_pair()
    self.mock_ip.get_devices.return_value = [l3_test_common.FakeDev(ri.fip_ns.get_ext_device_name(_uuid()))]
    ri.get_router_cidrs = mock.Mock(return_value={(vm_floating_ip + '/32'), '19.4.4.1/24'})
    self.device_exists.return_value = True
    ri.external_gateway_removed(ri.ex_gw_port, ri.get_external_device_name(ri.ex_gw_port['id']))
    ri.remove_floating_ip.assert_called_once_with(self.mock_ip_dev, '19.4.4.2/32')
def test_external_gateway_removed_ext_gw_port_and_fip(self):
    agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
    agent.conf.agent_mode = lib_constants.L3_AGENT_MODE_DVR_SNAT
    router = l3_test_common.prepare_router_data(num_internal_ports=2)
    router['gw_port_host'] = HOSTNAME
    self.mock_driver.unplug.reset_mock()
    external_net_id = router['gw_port']['network_id']
    self._set_ri_kwargs(agent, router['id'], router)
    ri = dvr_edge_rtr.DvrEdgeRouter(HOSTNAME, **self.ri_kwargs)
    ri.remove_floating_ip = mock.Mock()
    agent._fetch_external_net_id = mock.Mock(return_value=external_net_id)
    ri.ex_gw_port = ri.router['gw_port']
    del ri.router['gw_port']
    ri.external_gateway_added(ri.ex_gw_port, ri.get_external_device_name(ri.ex_gw_port['id']))
    ri.fip_ns = None
    nat = ri.iptables_manager.ipv4['nat']
    nat.clear_rules_by_tag = mock.Mock()
    nat.add_rule = mock.Mock()
    ri.fip_ns = agent.get_fip_ns(external_net_id)
    subnet_id = _uuid()
    ri.fip_ns.agent_gateway_port = {'fixed_ips': [{'ip_address': '0.0.0.0', 'prefixlen': 24, 'subnet_id': subnet_id}], 'subnets': [{'id': subnet_id, 'cidr': '20.0.0.0/24', 'gateway_ip': '0.0.0.0'}], 'id': _uuid(), 'network_id': external_net_id, 'mac_address': 'ca:fe:de:ad:be:ef'}
    vm_floating_ip = '0.0.0.0'
    ri.floating_ips_dict[vm_floating_ip] = FIP_PRI
    ri.rtr_fip_subnet = ri.fip_ns.local_subnets.allocate(ri.router_id)
    (_, fip_to_rtr) = ri.rtr_fip_subnet.get_pair()
    self.mock_ip.get_devices.return_value = [l3_test_common.FakeDev(ri.fip_ns.get_ext_device_name(_uuid()))]
    ri.get_router_cidrs = mock.Mock(return_value={(vm_floating_ip + '/32'), '19.4.4.1/24'})
    self.device_exists.return_value = True
    ri.external_gateway_removed(ri.ex_gw_port, ri.get_external_device_name(ri.ex_gw_port['id']))
    ri.remove_floating_ip.assert_called_once_with(self.mock_ip_dev, '19.4.4.2/32')