| 1 |  |  | """Module to test the Path class.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from unittest import TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from unittest.mock import patch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from kytos.core.common import EntityStatus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | # pylint: disable=wrong-import-position | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | sys.path.insert(0, '/var/lib/kytos/napps/..') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | # pylint: enable=wrong-import-position | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from napps.kytos.mef_eline.models import Path  # NOQA pycodestyle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from tests.helpers import MockResponse, get_link_mocked  # NOQA pycodestyle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class TestPath(TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     """Class to test path methods.""" | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     def test_status_case_1(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         """Test if empty link is DISABLED.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         current_path = Path() | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         self.assertEqual(current_path.status, EntityStatus.DISABLED) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     # This method will be used by the mock to replace requests.get | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     def _mocked_requests_get_status_case_2(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         # pylint: disable=no-self-use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return MockResponse({'links': {'abc': {'active': False}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                                        'def': {'active': True}}}, 200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     @patch('requests.get', side_effect=_mocked_requests_get_status_case_2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     def test_status_case_2(self, requests_mocked): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         # pylint: disable=unused-argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         """Test if link status is DOWN.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         link1 = get_link_mocked() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         link2 = get_link_mocked() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         link1.id = 'def' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         link2.id = 'abc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         links = [link1, link2] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         current_path = Path(links) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         self.assertEqual(current_path.status, EntityStatus.DOWN) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     def test_status_case_3(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         """Test if link status is DISABLED.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         links = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         current_path = Path(links) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         self.assertEqual(current_path.status, EntityStatus.DISABLED) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     # This method will be used by the mock to replace requests.get | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     def _mocked_requests_get_status_case_4(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         # pylint: disable=no-self-use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return MockResponse({'links': {'abc': {'active': True}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                                        'def': {'active': True}}}, 200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     @patch('requests.get', side_effect=_mocked_requests_get_status_case_4) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     def test_status_case_4(self, requests_mocked): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         # pylint: disable=unused-argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         """Test if link status is UP.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         link1 = get_link_mocked() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         link2 = get_link_mocked() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         link1.id = 'def' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         link2.id = 'abc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         links = [link1, link2] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         current_path = Path(links) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         self.assertEqual(current_path.status, EntityStatus.UP) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     def test_compare_same_paths(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         """Test compare paths with same links.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         links = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                                 metadata={"s_vlan": 5}), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                                 metadata={"s_vlan": 6}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         path_1 = Path(links) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         path_2 = Path(links) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         self.assertEqual(path_1, path_2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     def test_compare_different_paths(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         """Test compare paths with different links.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         links_1 = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                                 metadata={"s_vlan": 5}), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                                 metadata={"s_vlan": 6}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         links_2 = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 get_link_mocked(endpoint_a_port=12, endpoint_b_port=11, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                                 metadata={"s_vlan": 5}), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 get_link_mocked(endpoint_a_port=14, endpoint_b_port=16, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                                 metadata={"s_vlan": 11}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         path_1 = Path(links_1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         path_2 = Path(links_2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         self.assertNotEqual(path_1, path_2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     def test_as_dict(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         """Test path as dict.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         links = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 get_link_mocked(link_dict={"id": 3}), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 get_link_mocked(link_dict={"id": 2}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         current_path = Path(links) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         expected_dict = [{"id": 3}, {"id": 2}] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  |         self.assertEqual(expected_dict, current_path.as_dict()) | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |