Пример #1
0
    def test_handle_features_reply(self, *args):
        """Test handle features reply."""
        (mock_freply_v0x01, mock_freply_v0x04, mock_send_desc_request_v0x01,
         mock_send_desc_request_v0x04, mock_send_set_config_v0x01,
         mock_send_set_config_v0x04, mock_buffers_put) = args
        mock_freply_v0x01.return_value = self.switch_v0x01.connection.switch
        mock_freply_v0x04.return_value = self.switch_v0x04.connection.switch

        self.switch_v0x01.connection.state = ConnectionState.SETUP
        self.switch_v0x01.connection.protocol.state = 'waiting_features_reply'
        name = 'kytos/of_core.v0x0[14].messages.in.ofpt_features_reply'
        content = {"source": self.switch_v0x01.connection}
        event = get_kytos_event_mock(name=name, content=content)
        self.napp.handle_features_reply(event)
        mock_freply_v0x01.assert_called_with(self.napp.controller, event)
        mock_send_desc_request_v0x01.assert_called_with(
            self.napp.controller, self.switch_v0x01.connection.switch)
        mock_send_set_config_v0x01.assert_called_with(
            self.napp.controller, self.switch_v0x01.connection.switch)

        self.switch_v0x04.connection.state = ConnectionState.SETUP
        self.switch_v0x04.connection.protocol.state = 'waiting_features_reply'
        content = {"source": self.switch_v0x04.connection}
        event = get_kytos_event_mock(name=name, content=content)
        self.napp.handle_features_reply(event)
        mock_freply_v0x04.assert_called_with(self.napp.controller, event)
        mock_send_desc_request_v0x04.assert_called_with(
            self.napp.controller, self.switch_v0x04.connection.switch)
        mock_send_set_config_v0x04.assert_called_with(
            self.napp.controller, self.switch_v0x04.connection.switch)

        mock_buffers_put.assert_called()
Пример #2
0
 def test_on_handshake_completed_request_flow_list(self, *args):
     """Test request flow list."""
     (mock_update_flow_list_v0x01, mock_update_flow_list_v0x04) = args
     mock_update_flow_list_v0x04.return_value = "ABC"
     name = 'kytos/of_core.handshake.completed'
     content = {"switch": self.switch_v0x01}
     event = get_kytos_event_mock(name=name, content=content)
     self.napp.on_handshake_completed_request_flow_list(event)
     mock_update_flow_list_v0x01.assert_called_with(self.napp.controller,
                                                    self.switch_v0x01)
     content = {"switch": self.switch_v0x04}
     event = get_kytos_event_mock(name=name, content=content)
     self.napp.on_handshake_completed_request_flow_list(event)
     mock_update_flow_list_v0x04.assert_called_with(self.napp.controller,
                                                    self.switch_v0x04)
Пример #3
0
    def test_install_table_miss_flow(self, *args):
        """Test _create_flow_mod method for flow_mod 1.3 packet."""
        (mock_flow_mod, mock_action, mock_instruction, mock_kytos_event,
         mock_buffer_put) = args

        flow_mod = MagicMock()
        flow_mod.instructions = []
        instruction = MagicMock()
        instruction.actions = []
        action = MagicMock()

        mock_flow_mod.return_value = flow_mod
        mock_action.return_value = action
        mock_instruction.return_value = instruction

        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
        event = get_kytos_event_mock(name='kytos/core.switch.new',
                                     content={'switch': switch})
        self.napp.install_table_miss_flow(event)

        self.assertEqual(flow_mod.command, 0)
        self.assertEqual(instruction.actions[0], action)
        self.assertEqual(flow_mod.instructions[0], instruction)

        event_call = call(name=('kytos/of_l2ls.messages.out.'
                                'ofpt_flow_mod'),
                          content={
                              'destination': switch.connection,
                              'message': flow_mod
                          })
        mock_kytos_event.assert_has_calls([event_call])
        mock_buffer_put.assert_called_once()
