1
|
|
|
"""Test v0x01.utils methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch, PropertyMock |
4
|
|
|
|
5
|
|
|
from kytos.lib.helpers import get_switch_mock, get_connection_mock |
6
|
|
|
from napps.kytos.of_core.v0x01.utils import (send_desc_request, send_echo, |
7
|
|
|
say_hello, send_set_config, |
8
|
|
|
handle_features_reply) |
9
|
|
|
|
10
|
|
|
from tests.helpers import get_controller_mock |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class TestUtils(TestCase): |
14
|
|
|
"""Test utils.""" |
15
|
|
|
|
16
|
|
|
def setUp(self): |
17
|
|
|
"""Execute steps before each tests.""" |
18
|
|
|
self.mock_controller = get_controller_mock() |
19
|
|
|
self.mock_switch = get_switch_mock('00:00:00:00:00:00:00:01', 0x01) |
20
|
|
|
self.mock_connection = get_connection_mock(0x01, self.mock_switch) |
21
|
|
|
|
22
|
|
|
@patch('napps.kytos.of_core.v0x01.utils.emit_message_out') |
23
|
|
|
def test_send_desc_request(self, mock_emit_message_out): |
24
|
|
|
"""Test send_desc_request.""" |
25
|
|
|
send_desc_request(self.mock_controller, self.mock_switch) |
26
|
|
|
mock_emit_message_out.assert_called() |
27
|
|
|
|
28
|
|
|
def test_handle_features_reply(self): |
29
|
|
|
"""test Handle features reply.""" |
30
|
|
|
mock_event = MagicMock() |
31
|
|
|
mock_features = MagicMock() |
32
|
|
|
mock_controller = MagicMock() |
33
|
|
|
self.mock_switch.get_interface_by_port_no.side_effect = [MagicMock(), |
34
|
|
|
False] |
35
|
|
|
type(mock_features).ports = PropertyMock(return_value=[MagicMock()]) |
36
|
|
|
type(mock_event).content = PropertyMock(return_value={'message': |
37
|
|
|
mock_features}) |
38
|
|
|
mock_controller.get_switch_or_create.return_value = self.mock_switch |
39
|
|
|
response = handle_features_reply(mock_controller, mock_event) |
40
|
|
|
self.assertEqual(self.mock_switch, response) |
41
|
|
|
self.assertEqual(self.mock_switch.update_features.call_count, 1) |
42
|
|
|
|
43
|
|
|
self.mock_switch.update_features.call_count = 0 |
44
|
|
|
response = handle_features_reply(mock_controller, mock_event) |
45
|
|
|
self.assertEqual(self.mock_switch, response) |
46
|
|
|
self.assertEqual(self.mock_switch.update_features.call_count, 1) |
47
|
|
|
|
48
|
|
|
@patch('napps.kytos.of_core.v0x01.utils.emit_message_out') |
49
|
|
|
def test_send_echo(self, mock_emit_message_out): |
50
|
|
|
"""Test send_echo.""" |
51
|
|
|
send_echo(self.mock_controller, self.mock_switch) |
52
|
|
|
mock_emit_message_out.assert_called() |
53
|
|
|
|
54
|
|
|
@patch('napps.kytos.of_core.v0x01.utils.emit_message_out') |
55
|
|
|
def test_set_config(self, mock_emit_message_out): |
56
|
|
|
"""Test set_config.""" |
57
|
|
|
send_set_config(self.mock_controller, self.mock_switch) |
58
|
|
|
mock_emit_message_out.assert_called() |
59
|
|
|
|
60
|
|
|
@patch('napps.kytos.of_core.v0x01.utils.emit_message_out') |
61
|
|
|
def test_say_hello(self, mock_emit_message_out): |
62
|
|
|
"""Test say_hello.""" |
63
|
|
|
say_hello(self.mock_controller, self.mock_switch) |
64
|
|
|
mock_emit_message_out.assert_called() |
65
|
|
|
|