def test_get_agent_by_host(self): helpers.register_l3_agent() helpers.register_dhcp_agent() helpers.register_ovs_agent() agent = l2pop_db.get_agent_by_host( self.ctx.session, helpers.HOST) self.assertEqual(constants.AGENT_TYPE_OVS, agent.agent_type)
def setUp(self): ml2_config.cfg.CONF.set_override('mechanism_drivers', ['logger', 'fake_agent'], 'ml2') super(TestBagpipeServiceDriverCallbacks, self).setUp(self._plugin_name) self.port_create_status = 'DOWN' self.plugin = manager.NeutronManager.get_plugin() self.plugin.start_rpc_listeners() self.bagpipe_driver = self.bgpvpn_plugin.driver self.patched_driver = mock.patch.object( self.bgpvpn_plugin.driver, '_retrieve_bgpvpn_network_info_for_port', return_value=BGPVPN_INFO) self.patched_driver.start() self.mock_attach_rpc = self.mocked_bagpipeAPI.attach_port_on_bgpvpn self.mock_detach_rpc = self.mocked_bagpipeAPI.detach_port_from_bgpvpn self.mock_update_bgpvpn_rpc = self.mocked_bagpipeAPI.update_bgpvpn self.mock_delete_bgpvpn_rpc = self.mocked_bagpipeAPI.delete_bgpvpn # we choose an agent of type const.AGENT_TYPE_OFA # because this is the type used by the fake_agent mech driver helpers.register_ovs_agent(helpers.HOST, const.AGENT_TYPE_OFA) helpers.register_l3_agent()
def test_get_agent_by_host_no_candidate(self): # Register a bunch of non-L2 agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() agent = self.db_mixin.get_agent_by_host( self.ctx.session, helpers.HOST) self.assertIsNone(agent)
def _register_agent_states(self, lbaas_agents=False): """Register two L3 agents and two DHCP agents.""" l3_hosta = helpers._get_l3_agent_dict( L3_HOSTA, constants.L3_AGENT_MODE_LEGACY) l3_hostb = helpers._get_l3_agent_dict( L3_HOSTB, constants.L3_AGENT_MODE_LEGACY) dhcp_hosta = helpers._get_dhcp_agent_dict(DHCP_HOSTA) dhcp_hostc = helpers._get_dhcp_agent_dict(DHCP_HOSTC) helpers.register_l3_agent(host=L3_HOSTA) helpers.register_l3_agent(host=L3_HOSTB) helpers.register_dhcp_agent(host=DHCP_HOSTA) helpers.register_dhcp_agent(host=DHCP_HOSTC) res = [l3_hosta, l3_hostb, dhcp_hosta, dhcp_hostc] if lbaas_agents: lbaas_hosta = { 'binary': 'neutron-loadbalancer-agent', 'host': LBAAS_HOSTA, 'topic': 'LOADBALANCER_AGENT', 'configurations': {'device_drivers': ['haproxy_ns']}, 'agent_type': constants.AGENT_TYPE_LOADBALANCER} lbaas_hostb = copy.deepcopy(lbaas_hosta) lbaas_hostb['host'] = LBAAS_HOSTB callback = agents_db.AgentExtRpcCallback() callback.report_state( self.adminContext, agent_state={'agent_state': lbaas_hosta}, time=datetime.utcnow().strftime(constants.ISO8601_TIME_FORMAT)) callback.report_state( self.adminContext, agent_state={'agent_state': lbaas_hostb}, time=datetime.utcnow().strftime(constants.ISO8601_TIME_FORMAT)) res += [lbaas_hosta, lbaas_hostb] return res
def _test_router_remove_from_agent_on_vm_port_deletion( self, non_admin_port=False): # register l3 agent in dvr mode in addition to existing dvr_snat agent HOST = 'host1' non_admin_tenant = 'tenant1' helpers.register_l3_agent( host=HOST, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router() with self.network(shared=True) as net,\ self.subnet(network=net) as subnet,\ self.port(subnet=subnet, device_owner=DEVICE_OWNER_COMPUTE, tenant_id=non_admin_tenant, set_context=non_admin_port) as port: self.core_plugin.update_port( self.context, port['port']['id'], {'port': {portbindings.HOST_ID: HOST}}) self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) with mock.patch.object(self.l3_plugin.l3_rpc_notifier, 'router_removed_from_agent') as remove_mock: ctx = context.Context( '', non_admin_tenant) if non_admin_port else self.context self._delete('ports', port['port']['id'], neutron_context=ctx) remove_mock.assert_called_once_with( mock.ANY, router['id'], HOST)
def test_remove_router_interface(self): HOST1 = 'host1' helpers.register_l3_agent( host=HOST1, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router() arg_list = (portbindings.HOST_ID,) with self.subnet() as subnet,\ self.port(subnet=subnet, device_owner=DEVICE_OWNER_COMPUTE, arg_list=arg_list, **{portbindings.HOST_ID: HOST1}): l3_notifier = mock.Mock() self.l3_plugin.l3_rpc_notifier = l3_notifier self.l3_plugin.agent_notifiers[ constants.AGENT_TYPE_L3] = l3_notifier self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) self.l3_plugin.schedule_router(self.context, router['id']) self.l3_plugin.remove_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) l3_notifier.router_removed_from_agent.assert_called_once_with( self.context, router['id'], HOST1)
def _test_update_floating_ip_agent_notification(self, dvr=True): with self.subnet() as ext_subnet, self.subnet(cidr="20.0.0.0/24") as int_subnet1, self.subnet( cidr="30.0.0.0/24" ) as int_subnet2, self.port(subnet=int_subnet1, device_owner=DEVICE_OWNER_COMPUTE) as int_port1, self.port( subnet=int_subnet2, device_owner=DEVICE_OWNER_COMPUTE ) as int_port2: # locate internal ports on different hosts self.core_plugin.update_port( self.context, int_port1["port"]["id"], {"port": {portbindings.HOST_ID: "host1"}} ) self.core_plugin.update_port( self.context, int_port2["port"]["id"], {"port": {portbindings.HOST_ID: "host2"}} ) # and create l3 agents on corresponding hosts helpers.register_l3_agent(host="host1", agent_mode=constants.L3_AGENT_MODE_DVR) helpers.register_l3_agent(host="host2", agent_mode=constants.L3_AGENT_MODE_DVR) # make net external ext_net_id = ext_subnet["subnet"]["network_id"] self._update("networks", ext_net_id, {"network": {external_net.EXTERNAL: True}}) router1 = self._create_router(distributed=dvr) router2 = self._create_router(distributed=dvr) for router in (router1, router2): self.l3_plugin.update_router( self.context, router["id"], {"router": {"external_gateway_info": {"network_id": ext_net_id}}} ) self.l3_plugin.add_router_interface(self.context, router1["id"], {"subnet_id": int_subnet1["subnet"]["id"]}) self.l3_plugin.add_router_interface(self.context, router2["id"], {"subnet_id": int_subnet2["subnet"]["id"]}) floating_ip = { "floating_network_id": ext_net_id, "router_id": router1["id"], "port_id": int_port1["port"]["id"], "tenant_id": int_port1["port"]["tenant_id"], "dns_name": "", "dns_domain": "", } floating_ip = self.l3_plugin.create_floatingip(self.context, {"floatingip": floating_ip}) with mock.patch.object(self.l3_plugin, "_l3_rpc_notifier") as l3_notif: updated_floating_ip = {"router_id": router2["id"], "port_id": int_port2["port"]["id"]} self.l3_plugin.update_floatingip(self.context, floating_ip["id"], {"floatingip": updated_floating_ip}) if dvr: self.assertEqual(2, l3_notif.routers_updated_on_host.call_count) expected_calls = [ mock.call(self.context, [router1["id"]], "host1"), mock.call(self.context, [router2["id"]], "host2"), ] l3_notif.routers_updated_on_host.assert_has_calls(expected_calls) self.assertFalse(l3_notif.routers_updated.called) else: self.assertEqual(2, l3_notif.routers_updated.call_count) expected_calls = [ mock.call(self.context, [router1["id"]], None), mock.call(self.context, [router2["id"]], None), ] l3_notif.routers_updated.assert_has_calls(expected_calls) self.assertFalse(l3_notif.routers_updated_on_host.called)
def test_get_agent_by_host(self): # Register a L2 agent + A bunch of other agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() helpers.register_ovs_agent() agent = self.db_mixin.get_agent_by_host( self.ctx.session, helpers.HOST) self.assertEqual(constants.AGENT_TYPE_OVS, agent.agent_type)
def test_get_dvr_active_network_ports_no_candidate(self): self._setup_port_binding() # Register a bunch of non-L2 agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() tunnel_network_ports = self.db_mixin.get_dvr_active_network_ports( self.ctx.session, 'network_id') self.assertEqual(0, len(tunnel_network_ports))
def test_get_nondistributed_active_network_ports_no_candidate(self): self._setup_port_binding(dvr=False) # Register a bunch of non-L2 agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() fdb_network_ports = l2pop_db.get_nondistributed_active_network_ports( self.ctx.session, TEST_NETWORK_ID) self.assertEqual(0, len(fdb_network_ports))
def test_get_distributed_active_network_ports_no_candidate(self): self._setup_port_binding( device_owner=constants.DEVICE_OWNER_DVR_INTERFACE) # Register a bunch of non-L2 agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() tunnel_network_ports = l2pop_db.get_distributed_active_network_ports( self.ctx.session, TEST_NETWORK_ID) self.assertEqual(0, len(tunnel_network_ports))
def test_get_nondvr_active_network_ports(self): self._setup_port_binding(dvr=False) # Register a L2 agent + A bunch of other agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() helpers.register_ovs_agent() fdb_network_ports = self.db_mixin.get_nondvr_active_network_ports( self.ctx.session, 'network_id') self.assertEqual(1, len(fdb_network_ports)) _, agent = fdb_network_ports[0] self.assertEqual(constants.AGENT_TYPE_OVS, agent.agent_type)
def test_update_vm_port_host_router_update(self): # register l3 agents in dvr mode in addition to existing dvr_snat agent HOST1 = 'host1' dvr_agent1 = helpers.register_l3_agent( host=HOST1, agent_mode=constants.L3_AGENT_MODE_DVR) HOST2 = 'host2' dvr_agent2 = helpers.register_l3_agent( host=HOST2, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router() with self.subnet() as subnet: self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) # since there are no vm ports on HOST, and the router # has no external gateway at this point the router # should neither be scheduled to dvr nor to dvr_snat agents agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id'])['agents'] self.assertEqual(0, len(agents)) with mock.patch.object(self.l3_plugin, '_l3_rpc_notifier') as l3_notifier,\ self.port(subnet=subnet, device_owner=DEVICE_OWNER_COMPUTE) as port: self.l3_plugin.agent_notifiers[ constants.AGENT_TYPE_L3] = l3_notifier self.core_plugin.update_port( self.context, port['port']['id'], {'port': {portbindings.HOST_ID: HOST1}}) # now router should be scheduled to agent on HOST1 agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id'])['agents'] self.assertEqual(1, len(agents)) self.assertEqual(dvr_agent1['id'], agents[0]['id']) # and notification should only be sent to the agent on HOST1 l3_notifier.routers_updated_on_host.assert_called_once_with( self.context, {router['id']}, HOST1) self.assertFalse(l3_notifier.routers_updated.called) # updating port's host (instance migration) l3_notifier.reset_mock() self.core_plugin.update_port( self.context, port['port']['id'], {'port': {portbindings.HOST_ID: HOST2}}) # now router should only be scheduled to dvr agent on host2 agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id'])['agents'] self.assertEqual(1, len(agents)) self.assertEqual(dvr_agent2['id'], agents[0]['id']) l3_notifier.routers_updated_on_host.assert_called_once_with( self.context, {router['id']}, HOST2) l3_notifier.router_removed_from_agent.assert_called_once_with( mock.ANY, router['id'], HOST1)
def test_include_dvr_snat_agents_for_ha_candidates(self): """Test dvr agents configured with "dvr_snat" are excluded. This test case tests that when get_number_of_agents_for_scheduling is called, it ounts dvr_snat agents. """ # Test setup registers two l3 agents. # Register another l3 agent with dvr mode and assert that # get_number_of_ha_agent_candidates return 2. helpers.register_l3_agent("host_3", constants.L3_AGENT_MODE_DVR_SNAT) num_ha_candidates = self.plugin.get_number_of_agents_for_scheduling(self.admin_ctx) self.assertEqual(3, num_ha_candidates)
def test_get_nondistributed_active_network_ports(self): self._setup_port_binding(dvr=False) # Register a L2 agent + A bunch of other agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() helpers.register_ovs_agent() fdb_network_ports = l2pop_db.get_nondistributed_active_network_ports( self.ctx.session, TEST_NETWORK_ID) self.assertEqual(1, len(fdb_network_ports)) _, agent = fdb_network_ports[0] self.assertEqual(constants.AGENT_TYPE_OVS, agent.agent_type)
def test__get_ha_router_interface_ids_with_ha_replicated_port(self): helpers.register_dhcp_agent() helpers.register_l3_agent() helpers.register_ovs_agent() self._create_ha_router() self._setup_port_binding( device_owner=constants.DEVICE_OWNER_HA_REPLICATED_INT, device_id=TEST_ROUTER_ID) ha_iface_ids = l2pop_db._get_ha_router_interface_ids( self.ctx.session, TEST_NETWORK_ID) self.assertEqual(1, len(list(ha_iface_ids)))
def test_get_dvr_active_network_ports(self): self._setup_port_binding() # Register a L2 agent + A bunch of other agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() helpers.register_ovs_agent() tunnel_network_ports = l2pop_db.get_dvr_active_network_ports( self.ctx.session, 'network_id') self.assertEqual(1, len(tunnel_network_ports)) _, agent = tunnel_network_ports[0] self.assertEqual(constants.AGENT_TYPE_OVS, agent.agent_type)
def test_get_ha_agents_by_router_id(self): helpers.register_dhcp_agent() helpers.register_l3_agent() helpers.register_ovs_agent() self._create_ha_router() self._setup_port_binding( device_owner=constants.DEVICE_OWNER_ROUTER_SNAT, device_id=TEST_ROUTER_ID) agents = l2pop_db.get_ha_agents_by_router_id( self.ctx.session, TEST_ROUTER_ID) ha_agents = [agent.host for agent in agents] self.assertEqual(tools.UnorderedList([HOST, HOST_2]), ha_agents)
def test_get_distributed_active_network_ports(self): self._setup_port_binding( device_owner=constants.DEVICE_OWNER_DVR_INTERFACE) # Register a L2 agent + A bunch of other agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() helpers.register_ovs_agent() tunnel_network_ports = l2pop_db.get_distributed_active_network_ports( self.ctx.session, TEST_NETWORK_ID) self.assertEqual(1, len(tunnel_network_ports)) _, agent = tunnel_network_ports[0] self.assertEqual(constants.AGENT_TYPE_OVS, agent.agent_type)
def test_exclude_dvr_agents_for_ha_candidates(self): """Test dvr agents are not counted in the ha candidates. This test case tests that when get_number_of_agents_for_scheduling is called, it doesn't count dvr agents. """ # Test setup registers two l3 agents. # Register another l3 agent with dvr mode and assert that # get_number_of_ha_agent_candidates return 2. helpers.register_l3_agent('host_3', constants.L3_AGENT_MODE_DVR) num_ha_candidates = self.plugin.get_number_of_agents_for_scheduling( self.admin_ctx) self.assertEqual(2, num_ha_candidates)
def test_exclude_dvr_agents_for_ha_candidates(self): """Test dvr agents configured with "dvr" only, as opposed to "dvr_snat", are excluded. This test case tests that when get_number_of_agents_for_scheduling is called, it does not count dvr only agents. """ # Test setup registers two l3 agents. # Register another l3 agent with dvr mode and assert that # get_number_of_ha_agent_candidates return 2. helpers.register_l3_agent('host_3', constants.L3_AGENT_MODE_DVR) num_ha_candidates = self.plugin.get_number_of_agents_for_scheduling( self.admin_ctx) self.assertEqual(2, num_ha_candidates)
def test_active_port_count_with_ha_dvr_snat_port(self): helpers.register_dhcp_agent() helpers.register_l3_agent() helpers.register_ovs_agent() self._create_ha_router() self._setup_port_binding( device_owner=constants.DEVICE_OWNER_ROUTER_SNAT, device_id=TEST_ROUTER_ID) port_count = l2pop_db.get_agent_network_active_port_count( self.ctx.session, HOST, TEST_NETWORK_ID) self.assertEqual(1, port_count) port_count = l2pop_db.get_agent_network_active_port_count( self.ctx.session, HOST_2, TEST_NETWORK_ID) self.assertEqual(1, port_count)
def setUp(self): super(L3HATestFramework, self).setUp() self.admin_ctx = context.get_admin_context() self.setup_coreplugin("neutron.plugins.ml2.plugin.Ml2Plugin") self.core_plugin = manager.NeutronManager.get_plugin() notif_p = mock.patch.object(l3_hamode_db.L3_HA_NAT_db_mixin, "_notify_ha_interfaces_updated") self.notif_m = notif_p.start() cfg.CONF.set_override("allow_overlapping_ips", True) self.plugin = FakeL3PluginWithAgents() self.plugin.router_scheduler = l3_agent_scheduler.ChanceScheduler() self.agent1 = helpers.register_l3_agent() self.agent2 = helpers.register_l3_agent("host_2", constants.L3_AGENT_MODE_DVR_SNAT)
def _register_agent_states(self): """Register two L3 agents and two DHCP agents.""" l3_hosta = helpers._get_l3_agent_dict( L3_HOSTA, constants.L3_AGENT_MODE_LEGACY) l3_hostb = helpers._get_l3_agent_dict( L3_HOSTB, constants.L3_AGENT_MODE_LEGACY) dhcp_hosta = helpers._get_dhcp_agent_dict(DHCP_HOSTA) dhcp_hostc = helpers._get_dhcp_agent_dict(DHCP_HOSTC) helpers.register_l3_agent(host=L3_HOSTA) helpers.register_l3_agent(host=L3_HOSTB) helpers.register_dhcp_agent(host=DHCP_HOSTA) helpers.register_dhcp_agent(host=DHCP_HOSTC) return [l3_hosta, l3_hostb, dhcp_hosta, dhcp_hostc]
def test_get_firewall_tenant_ids_on_host_without_associated_router(self): agent1 = helpers.register_l3_agent("host1") helpers.register_l3_agent("host2") tenant_id = uuidutils.generate_uuid() ctxt = context.get_admin_context() with self.router(name='router1', admin_state_up=True, tenant_id=tenant_id) as router1: router_id = router1['router']['id'] self.l3_plugin.add_router_to_l3_agent(ctxt, agent1.id, router_id) with self.firewall(tenant_id=tenant_id, router_ids=[router_id]): tenant_ids = self.plugin.get_firewall_tenant_ids_on_host( ctxt, 'host_2') self.assertEqual([], tenant_ids)
def test_active_network_ports_with_dvr_snat_port(self): # Test to get agent hosting dvr snat port helpers.register_l3_agent() helpers.register_dhcp_agent() helpers.register_ovs_agent() # create DVR router self._create_router() # setup DVR snat port self._setup_port_binding( device_owner=constants.DEVICE_OWNER_ROUTER_SNAT, device_id=TEST_ROUTER_ID) helpers.register_dhcp_agent() fdb_network_ports = l2pop_db.get_nondistributed_active_network_ports( self.ctx.session, TEST_NETWORK_ID) self.assertEqual(1, len(fdb_network_ports))
def setUp(self): super(L3HATestFramework, self).setUp() self.admin_ctx = context.get_admin_context() self.setup_coreplugin('neutron.plugins.ml2.plugin.Ml2Plugin') self.core_plugin = manager.NeutronManager.get_plugin() notif_p = mock.patch.object(l3_hamode_db.L3_HA_NAT_db_mixin, '_notify_ha_interfaces_updated') self.notif_m = notif_p.start() cfg.CONF.set_override('allow_overlapping_ips', True) self.plugin = FakeL3PluginWithAgents() self.agent1 = helpers.register_l3_agent() self.agent2 = helpers.register_l3_agent( 'host_2', constants.L3_AGENT_MODE_DVR_SNAT)
def test_get_number_of_agents_for_scheduling_not_enough_agents(self): cfg.CONF.set_override('min_l3_agents_per_router', 3) agent_to_bring_down = helpers.register_l3_agent(host='l3host_3') self._bring_down_agent(agent_to_bring_down['id']) self.assertRaises(l3_ext_ha_mode.HANotEnoughAvailableAgents, self.plugin.get_number_of_agents_for_scheduling, self.admin_ctx)
def test_update_vm_port_host_router_update(self): # register l3 agent in dvr mode in addition to existing dvr_snat agent HOST = 'host1' dvr_agent = helpers.register_l3_agent( host=HOST, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router() with self.subnet() as subnet: self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) # since there are no vm ports on HOST, at this point the router # should be scheduled to only dvr_snat agent agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id']) self.assertEqual(1, len(agents['agents'])) self.assertEqual(self.l3_agent['id'], agents['agents'][0]['id']) with mock.patch.object(self.l3_plugin, '_l3_rpc_notifier') as l3_notifier,\ self.port(subnet=subnet, device_owner=DEVICE_OWNER_COMPUTE) as port: self.core_plugin.update_port( self.context, port['port']['id'], {'port': {'binding:host_id': HOST}}) # now router should be scheduled to both agents agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id']) self.assertEqual(2, len(agents['agents'])) self.assertIn(dvr_agent['id'], [agent['id'] for agent in agents['agents']]) # and notification should only be sent to the agent on HOST l3_notifier.routers_updated_on_host.assert_called_once_with( self.context, {router['id']}, HOST) self.assertFalse(l3_notifier.routers_updated.called)
def _test_router_remove_from_agent_on_vm_port_deletion(self, non_admin_port=False): # register l3 agent in dvr mode in addition to existing dvr_snat agent HOST = "host1" non_admin_tenant = "tenant1" dvr_agent = helpers.register_l3_agent(host=HOST, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router() with self.network(shared=True) as net, self.subnet(network=net) as subnet, self.port( subnet=subnet, device_owner=DEVICE_OWNER_COMPUTE, tenant_id=non_admin_tenant, set_context=non_admin_port ) as port: self.core_plugin.update_port(self.context, port["port"]["id"], {"port": {portbindings.HOST_ID: HOST}}) self.l3_plugin.add_router_interface(self.context, router["id"], {"subnet_id": subnet["subnet"]["id"]}) # router should be scheduled to agent on HOST agents = self.l3_plugin.list_l3_agents_hosting_router(self.context, router["id"]) self.assertEqual(1, len(agents["agents"])) self.assertEqual(dvr_agent["id"], agents["agents"][0]["id"]) notifier = self.l3_plugin.agent_notifiers[constants.AGENT_TYPE_L3] with mock.patch.object(notifier, "router_removed_from_agent") as remove_mock: ctx = context.Context("", non_admin_tenant) if non_admin_port else self.context self._delete("ports", port["port"]["id"], neutron_context=ctx) # now when port is deleted the router should be unscheduled agents = self.l3_plugin.list_l3_agents_hosting_router(self.context, router["id"]) self.assertEqual(0, len(agents["agents"])) remove_mock.assert_called_once_with(mock.ANY, router["id"], HOST)
def test_get_firewall_tenant_ids_on_host_with_routers(self): agent1 = helpers.register_l3_agent("host1") tenant_id1 = uuidutils.generate_uuid() tenant_id2 = uuidutils.generate_uuid() ctxt = context.get_admin_context() with self.router(name='router1', admin_state_up=True, tenant_id=tenant_id1) as router1: with self.router(name='router2', admin_state_up=True, tenant_id=tenant_id2) as router2: router_id1 = router1['router']['id'] router_id2 = router2['router']['id'] self.l3_plugin.add_router_to_l3_agent(ctxt, agent1.id, router_id1) self.l3_plugin.add_router_to_l3_agent(ctxt, agent1.id, router_id2) with self.firewall(tenant_id=tenant_id1, router_ids=[router_id1]): with self.firewall(tenant_id=tenant_id2, router_ids=[router_id2]): tenant_ids = ( self.plugin.get_firewall_tenant_ids_on_host( ctxt, 'host1')) self.assertItemsEqual([tenant_id1, tenant_id2], tenant_ids)
def test_update_vm_port_host_router_update(self): # register l3 agent in dvr mode in addition to existing dvr_snat agent HOST = "host1" dvr_agent = helpers.register_l3_agent(host=HOST, agent_mode=l3_const.L3_AGENT_MODE_DVR) router = self._create_router() with self.subnet() as subnet: self.l3_plugin.add_router_interface(self.context, router["id"], {"subnet_id": subnet["subnet"]["id"]}) # since there are no vm ports on HOST, at this point the router # should be scheduled to only dvr_snat agent agents = self.l3_plugin.list_l3_agents_hosting_router(self.context, router["id"]) self.assertEqual(1, len(agents["agents"])) self.assertEqual(self.l3_agent["id"], agents["agents"][0]["id"]) with mock.patch.object(self.l3_plugin, "_l3_rpc_notifier") as l3_notifier, self.port( subnet=subnet, device_owner="compute:None" ) as port: self.core_plugin.update_port(self.context, port["port"]["id"], {"port": {"binding:host_id": HOST}}) # now router should be scheduled to both agents agents = self.l3_plugin.list_l3_agents_hosting_router(self.context, router["id"]) self.assertEqual(2, len(agents["agents"])) self.assertIn(dvr_agent["id"], [agent["id"] for agent in agents["agents"]]) # and notification should only be sent to the agent on HOST l3_notifier.routers_updated_on_host.assert_called_once_with(self.context, {router["id"]}, HOST) self.assertFalse(l3_notifier.routers_updated.called)
def test_active_network_ports_with_ha_dvr_snat_port(self): # test to get HA agents hosting HA+DVR snat port helpers.register_dhcp_agent() helpers.register_l3_agent() helpers.register_ovs_agent() # create HA+DVR router self._create_ha_router() # setup HA snat port self._setup_port_binding( device_owner=constants.DEVICE_OWNER_ROUTER_SNAT, device_id=TEST_ROUTER_ID) fdb_network_ports = l2pop_db.get_nondistributed_active_network_ports( self.ctx, TEST_NETWORK_ID) self.assertEqual(0, len(fdb_network_ports)) ha_ports = l2pop_db.get_ha_active_network_ports( self.ctx, TEST_NETWORK_ID) self.assertEqual(2, len(ha_ports))
def test_get_sync_data_metering(self): with self.subnet() as subnet: s = subnet["subnet"] self._set_net_external(s["network_id"]) with self.router(name="router1", subnet=subnet) as router: r = router["router"] self._add_external_gateway_to_router(r["id"], s["network_id"]) with self.metering_label(tenant_id=r["tenant_id"]): callbacks = metering_rpc.MeteringRpcCallbacks(self.meter_plugin) data = callbacks.get_sync_data_metering(self.adminContext, host="agent1") self.assertEqual("router1", data[0]["name"]) helpers.register_l3_agent(host="agent2") data = callbacks.get_sync_data_metering(self.adminContext, host="agent2") self.assertFalse(data) self._remove_external_gateway_from_router(r["id"], s["network_id"])
def _register_agent_states(self, lbaas_agents=False): """Register two L3 agents and two DHCP agents.""" l3_hosta = helpers._get_l3_agent_dict( L3_HOSTA, constants.L3_AGENT_MODE_LEGACY) l3_hostb = helpers._get_l3_agent_dict( L3_HOSTB, constants.L3_AGENT_MODE_LEGACY) dhcp_hosta = { 'binary': 'neutron-dhcp-agent', 'host': DHCP_HOSTA, 'topic': 'DHCP_AGENT', 'configurations': {'dhcp_driver': 'dhcp_driver', 'use_namespaces': True, }, 'agent_type': constants.AGENT_TYPE_DHCP} dhcp_hostc = copy.deepcopy(dhcp_hosta) dhcp_hostc['host'] = DHCP_HOSTC lbaas_hosta = { 'binary': 'neutron-loadbalancer-agent', 'host': LBAAS_HOSTA, 'topic': 'LOADBALANCER_AGENT', 'configurations': {'device_drivers': ['haproxy_ns']}, 'agent_type': constants.AGENT_TYPE_LOADBALANCER} lbaas_hostb = copy.deepcopy(lbaas_hosta) lbaas_hostb['host'] = LBAAS_HOSTB callback = agents_db.AgentExtRpcCallback() helpers.register_l3_agent(host=L3_HOSTA) helpers.register_l3_agent(host=L3_HOSTB) callback.report_state(self.adminContext, agent_state={'agent_state': dhcp_hosta}, time=timeutils.strtime()) callback.report_state(self.adminContext, agent_state={'agent_state': dhcp_hostc}, time=timeutils.strtime()) res = [l3_hosta, l3_hostb, dhcp_hosta, dhcp_hostc] if lbaas_agents: callback.report_state(self.adminContext, agent_state={'agent_state': lbaas_hosta}, time=timeutils.strtime()) callback.report_state(self.adminContext, agent_state={'agent_state': lbaas_hostb}, time=timeutils.strtime()) res += [lbaas_hosta, lbaas_hostb] return res
def _create_ha_router(self, distributed=False): helpers.register_l3_agent(HOST_2) helpers.register_ovs_agent(HOST_2, tunneling_ip=HOST_2_TUNNELING_IP) # Register l3 agent on host3, which doesn't host any HA router. # Tests should test that host3 is not a HA agent host. helpers.register_l3_agent(HOST_3) helpers.register_ovs_agent(HOST_3, tunneling_ip=HOST_3_TUNNELING_IP) with self.ctx.session.begin(subtransactions=True): self.ctx.session.add(models_v2.Network(id=TEST_HA_NETWORK_ID)) self._create_router(distributed=distributed, ha=True) for state, host in [(n_const.HA_ROUTER_STATE_ACTIVE, HOST), (n_const.HA_ROUTER_STATE_STANDBY, HOST_2)]: self._setup_port_binding( network_id=TEST_HA_NETWORK_ID, device_owner=constants.DEVICE_OWNER_ROUTER_HA_INTF, device_id=TEST_ROUTER_ID, host_state=state, host=host)
def _create_l3_agent(self, host, context, agent_mode='legacy', state=True, ext_net_id=''): agent = helpers.register_l3_agent(host, agent_mode) helpers.set_agent_admin_state(agent.id, state) return agent
def _create_l3_agent(self, host, context, agent_mode='legacy', plugin=None, state=True): agent = helpers.register_l3_agent(host, agent_mode) helpers.set_agent_admin_state(agent.id, state) return agent
def _create_ha_router(self, distributed=False): helpers.register_l3_agent(HOST_2) helpers.register_ovs_agent(HOST_2, tunneling_ip=HOST_2_TUNNELING_IP) # Register l3 agent on host3, which doesn't host any HA router. # Tests should test that host3 is not a HA agent host. helpers.register_l3_agent(HOST_3) helpers.register_ovs_agent(HOST_3, tunneling_ip=HOST_3_TUNNELING_IP) with db_api.CONTEXT_WRITER.using(self.ctx): network_obj.Network(self.ctx, id=TEST_HA_NETWORK_ID).create() self._create_router(distributed=distributed, ha=True) for state, host in [(constants.HA_ROUTER_STATE_ACTIVE, HOST), (constants.HA_ROUTER_STATE_STANDBY, HOST_2)]: self._setup_port_binding( network_id=TEST_HA_NETWORK_ID, device_owner=constants.DEVICE_OWNER_ROUTER_HA_INTF, device_id=TEST_ROUTER_ID, host_state=state, host=host)
def test_update_vm_port_host_router_update(self): # register l3 agents in dvr mode in addition to existing dvr_snat agent HOST1 = 'host1' helpers.register_l3_agent(host=HOST1, agent_mode=constants.L3_AGENT_MODE_DVR) HOST2 = 'host2' helpers.register_l3_agent(host=HOST2, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router() with self.subnet() as subnet: self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) with mock.patch.object(self.l3_plugin, '_l3_rpc_notifier') as l3_notifier,\ self.port(subnet=subnet, device_owner=DEVICE_OWNER_COMPUTE) as port: self.l3_plugin.agent_notifiers[ constants.AGENT_TYPE_L3] = l3_notifier self.core_plugin.update_port( self.context, port['port']['id'], {'port': { portbindings.HOST_ID: HOST1 }}) l3_notifier.routers_updated_on_host.assert_called_once_with( self.context, {router['id']}, HOST1) self.assertFalse(l3_notifier.routers_updated.called) # updating port's host (instance migration) l3_notifier.reset_mock() self.core_plugin.update_port( self.context, port['port']['id'], {'port': { portbindings.HOST_ID: HOST2 }}) l3_notifier.routers_updated_on_host.assert_called_once_with( self.context, {router['id']}, HOST2) l3_notifier.router_removed_from_agent.assert_called_once_with( mock.ANY, router['id'], HOST1)
def setUp(self): service_plugins = {'metering_plugin_name': METERING_SERVICE_PLUGIN_KLASS} plugin = ('neutron.tests.unit.extensions.test_l3.' 'TestL3NatIntAgentSchedulingPlugin') ext_mgr = MeteringTestExtensionManager() super(TestMeteringPluginRpcFromL3Agent, self).setUp(plugin=plugin, service_plugins=service_plugins, ext_mgr=ext_mgr) self.meter_plugin = directory.get_plugin(constants.METERING) self.tenant_id = 'admin_tenant_id' self.tenant_id_1 = 'tenant_id_1' self.tenant_id_2 = 'tenant_id_2' self.adminContext = context.get_admin_context() helpers.register_l3_agent(host='agent1')
def test_get_sync_data_metering(self): with self.subnet() as subnet: s = subnet['subnet'] self._set_net_external(s['network_id']) with self.router(name='router1', subnet=subnet) as router: r = router['router'] self._add_external_gateway_to_router(r['id'], s['network_id']) with self.metering_label(tenant_id=r['tenant_id']): callbacks = metering_rpc.MeteringRpcCallbacks( self.meter_plugin) data = callbacks.get_sync_data_metering(self.adminContext, host='agent1') self.assertEqual('router1', data[0]['name']) helpers.register_l3_agent(host='agent2') data = callbacks.get_sync_data_metering(self.adminContext, host='agent2') self.assertFalse(data) self._remove_external_gateway_from_router( r['id'], s['network_id'])
def setUp(self): ml2_config.cfg.CONF.set_override('mechanism_drivers', ['logger', 'fake_agent'], 'ml2') super(TestBagpipeServiceDriverCallbacks, self).setUp(self._plugin_name) self.port_create_status = 'DOWN' self.plugin = directory.get_plugin() self.plugin.start_rpc_listeners() self.bagpipe_driver = self.bgpvpn_plugin.driver self.patched_driver = mock.patch.object( self.bgpvpn_plugin.driver, '_retrieve_bgpvpn_network_info_for_port', return_value=BGPVPN_INFO) self.patched_driver.start() # we choose an agent of type const.AGENT_TYPE_OFA # because this is the type used by the fake_agent mech driver helpers.register_ovs_agent(helpers.HOST, const.AGENT_TYPE_OFA) helpers.register_l3_agent()
def _register_agent_states(self, lbaas_agents=False): """Register two L3 agents and two DHCP agents.""" l3_hosta = helpers._get_l3_agent_dict(L3_HOSTA, constants.L3_AGENT_MODE_LEGACY) l3_hostb = helpers._get_l3_agent_dict(L3_HOSTB, constants.L3_AGENT_MODE_LEGACY) dhcp_hosta = helpers._get_dhcp_agent_dict(DHCP_HOSTA) dhcp_hostc = helpers._get_dhcp_agent_dict(DHCP_HOSTC) helpers.register_l3_agent(host=L3_HOSTA) helpers.register_l3_agent(host=L3_HOSTB) helpers.register_dhcp_agent(host=DHCP_HOSTA) helpers.register_dhcp_agent(host=DHCP_HOSTC) res = [l3_hosta, l3_hostb, dhcp_hosta, dhcp_hostc] if lbaas_agents: lbaas_hosta = { 'binary': 'neutron-loadbalancer-agent', 'host': LBAAS_HOSTA, 'topic': 'LOADBALANCER_AGENT', 'configurations': { 'device_drivers': ['haproxy_ns'] }, 'agent_type': constants.AGENT_TYPE_LOADBALANCER } lbaas_hostb = copy.deepcopy(lbaas_hosta) lbaas_hostb['host'] = LBAAS_HOSTB callback = agents_db.AgentExtRpcCallback() callback.report_state(self.adminContext, agent_state={'agent_state': lbaas_hosta}, time=datetime.utcnow().strftime( constants.ISO8601_TIME_FORMAT)) callback.report_state(self.adminContext, agent_state={'agent_state': lbaas_hostb}, time=datetime.utcnow().strftime( constants.ISO8601_TIME_FORMAT)) res += [lbaas_hosta, lbaas_hostb] return res
def setUp(self): cfg.CONF.set_override( 'service_plugins', ['neutron.services.l3_router.l3_router_plugin.L3RouterPlugin', 'neutron.services.flavors.flavors_plugin.FlavorsPlugin']) super(TestL3AgentShimControllers, self).setUp() policy.init() policy._ENFORCER.set_rules( oslo_policy.Rules.from_dict( {'get_l3-agents': 'role:admin', 'get_l3-routers': 'role:admin'}), overwrite=False) ctx = context.get_admin_context() l3_plugin = directory.get_plugin(n_const.L3) self.router = pecan_utils.create_router(ctx, l3_plugin) self.agent = helpers.register_l3_agent() # NOTE(blogan): Not sending notifications because this test is for # testing the shim controllers l3_plugin.agent_notifiers[n_const.AGENT_TYPE_L3] = None
def _test_router_remove_from_agent_on_vm_port_deletion( self, non_admin_port=False): # register l3 agent in dvr mode in addition to existing dvr_snat agent HOST = 'host1' non_admin_tenant = 'tenant1' dvr_agent = helpers.register_l3_agent( host=HOST, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router() with self.network(shared=True) as net,\ self.subnet(network=net) as subnet,\ self.port(subnet=subnet, device_owner=DEVICE_OWNER_COMPUTE, tenant_id=non_admin_tenant, set_context=non_admin_port) as port: self.core_plugin.update_port( self.context, port['port']['id'], {'port': { portbindings.HOST_ID: HOST }}) self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) # router should be scheduled to agent on HOST agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id']) self.assertEqual(1, len(agents['agents'])) self.assertEqual(dvr_agent['id'], agents['agents'][0]['id']) notifier = self.l3_plugin.agent_notifiers[constants.AGENT_TYPE_L3] with mock.patch.object(notifier, 'router_removed_from_agent') as remove_mock: ctx = context.Context( '', non_admin_tenant) if non_admin_port else self.context self._delete('ports', port['port']['id'], neutron_context=ctx) # now when port is deleted the router should be unscheduled agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id']) self.assertEqual(0, len(agents['agents'])) remove_mock.assert_called_once_with(mock.ANY, router['id'], HOST)
def test_remove_router_interface(self): HOST1 = 'host1' dvr_agent = helpers.register_l3_agent( host=HOST1, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router() arg_list = (portbindings.HOST_ID, ) with self.subnet() as subnet,\ self.port(subnet=subnet, device_owner=DEVICE_OWNER_COMPUTE, arg_list=arg_list, **{portbindings.HOST_ID: HOST1}): l3_notifier = mock.Mock() self.l3_plugin.l3_rpc_notifier = l3_notifier self.l3_plugin.agent_notifiers[ constants.AGENT_TYPE_L3] = l3_notifier self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) self.l3_plugin.schedule_router(self.context, router['id']) # router should be scheduled to the agent on HOST1 agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id'])['agents'] self.assertEqual(1, len(agents)) self.assertEqual(dvr_agent['id'], agents[0]['id']) self.l3_plugin.remove_router_interface( self.context, router['id'], {'subnet_id': subnet['subnet']['id']}) agents = self.l3_plugin.list_l3_agents_hosting_router( self.context, router['id'])['agents'] self.assertEqual(0, len(agents)) l3_notifier.router_removed_from_agent.assert_called_once_with( self.context, router['id'], HOST1)
def setUp(self): super(L3DvrHATestCase, self).setUp() self.l3_agent_2 = helpers.register_l3_agent( host="standby", agent_mode=constants.L3_AGENT_MODE_DVR_SNAT)
def test_router_notifications(self): """Check that notifications go to the right hosts in different conditions """ # register l3 agents in dvr mode in addition to existing dvr_snat agent HOST1, HOST2, HOST3 = 'host1', 'host2', 'host3' for host in [HOST1, HOST2, HOST3]: helpers.register_l3_agent(host=host, agent_mode=constants.L3_AGENT_MODE_DVR) router = self._create_router(distributed=True, ha=True) arg_list = (portbindings.HOST_ID, ) with self.subnet() as ext_subnet, \ self.subnet(cidr='20.0.0.0/24') as subnet1, \ self.subnet(cidr='30.0.0.0/24') as subnet2, \ self.subnet(cidr='40.0.0.0/24') as subnet3, \ self.port(subnet=subnet1, device_owner=DEVICE_OWNER_COMPUTE, arg_list=arg_list, **{portbindings.HOST_ID: HOST1}), \ self.port(subnet=subnet2, device_owner=constants.DEVICE_OWNER_DHCP, arg_list=arg_list, **{portbindings.HOST_ID: HOST2}), \ self.port(subnet=subnet3, device_owner=constants.DEVICE_OWNER_NEUTRON_PREFIX, arg_list=arg_list, **{portbindings.HOST_ID: HOST3}): # make net external ext_net_id = ext_subnet['subnet']['network_id'] self._update('networks', ext_net_id, {'network': { external_net.EXTERNAL: True }}) with mock.patch.object(self.l3_plugin.l3_rpc_notifier.client, 'prepare') as mock_prepare: # add external gateway to router self.l3_plugin.update_router( self.context, router['id'], { 'router': { 'external_gateway_info': { 'network_id': ext_net_id } } }) # router has no interfaces so notification goes # to only dvr_snat agents (self.l3_agent and self.l3_agent_2) self.assertEqual(2, mock_prepare.call_count) expected = [ mock.call(server=self.l3_agent['host'], topic=topics.L3_AGENT, version='1.1'), mock.call(server=self.l3_agent_2['host'], topic=topics.L3_AGENT, version='1.1') ] mock_prepare.assert_has_calls(expected, any_order=True) mock_prepare.reset_mock() self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet1['subnet']['id']}) self.assertEqual(3, mock_prepare.call_count) expected = [ mock.call(server=self.l3_agent['host'], topic=topics.L3_AGENT, version='1.1'), mock.call(server=self.l3_agent_2['host'], topic=topics.L3_AGENT, version='1.1'), mock.call(server=HOST1, topic=topics.L3_AGENT, version='1.1') ] mock_prepare.assert_has_calls(expected, any_order=True) mock_prepare.reset_mock() self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet2['subnet']['id']}) self.assertEqual(4, mock_prepare.call_count) expected = [ mock.call(server=self.l3_agent['host'], topic=topics.L3_AGENT, version='1.1'), mock.call(server=self.l3_agent_2['host'], topic=topics.L3_AGENT, version='1.1'), mock.call(server=HOST1, topic=topics.L3_AGENT, version='1.1'), mock.call(server=HOST2, topic=topics.L3_AGENT, version='1.1') ] mock_prepare.assert_has_calls(expected, any_order=True) mock_prepare.reset_mock() self.l3_plugin.add_router_interface( self.context, router['id'], {'subnet_id': subnet3['subnet']['id']}) # there are no dvr serviceable ports on HOST3, so notification # goes to the same hosts self.assertEqual(4, mock_prepare.call_count) expected = [ mock.call(server=self.l3_agent['host'], topic=topics.L3_AGENT, version='1.1'), mock.call(server=self.l3_agent_2['host'], topic=topics.L3_AGENT, version='1.1'), mock.call(server=HOST1, topic=topics.L3_AGENT, version='1.1'), mock.call(server=HOST2, topic=topics.L3_AGENT, version='1.1') ] mock_prepare.assert_has_calls(expected, any_order=True)
def test_get_agent_by_host_no_candidate(self): helpers.register_l3_agent() helpers.register_dhcp_agent() agent = l2pop_db.get_agent_by_host(self.ctx.session, helpers.HOST) self.assertIsNone(agent)
def test_get_agent_by_host(self): helpers.register_l3_agent() helpers.register_dhcp_agent() helpers.register_ovs_agent() agent = l2pop_db.get_agent_by_host(self.ctx.session, helpers.HOST) self.assertEqual(constants.AGENT_TYPE_OVS, agent.agent_type)
def _register_l3_agent(self, host): helpers.register_l3_agent(host)
def test_get_agent_by_host_no_candidate(self): # Register a bunch of non-L2 agents on the same host helpers.register_l3_agent() helpers.register_dhcp_agent() agent = l2pop_db.get_agent_by_host(self.ctx.session, helpers.HOST) self.assertIsNone(agent)
def _register_azs(self): self.agent1 = helpers.register_dhcp_agent(host='host1', az='nova1') self.agent2 = helpers.register_dhcp_agent(host='host2', az='nova2') self.agent3 = helpers.register_l3_agent(host='host2', az='nova2') self.agent4 = helpers.register_l3_agent(host='host3', az='nova3') self.agent5 = helpers.register_l3_agent(host='host4', az='nova2')