Passed
Push — master ( acb8d9...8107f9 )
by Vinicius
03:43 queued 14s
created

TestMain.test_build_lldp_packet_out()   A

Complexity

Conditions 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nop 2
dl 0
loc 33
rs 9.256
c 0
b 0
f 0
1
"""Test Main methods."""
2
from unittest import TestCase
3
from unittest.mock import MagicMock, call, patch
4
5
from kytos.lib.helpers import (get_controller_mock, get_kytos_event_mock,
6
                               get_switch_mock, get_test_client)
7
8
from tests.helpers import get_topology_mock
9
10
11
# pylint: disable=protected-access
12
class TestMain(TestCase):
13
    """Tests for the Main class."""
14
15
    def setUp(self):
16
        """Execute steps before each tests."""
17
        self.server_name_url = 'http://127.0.0.1:8181/api/kytos/of_lldp'
18
19
        patch('kytos.core.helpers.run_on_thread', lambda x: x).start()
20
        # pylint: disable=bad-option-value, import-outside-toplevel
21
        from napps.kytos.of_lldp.main import Main
22
        self.addCleanup(patch.stopall)
23
24
        self.topology = get_topology_mock()
25
        controller = get_controller_mock()
26
        controller.switches = self.topology.switches
27
28
        self.napp = Main(controller)
29
30
    def get_topology_interfaces(self):
31
        """Return interfaces present in topology."""
32
        interfaces = []
33
        for switch in list(self.topology.switches.values()):
34
            interfaces += list(switch.interfaces.values())
35
        return interfaces
36
37
    @patch('kytos.core.buffers.KytosEventBuffer.put')
38
    @patch('napps.kytos.of_lldp.main.Main._build_lldp_packet_out')
39
    @patch('napps.kytos.of_lldp.main.KytosEvent')
40
    @patch('napps.kytos.of_lldp.main.VLAN')
41
    @patch('napps.kytos.of_lldp.main.Ethernet')
42
    @patch('napps.kytos.of_lldp.main.DPID')
43
    @patch('napps.kytos.of_lldp.main.LLDP')
44
    def test_execute(self, *args):
45
        """Test execute method."""
46
        (_, _, mock_ethernet, _, mock_kytos_event, mock_build_lldp_packet_out,
47
         mock_buffer_put) = args
48
49
        ethernet = MagicMock()
50
        ethernet.pack.return_value = 'pack'
51
        interfaces = self.get_topology_interfaces()
52
        po_args = [(interface.switch.connection.protocol.version,
53
                    interface.port_number, 'pack') for interface in interfaces]
54
55
        mock_ethernet.return_value = ethernet
56
        mock_kytos_event.side_effect = po_args
57
58
        self.napp.execute()
59
60
        mock_build_lldp_packet_out.assert_has_calls([call(*(arg))
61
                                                     for arg in po_args])
62
        mock_buffer_put.assert_has_calls([call(arg)
63
                                          for arg in po_args])
64
65
    @patch('requests.delete')
66
    @patch('requests.post')
67
    def test_handle_lldp_flows(self, mock_post, mock_delete):
68
        """Test handle_lldp_flow method."""
69
        dpid = "00:00:00:00:00:00:00:01"
70
        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
71
        self.napp.controller.switches = {dpid: switch}
72
        event_post = get_kytos_event_mock(name='kytos/topology.switch.enabled',
73
                                          content={'dpid': dpid})
74
75
        event_del = get_kytos_event_mock(name='kytos/topology.switch.disabled',
76
                                         content={'dpid': dpid})
77
78
        self.napp.handle_lldp_flows(event_post)
79
        mock_post.assert_called()
80
81
        self.napp.handle_lldp_flows(event_del)
82
        mock_delete.assert_called()
83
84
    @patch("time.sleep")
85
    @patch("requests.post")
86
    def test_handle_lldp_flows_retries(self, mock_post, _):
87
        """Test handle_lldp_flow method retries."""
88
        dpid = "00:00:00:00:00:00:00:01"
89
        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
90
        self.napp.controller.switches = {dpid: switch}
91
        event_post = get_kytos_event_mock(name="kytos/topology.switch.enabled",
92
                                          content={"dpid": dpid})
93
94
        mock = MagicMock()
