Passed
Push — master ( 55f662...e18d69 )
by Humberto
02:50 queued 11s
created

build.tests.unit.v0x04.test_utils   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 56
dl 0
loc 79
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A TestUtils.test_send_desc_request() 0 5 1
A TestUtils.setUp() 0 5 1
A TestUtils.test_port_request() 0 5 1
A TestUtils.test_handle_features_reply() 0 8 1
A TestUtils.test_set_config() 0 5 1
A TestUtils.test_send_echo() 0 5 1
A TestUtils.test_handle_port_desc() 0 17 1
A TestUtils.test_say_hello() 0 5 1
1
"""Test v0x04.utils methods."""
2
from unittest import TestCase
3
from unittest.mock import MagicMock, patch
4
5
from kytos.lib.helpers import get_switch_mock, get_connection_mock
6
from napps.kytos.of_core.v0x04.utils import (send_desc_request, say_hello,
7
                                             send_port_request, send_echo,
8
                                             send_set_config, handle_port_desc,
9
                                             handle_features_reply)
10
11
from tests.helpers import get_controller_mock
12
13
14
class TestUtils(TestCase):
15
    """Test utils."""
16
17
    def setUp(self):
18
        """Execute steps before each tests."""
19
        self.mock_controller = get_controller_mock()
20
        self.mock_switch = get_switch_mock('00:00:00:00:00:00:00:01', 0x04)
21
        self.mock_connection = get_connection_mock(0x04, self.mock_switch)
22
23
    @patch('napps.kytos.of_core.v0x04.utils.emit_message_out')
24
    def test_send_desc_request(self, mock_emit_message_out):
25
        """Test send_desc_request."""
26
        send_desc_request(self.mock_controller, self.mock_switch)
27
        mock_emit_message_out.assert_called()
28
29
    @patch('napps.kytos.of_core.v0x04.utils.emit_message_out')
30
    def test_port_request(self, mock_emit_message_out):
31
        """Test send_desc_request."""
32
        send_port_request(self.mock_controller, self.mock_switch)
33
        mock_emit_message_out.assert_called()
34
35
    def test_handle_features_reply(self):
36
        """Test Handle features reply."""
37
        mock_controller = MagicMock()
38
        mock_event = MagicMock()
39
        mock_controller.get_switch_or_create.return_value = self.mock_switch
40
        response = handle_features_reply(mock_controller, mock_event)
41
        self.assertEqual(self.mock_switch, response)
42
        self.assertEqual(self.mock_switch.update_features.call_count, 1)
43
44
    @patch('kytos.core.buffers.KytosEventBuffer.put')
45
    def test_handle_port_desc(self, mock_event_buffer):
46
        """Test Handle Port Desc."""
47
        mock_port = MagicMock()
48
        self.mock_switch.get_interface_by_port_no.side_effect = [MagicMock(),
49
                                                                 False]
50
        handle_port_desc(self.mock_controller, self.mock_switch, [mock_port])
51
        self.assertEqual(self.mock_switch.update_interface.call_count, 1)
52
        mock_event_buffer.assert_called()
53
        self.assertEqual(self.mock_controller.buffers.app.put.call_count, 1)
54
55
        self.mock_switch.update_interface.call_count = 0
56
        self.mock_controller.buffers.app.put.call_count = 0
57
        handle_port_desc(self.mock_controller, self.mock_switch, [mock_port])
58
        self.assertEqual(self.mock_switch.update_interface.call_count, 1)
59
        mock_event_buffer.assert_called()
60
        self.assertEqual(self.mock_controller.buffers.app.put.call_count, 1)
61
62
    @patch('napps.kytos.of_core.v0x04.utils.emit_message_out')
63
    def test_send_echo(self, mock_emit_message_out):
64
        """Test send_echo."""
65
        send_echo(self.mock_controller, self.mock_switch)
66
        mock_emit_message_out.assert_called()
67
68
    @patch('napps.kytos.of_core.v0x04.utils.emit_message_out')
69
    def test_set_config(self, mock_emit_message_out):
70
        """Test set_config."""
71
        send_set_config(self.mock_controller, self.mock_switch)
72
        mock_emit_message_out.assert_called()
73
74
    @patch('napps.kytos.of_core.v0x04.utils.emit_message_out')
75
    def test_say_hello(self, mock_emit_message_out):
76
        """Test say_hello."""
77
        say_hello(self.mock_controller, self.mock_switch)
78
        mock_emit_message_out.assert_called()
79