Passed
Push — master ( 61767c...941289 )
by Italo Valcy
01:54 queued 12s
created

TestMain.test_notify_port_created()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
"""Module to test the main napp file."""
2
import json
3
import time
4
from unittest import TestCase
5
from unittest.mock import MagicMock, create_autospec, patch
6
7
from kytos.core.interface import Interface
8
from kytos.core.link import Link
9
from kytos.core.switch import Switch
10
from kytos.lib.helpers import (get_interface_mock, get_link_mock,
11
                               get_switch_mock, get_test_client)
12
from napps.kytos.topology.exceptions import RestoreError
13
from tests.unit.helpers import get_controller_mock, get_napp_urls
14
15
16
# pylint: disable=too-many-public-methods
17
class TestMain(TestCase):
18
    """Test the Main class."""
19
20
    # pylint: disable=too-many-public-methods, protected-access,C0302
21
22
    def setUp(self):
23
        """Execute steps before each tests.
24
25
        Set the server_name_url_url from kytos/topology
26
        """
27
        self.server_name_url = 'http://localhost:8181/api/kytos/topology'
28
29
        patch('kytos.core.helpers.run_on_thread', lambda x: x).start()
30
        # pylint: disable=import-outside-toplevel
31
        from napps.kytos.topology.main import Main
32
        self.addCleanup(patch.stopall)
33
34
        self.napp = Main(get_controller_mock())
35
36
    def test_get_event_listeners(self):
37
        """Verify all event listeners registered."""
38
        expected_events = ['kytos/core.shutdown',
39
                           'kytos/core.shutdown.kytos/topology',
40
                           'kytos/maintenance.start_link',
41
                           'kytos/maintenance.end_link',
42
                           'kytos/maintenance.start_switch',
43
                           'kytos/maintenance.end_switch',
44
                           'kytos/storehouse.loaded',
45
                           '.*.network_status.updated',
46
                           '.*.interface.is.nni',
47
                           '.*.connection.lost',
48
                           '.*.switch.interface.created',
49
                           '.*.switch.interface.deleted',
50
                           '.*.switch.interface.link_down',
51
                           '.*.switch.interface.link_up',
52
                           '.*.switch.(new|reconnected)',
53
                           '.*.switch.port.created',
54
                           'kytos/topology.*.metadata.*']
55
        actual_events = self.napp.listeners()
56
        self.assertCountEqual(expected_events, actual_events)
57
58 View Code Duplication
    def test_verify_api_urls(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
59
        """Verify all APIs registered."""
60
        expected_urls = [
61
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/interfaces'),
62
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/switches'),
63
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/links'),
64
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/'),
65
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
66
          '/api/kytos/topology/v3/interfaces/switch/<dpid>/disable'),
67
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
68
          '/api/kytos/topology/v3/interfaces/switch/<dpid>/enable'),
69
         ({'key': '[key]', 'interface_id': '[interface_id]'},
70
          {'OPTIONS', 'DELETE'},
71
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata/<key>'),
72
         ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'},
73
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'),
74
         ({'interface_id': '[interface_id]'}, {'GET', 'OPTIONS', 'HEAD'},
75
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'),
76
         ({'interface_disable_id': '[interface_disable_id]'},
77
          {'POST', 'OPTIONS'},
78
          '/api/kytos/topology/v3/interfaces/<interface_disable_id>/disable'),
79
         ({'interface_enable_id': '[interface_enable_id]'},
80
          {'POST', 'OPTIONS'},
81
          '/api/kytos/topology/v3/interfaces/<interface_enable_id>/enable'),
82
         ({'dpid': '[dpid]', 'key': '[key]'}, {'OPTIONS', 'DELETE'},
83
          '/api/kytos/topology/v3/switches/<dpid>/metadata/<key>'),
84
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
85
          '/api/kytos/topology/v3/switches/<dpid>/metadata'),
86
         ({'dpid': '[dpid]'}, {'GET', 'OPTIONS', 'HEAD'},
87
          '/api/kytos/topology/v3/switches/<dpid>/metadata'),
88
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
89
          '/api/kytos/topology/v3/switches/<dpid>/disable'),
90
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
91
          '/api/kytos/topology/v3/switches/<dpid>/enable'),
92
         ({'link_id': '[link_id]', 'key': '[key]'}, {'OPTIONS', 'DELETE'},
93
          '/api/kytos/topology/v3/links/<link_id>/metadata/<key>'),
94
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
95
          '/api/kytos/topology/v3/links/<link_id>/metadata'),
96
         ({'link_id': '[link_id]'}, {'GET', 'OPTIONS', 'HEAD'},
97
          '/api/kytos/topology/v3/links/<link_id>/metadata'),
98
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
99
          '/api/kytos/topology/v3/links/<link_id>/disable'),
100
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
101
          '/api/kytos/topology/v3/links/<link_id>/enable')]
102
103
        urls = get_napp_urls(self.napp)
104
        self.assertEqual(expected_urls, urls)
105
106
    def test_get_link_or_create(self):
107
        """Test _get_link_or_create."""
108
        dpid_a = "00:00:00:00:00:00:00:01"
109
        dpid_b = "00:00:00:00:00:00:00:02"
110
        mock_switch_a = get_switch_mock(dpid_a, 0x04)
111
        mock_switch_b = get_switch_mock(dpid_b, 0x04)
112
        mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a)
113
        mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b)
114
        mock_interface_a.id = dpid_a
115
        mock_interface_b.id = dpid_b
116
117
        link = self.napp._get_link_or_create(mock_interface_a,
118
                                             mock_interface_b)
119
        self.assertEqual(link.endpoint_a.id, dpid_a)
120
        self.assertEqual(link.endpoint_b.id, dpid_b)
121
122
    def test_get_link_from_interface(self):
123
        """Test _get_link_from_interface."""
124
        mock_switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
125
        mock_switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04)
126
        mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a)
127
        mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b)
128
        mock_interface_c = get_interface_mock('s2-eth1', 2, mock_switch_b)
129
        mock_link = get_link_mock(mock_interface_a, mock_interface_b)
130
        self.napp.links = {'0e2b5d7bc858b9f38db11b69': mock_link}
131
        response = self.napp._get_link_from_interface(mock_interface_a)
132
        self.assertEqual(response, mock_link)
133
134
        response = self.napp._get_link_from_interface(mock_interface_c)
135
        self.assertEqual(response, None)
136
137
    def test_get_topology(self):
138
        """Test get_topology."""
139
        dpid_a = "00:00:00:00:00:00:00:01"
140
        dpid_b = "00:00:00:00:00:00:00:02"
141
        expected = {
142
                      "topology": {
143
                        "switches": {
144
                          "00:00:00:00:00:00:00:01": {
145
                            "metadata": {
146
                              "lat": "0.0",
147
                              "lng": "-30.0"
148
                            }
149
                          },
150
                          "00:00:00:00:00:00:00:02": {
151
                            "metadata": {
152
                              "lat": "0.0",
153
                              "lng": "-30.0"
154
                            }
155
                          }
156
                        },
157
                        "links": {
158
                          "cf0f4071be4": {
159
                            "id": "cf0f4071be4"
160
                          }
161
                        }
162
                      }
163
                    }
164
165
        mock_switch_a = get_switch_mock(dpid_a, 0x04)
166
        mock_switch_b = get_switch_mock(dpid_b, 0x04)
167
        mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a)
168
        mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b)
169
170
        mock_link = get_link_mock(mock_interface_a, mock_interface_b)
171
        mock_link.id = 'cf0f4071be4'
172
        mock_switch_a.id = dpid_a
173
        mock_switch_a.as_dict.return_value = {'metadata': {'lat': '0.0',
174
                                              'lng': '-30.0'}}
175
        mock_switch_b.id = dpid_b
176
        mock_switch_b.as_dict.return_value = {'metadata': {'lat': '0.0',
177
                                              'lng': '-30.0'}}
178
179
        self.napp.controller.switches = {dpid_a: mock_switch_a,
180
                                         dpid_b: mock_switch_b}
181
182
        self.napp.links = {"cf0f4071be4": mock_link}
183
        mock_link.as_dict.return_value = {"id": "cf0f4071be4"}
184
        api = get_test_client(self.napp.controller, self.napp)
185
186
        url = f'{self.server_name_url}/v3/'
187
        response = api.get(url)
188
        self.assertEqual(response.status_code, 200)
189
        self.assertEqual(json.loads(response.data), expected)
190
191
    @patch('napps.kytos.topology.main.StoreHouse.get_data')
192
    def test_load_network_status(self, mock_storehouse_get_data):
193
        """Test _load_network_status."""
194
        link_id = \
195
            'cf0f4071be426b3f745027f5d22bc61f8312ae86293c9b28e7e66015607a9260'
196
        dpid_a = '00:00:00:00:00:00:00:01'
197
        dpid_b = '00:00:00:00:00:00:00:02'
198
        status = {
199
            'network_status': {
200
                'id': 'network_status',
201
                'links': {
202
                    link_id: {
203
                        'enabled': True,
204
                        'endpoint_a': {
205
                            'switch': dpid_a,
206
                            'port_number': 2
207
                        },
208
                        'endpoint_b': {
209
                            'switch': dpid_b,
210
                            'port_number': 2
211
                        }
212
                    }
213
                },
214
                'switches': {
215
                    dpid_a: {
216
                        'dpid': dpid_a,
217
                        'enabled': True,
218
                        'id': dpid_a,
219
                        'interfaces': {
220
                            f'{dpid_a}:2': {
221
                                'enabled': True,
222
                                'lldp': True,
223
                                'port_number': 2,
224
                                'name': 's1-eth2'
225
                            }
226
                        }
227
                    },
228
                    dpid_b: {
229
                        'dpid': dpid_b,
230
                        'enabled': True,
231
                        'id': dpid_b,
232
                        'interfaces': {
233
                            f'{dpid_b}:2': {
234
                                'enabled': True,
235
                                'lldp': True,
236
                                'port_number': 2,
237
                                'name': 's2-eth2'
238
                            }
239
                        }
240
                    }
241
                }
242
            }
243
        }
244
        switches_expected = [dpid_a, dpid_b]
245
        interfaces_expected = [f'{dpid_a}:2', f'{dpid_b}:2']
246
        links_expected = [link_id]
247
        mock_storehouse_get_data.return_value = status
248
        self.napp._load_network_status()
249
        self.assertListEqual(switches_expected,
250
                             list(self.napp.controller.switches.keys()))
251
        interfaces = []
252
        for switch in self.napp.controller.switches.values():
253
            for iface in switch.interfaces.values():
254
                interfaces.append(iface.id)
255
        self.assertListEqual(interfaces_expected, interfaces)
256
        self.assertListEqual(links_expected, list(self.napp.links.keys()))
257
258
    @patch('napps.kytos.topology.main.StoreHouse.get_data')
259
    @patch('napps.kytos.topology.main.log')
260
    def test_load_network_status_fail(self, *args):
261
        """Test _load_network_status failure."""
262
        (mock_log, mock_storehouse_get_data) = args
263
        mock_log.error.return_value = True
264
        mock_storehouse_get_data.side_effect = FileNotFoundError('xpto')
265
        self.napp._load_network_status()
266
        error = 'Fail to load network status from storehouse: xpto'
267
        mock_log.error.assert_called_with(error)
268
269
    @patch('napps.kytos.topology.main.StoreHouse.get_data')
270
    @patch('napps.kytos.topology.main.log')
271
    def test_load_network_status_does_nothing(self, *args):
272
        """Test _load_network_status doing nothing."""
273
        (mock_log, mock_storehouse_get_data) = args
274
        mock_log.info.return_value = True
275
        mock_storehouse_get_data.return_value = {}
276
        self.napp._load_network_status()
277
        error = 'There is no status saved to restore.'
278
        mock_log.info.assert_called_with(error)
279
280
    @patch('napps.kytos.topology.main.StoreHouse.get_data')
281
    @patch('napps.kytos.topology.main.Main._load_switch')
282
    @patch('napps.kytos.topology.main.log')
283
    def test_load_network_status_fail_switch(self, *args):
284
        """Test _load_network_status failure in switch."""
285
        (mock_log, mock_load_switch, mock_get_data) = args
286
        status = {
287
            'network_status': {
288
                'id': 'network_status',
289
                'links': {},
290
                'switches': {
291
                    '1': {}
292
                }
293
            }
294
        }
295
        mock_log.error.return_value = True
296
        mock_get_data.return_value = status
297
        mock_load_switch.side_effect = Exception('xpto')
298
        self.napp._load_network_status()
299
        error = 'Error loading switch: xpto'
300
        mock_log.error.assert_called_with(error)
301
302
    @patch('napps.kytos.topology.main.StoreHouse.get_data')
303
    @patch('napps.kytos.topology.main.Main._load_link')
304
    @patch('napps.kytos.topology.main.log')
305
    def test_load_network_status_fail_link(self, *args):
306
        """Test _load_network_status failure in link."""
307
        (mock_log, mock_load_link, mock_get_data) = args
308
        status = {
309
            'network_status': {
310
                'id': 'network_status',
311
                'switches': {},
312
                'links': {
313
                    '1': {}
314
                }
315
            }
316
        }
317
        mock_log.error.return_value = True
318
        mock_get_data.return_value = status
319
        mock_load_link.side_effect = Exception('xpto')
320
        self.napp._load_network_status()
321
        error = 'Error loading link 1: xpto'
322
        mock_log.error.assert_called_with(error)
323
324
    @patch('napps.kytos.topology.main.KytosEvent')
325
    @patch('kytos.core.buffers.KytosEventBuffer.put')
326
    def test_load_switch(self, *args):
327
        """Test _load_switch."""
328
        (mock_buffers_put, mock_event) = args
329
        dpid_a = "00:00:00:00:00:00:00:01"
330
        dpid_x = "00:00:00:00:00:00:00:XX"
331
        iface_a = f'{dpid_a}:1'
332
        switch_attrs = {
333
            'dpid': dpid_a,
334
            'enabled': True,
335
            'id': dpid_a,
336
            'interfaces': {
337
                iface_a: {
338
                    'enabled': True,
339
                    'lldp': True,
340
                    'id': iface_a,
341
                    'switch': dpid_a,
342
                    'name': 's2-eth1',
343
                    'port_number': 1
344
                }
345
            }
346
        }
347
        self.napp._load_switch(dpid_a, switch_attrs)
348
349
        self.assertEqual(len(self.napp.controller.switches), 1)
350
        self.assertIn(dpid_a, self.napp.controller.switches)
351
        self.assertNotIn(dpid_x, self.napp.controller.switches)
352
        switch = self.napp.controller.switches[dpid_a]