95
        mock.request.method = "POST"
96
        mock.status_code = 500
97
        mock.text = "some_err"
98
        mock_post.return_value = mock
99
        self.napp._handle_lldp_flows(event_post)
100
        self.assertTrue(mock_post.call_count, 3)
101
102
    @patch('kytos.core.buffers.KytosEventBuffer.put')
103
    @patch('napps.kytos.of_lldp.main.KytosEvent')
104
    @patch('kytos.core.controller.Controller.get_switch_by_dpid')
105
    @patch('napps.kytos.of_lldp.main.Main._unpack_non_empty')
106
    @patch('napps.kytos.of_lldp.main.UBInt32')
107
    @patch('napps.kytos.of_lldp.main.DPID')
108
    @patch('napps.kytos.of_lldp.main.LLDP')
109
    @patch('napps.kytos.of_lldp.main.Ethernet')
110
    def test_notify_uplink_detected(self, *args):
111
        """Test notify_uplink_detected method."""
112
        (mock_ethernet, mock_lldp, mock_dpid, mock_ubint32,
113
         mock_unpack_non_empty, mock_get_switch_by_dpid, mock_kytos_event,
114
         mock_buffer_put) = args
115
116
        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
117
        message = MagicMock()
118
        message.in_port = 1
119
        message.data = 'data'
120
        event = get_kytos_event_mock(name='kytos/of_core.v0x0[14].messages.in.'
121
                                          'ofpt_packet_in',
122
                                     content={'source': switch.connection,
123
                                              'message': message})
124
125
        ethernet = MagicMock()
126
        ethernet.ether_type = 0x88CC
127
        ethernet.data = 'eth_data'
128
        lldp = MagicMock()
129
        lldp.chassis_id.sub_value = 'chassis_id'
130
        lldp.port_id.sub_value = 'port_id'
131
        dpid = MagicMock()
132
        dpid.value = "00:00:00:00:00:00:00:02"
133
        port_b = MagicMock()
134
135
        mock_unpack_non_empty.side_effect = [ethernet, lldp, dpid, port_b]
136
        mock_get_switch_by_dpid.return_value = get_switch_mock(dpid.value,
137
                                                               0x04)
138
        mock_kytos_event.return_value = 'nni'
139
140
        self.napp.notify_uplink_detected(event)
141
142
        calls = [call(mock_ethernet, message.data),
143
                 call(mock_lldp, ethernet.data),
144
                 call(mock_dpid, lldp.chassis_id.sub_value),
145
                 call(mock_ubint32, lldp.port_id.sub_value)]
146
        mock_unpack_non_empty.assert_has_calls(calls)
147
        mock_buffer_put.assert_called_with('nni')
148
149
    @patch('napps.kytos.of_lldp.main.PO13')
150
    @patch('napps.kytos.of_lldp.main.PO10')
151
    @patch('napps.kytos.of_lldp.main.AO13')
152
    @patch('napps.kytos.of_lldp.main.AO10')
153
    def test_build_lldp_packet_out(self, *args):
154
        """Test _build_lldp_packet_out method."""
155
        (mock_ao10, mock_ao13, mock_po10, mock_po13) = args
156
157
        ao10 = MagicMock()
158
        ao13 = MagicMock()
159
        po10 = MagicMock()
160
        po10.actions = []
161
        po13 = MagicMock()
162
        po13.actions = []
163
164
        mock_ao10.return_value = ao10
165
        mock_ao13.return_value = ao13
166
        mock_po10.return_value = po10
167
        mock_po13.return_value = po13
168
169
        packet_out10 = self.napp._build_lldp_packet_out(0x01, 1, 'data1')
170
        packet_out13 = self.napp._build_lldp_packet_out(0x04, 2, 'data2')
171
        packet_out14 = self.napp._build_lldp_packet_out(0x05, 3, 'data3')
172
173
        self.assertEqual(packet_out10.data, 'data1')
174
        self.assertEqual(packet_out10.actions, [ao10])
175
        self.assertEqual(packet_out10.actions[0].port, 1)
176
177
        self.assertEqual(packet_out13.data, 'data2')
178
        self.assertEqual(packet_out13.actions, [ao13])
179
        self.assertEqual(packet_out13.actions[0].port, 2)
180
181
        self.assertIsNone(packet_out14)
182
183
    @patch('napps.kytos.of_lldp.main.settings')
184
    @patch('napps.kytos.of_lldp.main.EtherType')
185
    @patch('napps.kytos.of_lldp.main.Port13')
186
    @patch('napps.kytos.of_lldp.main.Port10')
187
    def test_build_lldp_flow(self, *args):
188
        """Test _build_lldp_flow method."""
189
        (mock_v0x01_port, mock_v0x04_port, mock_ethertype,
190
         mock_settings) = args
191
        self.napp.vlan_id = None
192
        mock_v0x01_port.OFPP_CONTROLLER = 123
193
        mock_v0x04_port.OFPP_CONTROLLER = 1234
194
195
        mock_ethertype.LLDP = 10
196
        mock_settings.FLOW_VLAN_VID = None
197
        mock_settings.FLOW_PRIORITY = 1500
198
        mock_settings.TABLE_ID = 0
199
200
        flow = {}
201
        match = {}
202
        flow['priority'] = 1500
203
        flow['table_id'] = 0
204
        match['dl_type'] = 10
205
206
        flow['match'] = match
207
        expected_flow_v0x01 = flow.copy()
208
        expected_flow_v0x04 = flow.copy()
209
210
        expected_flow_v0x01['actions'] = [{'action_type': 'output',
211
                                           'port': 123}]
212
213
        expected_flow_v0x04['actions'] = [{'action_type': 'output',
214
                                           'port': 1234}]
215
216
        flow_mod10 = self.napp._build_lldp_flow(0x01)
217
        flow_mod13 = self.napp._build_lldp_flow(0x04)
218
219
        self.assertDictEqual(flow_mod10, expected_flow_v0x01)
220
        self.assertDictEqual(flow_mod13, expected_flow_v0x04)
221
222
    def test_unpack_non_empty(self):
223
        """Test _unpack_non_empty method."""
224
        desired_class = MagicMock()
225
        data = MagicMock()
226
        data.value = 'data'
227
228
        obj = self.napp._unpack_non_empty(desired_class, data)
229
230
        obj.unpack.assert_called_with('data')
231
232
    def test_get_data(self):
233
        """Test _get_data method."""
234
        req = MagicMock()
235
        interfaces = ['00:00:00:00:00:00:00:01:1', '00:00:00:00:00:00:00:01:2']
236
        req.get_json.return_value = {'interfaces': interfaces}
237
238
        data = self.napp._get_data(req)
239
240
        self.assertEqual(data, interfaces)
241
242
    def test_get_interfaces(self):
243
        """Test _get_interfaces method."""
244
        expected_interfaces = self.get_topology_interfaces()
245
246
        interfaces = self.napp._get_interfaces()
247
248
        self.assertEqual(interfaces, expected_interfaces)
249
250
    def test_get_interfaces_dict(self):
251
        """Test _get_interfaces_dict method."""
252
        interfaces = self.napp._get_interfaces()
253
        expected_interfaces = {inter.id: inter for inter in interfaces}
254
255
        interfaces_dict = self.napp._get_interfaces_dict(interfaces)
256
257
        self.assertEqual(interfaces_dict, expected_interfaces)
258
259
    def test_get_lldp_interfaces(self):
260
        """Test _get_lldp_interfaces method."""
261
        lldp_interfaces = self.napp._get_lldp_interfaces()
262
263
        expected_interfaces = ['00:00:00:00:00:00:00:01:1',
264
                               '00:00:00:00:00:00:00:01:2',
265
                               '00:00:00:00:00:00:00:02:1',
266
                               '00:00:00:00:00:00:00:02:2',
267
                               '00:00:00:00:00:00:00:03:1',
268
                               '00:00:00:00:00:00:00:03:2']
269
270
        self.assertEqual(lldp_interfaces, expected_interfaces)
271
272
    def test_rest_get_lldp_interfaces(self):
273
        """Test get_lldp_interfaces method."""
274
        api = get_test_client(self.napp.controller, self.napp)
275
        url = f'{self.server_name_url}/v1/interfaces'
276
        response = api.open(url, method='GET')
