1
|
|
|
"""Module with utilities to create tests.""" |
2
|
|
|
from unittest.mock import MagicMock, Mock, create_autospec |
3
|
|
|
|
4
|
|
|
from kytos.core import Controller |
5
|
|
|
from kytos.core.config import KytosConfig |
6
|
|
|
from kytos.core.connection import (Connection, ConnectionProtocol, |
7
|
|
|
ConnectionState) |
8
|
|
|
from kytos.core.events import KytosEvent |
9
|
|
|
from kytos.core.interface import Interface |
10
|
|
|
from kytos.core.link import Link |
11
|
|
|
from kytos.core.switch import Switch |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def get_controller_mock(): |
15
|
|
|
"""Return a controller mock.""" |
16
|
|
|
options = KytosConfig().options['daemon'] |
17
|
|
|
controller = Controller(options) |
18
|
|
|
controller.log = Mock() |
19
|
|
|
return controller |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def get_interface_mock(name, port_number, switch, address="00:00:00:00:00:00"): |
23
|
|
|
"""Return a interface mock.""" |
24
|
|
|
interface = create_autospec(Interface) |
25
|
|
|
interface.id = "{}:{}".format(switch.dpid, port_number) |
26
|
|
|
interface.name = name |
27
|
|
|
interface.port_number = port_number |
28
|
|
|
interface.switch = switch |
29
|
|
|
interface.address = address |
30
|
|
|
interface.lldp = True |
31
|
|
|
return interface |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
def get_link_mock(endpoint_a, endpoint_b): |
35
|
|
|
"""Return a link mock.""" |
36
|
|
|
link = create_autospec(Link) |
37
|
|
|
link.endpoint_a = endpoint_a |
38
|
|
|
link.endpoint_b = endpoint_b |
39
|
|
|
link.metadata = {"A": 0} |
40
|
|
|
return link |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def get_switch_mock(dpid, of_version=None): |
44
|
|
|
"""Return a switch mock.""" |
45
|
|
|
switch = create_autospec(Switch) |
46
|
|
|
switch.dpid = dpid |
47
|
|
|
if of_version: |
48
|
|
|
switch.ofp_version = '0x0' + str(of_version) |
49
|
|
|
switch.connection = get_connection_mock(of_version, switch) |
50
|
|
|
return switch |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def get_connection_mock(of_version, switch, address="00:00:00:00:00:00", |
54
|
|
|
state=ConnectionState.NEW): |
55
|
|
|
"""Return a connection mock.""" |
56
|
|
|
protocol = create_autospec(ConnectionProtocol) |
57
|
|
|
protocol.version = of_version |
58
|
|
|
connection = create_autospec(Connection) |
59
|
|
|
connection.protocol = protocol |
60
|
|
|
connection.switch = switch |
61
|
|
|
connection.address = address |
62
|
|
|
connection.state = state |
63
|
|
|
return connection |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def get_kytos_event_mock(name, content): |
67
|
|
|
"""Return a kytos event mock.""" |
68
|
|
|
event = create_autospec(KytosEvent) |
69
|
|
|
event.name = name |
70
|
|
|
event.content = content |
71
|
|
|
event.message = content.get('message') |
72
|
|
|
event.destination = content.get('destination') |
73
|
|
|
event.source = content.get('source') |
74
|
|
|
return event |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def get_test_client(controller, napp): |
78
|
|
|
"""Return a flask api test client.""" |
79
|
|
|
controller.api_server.register_napp_endpoints(napp) |
80
|
|
|
return controller.api_server.app.test_client() |
81
|
|
|
|