Пример #1
0
    def test_create_fip_agent_gw_port_if_not_exists_with_l3_agent(self):
        fport_db = {'id': _uuid()}
        self.mixin._get_agent_gw_ports_exist_for_network = mock.Mock(
            return_value=fport_db)

        fipagent = agent_obj.Agent(
            self.ctx,
            id=_uuid(),
            binary='foo-agent',
            host='host',
            agent_type='L3 agent',
            topic='foo_topic',
            configurations={"agent_mode": "dvr_no_external"})
        self.mixin._get_agent_by_type_and_host = mock.Mock(
            return_value=fipagent)
        fport = self.mixin.create_fip_agent_gw_port_if_not_exists(
            self.ctx, 'network_id', 'host')
        self.assertIsNone(fport)

        fipagent = agent_obj.Agent(self.ctx,
                                   id=_uuid(),
                                   binary='foo-agent',
                                   host='host',
                                   agent_type='L3 agent',
                                   topic='foo_topic',
                                   configurations={"agent_mode": "dvr"})
        self.mixin._get_agent_by_type_and_host = mock.Mock(
            return_value=fipagent)
        fport = self.mixin.create_fip_agent_gw_port_if_not_exists(
            self.ctx, 'network_id', 'host')
        self.assertIsNotNone(fport)
    def _create_dhcp_agents(self):
        timestamp = datetime.datetime.now()
        dhcp_agent_ids = [uuidutils.generate_uuid() for x in range(2)]
        dhcp_agent_1 = agent.Agent(self.ctx,
                                   id=dhcp_agent_ids[0],
                                   agent_type='DHCP Agent',
                                   topic='fake_topic',
                                   host='fake_host',
                                   binary='fake_binary',
                                   created_at=timestamp,
                                   started_at=timestamp,
                                   heartbeat_timestamp=timestamp,
                                   configurations={},
                                   load=0)
        dhcp_agent_1.create()

        dhcp_agent_2 = agent.Agent(self.ctx,
                                   id=dhcp_agent_ids[1],
                                   agent_type='DHCP Agent',
                                   topic='fake_topic',
                                   host='fake_host_1',
                                   binary='fake_binary',
                                   created_at=timestamp,
                                   started_at=timestamp,
                                   heartbeat_timestamp=timestamp,
                                   configurations={},
                                   load=0)
        dhcp_agent_2.create()
        return [dhcp_agent_1.id, dhcp_agent_2.id]
 def test__get_enabled_agents(self):
     agent1 = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid())
     agent1.admin_state_up = True
     agent1.heartbeat_timestamp = timeutils.utcnow()
     agent2 = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid())
     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 = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid())
     agent1.admin_state_up = True
     agent1.heartbeat_timestamp = timeutils.utcnow()
     agent2 = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid())
     agent2.admin_state_up = False
     agent2.heartbeat_timestamp = timeutils.utcnow()
     network = {'id': 'foo_network_id'}
     self._test__get_enabled_agents(network, agents=[agent1, agent2])
Пример #5
0
 def test__get_enabled_agents_with_inactive_ones(self):
     agent1 = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid())
     agent1.admin_state_up = True
     agent1.heartbeat_timestamp = timeutils.utcnow()
     agent2 = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid())
     agent2.admin_state_up = True
     # This is effectively an inactive agent
     agent2.heartbeat_timestamp = datetime.datetime(2000, 1, 1, 0, 0)
     self._test__get_enabled_agents(network_id='foo_network_id',
                                    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,
            'distributed': False,
            '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,
            'distributed': False,
            'tenant_id': self.tenant_id,
            '_metering_labels': [{
                'rules': [],
                'id': second_uuid
            }],
            'id': second_uuid
        }]

        # bind each router to a specific agent
        agent1 = agent_obj.Agent(mock.ANY, host='agent1')
        agent2 = agent_obj.Agent(mock.ANY, 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))
Пример #7
0
    def setUp(self):
        super(L3AgentNDPProxyTestFramework, self).setUp()
        self.conf.set_override('extensions', ['ndp_proxy'], 'agent')
        self.agent = neutron_l3_agent.L3NATAgentWithStateReport(HOSTNAME,
                                                                self.conf)
        self.np_ext = np.NDPProxyAgentExtension()

        port_id1 = uuidutils.generate_uuid()
        port1_binding = ports_obj.PortBinding(port_id=port_id1,
                                              host=self.agent.host)
        port1_obj = ports_obj.Port(id=port_id1, bindings=[port1_binding])
        port_id2 = uuidutils.generate_uuid()
        port2_binding = ports_obj.PortBinding(port_id=port_id1,
                                              host='fake_host')
        port2_obj = ports_obj.Port(id=port_id2, bindings=[port2_binding])
        self.ports = [port1_obj, port2_obj]
        self.port_binding_map = {port_id1: port1_binding,
                                 port_id2: port2_binding}
        self.ndpproxy1 = np_obj.NDPProxy(
            context=None, id=uuidutils.generate_uuid(),
            router_id=uuidutils.generate_uuid(),
            port_id=port_id1, ip_address='2002::1:3')
        self.ndpproxy2 = np_obj.NDPProxy(
            context=None, id=uuidutils.generate_uuid(),
            router_id=uuidutils.generate_uuid(),
            port_id=port_id2, ip_address='2002::1:4')
        self.ndp_proxies = [self.ndpproxy1, self.ndpproxy2]
        agent_configurations = {
            'agent_mode': constants.L3_AGENT_MODE_DVR_NO_EXTERNAL}
        self.agent_obj = agent_obj.Agent(
            id=uuidutils.generate_uuid(), host=self.agent.host,
            agent_type=constants.AGENT_TYPE_L3,
            configurations=agent_configurations)
        self._set_pull_mock()
 def test__get_enabled_agents_with_notification_required(self):
     network = {'id': 'foo_network_id', 'subnets': ['foo_subnet_id']}
     agent = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid())
     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)
