Debian Codegraph @

networking_l2gw/tests/unit/services/l2gateway/test_agent_scheduler.py

raw
1 # Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16
17 import copy
18 import datetime
19 from unittest import mock
20
21 from oslo_config import cfg
22 from oslo_service import loopingcall
23 from oslo_utils import timeutils
24
25 from neutron.agent.common import utils
26 from neutron.db import agents_db
27 from neutron import manager
28 from neutron.plugins.ml2 import rpc
29 from neutron.tests import base
30 from neutron_lib.agent import topics
31 from neutron_lib import context as neutron_context
32
33 from networking_l2gw.services.l2gateway import agent_scheduler
34 from networking_l2gw.services.l2gateway.common import config as l2gw_config
35 from networking_l2gw.services.l2gateway.common import constants as srv_const
36 from networking_l2gw.services.l2gateway.common import topics as l2gw_topics
37 from networking_l2gw.services.l2gateway.service_drivers import agent_api
38
39
40 def make_active_agent(fake_id, fake_agent_type, config=None):
41 agent_dict = dict(id=fake_id,
42 agent_type=fake_agent_type,
43 host='localhost_' + str(fake_id),
44 heartbeat_timestamp=timeutils.utcnow(),
45 started_at=timeutils.utcnow(),
46 configurations=config)
47 return agent_dict
48
49
50 def make_inactive_agent(fake_id, fake_agent_type, delta, config=None):
51 agent_dict = dict(id=fake_id,
52 agent_type=fake_agent_type,
53 host='remotehost_' + str(fake_id),
54 heartbeat_timestamp=(timeutils.utcnow() - datetime.
55 timedelta(delta)),
56 configurations=config)
57 return agent_dict
58
59
60 class FakePlugin(agents_db.AgentDbMixin):
61
62 def __init__(self):
63 self.notifier = rpc.AgentNotifierApi(topics.AGENT)
64
65
66 class TestAgentScheduler(base.BaseTestCase):
67
68 fake_a_agent_list = []
69 fake_i_agent_list = []
70
71 def setUp(self):
72 super().setUp()
73 l2gw_config.register_l2gw_opts_helper()
74 cfg.CONF.set_override('core_plugin',
75 "neutron.plugins.ml2.plugin.Ml2Plugin")
76 self.plugin = FakePlugin()
77 self.agent_rpc = agent_api.L2gatewayAgentApi(
78 l2gw_topics.L2GATEWAY_AGENT, cfg.CONF.host)
79 self.mock_context = mock.Mock()
80 mock.patch.object(neutron_context, 'get_admin_context',
81 return_value=self.mock_context).start()
82 self.context = self.mock_context
83 cfg.CONF.set_override('agent_down_time', 10)
84 cfg.CONF.set_override('periodic_monitoring_interval', 5)
85 self.agentsch = agent_scheduler.L2GatewayAgentScheduler(self.agent_rpc,
86 cfg.CONF)
87 self.agentsch._plugin = self.plugin
88 self.agentsch.context = self.context
89 self.agentsch.agent_ext_support = True
90 self.LOG = agent_scheduler.LOG
91
92 def populate_agent_lists(self, config=None):
93 self.fake_a_agent_list = []
94 self.fake_a_agent_list.append(make_active_agent(
95 '1000', srv_const.AGENT_TYPE_L2GATEWAY, config))
96
97 self.fake_i_agent_list = []
98 self.fake_i_agent_list.append(make_inactive_agent(
99 '2000', srv_const.AGENT_TYPE_L2GATEWAY, 52, config))
100
101 @mock.patch.object(loopingcall, 'FixedIntervalLoopingCall')
102 @mock.patch.object(agent_scheduler.LOG, 'debug')
103 @mock.patch.object(agent_scheduler.LOG, 'error')
104 def test_initialize_thread(self, err, debug, loop_call):
105 self.agentsch.initialize_thread()
106 self.assertTrue(loop_call.called)
107 self.assertTrue(debug.called)
108 self.assertFalse(err.called)
109
110 @mock.patch.object(loopingcall, 'FixedIntervalLoopingCall',
111 side_effect=RuntimeError)
112 def test_initialize_thread_loop_call_exception(self, loop_call):
113 with mock.patch.object(self.LOG, 'error') as log_err:
114 self.agentsch.initialize_thread()
115 self.assertTrue(loop_call.called)
116 self.assertTrue(log_err.called)
117
118 @mock.patch.object(manager, 'NeutronManager')
119 def test_select_agent_type_one_active(self, mgr):
120 config = {srv_const.L2GW_AGENT_TYPE: ''}
121 self.populate_agent_lists(config)
122
123 with mock.patch.object(self.LOG, 'exception'):
124 self.agentsch._l2gwplugin = mock.Mock()
125 self.agentsch._select_agent_type(self.context,
126 self.fake_a_agent_list)
127 self.agentsch.l2gwplugin.agent_rpc.set_monitor_agent_called_with(
128 self.context, self.fake_a_agent_list[0]['host'])
129
130 @mock.patch.object(manager, 'NeutronManager')
131 def test_select_agent_type_multiple_active(self, mgr):
132 config = {srv_const.L2GW_AGENT_TYPE: ''}
133 self.populate_agent_lists(config)
134 self.fake_a_agent_list.append(make_active_agent(
135 '1001', srv_const.AGENT_TYPE_L2GATEWAY, config))
136 self.agentsch._l2gwplugin = mock.Mock()
137
138 with mock.patch.object(self.LOG, 'exception'):
139 self.agentsch._select_agent_type(self.context,
140 self.fake_a_agent_list)
141 self.agentsch.l2gwplugin.agent_rpc.set_monitor_agent_called_with(
142 self.context, self.fake_a_agent_list[0]['host'])
143
144 def test_monitor_agent_state(self):
145 config = {srv_const.L2GW_AGENT_TYPE: ''}
146 self.populate_agent_lists(config)
147 fake_all_agent_list = copy.deepcopy(self.fake_i_agent_list)
148 fake_all_agent_list.extend(self.fake_a_agent_list)
149 self.fake_a_agent_list.append(make_active_agent(
150 '1001', srv_const.AGENT_TYPE_L2GATEWAY, config))
151
152 with mock.patch.object(self.agentsch,
153 '_select_agent_type') as select_agent, \
154 mock.patch.object(
155 self.plugin, 'get_agents',
156 return_value=fake_all_agent_list) as get_agent_list, \
157 mock.patch.object(utils, 'is_agent_down',
158 return_value=False) as is_agt:
159 self.agentsch.monitor_agent_state()
160 self.assertTrue(get_agent_list.called)
161 self.assertTrue(select_agent.called)
162 self.assertTrue(is_agt.called)
163
164 def test_monitor_agent_state_exception_get_agents(self):
165 with mock.patch.object(
166 self.plugin, 'get_agents',
167 side_effect=Exception) as get_agent_list, \
168 mock.patch.object(self.LOG, 'exception') as exception_log:
169 self.agentsch.monitor_agent_state()
170 self.assertTrue(get_agent_list.called)
171 self.assertTrue(exception_log.called)