353
354
        self.assertEqual(switch.id, dpid_a)
355
        self.assertEqual(switch.dpid, dpid_a)
356
        self.assertTrue(switch.is_enabled())
357
        self.assertFalse(switch.is_active())
358
359
        self.assertEqual(len(switch.interfaces), 1)
360
        self.assertIn(1, switch.interfaces)
361
        self.assertNotIn(2, switch.interfaces)
362
        mock_event.assert_called()
363
        mock_buffers_put.assert_called()
364
365
        interface = switch.interfaces[1]
366
        self.assertEqual(interface.id, iface_a)
367
        self.assertEqual(interface.switch.id, dpid_a)
368
        self.assertEqual(interface.port_number, 1)
369
        self.assertTrue(interface.is_enabled())
370
        self.assertTrue(interface.lldp)
371
        self.assertTrue(interface.uni)
372
        self.assertFalse(interface.nni)
373
374
    def test_load_switch_attrs(self):
375
        """Test _load_switch."""
376
        dpid_b = "00:00:00:00:00:00:00:02"
377
        iface_b = f'{dpid_b}:1'
378
        switch_attrs = {
379
            "active": True,
380
            "connection": "127.0.0.1:43230",
381
            "data_path": "XX Human readable desc of dp",
382
            "dpid": "00:00:00:00:00:00:00:02",
383
            "enabled": False,
384
            "hardware": "Open vSwitch",
385
            "id": "00:00:00:00:00:00:00:02",
386
            "interfaces": {
387
                "00:00:00:00:00:00:00:02:1": {
388
                    "active": True,
389
                    "enabled": False,
390
                    "id": "00:00:00:00:00:00:00:02:1",
391
                    "link": "",
392
                    "lldp": False,
393
                    "mac": "de:58:c3:30:b7:b7",
394
                    "metadata": {},
395
                    "name": "s2-eth1",
396
                    "nni": False,
397
                    "port_number": 1,
398
                    "speed": 1250000000,
399
                    "switch": "00:00:00:00:00:00:00:02",
400
                    "type": "interface",
401
                    "uni": True
402
                },
403
            },
404
            "manufacturer": "Nicira, Inc.",
405
            "metadata": {},
406
            "name": "00:00:00:00:00:00:00:04",
407
            "ofp_version": "0x04",
408
            "serial": "XX serial number",
409
            "software": "2.10.7",
410
            "type": "switch"
411
        }
412
413
        self.napp._load_switch(dpid_b, switch_attrs)
414
415
        self.assertEqual(len(self.napp.controller.switches), 1)
416
        self.assertIn(dpid_b, self.napp.controller.switches)
417
418
        switch = self.napp.controller.switches[dpid_b]
419
        self.assertEqual(switch.id, dpid_b)
420
        self.assertEqual(switch.dpid, dpid_b)
421
        self.assertFalse(switch.is_enabled())
422
        self.assertFalse(switch.is_active())
423
        self.assertEqual(switch.description['manufacturer'], 'Nicira, Inc.')
424
        self.assertEqual(switch.description['hardware'], 'Open vSwitch')
425
        self.assertEqual(switch.description['software'], '2.10.7')
426
        self.assertEqual(switch.description['serial'], 'XX serial number')
427
        self.assertEqual(switch.description['data_path'],
428
                         'XX Human readable desc of dp')
429
430
        self.assertEqual(len(switch.interfaces), 1)
431
        self.assertIn(1, switch.interfaces)
432
        self.assertNotIn(2, switch.interfaces)
433
434
        interface = switch.interfaces[1]
435
        self.assertEqual(interface.id, iface_b)
436
        self.assertEqual(interface.switch.id, dpid_b)
437
        self.assertEqual(interface.port_number, 1)
438
        self.assertFalse(interface.is_enabled())
439
        self.assertFalse(interface.lldp)
440
        self.assertTrue(interface.uni)
441
        self.assertFalse(interface.nni)
442
443
    def test_load_link(self):
444
        """Test _load_link."""
445
        dpid_a = "00:00:00:00:00:00:00:01"
446
        dpid_b = "00:00:00:00:00:00:00:02"
447
        mock_switch_a = get_switch_mock(dpid_a, 0x04)
448
        mock_switch_b = get_switch_mock(dpid_b, 0x04)
449
        mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a)
450
        mock_interface_a.id = dpid_a + ':1'
451
        mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b)
452
        mock_interface_b.id = dpid_b + ':1'
453
        mock_switch_a.interfaces = {1: mock_interface_a}
454
        mock_switch_b.interfaces = {1: mock_interface_b}
455
        self.napp.controller.switches[dpid_a] = mock_switch_a
456
        self.napp.controller.switches[dpid_b] = mock_switch_b
457
        link_attrs = {
458
            'enabled': True,
459
            'endpoint_a': {
460
                'switch': dpid_a,
461
                'port_number': 1
462
            },
463
            'endpoint_b': {
464
                'switch': dpid_b,
465
                'port_number': 1
466
            }
467
        }
468
469
        self.napp._load_link(link_attrs)
470
471
        self.assertEqual(len(self.napp.links), 1)
472
        link = list(self.napp.links.values())[0]
473
474
        self.assertEqual(link.endpoint_a.id, mock_interface_a.id)
475
        self.assertEqual(link.endpoint_b.id, mock_interface_b.id)
476
        self.assertTrue(mock_interface_a.nni)
477
        self.assertTrue(mock_interface_b.nni)
478
        self.assertEqual(mock_interface_a.update_link.call_count, 1)
479
        self.assertEqual(mock_interface_b.update_link.call_count, 1)
480
481
        # test enable/disable
482
        link_id = '4d42dc08522'
483
        mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a)
484
        mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b)
485
        mock_link = get_link_mock(mock_interface_a, mock_interface_b)
486
        mock_link.id = link_id
487
        with patch('napps.kytos.topology.main.Main._get_link_or_create',
488
                   return_value=mock_link):
489
            # enable link
490
            link_attrs['enabled'] = True
491
            self.napp.links = {link_id: mock_link}
492
            self.napp._load_link(link_attrs)
493
            self.assertEqual(mock_link.enable.call_count, 1)
494
            # disable link
495
            link_attrs['enabled'] = False
496
            self.napp.links = {link_id: mock_link}
497
            self.napp._load_link(link_attrs)
498
            self.assertEqual(mock_link.disable.call_count, 1)
499
500
    @patch('napps.kytos.topology.main.Main._get_link_or_create')
501
    def test_fail_load_link(self, get_link_or_create_mock):
502
        """Test fail load_link."""
503
        dpid_a = '00:00:00:00:00:00:00:01'
504
        dpid_b = '00:00:00:00:00:00:00:02'
505
        link_id = '4d42dc08522'
506
        mock_switch_a = get_switch_mock(dpid_a)
507
        mock_switch_b = get_switch_mock(dpid_b)
508
        mock_interface_a_1 = get_interface_mock('s1-eth1', 1, mock_switch_a)
509
        mock_interface_b_1 = get_interface_mock('s2-eth1', 1, mock_switch_b)
510
        mock_link = get_link_mock(mock_interface_a_1, mock_interface_b_1)
511
        mock_link.id = link_id
512
        self.napp.links = {link_id: mock_link}
513
        get_link_or_create_mock.return_value = mock_link
514
515
        link_attrs_fail = {
516
            'enabled': True,
517
            'endpoint_a': {
518
                'switch': dpid_a,
519
                'port_number': 999
520
            },
521
            'endpoint_b': {
522
                'switch': dpid_b,
523
                'port_number': 999
524
            }
525
        }
