1
|
|
|
"""Async TCP Server tests.""" |
2
|
|
|
|
3
|
1 |
|
import asyncio |
4
|
1 |
|
import errno |
5
|
1 |
|
import logging |
6
|
1 |
|
from unittest.mock import MagicMock, patch |
7
|
|
|
|
8
|
1 |
|
from kytos.core.atcp_server import (KytosServer, KytosServerProtocol, |
9
|
|
|
exception_handler) |
10
|
|
|
|
11
|
|
|
# Using "nettest" TCP port as a way to avoid conflict with a running |
12
|
|
|
# Kytos server on 6653. |
13
|
1 |
|
TEST_ADDRESS = ('127.0.0.1', 4138) |
14
|
|
|
# pylint: disable=protected-access |
15
|
|
|
|
16
|
|
|
|
17
|
1 |
|
class TestKytosServer: |
18
|
|
|
"""Test if a Kytos Server will go up and receive connections.""" |
19
|
|
|
|
20
|
1 |
|
def setup_method(self): |
21
|
|
|
"""Start new asyncio loop and a test TCP server.""" |
22
|
|
|
# pylint: disable=attribute-defined-outside-init |
23
|
1 |
|
self.server = KytosServer(TEST_ADDRESS, KytosServerProtocol, |
24
|
|
|
None, "openflow") |
25
|
1 |
|
self.server.serve_forever() |
26
|
|
|
|
27
|
1 |
|
def test_connection_to_server(self): |
28
|
|
|
"""Test if we really can connect to TEST_ADDRESS.""" |
29
|
1 |
|
async def wait_and_go(): |
30
|
|
|
"""Wait a little for the server to go up, then connect.""" |
31
|
1 |
|
await asyncio.sleep(0.01) |
32
|
|
|
# reader, writer = ... |
33
|
1 |
|
_ = await asyncio.open_connection(*TEST_ADDRESS) |
34
|
|
|
|
35
|
1 |
|
self.server.loop.run_until_complete(wait_and_go()) |
36
|
|
|
|
37
|
1 |
|
def test_exception_handler_oserror(self, caplog): |
38
|
|
|
"""Test the TCP Server Exception Handler. |
39
|
|
|
|
40
|
|
|
1. create mock OSError/TimeoutError instances |
41
|
|
|
2. call exception_handler with them |
42
|
|
|
3. ensure log is OK |
43
|
|
|
""" |
44
|
1 |
|
caplog.set_level(logging.INFO) |
45
|
|
|
|
46
|
1 |
|
exception = TimeoutError() |
47
|
1 |
|
context = {"exception": exception, |
48
|
|
|
"transport": "unit_tests"} |
49
|
1 |
|
exception_handler(self.server.loop, context) |
50
|
|
|
|
51
|
1 |
|
exception2 = OSError(errno.EBADF, "Bad file descriptor") |
52
|
1 |
|
context2 = {"exception": exception2, |
53
|
|
|
"transport": "unit_tests"} |
54
|
1 |
|
exception_handler(self.server.loop, context2) |
55
|
|
|
|
56
|
1 |
|
assert caplog.record_tuples == [ |
57
|
|
|
("kytos.core.atcp_server", |
58
|
|
|
logging.INFO, |
59
|
|
|
"Socket timeout: 'unit_tests'"), |
60
|
|
|
("kytos.core.atcp_server", |
61
|
|
|
logging.INFO, |
62
|
|
|
"Socket closed: 'unit_tests'"), |
63
|
|
|
] |
64
|
|
|
|
65
|
|
|
|
66
|
1 |
|
class TestKytosServerProtocol: |
67
|
|
|
"""KytosServerProtocol tests.""" |
68
|
|
|
|
69
|
1 |
|
def setup_method(self): |
70
|
|
|
"""Instantiate a KytosServerProtocol.""" |
71
|
|
|
# pylint: disable=attribute-defined-outside-init |
72
|
1 |
|
loop = asyncio.new_event_loop() |
73
|
1 |
|
asyncio.set_event_loop(loop) |
74
|
|
|
|
75
|
1 |
|
self.connection = MagicMock() |
76
|
1 |
|
self.connection.address = 'addr' |
77
|
1 |
|
self.connection.port = 123 |
78
|
|
|
|
79
|
1 |
|
self.server_protocol = KytosServerProtocol() |
80
|
1 |
|
self.server_protocol._loop = MagicMock() |
81
|
1 |
|
self.server_protocol.server = MagicMock() |
82
|
1 |
|
self.server_protocol.connection = self.connection |
83
|
|
|
|
84
|
1 |
|
@patch('kytos.core.atcp_server.KytosEvent') |
85
|
1 |
|
def test_data_received(self, mock_kytos_event): |
86
|
|
|
"""Test data_received method.""" |
87
|
1 |
|
buffers = self.server_protocol.server.controller.buffers |
88
|
1 |
|
self.connection.protocol.name = 'protocol' |
89
|
|
|
|
90
|
1 |
|
self.server_protocol.data_received(b'data') |
91
|
|
|
|
92
|
1 |
|
expected_content = {'source': self.connection, 'new_data': b'data'} |
93
|
1 |
|
expected_name = 'kytos/core.protocol.raw.in' |
94
|
1 |
|
mock_kytos_event.assert_called_with(content=expected_content, |
95
|
|
|
name=expected_name) |
96
|
1 |
|
buffers.raw.aput.assert_called_with(mock_kytos_event.return_value) |
97
|
|
|
|
98
|
1 |
|
@patch('kytos.core.atcp_server.KytosEvent') |
99
|
1 |
|
def test_connection_lost(self, mock_kytos_event): |
100
|
|
|
"""Test connection_lost method.""" |
101
|
1 |
|
buffers = self.server_protocol.server.controller.buffers |
102
|
1 |
|
self.connection.protocol.name = 'protocol' |
103
|
|
|
|
104
|
1 |
|
self.server_protocol.connection_lost('exc') |
105
|
|
|
|
106
|
1 |
|
self.connection.close.assert_called() |
107
|
1 |
|
expected_content = {'source': self.connection, 'exception': 'exc'} |
108
|
1 |
|
expected_name = 'kytos/core.protocol.connection.lost' |
109
|
1 |
|
mock_kytos_event.assert_called_with(content=expected_content, |
110
|
|
|
name=expected_name) |
111
|
|
|
buffers.conn.aput.assert_called_with(mock_kytos_event.return_value) |
112
|
|
|
|