def test__get_enabled_agents(self): agent1 = agents_db.Agent() agent1.admin_state_up = True agent1.heartbeat_timestamp = timeutils.utcnow() agent2 = agents_db.Agent() agent2.admin_state_up = False agent2.heartbeat_timestamp = timeutils.utcnow() network = {'id': 'foo_network_id'} self._test__get_enabled_agents(network, agents=[agent1])
def test__get_enabled_agents_with_admin_state_down(self): cfg.CONF.set_override( 'enable_services_on_agents_with_admin_state_down', True) agent1 = agents_db.Agent() agent1.admin_state_up = True agent1.heartbeat_timestamp = timeutils.utcnow() agent2 = agents_db.Agent() agent2.admin_state_up = False agent2.heartbeat_timestamp = timeutils.utcnow() network = {'id': 'foo_network_id'} self._test__get_enabled_agents(network, agents=[agent1, agent2])
def test_add_metering_label_rpc_call(self): second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84' expected1 = {'args': {'routers': [{'status': 'ACTIVE', 'name': 'router1', 'gw_port_id': None, 'admin_state_up': True, 'tenant_id': self.tenant_id, '_metering_labels': [ {'rules': [], 'id': second_uuid}], 'id': self.uuid}]}, 'namespace': None, 'method': 'add_metering_label'} expected2 = {'args': {'routers': [{'status': 'ACTIVE', 'name': 'router2', 'gw_port_id': None, 'admin_state_up': True, 'tenant_id': self.tenant_id, '_metering_labels': [ {'rules': [], 'id': second_uuid}], 'id': second_uuid}]}, 'namespace': None, 'method': 'add_metering_label'} # bind each router to a specific agent agent1 = agents_db.Agent(host='agent1') agent2 = agents_db.Agent(host='agent2') agents = {self.uuid: agent1, second_uuid: agent2} def side_effect(context, routers, admin_state_up, active): return [agents[routers[0]]] self.l3routers_mock.side_effect = side_effect with self.router(name='router1', tenant_id=self.tenant_id, set_context=True): self.mock_uuid.return_value = second_uuid with self.router(name='router2', tenant_id=self.tenant_id, set_context=True): with self.metering_label(tenant_id=self.tenant_id, set_context=True): topic1 = "%s.%s" % (self.topic, 'agent1') topic2 = "%s.%s" % (self.topic, 'agent2') # check if there is a call per agent expected = [mock.call(self.ctx, expected1, topic=topic1), mock.call(self.ctx, expected2, topic=topic2)] self.mock_cast.assert_has_calls(expected, any_order=True)
def test__get_enabled_agents_with_inactive_ones(self): agent1 = agents_db.Agent() agent1.admin_state_up = True agent1.heartbeat_timestamp = timeutils.utcnow() agent2 = agents_db.Agent() agent2.admin_state_up = True # This is effectively an inactive agent agent2.heartbeat_timestamp = datetime.datetime(2000, 1, 1, 0, 0) network = {'id': 'foo_network_id'} self._test__get_enabled_agents(network, agents=[agent1, agent2], expected_warnings=1, expected_errors=0)
def test_add_metering_label_rpc_call(self): second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84' expected = [{ 'status': 'ACTIVE', 'name': 'router1', 'gw_port_id': None, 'admin_state_up': True, 'tenant_id': self.tenant_id, '_metering_labels': [{ 'rules': [], 'id': second_uuid }], 'id': self.uuid }, { 'status': 'ACTIVE', 'name': 'router2', 'gw_port_id': None, 'admin_state_up': True, 'tenant_id': self.tenant_id, '_metering_labels': [{ 'rules': [], 'id': second_uuid }], 'id': second_uuid }] # bind each router to a specific agent agent1 = agents_db.Agent(host='agent1') agent2 = agents_db.Agent(host='agent2') agents = {self.uuid: agent1, second_uuid: agent2} def side_effect(context, routers, admin_state_up, active): return [agents[routers[0]]] self.l3routers_mock.side_effect = side_effect with self.router(name='router1', tenant_id=self.tenant_id, set_context=True): self.mock_uuid.return_value = second_uuid with self.router(name='router2', tenant_id=self.tenant_id, set_context=True): with self.metering_label(tenant_id=self.tenant_id, set_context=True): self.mock_add.assert_called_with( self.ctx, tools.UnorderedList(expected))
def _create_or_update_agent(self, context, agent): with context.session.begin(subtransactions=True): res_keys = ['agent_type', 'binary', 'host', 'topic'] res = dict((k, agent[k]) for k in res_keys) configurations_dict = agent.get('configurations', {}) res['configurations'] = jsonutils.dumps(configurations_dict) res['load'] = self._get_agent_load(agent) current_time = timeutils.utcnow() try: agent_db = self._get_agent_by_type_and_host( context, agent['agent_type'], agent['host']) if agent['topic'] == "l3_agent": self._update_l3_agent_router_bindings(context, agent_db.id) res['heartbeat_timestamp'] = current_time if agent.get('start_flag'): res['started_at'] = current_time greenthread.sleep(0) agent_db.update(res) except ext_agent.AgentNotFoundByTypeHost: greenthread.sleep(0) res['created_at'] = current_time res['started_at'] = current_time res['heartbeat_timestamp'] = current_time res['admin_state_up'] = True agent_db = agents_db.Agent(**res) greenthread.sleep(0) context.session.add(agent_db) greenthread.sleep(0)
def test_schedule_router_distributed(self): scheduler = l3_agent_scheduler.ChanceScheduler() agent = agents_db.Agent() agent.admin_state_up = True agent.heartbeat_timestamp = timeutils.utcnow() sync_router = {'id': 'foo_router_id', 'distributed': True} plugin = mock.Mock() plugin.get_router.return_value = sync_router plugin.get_l3_agents_hosting_routers.return_value = [] plugin.get_l3_agents.return_value = [agent] plugin.get_l3_agent_candidates.return_value = [agent] with mock.patch.object(scheduler, 'bind_router'): scheduler._schedule_router(plugin, self.adminContext, 'foo_router_id', None, {'gw_exists': True}) expected_calls = [ mock.call.get_router(mock.ANY, 'foo_router_id'), mock.call.schedule_snat_router(mock.ANY, 'foo_router_id', sync_router, True), mock.call.get_l3_agents_hosting_routers(mock.ANY, ['foo_router_id'], admin_state_up=True), mock.call.get_l3_agents(mock.ANY, active=True), mock.call.get_l3_agent_candidates(mock.ANY, sync_router, [agent], None), ] plugin.assert_has_calls(expected_calls)
def test_unbind_snat_servicenode(self): router_id = 'foo_router_id' core_plugin = mock.PropertyMock() type(self.dut)._core_plugin = core_plugin (self.dut._core_plugin.get_compute_ports_on_host_by_subnet.return_value ) = [] core_plugin.reset_mock() l3_notifier = mock.PropertyMock() type(self.dut).l3_rpc_notifier = l3_notifier binding = l3_dvrscheduler_db.CentralizedSnatL3AgentBinding( router_id=router_id, l3_agent_id='foo_l3_agent_id', l3_agent=agents_db.Agent()) with contextlib.nested( mock.patch.object(query.Query, 'one'), mock.patch.object(self.adminContext.session, 'delete'), mock.patch.object(query.Query, 'delete'), mock.patch.object( self.dut, 'get_subnet_ids_on_router')) as (mock_query, mock_session, mock_delete, mock_get_subnets): mock_query.return_value = binding mock_get_subnets.return_value = ['foo_subnet_id'] self.dut.unbind_snat_servicenode(self.adminContext, router_id) mock_get_subnets.assert_called_with(self.adminContext, router_id) self.assertTrue(mock_session.call_count) self.assertTrue(mock_delete.call_count) core_plugin.assert_called_once_with() l3_notifier.assert_called_once_with()
def test__get_enabled_agents_with_notification_required(self): network = {'id': 'foo_network_id', 'subnets': ['foo_subnet_id']} agent = agents_db.Agent() agent.admin_state_up = False agent.heartbeat_timestamp = timeutils.utcnow() self._test__get_enabled_agents(network, [agent], port_count=20, expected_warnings=0, expected_errors=1)
def test__schedule_network_no_existing_agents(self): agent = agents_db.Agent() agent.admin_state_up = True agent.heartbeat_timestamp = timeutils.utcnow() network = {'id': 'foo_net_id'} self._test__schedule_network(network, new_agents=None, existing_agents=[agent], expected_casts=0, expected_warnings=0)
def test_bind_routers_dvr(self): routers = [{'id': 'foo_router', 'distributed': True}] agent = agents_db.Agent(id='foo_agent') with mock.patch.object(self.scheduler, 'dvr_has_binding') as mock_dvr: with mock.patch.object(self.scheduler, 'bind_router') as mock_bind: self.scheduler.bind_routers(mock.ANY, routers, agent) mock_dvr.assert_called_once_with(mock.ANY, 'foo_router', 'foo_agent') self.assertFalse(mock_bind.called)
def _test__notify_agents_with_function( self, function, expected_scheduling=0, expected_casts=0): with mock.patch.object(self.notifier, '_schedule_network') as f: with mock.patch.object(self.notifier, '_get_enabled_agents') as g: agent = agents_db.Agent() agent.admin_state_up = True agent.heartbeat_timestamp = timeutils.utcnow() g.return_value = [agent] function() self.assertEqual(expected_scheduling, f.call_count) self.assertEqual(expected_casts, self.mock_cast.call_count)
def _get_agents(self, hosts): return [ agents_db.Agent(binary='neutron-dhcp-agent', host=host, topic=topics.DHCP_AGENT, configurations="", agent_type=constants.AGENT_TYPE_DHCP, created_at=timeutils.utcnow(), started_at=timeutils.utcnow(), heartbeat_timestamp=timeutils.utcnow()) for host in hosts ]
def _get_agents(self, hosts, agent_type): return [ agents_db.Agent(binary='foo-agent', host=host, agent_type=agent_type, topic='foo_topic', configurations="", created_at=timeutils.utcnow(), started_at=timeutils.utcnow(), heartbeat_timestamp=timeutils.utcnow()) for host in hosts ]
def _test__notify_agents(self, method, expected_scheduling=0, expected_casts=0): with mock.patch.object(self.notifier, '_schedule_network') as f: with mock.patch.object(self.notifier, '_get_enabled_agents') as g: agent = agents_db.Agent() agent.admin_state_up = True agent.heartbeat_timestamp = timeutils.utcnow() g.return_value = [agent] self.notifier._notify_agents(mock.Mock(), method, mock.ANY, 'foo_network_id') self.assertEqual(expected_scheduling, f.call_count) self.assertEqual(expected_casts, self.mock_cast.call_count)
def _add_agent(self, agent_id, agent_type, agent_host): with self.context.session.begin(subtransactions=True): now = timeutils.utcnow() agent = agents_db.Agent(id=agent_id, agent_type=agent_type, binary='foo_binary', topic='foo_topic', host=agent_host, created_at=now, started_at=now, admin_state_up=True, heartbeat_timestamp=now, configurations='') self.context.session.add(agent) return agent
def test_schedule_snat_router_with_snat_candidates(self): agent = agents_db.Agent() agent.admin_state_up = True agent.heartbeat_timestamp = timeutils.utcnow() with contextlib.nested( mock.patch.object(query.Query, 'first'), mock.patch.object(self.dut, 'get_l3_agents'), mock.patch.object(self.dut, 'get_snat_candidates'), mock.patch.object(self.dut, 'bind_snat_servicenode')) as ( mock_query, mock_agents, mock_candidates, mock_bind): mock_query.return_value = [] mock_agents.return_value = [agent] mock_candidates.return_value = [agent] self.dut.schedule_snat_router( self.adminContext, 'foo_router_id', mock.ANY, True) mock_bind.assert_called_once_with( self.adminContext, 'foo_router_id', [agent])
def test_add_metering_label_rpc_call(self): second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84' expected = { 'args': { 'routers': [{ 'status': 'ACTIVE', 'name': 'router1', 'gw_port_id': None, 'admin_state_up': True, 'tenant_id': self.tenant_id, '_metering_labels': [{ 'rules': [], 'id': second_uuid }], 'id': self.uuid }, { 'status': 'ACTIVE', 'name': 'router2', 'gw_port_id': None, 'admin_state_up': True, 'tenant_id': self.tenant_id, '_metering_labels': [{ 'rules': [], 'id': second_uuid }], 'id': second_uuid }] }, 'namespace': None, 'method': 'add_metering_label' } agent_host = 'l3_agent_host' agent = agents_db.Agent(host=agent_host) self.l3routers_mock.return_value = [agent] with self.router(name='router1', tenant_id=self.tenant_id, set_context=True): self.mock_uuid.return_value = second_uuid with self.router(name='router2', tenant_id=self.tenant_id, set_context=True): with self.metering_label(tenant_id=self.tenant_id, set_context=True): topic = "%s.%s" % (self.topic, agent_host) self.mock_cast.assert_called_with(self.ctx, expected, topic=topic)