526
        with self.assertRaises(RestoreError):
527
            self.napp._load_link(link_attrs_fail)
528
529
        link_attrs_fail = {
530
            'enabled': True,
531
            'endpoint_a': {
532
                'switch': '00:00:00:00:00:00:00:99',
533
                'port_number': 1
534
            },
535
            'endpoint_b': {
536
                'switch': '00:00:00:00:00:00:00:77',
537
                'port_number': 1
538
            }
539
        }
540
        with self.assertRaises(RestoreError):
541
            self.napp._load_link(link_attrs_fail)
542
543 View Code Duplication
    @patch('napps.kytos.topology.main.Main.save_status_on_storehouse')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
544
    def test_enable_switch(self, mock_save_status):
545
        """Test enable_switch."""
546
        dpid = "00:00:00:00:00:00:00:01"
547
        mock_switch = get_switch_mock(dpid)
548
        self.napp.controller.switches = {dpid: mock_switch}
549
        api = get_test_client(self.napp.controller, self.napp)
550
551
        url = f'{self.server_name_url}/v3/switches/{dpid}/enable'
552
        response = api.post(url)
553
        self.assertEqual(response.status_code, 201, response.data)
554
        self.assertEqual(mock_switch.enable.call_count, 1)
555
        mock_save_status.assert_called()
556
557
        # fail case
558
        mock_switch.enable.call_count = 0
559
        dpid = "00:00:00:00:00:00:00:02"
560
        url = f'{self.server_name_url}/v3/switches/{dpid}/enable'
561
        response = api.post(url)
562
        self.assertEqual(response.status_code, 404, response.data)
563
        self.assertEqual(mock_switch.enable.call_count, 0)
564
565 View Code Duplication
    @patch('napps.kytos.topology.main.Main.save_status_on_storehouse')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
566
    def test_disable_switch(self, mock_save_status):
567
        """Test disable_switch."""
568
        dpid = "00:00:00:00:00:00:00:01"
569
        mock_switch = get_switch_mock(dpid)
570
        self.napp.controller.switches = {dpid: mock_switch}
571
        api = get_test_client(self.napp.controller, self.napp)
572
573
        url = f'{self.server_name_url}/v3/switches/{dpid}/disable'
574
        response = api.post(url)
575
        self.assertEqual(response.status_code, 201, response.data)
576
        self.assertEqual(mock_switch.disable.call_count, 1)
577
        mock_save_status.assert_called()
578
579
        # fail case
580
        mock_switch.disable.call_count = 0
581
        dpid = "00:00:00:00:00:00:00:02"
582
        url = f'{self.server_name_url}/v3/switches/{dpid}/disable'
583
        response = api.post(url)
584
        self.assertEqual(response.status_code, 404, response.data)
585
        self.assertEqual(mock_switch.disable.call_count, 0)
586
587
    def test_get_switch_metadata(self):
588
        """Test get_switch_metadata."""
589
        dpid = "00:00:00:00:00:00:00:01"
590
        mock_switch = get_switch_mock(dpid)
591
        mock_switch.metadata = "A"
592
        self.napp.controller.switches = {dpid: mock_switch}
593
        api = get_test_client(self.napp.controller, self.napp)
594
595
        url = f'{self.server_name_url}/v3/switches/{dpid}/metadata'
596
        response = api.get(url)
597
        self.assertEqual(response.status_code, 200, response.data)
598
599
        # fail case
600
        dpid = "00:00:00:00:00:00:00:02"
601
        url = f'{self.server_name_url}/v3/switches/{dpid}/metadata'
602
        response = api.get(url)
603
        self.assertEqual(response.status_code, 404, response.data)
604
605
    @patch('napps.kytos.topology.main.Main.notify_metadata_changes')
606
    def test_add_switch_metadata(self, mock_metadata_changes):
607
        """Test add_switch_metadata."""
608
        dpid = "00:00:00:00:00:00:00:01"
609
        mock_switch = get_switch_mock(dpid)
610
        self.napp.controller.switches = {dpid: mock_switch}
611
        api = get_test_client(self.napp.controller, self.napp)
612
        payload = {"data": "A"}
613
614
        url = f'{self.server_name_url}/v3/switches/{dpid}/metadata'
615
        response = api.post(url, data=json.dumps(payload),
616
                            content_type='application/json')
617
        self.assertEqual(response.status_code, 201, response.data)
618
        mock_metadata_changes.assert_called()
619
620
        # fail case
621
        dpid = "00:00:00:00:00:00:00:02"
622
        url = f'{self.server_name_url}/v3/switches/{dpid}/metadata'
623
        response = api.post(url, data=json.dumps(payload),
624
                            content_type='application/json')
625
        self.assertEqual(response.status_code, 404, response.data)
626
627
    def test_add_switch_metadata_wrong_format(self):
628
        """Test add_switch_metadata_wrong_format."""
629
        dpid = "00:00:00:00:00:00:00:01"
630
        api = get_test_client(self.napp.controller, self.napp)
631
        payload = 'A'
632
633
        url = f'{self.server_name_url}/v3/switches/{dpid}/metadata'
634
        response = api.post(url, data=payload, content_type='application/json')
635
        self.assertEqual(response.status_code, 400, response.data)
636
637
        payload = None
638
        response = api.post(url, data=json.dumps(payload),
639
                            content_type='application/json')
640
        self.assertEqual(response.status_code, 415, response.data)
641
642
    @patch('napps.kytos.topology.main.Main.notify_metadata_changes')
643
    def test_delete_switch_metadata(self, mock_metadata_changes):
644
        """Test delete_switch_metadata."""
645
        dpid = "00:00:00:00:00:00:00:01"
646
        mock_switch = get_switch_mock(dpid)
647
        self.napp.controller.switches = {dpid: mock_switch}
648
        api = get_test_client(self.napp.controller, self.napp)
649
650
        key = "A"
651
        url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}'
652
        response = api.delete(url)
653
        mock_metadata_changes.assert_called()
654
        self.assertEqual(response.status_code, 200, response.data)
655
656
        # fail case
657
        key = "A"
658
        dpid = "00:00:00:00:00:00:00:02"
659
        url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}'
660
        response = api.delete(url)
661
        mock_metadata_changes.assert_called()
662
        self.assertEqual(response.status_code, 404, response.data)
663
664 View Code Duplication
    @patch('napps.kytos.topology.main.Main.save_status_on_storehouse')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
665
    def test_enable_interfaces(self, mock_save_status):
666
        """Test enable_interfaces."""
667
        dpid = '00:00:00:00:00:00:00:01'
668
        mock_switch = get_switch_mock(dpid)
669
        mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch)
670
        mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch)
671
        mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2}
672
        self.napp.controller.switches = {dpid: mock_switch}
673
        api = get_test_client(self.napp.controller, self.napp)
674
675
        interface_id = '00:00:00:00:00:00:00:01:1'
676
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable'
677
        response = api.post(url)
678
        self.assertEqual(response.status_code, 200, response.data)
679
        self.assertEqual(mock_interface_1.enable.call_count, 1)
680
        self.assertEqual(mock_interface_2.enable.call_count, 0)
681
        mock_save_status.assert_called()
682
683
        mock_interface_1.enable.call_count = 0
684
        mock_interface_2.enable.call_count = 0
685
        url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable'
686
        response = api.post(url)
687
        self.assertEqual(response.status_code, 200, response.data)
688
        self.assertEqual(mock_interface_1.enable.call_count, 1)
689
        self.assertEqual(mock_interface_2.enable.call_count, 1)
690
691
        # test interface not found
692
        interface_id = '00:00:00:00:00:00:00:01:3'
693
        mock_interface_1.enable.call_count = 0
694
        mock_interface_2.enable.call_count = 0
695
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable'
696
        response = api.post(url)
697
        self.assertEqual(response.status_code, 409, response.data)
698
        self.assertEqual(mock_interface_1.enable.call_count, 0)
699
        self.assertEqual(mock_interface_2.enable.call_count, 0)
700
701
        # test switch not found
702
        dpid = '00:00:00:00:00:00:00:02'
703
        url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable'
704
        response = api.post(url)
705
        self.assertEqual(response.status_code, 404, response.data)
706
        self.assertEqual(mock_interface_1.enable.call_count, 0)
707
        self.assertEqual(mock_interface_2.enable.call_count, 0)
708
709 View Code Duplication
    @patch('napps.kytos.topology.main.Main.save_status_on_storehouse')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
710
    def test_disable_interfaces(self, mock_save_status):
711
        """Test disable_interfaces."""
712
        interface_id = '00:00:00:00:00:00:00:01:1'
713
        dpid = '00:00:00:00:00:00:00:01'
714
        mock_switch = get_switch_mock(dpid)
715
        mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch)
716
        mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch)
717
        mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2}
718
        self.napp.controller.switches = {dpid: mock_switch}
719
        api = get_test_client(self.napp.controller, self.napp)
720
721
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable'
722
        response = api.post(url)
723
        self.assertEqual(response.status_code, 200, response.data)
724
        self.assertEqual(mock_interface_1.disable.call_count, 1)
725
        self.assertEqual(mock_interface_2.disable.call_count, 0)
726
        mock_save_status.assert_called()
727
728
        mock_interface_1.disable.call_count = 0
729
        mock_interface_2.disable.call_count = 0
730
        url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable'
731
        response = api.post(url)
732
        self.assertEqual(response.status_code, 200, response.data)
733
        self.assertEqual(mock_interface_1.disable.call_count, 1)
734
        self.assertEqual(mock_interface_2.disable.call_count, 1)
735
736
        # test interface not found
737
        interface_id = '00:00:00:00:00:00:00:01:3'
738
        mock_interface_1.disable.call_count = 0
739
        mock_interface_2.disable.call_count = 0
740
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable'
741
        response = api.post(url)
742
        self.assertEqual(response.status_code, 409, response.data)
743
        self.assertEqual(mock_interface_1.disable.call_count, 0)
744
        self.assertEqual(mock_interface_2.disable.call_count, 0)
745
746
        # test switch not found
747
        dpid = '00:00:00:00:00:00:00:02'
748
        url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable'
749
        response = api.post(url)
750
        self.assertEqual(response.status_code, 404, response.data)
751
        self.assertEqual(mock_interface_1.disable.call_count, 0)
752
        self.assertEqual(mock_interface_2.disable.call_count, 0)
753
754
    def test_get_interface_metadata(self):
755
        """Test get_interface_metada."""
756
        interface_id = '00:00:00:00:00:00:00:01:1'
757
        dpid = '00:00:00:00:00:00:00:01'
758
        mock_switch = get_switch_mock(dpid)
759
        mock_interface = get_interface_mock('s1-eth1', 1, mock_switch)
760
        mock_interface.metadata = {"metada": "A"}
761
        mock_switch.interfaces = {1: mock_interface}
762
        self.napp.controller.switches = {dpid: mock_switch}
763
        api = get_test_client(self.napp.controller, self.napp)
764
765
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata'
766
        response = api.get(url)
767
        self.assertEqual(response.status_code, 200, response.data)
768
769
        # fail case switch not found
770
        interface_id = '00:00:00:00:00:00:00:02:1'
771
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata'
772
        response = api.get(url)
773
        self.assertEqual(response.status_code, 404, response.data)
774
775
        # fail case interface not found
776
        interface_id = '00:00:00:00:00:00:00:01:2'
777
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata'
778
        response = api.get(url)
779
        self.assertEqual(response.status_code, 404, response.data)
780
781
    @patch('napps.kytos.topology.main.Main.notify_metadata_changes')
782
    def test_add_interface_metadata(self, mock_metadata_changes):
783
        """Test add_interface_metadata."""
784
        interface_id = '00:00:00:00:00:00:00:01:1'
785
        dpid = '00:00:00:00:00:00:00:01'
786
        mock_switch = get_switch_mock(dpid)
787
        mock_interface = get_interface_mock('s1-eth1', 1, mock_switch)
788
        mock_interface.metadata = {"metada": "A"}
789
        mock_switch.interfaces = {1: mock_interface}
790
        self.napp.controller.switches = {dpid: mock_switch}
791
        api = get_test_client(self.napp.controller, self.napp)
792
793
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata'
794
        payload = {"metada": "A"}
795
        response = api.post(url, data=json.dumps(payload),
796
                            content_type='application/json')
797
        self.assertEqual(response.status_code, 201, response.data)
798
        mock_metadata_changes.assert_called()
799
800
        # fail case switch not found
801
        interface_id = '00:00:00:00:00:00:00:02:1'
802
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata'
803
        response = api.post(url, data=json.dumps(payload),
804
                            content_type='application/json')
805
        self.assertEqual(response.status_code, 404, response.data)
806
807
        # fail case interface not found
808
        interface_id = '00:00:00:00:00:00:00:01:2'
809
        url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata'
810
        response = api.post(url, data=json.dumps(payload),
811
                            content_type='application/json')
812
        self.assertEqual(response.status_code, 404, response.data)
813
814
    def test_add_interface_metadata_wrong_format(self):
815
        """Test add_interface_metadata_wrong_format."""
816
        dpid = "00:00:00:00:00:00:00:01:1"
817
        api = get_test_client(self.napp.controller, self.napp)
818
        payload = 'A'
819
820
        url = f'{self.server_name_url}/v3/interfaces/{dpid}/metadata'
821
        response = api.post(url, data=payload, content_type='application/json')
822
        self.assertEqual(response.status_code, 400, response.data)
823
824
        payload = None
825
        response = api.post(url, data=json.dumps(payload),
826
                            content_type='application/json')
827
        self.assertEqual(response.status_code, 415, response.data)
828
829
    def test_delete_interface_metadata(self):
830
        """Test delete_interface_metadata."""
831
        interface_id = '00:00:00:00:00:00:00:01:1'
832
        dpid = '00:00:00:00:00:00:00:01'
833
        iface_url = '/v3/interfaces/'
834
        mock_switch = get_switch_mock(dpid)
835
        mock_interface = get_interface_mock('s1-eth1', 1, mock_switch)
836
        mock_interface.remove_metadata.side_effect = [True, False]
837
        mock_interface.metadata = {"metada": "A"}
838
        mock_switch.interfaces = {1: mock_interface}
839
        self.napp.controller.switches = {'00:00:00:00:00:00:00:01':
840
                                         mock_switch}
841
        api = get_test_client(self.napp.controller, self.napp)
842
843
        key = 'A'
844
        url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}'
845
        response = api.delete(url)
846
        self.assertEqual(response.status_code, 200, response.data)
