1
|
|
|
"""Module to help 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): |
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 = "00:00:00:00:00:00" |
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, state=ConnectionState.NEW): |
54
|
|
|
"""Return a connection mock.""" |
55
|
|
|
protocol = create_autospec(ConnectionProtocol) |
56
|
|
|
protocol.version = of_version |
57
|
|
|
connection = create_autospec(Connection) |
58
|
|
|
connection.protocol = protocol |
59
|
|
|
connection.switch = switch |
60
|
|
|
connection.address = "00:00:00:00:00:00" |
61
|
|
|
connection.state = state |
62
|
|
|
return connection |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def get_kytos_event_mock(name, content): |
66
|
|
|
"""Return a kytos event mock.""" |
67
|
|
|
event = create_autospec(KytosEvent) |
68
|
|
|
event.name = name |
69
|
|
|
event.content = content |
70
|
|
|
event.message = content.get('message') |
71
|
|
|
event.destination = content.get('destination') |
72
|
|
|
event.source = content.get('source') |
73
|
|
|
return event |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def get_topology_mock(): |
77
|
|
|
"""Create a default topology.""" |
78
|
|
|
switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
79
|
|
|
switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) |
80
|
|
|
switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01) |
81
|
|
|
|
82
|
|
|
interface_a1 = get_interface_mock("s1-eth1", 1, switch_a) |
83
|
|
|
interface_a2 = get_interface_mock("s1-eth2", 2, switch_a) |
84
|
|
|
|
85
|
|
|
interface_b1 = get_interface_mock("s2-eth1", 1, switch_b) |
86
|
|
|
interface_b2 = get_interface_mock("s2-eth2", 2, switch_b) |
87
|
|
|
|
88
|
|
|
interface_c1 = get_interface_mock("s3-eth1", 1, switch_c) |
89
|
|
|
interface_c2 = get_interface_mock("s3-eth2", 2, switch_c) |
90
|
|
|
|
91
|
|
|
switch_a.interfaces = {interface_a1.id: interface_a1, |
92
|
|
|
interface_a2.id: interface_a2} |
93
|
|
|
switch_b.interfaces = {interface_b1.id: interface_b1, |
94
|
|
|
interface_b2.id: interface_b2} |
95
|
|
|
switch_c.interfaces = {interface_c1.id: interface_c1, |
96
|
|
|
interface_c2.id: interface_c2} |
97
|
|
|
|
98
|
|
|
link_1 = get_link_mock(interface_a1, interface_b1) |
99
|
|
|
link_2 = get_link_mock(interface_a2, interface_c1) |
100
|
|
|
link_3 = get_link_mock(interface_b2, interface_c2) |
101
|
|
|
|
102
|
|
|
topology = MagicMock() |
103
|
|
|
topology.links = {"1": link_1, "2": link_2, "3": link_3} |
104
|
|
|
topology.switches = {switch_a.dpid: switch_a, |
105
|
|
|
switch_b.dpid: switch_b, |
106
|
|
|
switch_c.dpid: switch_c} |
107
|
|
|
return topology |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def get_test_client(controller, napp): |
111
|
|
|
"""Return a flask api test client.""" |
112
|
|
|
controller.api_server.register_napp_endpoints(napp) |
113
|
|
|
return controller.api_server.app.test_client() |
114
|
|
|
|