Пример #4
0
    def test_install_table_miss_flow(self, *args):
        """Test _create_flow_mod method for flow 1.3 packet."""
        (mock_get_switch_by_dpid, mock_settings, mock_requests,
         mock_port13) = args

        mock_port13.OFPP_CONTROLLER = 123
        flow_manager_url = 'http://localhost:8181/api/kytos/flow_manager/v2'

        mock_settings.FLOW_MANAGER_URL = flow_manager_url
        mock_settings.TABLE_ID = 0
        expected_flow = {}
        expected_flow['priority'] = 0
        expected_flow['table_id'] = 0
        expected_flow['actions'] = [{'action_type': 'output', 'port': 123}]

        dpid = "00:00:00:00:00:00:00:01"
        switch = get_switch_mock(dpid, 0x04)
        mock_get_switch_by_dpid.return_value = switch

        destination = switch.id
        endpoint = f'{flow_manager_url}/flows/{destination}'

        event = get_kytos_event_mock(name='kytos/topology.switch.enabled',
                                     content={'dpid': dpid})
        self.napp.install_table_miss_flow(event)

        data = {'flows': [expected_flow]}
        mock_requests.post.assert_called_with(endpoint, json=data)
Пример #5
0
    def test_handle_errors(self, mock_send_napp_event):
        """Test handle_errors method."""
        flow = MagicMock()
        self.napp._flow_mods_sent[0] = (flow, 'add')

        switch = get_switch_mock("00:00:00:00:00:00:00:01")
        switch.connection = get_connection_mock(
            0x04, get_switch_mock("00:00:00:00:00:00:00:02"))

        protocol = MagicMock()
        protocol.unpack.return_value = 'error_packet'

        switch.connection.protocol = protocol

        message = MagicMock()
        message.header.xid.value = 0
        message.error_type = 2
        message.code = 5
        event = get_kytos_event_mock(name='.*.of_core.*.ofpt_error',
                                     content={
                                         'message': message,
                                         'source': switch.connection
                                     })
        self.napp.handle_errors(event)

        mock_send_napp_event.assert_called_with(flow.switch,
                                                flow,
                                                'error',
                                                error_command='add',
                                                error_code=5,
                                                error_type=2)
Пример #6
0
    def test_handle_raw_in(self, *args):
        """Test handle_raw_in."""
        (mock_emit_message_in, mock_negotiate, mock_of_slicer) = args

        mock_packets = MagicMock()
        mock_data = MagicMock()
        mock_connection = MagicMock()
        mock_connection.is_new.side_effect = [True, False, True, False]
        mock_connection.is_during_setup.return_value = False
        mock_of_slicer.return_value = [[mock_packets, mock_packets], b'']
        name = 'kytos/core.openflow.raw.in'
        content = {'source': mock_connection, 'new_data': mock_data}
        mock_event = get_kytos_event_mock(name=name, content=content)

        self.napp.handle_raw_in(mock_event)
        mock_negotiate.assert_called()
        mock_emit_message_in.assert_called()

        # Test Fail
        mock_negotiate.side_effect = NegotiationException('Foo')
        self.napp.handle_raw_in(mock_event)
        self.assertEqual(mock_connection.close.call_count, 1)

        mock_connection.close.call_count = 0
        mock_connection.protocol.unpack.side_effect = AttributeError()
        self.napp.handle_raw_in(mock_event)
        self.assertEqual(mock_connection.close.call_count, 1)
Пример #7
0
    def test_handle_lldp_flows(self, mock_post, mock_delete):
        """Test handle_lldp_flow method."""
        dpid = "00:00:00:00:00:00:00:01"
        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
        self.napp.controller.switches = {dpid: switch}
        event_post = get_kytos_event_mock(name='kytos/topology.switch.enabled',
                                          content={'dpid': dpid})

        event_del = get_kytos_event_mock(name='kytos/topology.switch.disabled',
                                         content={'dpid': dpid})

        self.napp.handle_lldp_flows(event_post)
        mock_post.assert_called()

        self.napp.handle_lldp_flows(event_del)
        mock_delete.assert_called()
Пример #8
0
 def test_handle_openflow_in_hello_failed(self):
     """Test handle_openflow_in_hello_failed."""
     mock_destination = MagicMock()
     content = {'destination': mock_destination}
     mock_event = get_kytos_event_mock(name='kytos/of_core',
                                       content=content)
     self.napp.handle_openflow_in_hello_failed(mock_event)
     self.assertEqual(mock_event.destination.close.call_count, 1)
