| 1 |  |  | """Test Main methods.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from unittest import TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from unittest.mock import MagicMock, patch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from kytos.lib.helpers import (get_controller_mock, get_kytos_event_mock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |                                get_switch_mock, get_test_client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class TestMain(TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     """Tests for the Main class.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     API_URL = 'http://localhost:8181/api/kytos/flow_manager' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         patch('kytos.core.helpers.run_on_thread', lambda x: x).start() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         from napps.kytos.flow_manager.main import Main | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         self.addCleanup(patch.stopall) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         controller = get_controller_mock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         self.switch_01 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         self.switch_01.is_enabled.return_value = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         self.switch_01.flows = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         self.switch_02 = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         self.switch_02.is_enabled.return_value = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         self.switch_02.flows = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         controller.switches = {"00:00:00:00:00:00:00:01": self.switch_01, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                                "00:00:00:00:00:00:00:02": self.switch_02} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         self.napp = Main(controller) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     def test_rest_list_without_dpid(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         """Test list rest method withoud dpid.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         flow_1 = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         flow_1.as_dict.return_value = {'flow_1': 'data'} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         flow_2 = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         flow_2.as_dict.return_value = {'flow_2': 'data'} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         self.switch_01.flows.append(flow_1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         self.switch_02.flows.append(flow_2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         api = get_test_client(self.napp.controller, self.napp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         url = f'{self.API_URL}/v2/flows' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         response = api.get(url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         expected = {'00:00:00:00:00:00:00:01': {'flows': [{'flow_1': 'data'}]}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     '00:00:00:00:00:00:00:02': {'flows': [{'flow_2': 'data'}]}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         self.assertEqual(response.json, expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         self.assertEqual(response.status_code, 200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     def test_rest_list_with_dpid(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         """Test list rest method with dpid.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         flow_1 = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         flow_1.as_dict.return_value = {'flow_1': 'data'} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         self.switch_01.flows.append(flow_1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         api = get_test_client(self.napp.controller, self.napp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         url = f'{self.API_URL}/v2/flows/00:00:00:00:00:00:00:01' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         response = api.get(url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         expected = {'00:00:00:00:00:00:00:01': {'flows': [{'flow_1': 'data'}]}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         self.assertEqual(response.json, expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         self.assertEqual(response.status_code, 200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     @patch('napps.kytos.flow_manager.main.Main._install_flows') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     def test_rest_add_and_delete_without_dpid(self, mock_install_flows): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         """Test add and delete rest method without dpid.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         api = get_test_client(self.napp.controller, self.napp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         for method in ['flows', 'delete']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             url = f'{self.API_URL}/v2/{method}' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             response_1 = api.post(url, json={'data': '123'}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             response_2 = api.post(url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             self.assertEqual(response_1.status_code, 200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             self.assertEqual(response_2.status_code, 404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         self.assertEqual(mock_install_flows.call_count, 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     @patch('napps.kytos.flow_manager.main.Main._install_flows') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     def test_rest_add_and_delete_with_dpid(self, mock_install_flows): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         """Test add and delete rest method with dpid.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         api = get_test_client(self.napp.controller, self.napp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         for method in ['flows', 'delete']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             url_1 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:01' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             url_2 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:02' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             url_3 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:03' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             response_1 = api.post(url_1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             response_2 = api.post(url_1, json={'data': '123'}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             response_3 = api.post(url_2, json={'data': '123'}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             response_4 = api.post(url_3, json={'data': '123'}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             self.assertEqual(response_1.status_code, 404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             self.assertEqual(response_2.status_code, 200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             self.assertEqual(response_3.status_code, 404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             self.assertEqual(response_4.status_code, 404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         self.assertEqual(mock_install_flows.call_count, 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     def test_get_all_switches_enabled(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         """Test _get_all_switches_enabled method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         switches = self.napp._get_all_switches_enabled() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         self.assertEqual(switches, [self.switch_01]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     @patch('napps.kytos.flow_manager.main.Main._send_napp_event') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     @patch('napps.kytos.flow_manager.main.Main._add_flow_mod_sent') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     @patch('napps.kytos.flow_manager.main.Main._send_flow_mod') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     @patch('napps.kytos.flow_manager.main.FlowFactory.get_class') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     def test_install_flows(self, *args): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         """Test _install_flows method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         (mock_flow_factory, mock_send_flow_mod, mock_add_flow_mod_sent, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |          mock_send_napp_event) = args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         serializer = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         flow = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         flow_mod = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         flow.as_of_add_flow_mod.return_value = flow_mod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         serializer.from_dict.return_value = flow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         mock_flow_factory.return_value = serializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         flows_dict = {'flows': [MagicMock()]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         switches = [self.switch_01] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         self.napp._install_flows('add', flows_dict, switches) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         mock_send_flow_mod.assert_called_with(flow.switch, flow_mod) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         mock_add_flow_mod_sent.assert_called_with(flow_mod.header.xid, flow) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         mock_send_napp_event.assert_called_with(self.switch_01, flow, 'add') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     def test_add_flow_mod_sent(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         """Test _add_flow_mod_sent method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         xid = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         flow = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         self.napp._add_flow_mod_sent(xid, flow) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         self.assertEqual(self.napp._flow_mods_sent[xid], flow) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     @patch('kytos.core.buffers.KytosEventBuffer.put') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     def test_send_flow_mod(self, mock_buffers_put): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         """Test _send_flow_mod method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         flow_mod = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         self.napp._send_flow_mod(switch, flow_mod) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         mock_buffers_put.assert_called() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     @patch('kytos.core.buffers.KytosEventBuffer.put') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     def test_send_napp_event(self, mock_buffers_put): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         """Test _send_napp_event method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         flow = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         for command in ['add', 'delete', 'error']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             self.napp._send_napp_event(switch, flow, command) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         self.assertEqual(mock_buffers_put.call_count, 3) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |     @patch('napps.kytos.flow_manager.main.Main._send_napp_event') | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |     def test_handle_errors(self, mock_send_napp_event): | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |         """Test handle_errors method.""" | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |         flow = MagicMock() | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |         self.napp._flow_mods_sent[0] = flow | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |         message = MagicMock() | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |         message.header.xid.value = 0 | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |         event = get_kytos_event_mock(name='.*.of_core.*.ofpt_error', | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |                                      content={'message': message}) | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |         self.napp.handle_errors(event) | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |         mock_send_napp_event.assert_called_with(flow.switch, flow, 'error') | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |  |