277
278
        expected_data = {"interfaces": ['00:00:00:00:00:00:00:01:1',
279
                                        '00:00:00:00:00:00:00:01:2',
280
                                        '00:00:00:00:00:00:00:02:1',
281
                                        '00:00:00:00:00:00:00:02:2',
282
                                        '00:00:00:00:00:00:00:03:1',
283
                                        '00:00:00:00:00:00:00:03:2']}
284
        self.assertEqual(response.json, expected_data)
285
        self.assertEqual(response.status_code, 200)
286
287
    def test_enable_disable_lldp_200(self):
288
        """Test 200 response for enable_lldp and disable_lldp methods."""
289
        data = {"interfaces": ['00:00:00:00:00:00:00:01:1',
290
                               '00:00:00:00:00:00:00:01:2',
291
                               '00:00:00:00:00:00:00:02:1',
292
                               '00:00:00:00:00:00:00:02:2',
293
                               '00:00:00:00:00:00:00:03:1',
294
                               '00:00:00:00:00:00:00:03:2']}
295
296
        api = get_test_client(self.napp.controller, self.napp)
297
298
        url = f'{self.server_name_url}/v1/interfaces/disable'
299
        disable_response = api.open(url, method='POST', json=data)
300
301
        url = f'{self.server_name_url}/v1/interfaces/enable'
302
        enable_response = api.open(url, method='POST', json=data)
303
304
        self.assertEqual(disable_response.status_code, 200)
305
        self.assertEqual(enable_response.status_code, 200)
306
307
    def test_enable_disable_lldp_404(self):
308
        """Test 404 response for enable_lldp and disable_lldp methods."""
309
        data = {"interfaces": []}
310
311
        self.napp.controller.switches = {}
312
        api = get_test_client(self.napp.controller, self.napp)
313
314
        url = f'{self.server_name_url}/v1/interfaces/disable'
315
        disable_response = api.open(url, method='POST', json=data)
316
317
        url = f'{self.server_name_url}/v1/interfaces/enable'
318
        enable_response = api.open(url, method='POST', json=data)
319
320
        self.assertEqual(disable_response.status_code, 404)
321
        self.assertEqual(enable_response.status_code, 404)
322
323
    def test_enable_disable_lldp_400(self):
324
        """Test 400 response for enable_lldp and disable_lldp methods."""
325
        data = {"interfaces": ['00:00:00:00:00:00:00:01:1',
326
                               '00:00:00:00:00:00:00:01:2',
327
                               '00:00:00:00:00:00:00:02:1',
328
                               '00:00:00:00:00:00:00:02:2',
329
                               '00:00:00:00:00:00:00:03:1',
330
                               '00:00:00:00:00:00:00:03:2',
331
                               '00:00:00:00:00:00:00:04:1']}
332
333
        api = get_test_client(self.napp.controller, self.napp)
334
335
        url = f'{self.server_name_url}/v1/interfaces/disable'
336
        disable_response = api.open(url, method='POST', json=data)
337
338
        url = f'{self.server_name_url}/v1/interfaces/enable'
339
        enable_response = api.open(url, method='POST', json=data)
340
341
        self.assertEqual(disable_response.status_code, 400)
342
        self.assertEqual(enable_response.status_code, 400)
343
344
    def test_get_time(self):
345
        """Test get polling time."""
346
        api = get_test_client(self.napp.controller, self.napp)
347
348
        url = f'{self.server_name_url}/v1/polling_time'
349
        response = api.open(url, method='GET')
350
351
        self.assertEqual(response.status_code, 200)
352
353
    def test_set_time(self):
354
        """Test update polling time."""
355
        data = {"polling_time": 5}
356
357
        api = get_test_client(self.napp.controller, self.napp)
358
359
        url = f'{self.server_name_url}/v1/polling_time'
360
        response = api.open(url, method='POST', json=data)
361
362
        self.assertEqual(response.status_code, 200)
363
        self.assertEqual(self.napp.polling_time, data['polling_time'])
364
365
    def test_set_time_400(self):
366
        """Test fail case the update polling time."""
367
        api = get_test_client(self.napp.controller, self.napp)
368
369
        url = f'{self.server_name_url}/v1/polling_time'
370
371
        data = {'polling_time': 'A'}
372
        response = api.open(url, method='POST', json=data)
373
        self.assertEqual(response.status_code, 400)
374