Test Failed
Pull Request — master (#56)
by Carlos
01:55
created

build.tests.unit.test_main.TestMain.test_execute()   A

Complexity

Conditions 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nop 2
dl 0
loc 27
rs 9.352
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
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.post')
66
    def test_install_lldp_flow(self, mock_request):
67
        """Test install_lldp_flow method."""
68
        dpid = "00:00:00:00:00:00:00:01"
69
        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
70
        self.napp.controller.switches = {dpid: switch}
71
        event = get_kytos_event_mock(name='kytos/topology.switch.enabled',
72
                                     content={'dpid': dpid})
73
74
        self.napp.install_lldp_flow(event)
75
        mock_request.assert_called()
76
77
    @patch('kytos.core.buffers.KytosEventBuffer.put')
78
    @patch('napps.kytos.of_lldp.main.KytosEvent')
79
    @patch('kytos.core.controller.Controller.get_switch_by_dpid')
80
    @patch('napps.kytos.of_lldp.main.Main._unpack_non_empty')
81
    @patch('napps.kytos.of_lldp.main.UBInt32')
82
    @patch('napps.kytos.of_lldp.main.DPID')
83
    @patch('napps.kytos.of_lldp.main.LLDP')
84
    @patch('napps.kytos.of_lldp.main.Ethernet')
85
    def test_notify_uplink_detected(self, *args):
86
        """Test notify_uplink_detected method."""
87
        (mock_ethernet, mock_lldp, mock_dpid, mock_ubint32,
88
         mock_unpack_non_empty, mock_get_switch_by_dpid, mock_kytos_event,
89
         mock_buffer_put) = args
90
91
        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
92
        message = MagicMock()
93
        message.in_port = 1
94
        message.data = 'data'
95
        event = get_kytos_event_mock(name='kytos/of_core.v0x0[14].messages.in.'
96
                                          'ofpt_packet_in',
97
                                     content={'source': switch.connection,
98
                                              'message': message})
99
100
        ethernet = MagicMock()
101
        ethernet.ether_type = 0x88CC
102
        ethernet.data = 'eth_data'
103
        lldp = MagicMock()
104
        lldp.chassis_id.sub_value = 'chassis_id'
105
        lldp.port_id.sub_value = 'port_id'
106
        dpid = MagicMock()
107
        dpid.value = "00:00:00:00:00:00:00:02"
108
        port_b = MagicMock()
109
110
        mock_unpack_non_empty.side_effect = [ethernet, lldp, dpid, port_b]
111
        mock_get_switch_by_dpid.return_value = get_switch_mock(dpid.value,
112
                                                               0x04)
113
        mock_kytos_event.return_value = 'nni'
114
115
        self.napp.notify_uplink_detected(event)
116
117
        calls = [call(mock_ethernet, message.data),
118
                 call(mock_lldp, ethernet.data),
119
                 call(mock_dpid, lldp.chassis_id.sub_value),
120
                 call(mock_ubint32, lldp.port_id.sub_value)]
121
        mock_unpack_non_empty.assert_has_calls(calls)
122
        mock_buffer_put.assert_called_with('nni')
123
124
    @patch('napps.kytos.of_lldp.main.PO13')
125
    @patch('napps.kytos.of_lldp.main.PO10')
126
    @patch('napps.kytos.of_lldp.main.AO13')
127
    @patch('napps.kytos.of_lldp.main.AO10')
128
    def test_build_lldp_packet_out(self, *args):
129
        """Test _build_lldp_packet_out method."""
130
        (mock_ao10, mock_ao13, mock_po10, mock_po13) = args
131
132
        ao10 = MagicMock()
133
        ao13 = MagicMock()
134
        po10 = MagicMock()
135
        po10.actions = []
136
        po13 = MagicMock()
137
        po13.actions = []
138
139
        mock_ao10.return_value = ao10
140
        mock_ao13.return_value = ao13
141
        mock_po10.return_value = po10
142
        mock_po13.return_value = po13
143
144
        packet_out10 = self.napp._build_lldp_packet_out(0x01, 1, 'data1')
145
        packet_out13 = self.napp._build_lldp_packet_out(0x04, 2, 'data2')
146
        packet_out14 = self.napp._build_lldp_packet_out(0x05, 3, 'data3')
147
148
        self.assertEqual(packet_out10.data, 'data1')
149
        self.assertEqual(packet_out10.actions, [ao10])
150
        self.assertEqual(packet_out10.actions[0].port, 1)
151
152
        self.assertEqual(packet_out13.data, 'data2')
153
        self.assertEqual(packet_out13.actions, [ao13])
154
        self.assertEqual(packet_out13.actions[0].port, 2)
155
156
        self.assertIsNone(packet_out14)
157
158
    @patch('napps.kytos.of_lldp.main.settings')
159
    @patch('napps.kytos.of_lldp.main.EtherType')
160
    @patch('napps.kytos.of_lldp.main.Port13')
161
    @patch('napps.kytos.of_lldp.main.Port10')
162
    def test_build_lldp_flow(self, *args):
163
        """Test _build_lldp_flow method."""
164
        (mock_v0x01_port, mock_v0x04_port, mock_ethertype,
165
         mock_settings) = args
166
        self.napp.vlan_id = None
167
        mock_v0x01_port.OFPP_CONTROLLER = 123
168
        mock_v0x04_port.OFPP_CONTROLLER = 1234
169
170
        mock_ethertype.LLDP = 10
171
        mock_settings.FLOW_VLAN_VID = None
172
        mock_settings.FLOW_PRIORITY = 1500
173
174
        flow = {}
175
        match = {}
176
        flow['priority'] = 1500
177
        match['dl_type'] = 10
178
179
        flow['match'] = match
180
        expected_flow_v0x01 = flow.copy()
181
        expected_flow_v0x04 = flow.copy()
182
183
        expected_flow_v0x01['actions'] = [{'action_type': 'output',
184
                                           'port': 123}]
185
186
        expected_flow_v0x04['actions'] = [{'action_type': 'output',
187
                                           'port': 1234}]
188
189
        flow_mod10 = self.napp._build_lldp_flow(0x01)
190
        flow_mod13 = self.napp._build_lldp_flow(0x04)
191
192
        self.assertDictEqual(flow_mod10, expected_flow_v0x01)
193
        self.assertDictEqual(flow_mod13, expected_flow_v0x04)
194
195
    def test_unpack_non_empty(self):
196
        """Test _unpack_non_empty method."""
197
        desired_class = MagicMock()
198
        data = MagicMock()
199
        data.value = 'data'
200
201
        obj = self.napp._unpack_non_empty(desired_class, data)
202
203
        obj.unpack.assert_called_with('data')
204
205
    def test_get_data(self):
206
        """Test _get_data method."""
207
        req = MagicMock()
208
        interfaces = ['00:00:00:00:00:00:00:01:1', '00:00:00:00:00:00:00:01:2']
209
        req.get_json.return_value = {'interfaces': interfaces}
210
211
        data = self.napp._get_data(req)
212
213
        self.assertEqual(data, interfaces)
214
215
    def test_get_interfaces(self):
216
        """Test _get_interfaces method."""
217
        expected_interfaces = self.get_topology_interfaces()
218
219
        interfaces = self.napp._get_interfaces()
220
221
        self.assertEqual(interfaces, expected_interfaces)
222
223
    def test_get_interfaces_dict(self):
224
        """Test _get_interfaces_dict method."""
225
        interfaces = self.napp._get_interfaces()
226
        expected_interfaces = {inter.id: inter for inter in interfaces}
227
228
        interfaces_dict = self.napp._get_interfaces_dict(interfaces)
229
230
        self.assertEqual(interfaces_dict, expected_interfaces)
231
232
    def test_get_lldp_interfaces(self):
233
        """Test _get_lldp_interfaces method."""
234
        lldp_interfaces = self.napp._get_lldp_interfaces()
235
236
        expected_interfaces = ['00:00:00:00:00:00:00:01:1',
237
                               '00:00:00:00:00:00:00:01:2',
238
                               '00:00:00:00:00:00:00:02:1',
239
                               '00:00:00:00:00:00:00:02:2',
240
                               '00:00:00:00:00:00:00:03:1',
241
                               '00:00:00:00:00:00:00:03:2']
242
243
        self.assertEqual(lldp_interfaces, expected_interfaces)
244
245
    def test_rest_get_lldp_interfaces(self):
246
        """Test get_lldp_interfaces method."""
247
        api = get_test_client(self.napp.controller, self.napp)
248
        url = f'{self.server_name_url}/v1/interfaces'
249
        response = api.open(url, method='GET')
250
251
        expected_data = {"interfaces": ['00:00:00:00:00:00:00:01:1',
252
                                        '00:00:00:00:00:00:00:01:2',
253
                                        '00:00:00:00:00:00:00:02:1',
254
                                        '00:00:00:00:00:00:00:02:2',
255
                                        '00:00:00:00:00:00:00:03:1',
256
                                        '00:00:00:00:00:00:00:03:2']}
257
        self.assertEqual(response.json, expected_data)
258
        self.assertEqual(response.status_code, 200)
259
260
    def test_enable_disable_lldp_200(self):
261
        """Test 200 response for enable_lldp and disable_lldp methods."""
262
        data = {"interfaces": ['00:00:00:00:00:00:00:01:1',
263
                               '00:00:00:00:00:00:00:01:2',
264
                               '00:00:00:00:00:00:00:02:1',
265
                               '00:00:00:00:00:00:00:02:2',
266
                               '00:00:00:00:00:00:00:03:1',
267
                               '00:00:00:00:00:00:00:03:2']}
268
269
        api = get_test_client(self.napp.controller, self.napp)
270
271
        url = f'{self.server_name_url}/v1/interfaces/disable'
272
        disable_response = api.open(url, method='POST', json=data)
273
274
        url = f'{self.server_name_url}/v1/interfaces/enable'
275
        enable_response = api.open(url, method='POST', json=data)
276
277
        self.assertEqual(disable_response.status_code, 200)
278
        self.assertEqual(enable_response.status_code, 200)
279
280
    def test_enable_disable_lldp_404(self):
281
        """Test 404 response for enable_lldp and disable_lldp methods."""
282
        data = {"interfaces": []}
283
284
        self.napp.controller.switches = {}
285
        api = get_test_client(self.napp.controller, self.napp)
286
287
        url = f'{self.server_name_url}/v1/interfaces/disable'
288
        disable_response = api.open(url, method='POST', json=data)
289
290
        url = f'{self.server_name_url}/v1/interfaces/enable'
291
        enable_response = api.open(url, method='POST', json=data)
292
293
        self.assertEqual(disable_response.status_code, 404)
294
        self.assertEqual(enable_response.status_code, 404)
295
296
    def test_enable_disable_lldp_400(self):
297
        """Test 400 response for enable_lldp and disable_lldp methods."""
298
        data = {"interfaces": ['00:00:00:00:00:00:00:01:1',
299
                               '00:00:00:00:00:00:00:01:2',
300
                               '00:00:00:00:00:00:00:02:1',
301
                               '00:00:00:00:00:00:00:02:2',
302
                               '00:00:00:00:00:00:00:03:1',
303
                               '00:00:00:00:00:00:00:03:2',
304
                               '00:00:00:00:00:00:00:04:1']}
305
306
        api = get_test_client(self.napp.controller, self.napp)
307
308
        url = f'{self.server_name_url}/v1/interfaces/disable'
309
        disable_response = api.open(url, method='POST', json=data)
310
311
        url = f'{self.server_name_url}/v1/interfaces/enable'
312
        enable_response = api.open(url, method='POST', json=data)
313
314
        self.assertEqual(disable_response.status_code, 400)
315
        self.assertEqual(enable_response.status_code, 400)
316
317
    def test_get_time(self):
318
        """Test get polling time."""
319
        api = get_test_client(self.napp.controller, self.napp)
320
321
        url = f'{self.server_name_url}/v1/polling_time'
322
        response = api.open(url, method='GET')
323
324
        self.assertEqual(response.status_code, 200)
325
326
    def test_set_time(self):
327
        """Test update polling time."""
328
        data = {"polling_time": 5}
329
330
        api = get_test_client(self.napp.controller, self.napp)
331
332
        url = f'{self.server_name_url}/v1/polling_time'
333
        response = api.open(url, method='POST', json=data)
334
335
        self.assertEqual(response.status_code, 200)
336
        self.assertEqual(self.napp.polling_time, data['polling_time'])
337
338
    def test_set_time_400(self):
339
        """Test fail case the update polling time."""
340
        api = get_test_client(self.napp.controller, self.napp)
341
342
        url = f'{self.server_name_url}/v1/polling_time'
343
344
        data = {'polling_time': 'A'}
345
        response = api.open(url, method='POST', json=data)
346
        self.assertEqual(response.status_code, 400)
347