Пример #9
0
    def create_or_update_agent(self, context, agent_state):
        """Registers new agent in the database or updates existing.

        Returns tuple of agent status and state.
        Status is from server point of view: alive, new or revived.
        It could be used by agent to do some sync with the server if needed.
        """
        status = agent_consts.AGENT_ALIVE
        with context.session.begin(subtransactions=True):
            res_keys = ['agent_type', 'binary', 'host', 'topic']
            res = dict((k, agent_state[k]) for k in res_keys)
            if 'availability_zone' in agent_state:
                res['availability_zone'] = agent_state['availability_zone']
            configurations_dict = agent_state.get('configurations', {})
            res['configurations'] = jsonutils.dumps(configurations_dict)
            resource_versions_dict = agent_state.get('resource_versions')
            if resource_versions_dict:
                res['resource_versions'] = jsonutils.dumps(
                    resource_versions_dict)
            res['load'] = self._get_agent_load(agent_state)
            current_time = timeutils.utcnow()
            try:
                agent = self._get_agent_by_type_and_host(
                    context, agent_state['agent_type'], agent_state['host'])
                if not agent.is_active:
                    status = agent_consts.AGENT_REVIVED
                    if 'resource_versions' not in agent_state:
                        # updating agent_state with resource_versions taken
                        # from db so that
                        # _update_local_agent_resource_versions() will call
                        # version_manager and bring it up to date
                        agent_state['resource_versions'] = self._get_dict(
                            agent, 'resource_versions', ignore_missing=True)
                res['heartbeat_timestamp'] = current_time
                if agent_state.get('start_flag'):
                    res['started_at'] = current_time
                greenthread.sleep(0)
                self._log_heartbeat(agent_state, agent, configurations_dict)
                agent.update_fields(res)
                agent.update()
                event_type = events.AFTER_UPDATE
            except agent_exc.AgentNotFoundByTypeHost:
                greenthread.sleep(0)
                res['created_at'] = current_time
                res['started_at'] = current_time
                res['heartbeat_timestamp'] = current_time
                res['admin_state_up'] = cfg.CONF.enable_new_agents
                agent = agent_obj.Agent(context=context, **res)
                greenthread.sleep(0)
                agent.create()
                event_type = events.AFTER_CREATE
                self._log_heartbeat(agent_state, agent, configurations_dict)
                status = agent_consts.AGENT_NEW
            greenthread.sleep(0)

        registry.notify(resources.AGENT, event_type, self, context=context,
                        host=agent_state['host'], plugin=self,
                        agent=agent_state)
        return status, agent_state
 def test__schedule_network_no_existing_agents(self):
     agent = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid())
     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)
Пример #11
0
 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 = agent_obj.Agent(mock.ANY, id=uuidutils.generate_uuid(),
                                     host='host', topic='topic')
             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)
Пример #12
0
 def _get_agents(self, hosts, agent_type):
     return [
         agent_obj.Agent(context=self.context,
                         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
     ]
Пример #13
0
 def setUp(self):
     super(NDPProxyExtensionTestCaseBase, self).setUp()
     self.context = context.get_admin_context()
     self.connection = mock.Mock()
     self.ext_port_id = _uuid()
     self.ex_net_id = _uuid()
     self.ex_gw_port = {
         'id': self.ext_port_id,
         'network_id': self.ex_net_id,
         'gw_port_host': HOSTNAME
     }
     self.fake_router_id = _uuid()
     self.port_id = _uuid()
     self.agent_api = l3_ext_api.L3AgentExtensionAPI(None, None)
     self.np_ext = np.NDPProxyAgentExtension()
     self.np_ext.consume_api(self.agent_api)
     self.np_ext.initialize(self.connection, lib_const.L3_AGENT_MODE)
     self.ndpproxy = np_obj.NDPProxy(context=None,
                                     id=_uuid(),
                                     router_id=self.fake_router_id,
                                     port_id=self.port_id,
                                     ip_address='2002::1:3')
     port_binding = ports_obj.PortBinding(port_id=self.port_id,
                                          host=HOSTNAME)
     port_obj = ports_obj.Port(id=self.port_id, bindings=[port_binding])
     self.ndp_proxies = [self.ndpproxy]
     self.ports = [port_obj]
     agent_configurations = {
         'agent_mode': lib_const.L3_AGENT_MODE_DVR_NO_EXTERNAL
     }
     self.agent_obj = agent_obj.Agent(id=_uuid(),
                                      host=HOSTNAME,
                                      agent_type=lib_const.AGENT_TYPE_L3,
                                      configurations=agent_configurations)
     self.ip_wrapper = mock.patch('neutron.agent.linux.'
                                  'ip_lib.IPWrapper').start()
     self._set_pull_mock()
Пример #14
0
 def test_resources_synced_10(self):
     obj = agent.Agent()
     primitive = obj.obj_to_primitive(target_version='1.0')
     self.assertNotIn('resources_synced',
                      primitive['versioned_object.data'])