847
848
        # fail case switch not found
849
        key = 'A'
850
        interface_id = '00:00:00:00:00:00:00:02:1'
851
        url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}'
852
        response = api.delete(url)
853
        self.assertEqual(response.status_code, 404, response.data)
854
855
        # fail case interface not found
856
        key = 'A'
857
        interface_id = '00:00:00:00:00:00:00:01:2'
858
        url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}'
859
        response = api.delete(url)
860
        self.assertEqual(response.status_code, 404, response.data)
861
862
        # fail case metadata not found
863
        key = 'A'
864
        interface_id = '00:00:00:00:00:00:00:01:1'
865
        url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}'
866
        response = api.delete(url)
867
        self.assertEqual(response.status_code, 404, response.data)
868
869
    @patch('napps.kytos.topology.main.Main.save_status_on_storehouse')
870
    def test_enable_link(self, mock_save_status):
871
        """Test enable_link."""
872
        mock_link = MagicMock(Link)
873
        self.napp.links = {'1': mock_link}
874
        api = get_test_client(self.napp.controller, self.napp)
875
        mock_save_status.return_value = True
876
877
        link_id = 1
878
        url = f'{self.server_name_url}/v3/links/{link_id}/enable'
879
        response = api.post(url)
880
        self.assertEqual(response.status_code, 201, response.data)
881
        self.assertEqual(mock_link.enable.call_count, 1)
882
883
        # fail case
884
        link_id = 2
885
        url = f'{self.server_name_url}/v3/links/{link_id}/enable'
886
        response = api.post(url)
887
        self.assertEqual(response.status_code, 404, response.data)
888
889
    @patch('napps.kytos.topology.main.Main.save_status_on_storehouse')
890
    def test_disable_link(self, mock_save_status):
891
        """Test disable_link."""
892
        mock_link = MagicMock(Link)
893
        self.napp.links = {'1': mock_link}
894
        api = get_test_client(self.napp.controller, self.napp)
895
        mock_save_status.return_value = True
896
897
        link_id = 1
898
        url = f'{self.server_name_url}/v3/links/{link_id}/disable'
899
        response = api.post(url)
900
        self.assertEqual(response.status_code, 201, response.data)
901
        self.assertEqual(mock_link.disable.call_count, 1)
902
903
        # fail case
904
        link_id = 2
905
        url = f'{self.server_name_url}/v3/links/{link_id}/disable'
906
        response = api.post(url)
907
        self.assertEqual(response.status_code, 404, response.data)
908
909
    @patch('napps.kytos.topology.main.Main.save_status_on_storehouse')
910
    def test_handle_network_status_updated(self, mock_save_status):
911
        """Test handle_link_maintenance_start."""
912
        event = MagicMock()
913
        event.name = 'kytos/of_lldp.network_status.updated'
914
        event.content = {'attribute': 'LLDP',
915
                         'state': 'enabled',
916
                         'interface_ids': '00:00:00:00:00:00:00:01:1'}
917
        self.napp.handle_network_status_updated(event)
918
        mock_save_status.assert_called_once()
919
920
    def test_get_link_metadata(self):
921
        """Test get_link_metadata."""
922
        mock_link = MagicMock(Link)
923
        mock_link.metadata = "A"
924
        self.napp.links = {'1': mock_link}
925
        msg_success = {"metadata": "A"}
926
        api = get_test_client(self.napp.controller, self.napp)
927
928
        link_id = 1
929
        url = f'{self.server_name_url}/v3/links/{link_id}/metadata'
930
        response = api.get(url)
931
        self.assertEqual(response.status_code, 200, response.data)
932
        self.assertEqual(msg_success, json.loads(response.data))
933
934
        # fail case
935
        link_id = 2
936
        url = f'{self.server_name_url}/v3/links/{link_id}/metadata'
937
        response = api.get(url)
938
        self.assertEqual(response.status_code, 404, response.data)
939
940
    @patch('napps.kytos.topology.main.Main.notify_metadata_changes')
941
    def test_add_link_metadata(self, mock_metadata_changes):
942
        """Test add_link_metadata."""
943
        mock_link = MagicMock(Link)
944
        mock_link.metadata = "A"
945
        self.napp.links = {'1': mock_link}
946
        payload = {"metadata": "A"}
947
        api = get_test_client(self.napp.controller, self.napp)
948
949
        link_id = 1
950
        url = f'{self.server_name_url}/v3/links/{link_id}/metadata'
951
        response = api.post(url, data=json.dumps(payload),
952
                            content_type='application/json')
953
        self.assertEqual(response.status_code, 201, response.data)
954
        mock_metadata_changes.assert_called()
955
956
        # fail case
957
        link_id = 2
958
        url = f'{self.server_name_url}/v3/links/{link_id}/metadata'
959
        response = api.post(url, data=json.dumps(payload),
960
                            content_type='application/json')
961
        self.assertEqual(response.status_code, 404, response.data)
962
963
    def test_add_link_metadata_wrong_format(self):
964
        """Test add_link_metadata_wrong_format."""
965
        link_id = 'cf0f4071be426b3f745027f5d22'
966
        api = get_test_client(self.napp.controller, self.napp)
967
        payload = "A"
968
969
        url = f'{self.server_name_url}/v3/links/{link_id}/metadata'
970
        response = api.post(url, data=payload,
971
                            content_type='application/json')
972
        self.assertEqual(response.status_code, 400, response.data)
973
974
        payload = None
975
        response = api.post(url, data=json.dumps(payload),
976
                            content_type='application/json')
977
        self.assertEqual(response.status_code, 415, response.data)
978
979
    @patch('napps.kytos.topology.main.Main.notify_metadata_changes')
980
    def test_delete_link_metadata(self, mock_metadata_changes):
981
        """Test delete_link_metadata."""
982
        mock_link = MagicMock(Link)
983
        mock_link.metadata = "A"
984
        mock_link.remove_metadata.side_effect = [True, False]
985
        self.napp.links = {'1': mock_link}
986
        api = get_test_client(self.napp.controller, self.napp)
987
988
        link_id = 1
989
        key = 'A'
990
        url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}'
991
        response = api.delete(url)
992
        self.assertEqual(response.status_code, 200, response.data)
993
        mock_metadata_changes.assert_called()
994
995
        # fail case link not found
996
        link_id = 2
997
        key = 'A'
998
        url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}'
999
        response = api.delete(url)
1000
        self.assertEqual(response.status_code, 404, response.data)
1001
1002
        # fail case metadata not found
1003
        link_id = 1
1004
        key = 'A'
1005
        url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}'
1006
        response = api.delete(url)
1007
        self.assertEqual(response.status_code, 404, response.data)
1008
1009
    @patch('napps.kytos.topology.main.Main.notify_topology_update')
1010
    @patch('napps.kytos.topology.main.Main.update_instance_metadata')
1011
    def test_handle_new_switch(self, *args):
1012
        """Test handle_new_switch."""
1013
        (mock_instance_metadata, mock_notify_topology_update) = args
1014
        mock_event = MagicMock()
1015
        mock_switch = create_autospec(Switch)
1016
        mock_event.content['switch'] = mock_switch
1017
        self.napp.handle_new_switch(mock_event)
1018
        mock_notify_topology_update.assert_called()
1019
        mock_instance_metadata.assert_called()
1020
1021
    @patch('napps.kytos.topology.main.Main.notify_topology_update')
1022
    def test_handle_connection_lost(self, mock_notify_topology_update):
1023
        """Test handle connection_lost."""
1024
        mock_event = MagicMock()
1025
        mock_switch = create_autospec(Switch)
1026
        mock_switch.return_value = True
1027
        mock_event.content['source'] = mock_switch
1028
        self.napp.handle_connection_lost(mock_event)
1029
        mock_notify_topology_update.assert_called()
1030
1031
    @patch('napps.kytos.topology.main.Main.notify_topology_update')
1032
    @patch('napps.kytos.topology.main.Main.update_instance_metadata')
1033
    def test_handle_interface_up(self, *args):
1034
        """Test handle_interface_up."""
1035
        (mock_instance_metadata, mock_notify_topology_update) = args
1036
        mock_event = MagicMock()
1037
        mock_interface = create_autospec(Interface)
1038
        mock_event.content['interface'] = mock_interface
1039
        self.napp.handle_interface_up(mock_event)
1040
        mock_notify_topology_update.assert_called()
1041
        mock_instance_metadata.assert_called()
1042
1043
    @patch('napps.kytos.topology.main.Main.handle_interface_up')
1044
    def test_handle_interface_created(self, mock_handle_interface_up):
1045
        """Test handle interface created."""
1046
        mock_event = MagicMock()
1047
        self.napp.handle_interface_created(mock_event)
1048
        mock_handle_interface_up.assert_called()
1049
1050
    @patch('napps.kytos.topology.main.Main.notify_topology_update')
1051
    @patch('napps.kytos.topology.main.Main.handle_interface_link_down')
1052
    def test_handle_interface_down(self, *args):
1053
        """Test handle interface down."""
1054
        (mock_handle_interface_link_down, mock_notify_topology_update) = args
1055
        mock_event = MagicMock()
1056
        mock_interface = create_autospec(Interface)
1057
        mock_event.content['interface'] = mock_interface
1058
        self.napp.handle_interface_down(mock_event)
1059
        mock_handle_interface_link_down.assert_called()
1060
        mock_notify_topology_update.assert_called()
1061
1062
    @patch('napps.kytos.topology.main.Main.handle_interface_down')
1063
    def test_interface_deleted(self, mock_handle_interface_link_down):
1064
        """Test interface deleted."""
1065
        mock_event = MagicMock()
1066
        self.napp.handle_interface_deleted(mock_event)
1067
        mock_handle_interface_link_down.assert_called()
1068
1069
    @patch('napps.kytos.topology.main.Main._get_link_from_interface')
1070
    @patch('napps.kytos.topology.main.Main.notify_topology_update')
1071
    @patch('napps.kytos.topology.main.Main.update_instance_metadata')
1072
    @patch('napps.kytos.topology.main.Main.notify_link_status_change')
1073
    def test_interface_link_up(self, *args):
1074
        """Test interface link_up."""
1075
        (mock_status_change, mock_instance_metadata, mock_topology_update,
1076
         mock_link_from_interface) = args
1077
1078
        now = time.time()
1079
        mock_event = MagicMock()
1080
        mock_interface_a = create_autospec(Interface)
1081
        mock_interface_a.is_active.return_value = False
1082
        mock_interface_b = create_autospec(Interface)
1083
        mock_interface_b.is_active.return_value = True
1084
        mock_link = create_autospec(Link)
1085
        mock_link.get_metadata.return_value = now
1086
        mock_link.is_active.side_effect = [False, True]
1087
        mock_link.endpoint_a = mock_interface_a
1088
        mock_link.endpoint_b = mock_interface_b
1089
        mock_link_from_interface.return_value = mock_link
1090
        content = {'interface': mock_interface_a}
1091
        mock_event.content = content
1092
        self.napp.link_up_timer = 1
1093
        self.napp.handle_interface_link_up(mock_event)
1094
        mock_topology_update.assert_called()
1095
        mock_instance_metadata.assert_called()
1096
        mock_status_change.assert_called()
1097
1098
    @patch('napps.kytos.topology.main.Main._get_link_from_interface')
1099
    @patch('napps.kytos.topology.main.Main.notify_topology_update')
1100
    @patch('napps.kytos.topology.main.Main.notify_link_status_change')
1101
    def test_interface_link_down(self, *args):
1102
        """Test interface link down."""
1103
        (mock_status_change, mock_topology_update,
1104
         mock_link_from_interface) = args
1105
1106
        mock_event = MagicMock()
1107
        mock_interface = create_autospec(Interface)
1108
        mock_link = create_autospec(Link)
1109
        mock_link.is_active.return_value = True
1110
        mock_link_from_interface.return_value = mock_link
1111
        mock_event.content['interface'] = mock_interface
1112
        self.napp.handle_interface_link_down(mock_event)
1113
        mock_topology_update.assert_called()
1114
        mock_status_change.assert_called()
1115
1116
    @patch('napps.kytos.topology.main.Main._get_link_or_create')
1117
    @patch('napps.kytos.topology.main.Main.notify_topology_update')
1118
    def test_add_links(self, *args):
1119
        """Test add_links."""
1120
        (mock_notify_topology_update, mock_get_link_or_create) = args
1121
        mock_event = MagicMock()
1122
        self.napp.add_links(mock_event)
1123
        mock_get_link_or_create.assert_called()
1124
        mock_notify_topology_update.assert_called()
1125
1126
    @patch('napps.kytos.topology.main.Main._get_switches_dict')
1127
    @patch('napps.kytos.topology.main.StoreHouse.save_status')
1128
    def test_save_status_on_store(self, *args):
1129
        """Test save_status_on_storehouse."""
1130
        (mock_save_status, mock_get_switches_dict) = args
1131
        self.napp.save_status_on_storehouse()
1132
        mock_get_switches_dict.assert_called()
1133
        mock_save_status.assert_called()
1134
1135
    @patch('napps.kytos.topology.main.KytosEvent')
1136
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1137
    def test_notify_switch_enabled(self, *args):
1138
        """Test notify switch enabled."""
1139
        dpid = "00:00:00:00:00:00:00:01"
1140
        (mock_buffers_put, mock_event) = args
1141
        self.napp.notify_switch_enabled(dpid)
1142
        mock_event.assert_called()
1143
        mock_buffers_put.assert_called()
1144
1145
    @patch('napps.kytos.topology.main.KytosEvent')
1146
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1147
    def test_notify_switch_disabled(self, *args):
1148
        """Test notify switch disabled."""
1149
        dpid = "00:00:00:00:00:00:00:01"
1150
        (mock_buffers_put, mock_event) = args
1151
        self.napp.notify_switch_disabled(dpid)
1152
        mock_event.assert_called()
1153
        mock_buffers_put.assert_called()
1154
1155
    @patch('napps.kytos.topology.main.KytosEvent')
1156
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1157
    def test_notify_topology_update(self, *args):
1158
        """Test notify_topology_update."""
1159
        (mock_buffers_put, mock_event) = args
1160
        self.napp.notify_topology_update()
1161
        mock_event.assert_called()
1162
        mock_buffers_put.assert_called()
1163
1164
    @patch('napps.kytos.topology.main.KytosEvent')
1165
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1166
    def test_notify_link_status_change(self, *args):
1167
        """Test notify link status change."""
1168
        (mock_buffers_put, mock_event) = args
1169
        mock_link = create_autospec(Link)
1170
        self.napp.notify_link_status_change(mock_link)
1171
        mock_event.assert_called()
1172
        mock_buffers_put.assert_called()
1173
1174
    @patch('napps.kytos.topology.main.KytosEvent')
1175
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1176
    @patch('napps.kytos.topology.main.isinstance')
1177
    def test_notify_metadata_changes(self, *args):
1178
        """Test notify metadata changes."""
1179
        (mock_isinstance, mock_buffers_put, mock_event) = args
1180
        mock_isinstance.return_value = True
1181
        mock_obj = MagicMock()
1182
        mock_action = create_autospec(Switch)
1183
        self.napp.notify_metadata_changes(mock_obj, mock_action)
1184
        mock_event.assert_called()
1185
        mock_isinstance.assert_called()
1186
        mock_buffers_put.assert_called()
1187
1188
    @patch('napps.kytos.topology.main.KytosEvent')
1189
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1190
    def test_notify_port_created(self, *args):
1191
        """Test notify port created."""
1192
        (mock_buffers_put, mock_kytos_event) = args
1193
        mock_event = MagicMock()
1194
        self.napp.notify_port_created(mock_event)
1195
        mock_kytos_event.assert_called()
1196
        mock_buffers_put.assert_called()
1197
1198
    @patch('napps.kytos.topology.main.KytosEvent')
1199
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1200
    def test_save_metadata_on_store(self, *args):
1201
        """Test test_save_metadata_on_store."""
1202
        (mock_buffers_put, mock_kytos_event) = args
1203
        mock_event = MagicMock()
1204
        mock_switch = MagicMock()
1205
        mock_interface = MagicMock()
1206
        mock_link = MagicMock()
1207
        self.napp.store_items = {'switches': mock_switch,
1208
                                 'interfaces': mock_interface,
1209
                                 'links': mock_link}
1210
        # test switches
1211
        mock_event.content = {'switch': mock_switch}
1212
        self.napp.save_metadata_on_store(mock_event)
1213
        mock_kytos_event.assert_called()
1214
        mock_buffers_put.assert_called()
1215
1216
        # test interfaces
1217
        mock_event.content = {'interface': mock_interface}
1218
        self.napp.save_metadata_on_store(mock_event)
1219
        mock_kytos_event.assert_called()
1220
        mock_buffers_put.assert_called()
1221
1222
        # test link
1223
        mock_event.content = {'link': mock_link}
1224
        self.napp.save_metadata_on_store(mock_event)
1225
        mock_kytos_event.assert_called()
1226
        mock_buffers_put.assert_called()
1227
1228
    @patch('napps.kytos.topology.main.KytosEvent')
1229
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1230
    def test_verify_storehouse(self, *args):
1231
        """Test verify_storehouse."""
1232
        (mock_buffers_put, mock_kytos_event) = args
1233
        mock_entities = MagicMock()
1234
        self.napp.verify_storehouse(mock_entities)
1235
        mock_buffers_put.assert_called()
1236
        mock_kytos_event.assert_called()
1237
1238
    @patch('napps.kytos.topology.main.KytosEvent')
1239
    @patch('kytos.core.buffers.KytosEventBuffer.put')
1240
    def test_request_retrieve_entities(self, *args):
1241
        """Test retrive_entities."""
1242
        (mock_buffers_put, mock_kytos_event) = args
1243
        mock_event = MagicMock()
1244
        mock_data = MagicMock()
1245
        mock_error = MagicMock()
1246
        mock_event.content = {"namespace": "test_box"}
1247
        self.napp.request_retrieve_entities(mock_event, mock_data, mock_error)
1248
        mock_kytos_event.assert_called()
1249
        mock_buffers_put.assert_called()
1250
1251
        self.napp.request_retrieve_entities(mock_event, None, mock_error)
1252
        mock_kytos_event.assert_called()
1253
        mock_buffers_put.assert_called()
1254
1255
    @patch('napps.kytos.topology.main.Main.notify_link_status_change')
1256
    def test_handle_link_maintenance_start(self, status_change_mock):
1257
        """Test handle_link_maintenance_start."""
1258
        link1 = MagicMock()
1259
        link1.id = 2
1260
        link2 = MagicMock()
1261
        link2.id = 3
1262
        link3 = MagicMock()
1263
        link3.id = 4
1264
        content = {'links': [link1, link2]}
1265
        event = MagicMock()
1266
        event.content = content
1267
        self.napp.links = {2: link1, 4: link3}
1268
        self.napp.handle_link_maintenance_start(event)
1269
        status_change_mock.assert_called_once_with(link1, reason='maintenance')
1270
1271
    @patch('napps.kytos.topology.main.Main.notify_link_status_change')
1272
    def test_handle_link_maintenance_end(self, status_change_mock):
1273
        """Test handle_link_maintenance_end."""
1274
        link1 = MagicMock()
1275
        link1.id = 2
1276
        link2 = MagicMock()
1277
        link2.id = 3
1278
        link3 = MagicMock()
1279
        link3.id = 4
1280
        content = {'links': [link1, link2]}
1281
        event = MagicMock()
1282
        event.content = content
1283
        self.napp.links = {2: link1, 4: link3}
1284
        self.napp.handle_link_maintenance_end(event)
1285
        status_change_mock.assert_called_once_with(link1, reason='maintenance')
1286
1287 View Code Duplication
    @patch('napps.kytos.topology.main.Main.handle_link_down')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1288
    def test_handle_switch_maintenance_start(self, handle_link_down_mock):
1289
        """Test handle_switch_maintenance_start."""
1290
        switch1 = MagicMock()
1291
        interface1 = MagicMock()
1292
        interface1.is_active.return_value = True
1293
        interface2 = MagicMock()
1294
        interface2.is_active.return_value = False
1295
        interface3 = MagicMock()
1296
        interface3.is_active.return_value = True
1297
        switch1.interfaces = {1: interface1, 2: interface2, 3: interface3}
1298
        switch2 = MagicMock()
1299
        interface4 = MagicMock()
1300
        interface4.is_active.return_value = False
1301
        interface5 = MagicMock()
1302
        interface5.is_active.return_value = True
1303
        switch2.interfaces = {1: interface4, 2: interface5}
1304
        content = {'switches': [switch1, switch2]}
1305
        event = MagicMock()
1306
        event.content = content
1307
        self.napp.handle_switch_maintenance_start(event)
1308
        self.assertEqual(handle_link_down_mock.call_count, 3)
1309
1310 View Code Duplication
    @patch('napps.kytos.topology.main.Main.handle_link_up')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1311
    def test_handle_switch_maintenance_end(self, handle_link_up_mock):
1312
        """Test handle_switch_maintenance_end."""
1313
        switch1 = MagicMock()
1314
        interface1 = MagicMock()
1315
        interface1.is_active.return_value = True
1316
        interface2 = MagicMock()
1317
        interface2.is_active.return_value = False
1318
        interface3 = MagicMock()
1319
        interface3.is_active.return_value = True
1320
        switch1.interfaces = {1: interface1, 2: interface2, 3: interface3}
1321
        switch2 = MagicMock()
1322
        interface4 = MagicMock()
1323
        interface4.is_active.return_value = False
1324
        interface5 = MagicMock()
1325
        interface5.is_active.return_value = True
1326
        switch2.interfaces = {1: interface4, 2: interface5}
1327
        content = {'switches': [switch1, switch2]}
1328
        event = MagicMock()
1329
        event.content = content
1330
        self.napp.handle_switch_maintenance_end(event)
1331
        self.assertEqual(handle_link_up_mock.call_count, 5)
1332