Пример #9
0
 def test_handle_features_request_sent(self):
     """Test tests_handle_features_request_sent."""
     mock_protocol = MagicMock()
     mock_protocol.protocol.state = 'sending_features'
     expected = 'waiting_features_reply'
     name = 'kytos/of_core.v0x0[14].messages.out.ofpt_features_request'
     content = {'destination': mock_protocol}
     mock_event = get_kytos_event_mock(name=name, content=content)
     self.napp.handle_features_request_sent(mock_event)
     self.assertEqual(mock_event.destination.protocol.state, expected)
Пример #10
0
    def test_event_delete_failure_case(self, *args):
        """Test event_delete method to failure case."""
        (mock_delete_metadata, _) = args
        event = get_kytos_event_mock(name='kytos.storehouse.delete',
                                     content={})

        self.napp.event_delete(event)

        self.napp.backend.delete.assert_not_called()
        mock_delete_metadata.assert_not_called()
Пример #11
0
    def test_event_list_success_case(self, mock_execute_callback):
        """Test event_list method to success case."""
        result = MagicMock()
        self.napp.backend.list.return_value = result

        event = get_kytos_event_mock(name='kytos.storehouse.list',
                                     content={'namespace': 'namespace'})
        self.napp.event_list(event)

        mock_execute_callback.assert_called_with(event, result, None)
Пример #12
0
    def test_event_list_failure_case(self, mock_execute_callback):
        """Test event_list method to failure case."""
        error = KeyError()
        self.napp.backend.list.side_effect = [error]

        event = get_kytos_event_mock(name='kytos.storehouse.list',
                                     content={'namespace': 'namespace'})
        self.napp.event_list(event)

        mock_execute_callback.assert_called_with(event, None, error)
Пример #13
0
 def test_kytos_event_mock(self):
     """Test kytos_event mock."""
     kytos_event_mock = get_kytos_event_mock(name='event',
                                             content={
                                                 'message': 'msg',
                                                 'destination': 'dest',
                                                 'source': 'src'
                                             })
     self.assertEqual(kytos_event_mock.name, 'event')
     self.assertEqual(kytos_event_mock.message, 'msg')
     self.assertEqual(kytos_event_mock.destination, 'dest')
     self.assertEqual(kytos_event_mock.source, 'src')
Пример #14
0
    def test_event_update_failure_case(self, mock_execute_callback):
        """Test event_update method to failure case."""
        self.napp.backend.retrieve.return_value = None

        event = get_kytos_event_mock(name='kytos.storehouse.update',
                                     content={
                                         'namespace': 'namespace',
                                         'box_id': '123'
                                     })
        self.napp.event_update(event)

        self.napp.backend.update.assert_not_called()
Пример #15
0
 def test_event_add_flow(self, mock_install_flows):
     """Test method for installing flows on the switches through events."""
     dpid = "00:00:00:00:00:00:00:01"
     switch = get_switch_mock(dpid)
     self.napp.controller.switches = {dpid: switch}
     mock_flow_dict = MagicMock()
     event = get_kytos_event_mock(name='kytos.flow_manager.flows.install',
                                  content={
                                      'dpid': dpid,
                                      'flow_dict': mock_flow_dict
                                  })
     self.napp.event_flows_install_delete(event)
     mock_install_flows.assert_called_with('add', mock_flow_dict, [switch])
Пример #16
0
    def test_event_update_success_case(self, mock_execute_callback):
        """Test event_update method to success case."""
        box = MagicMock()
        self.napp.backend.retrieve.return_value = box

        event = get_kytos_event_mock(name='kytos.storehouse.update',
                                     content={
                                         'namespace': 'namespace',
                                         'box_id': '123'
                                     })
        self.napp.event_update(event)

        self.napp.backend.update.assert_called_with('namespace', box)
Пример #17
0
    def test_handle_packet_in(self, *args):
        """Test handle_packet_in method."""
        (mock_ethernet, mock_kytos_event, mock_create_flow_mod,
         mock_create_packet_out, mock_buffer_put) = args

        ethernet = MagicMock()
        mock_ethernet.return_value = ethernet

        flow_mod = MagicMock()
        mock_create_flow_mod.return_value = flow_mod

        packet_out = MagicMock()
        mock_create_packet_out.return_value = packet_out

        ports = [1, 2]
        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
        switch.where_is_mac.return_value = ports
        message = MagicMock()
        message.reason = 0
        message.in_port = 1
        event = get_kytos_event_mock(name='kytos/of_core.v0x0[14].messages.in.'
                                     'ofpt_packet_in',
                                     content={
                                         'source': switch.connection,
                                         'message': message
                                     })

        self.napp.handle_packet_in(event)

        switch.update_mac_table.assert_called_with(ethernet.source,
                                                   message.in_port)
        mock_create_flow_mod.assert_called_with(switch.ofp_version, ethernet,
                                                ports[0])
        mock_create_packet_out.assert_called_with(switch.ofp_version, message,
                                                  ports)
        calls = [
            call(name=('kytos/of_l2ls.messages.out.'
                       'ofpt_flow_mod'),
                 content={
                     'destination': event.source,
                     'message': flow_mod
                 }),
            call(name=('kytos/of_l2ls.messages.out.'
                       'ofpt_packet_out'),
                 content={
                     'destination': event.source,
                     'message': packet_out
                 })
        ]
        mock_kytos_event.assert_has_calls(calls)
        self.assertEqual(mock_buffer_put.call_count, 2)
Пример #18
0
    def test_handle_multipart_reply(self, *args):
        """Test handle multipart reply."""
        (mock_of_core_v0x04_utils, mock_from_of_flow_stats_v0x04) = args

        flow_msg = MagicMock()
        flow_msg.multipart_type = MultipartType.OFPMP_FLOW
        name = 'kytos/of_core.v0x04.messages.in.ofpt_multipart_reply'
        content = {"source": self.switch_v0x04.connection, "message": flow_msg}
        event = get_kytos_event_mock(name=name, content=content)

        self.napp.handle_multipart_reply(event)
        mock_from_of_flow_stats_v0x04.assert_called_with(
            flow_msg, self.switch_v0x04.connection.switch)

        ofpmp_port_desc = MagicMock()
        ofpmp_port_desc.body = "A"
        ofpmp_port_desc.multipart_type = MultipartType.OFPMP_PORT_DESC
        content = {
            "source": self.switch_v0x04.connection,
            "message": ofpmp_port_desc
        }
        event = get_kytos_event_mock(name=name, content=content)
        self.napp.handle_multipart_reply(event)
        mock_of_core_v0x04_utils.assert_called_with(
            self.napp.controller, self.switch_v0x04.connection.switch,
            ofpmp_port_desc.body)

        ofpmp_desc = MagicMock()
        ofpmp_desc.body = "A"
        ofpmp_desc.multipart_type = MultipartType.OFPMP_DESC
        content = {
            "source": self.switch_v0x04.connection,
            "message": ofpmp_desc
        }
        event = get_kytos_event_mock(name=name, content=content)
        switch_update = self.switch_v0x04.connection.switch.update_description
        self.napp.handle_multipart_reply(event)
        self.assertEqual(switch_update.call_count, 1)
Пример #19
0
    def test_handle_stats_reply(self, mock_from_of_flow_stats_v0x01):
        """Test handle stats reply."""
        mock_from_of_flow_stats_v0x01.return_value = "ABC"

        flow_msg = MagicMock()
        flow_msg.body = "A"
        flow_msg.body_type = StatsType.OFPST_FLOW

        name = 'kytos/of_core.v0x01.messages.in.ofpt_stats_reply'
        content = {"source": self.switch_v0x01.connection, "message": flow_msg}
        event = get_kytos_event_mock(name=name, content=content)
        self.napp.handle_stats_reply(event)
        mock_from_of_flow_stats_v0x01.assert_called_with(
            flow_msg.body, self.switch_v0x01.connection.switch)

        desc_msg = MagicMock()
        desc_msg.body = "A"
        desc_msg.body_type = StatsType.OFPST_DESC
        content = {"source": self.switch_v0x01.connection, "message": desc_msg}
        event = get_kytos_event_mock(name=name, content=content)
        switch_update = self.switch_v0x01.connection.switch.update_description
        self.napp.handle_stats_reply(event)
        self.assertEqual(switch_update.call_count, 1)
Пример #20
0
    def test_event_delete_success_case(self, *args):
        """Test event_delete method to success case."""
        (mock_delete_metadata, _) = args
        self.napp.backend.delete.return_value = 'result'

        event = get_kytos_event_mock(name='kytos.storehouse.delete',
                                     content={
                                         'namespace': 'namespace',
                                         'box_id': '123'
                                     })

        self.napp.event_delete(event)

        self.napp.backend.delete.assert_called_once_with('namespace', '123')
        mock_delete_metadata.assert_called_once_with('namespace', '123')
Пример #21
0
    def test_handle_errors(self, mock_send_napp_event):
        """Test handle_errors method."""
        flow = MagicMock()
        self.napp._flow_mods_sent[0] = flow

        message = MagicMock()
        message.header.xid.value = 0
        message.error_type = 2
        message.code = 5
        event = get_kytos_event_mock(name='.*.of_core.*.ofpt_error',
                                     content={'message': message})
        self.napp.handle_errors(event)

        mock_send_napp_event.assert_called_with(flow.switch, flow, 'error',
                                                error_code=5, error_type=2)
Пример #22
0
    def test_event_create_failure_case(self, *args):
        """Test event_create method to failure case."""
        (mock_search_metadata_by, mock_add_metadata_to_cache, _) = args
        mock_search_metadata_by.return_value = True

        event = get_kytos_event_mock(name='kytos.storehouse.create',
                                     content={
                                         'namespace': 'namespace',
                                         'box_id': '123',
                                         'data': 'data'
                                     })

        self.napp.event_create(event)

        self.napp.backend.create.assert_not_called()
        mock_add_metadata_to_cache.assert_not_called()
Пример #23
0
    def test_event_create_success_case(self, *args):
        """Test event_create method to success case."""
        (mock_search_metadata_by, mock_add_metadata_to_cache, _,
         mock_box) = args
        mock_search_metadata_by.return_value = False

        event = get_kytos_event_mock(name='kytos.storehouse.create',
                                     content={
                                         'namespace': 'namespace',
                                         'box_id': 'box_id',
                                         'data': 'data'
                                     })

        self.napp.event_create(event)

        self.napp.backend.create.assert_called_with(mock_box.return_value)
        mock_add_metadata_to_cache.assert_called_with(mock_box.return_value)
Пример #24
0
    def test_install_lldp_flow(self, *args):
        """Test install_lldp_flow method."""
        (mock_build_lldp_packet_out, mock_kytos_event, mock_buffer_put) = args

        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
        event = get_kytos_event_mock(name='kytos/of_core.handshake.completed',
                                     content={'switch': switch})

        mock_build_lldp_packet_out.side_effect = ['flow_mod', None]
        mock_kytos_event.return_value = 'ofpt_flow_mod'

        self.napp.install_lldp_flow(event)

        switch.connection = None

        self.napp.install_lldp_flow(event)

        mock_buffer_put.assert_called_once_with('ofpt_flow_mod')
Пример #25
0
    def test_notify_uplink_detected(self, *args):
        """Test notify_uplink_detected method."""
        (mock_ethernet, mock_lldp, mock_dpid, mock_ubint32,
         mock_unpack_non_empty, mock_get_switch_by_dpid, mock_kytos_event,
         mock_buffer_put) = args

        switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
        message = MagicMock()
        message.in_port = 1
        message.data = 'data'
        event = get_kytos_event_mock(name='kytos/of_core.v0x0[14].messages.in.'
                                     'ofpt_packet_in',
                                     content={
                                         'source': switch.connection,
                                         'message': message
                                     })

        ethernet = MagicMock()
        ethernet.ether_type = 0x88CC
        ethernet.data = 'eth_data'
        lldp = MagicMock()
        lldp.chassis_id.sub_value = 'chassis_id'
        lldp.port_id.sub_value = 'port_id'
        dpid = MagicMock()
        dpid.value = "00:00:00:00:00:00:00:02"
        port_b = MagicMock()

        mock_unpack_non_empty.side_effect = [ethernet, lldp, dpid, port_b]
        mock_get_switch_by_dpid.return_value = get_switch_mock(
            dpid.value, 0x04)
        mock_kytos_event.return_value = 'nni'

        self.napp.notify_uplink_detected(event)

        calls = [
            call(mock_ethernet, message.data),
            call(mock_lldp, ethernet.data),
            call(mock_dpid, lldp.chassis_id.sub_value),
            call(mock_ubint32, lldp.port_id.sub_value)
        ]
        mock_unpack_non_empty.assert_has_calls(calls)
        mock_buffer_put.assert_called_with('nni')