|
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.interface import Interface |
|
9
|
|
|
from kytos.core.link import Link |
|
10
|
|
|
from kytos.core.switch import Switch |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def get_controller_mock(): |
|
14
|
|
|
"""Return a controller mock.""" |
|
15
|
|
|
options = KytosConfig().options['daemon'] |
|
16
|
|
|
controller = Controller(options) |
|
17
|
|
|
controller.log = Mock() |
|
18
|
|
|
return controller |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def get_interface_mock(name, port_number, switch): |
|
22
|
|
|
"""Return a interface mock.""" |
|
23
|
|
|
interface = create_autospec(Interface) |
|
24
|
|
|
interface.id = "{}:{}".format(switch.dpid, port_number) |
|
25
|
|
|
interface.name = name |
|
26
|
|
|
interface.port_number = port_number |
|
27
|
|
|
interface.switch = switch |
|
28
|
|
|
interface.address = "00:00:00:00:00:00" |
|
29
|
|
|
interface.lldp = True |
|
30
|
|
|
return interface |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
def get_link_mock(endpoint_a, endpoint_b): |
|
34
|
|
|
"""Return a link mock.""" |
|
35
|
|
|
link = create_autospec(Link) |
|
36
|
|
|
link.endpoint_a = endpoint_a |
|
37
|
|
|
link.endpoint_b = endpoint_b |
|
38
|
|
|
link.metadata = {"A": 0} |
|
39
|
|
|
return link |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def get_switch_mock(dpid, of_version=None): |
|
43
|
|
|
"""Return a switch mock.""" |
|
44
|
|
|
switch = create_autospec(Switch) |
|
45
|
|
|
switch.dpid = dpid |
|
46
|
|
|
if of_version: |
|
47
|
|
|
switch.connection = get_connection_mock(of_version, switch) |
|
48
|
|
|
switch.ofp_version = '0x0' + str(of_version) |
|
49
|
|
|
return switch |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def get_connection_mock(of_version, switch, state=ConnectionState.NEW): |
|
53
|
|
|
"""Return a connection mock.""" |
|
54
|
|
|
protocol = create_autospec(ConnectionProtocol) |
|
55
|
|
|
protocol.version = of_version |
|
56
|
|
|
connection = create_autospec(Connection) |
|
57
|
|
|
connection.protocol = protocol |
|
58
|
|
|
connection.switch = switch |
|
59
|
|
|
connection.address = "00:00:00:00:00:00" |
|
60
|
|
|
connection.state = state |
|
61
|
|
|
return connection |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def get_topology_mock(): |
|
65
|
|
|
"""Create a default topology.""" |
|
66
|
|
|
switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
67
|
|
|
switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) |
|
68
|
|
|
switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01) |
|
69
|
|
|
|
|
70
|
|
|
interface_a1 = get_interface_mock("s1-eth1", 1, switch_a) |
|
71
|
|
|
interface_a2 = get_interface_mock("s1-eth2", 2, switch_a) |
|
72
|
|
|
|
|
73
|
|
|
interface_b1 = get_interface_mock("s2-eth1", 1, switch_b) |
|
74
|
|
|
interface_b2 = get_interface_mock("s2-eth2", 2, switch_b) |
|
75
|
|
|
|
|
76
|
|
|
interface_c1 = get_interface_mock("s3-eth1", 1, switch_c) |
|
77
|
|
|
interface_c2 = get_interface_mock("s3-eth2", 2, switch_c) |
|
78
|
|
|
|
|
79
|
|
|
switch_a.interfaces = {interface_a1.id: interface_a1, |
|
80
|
|
|
interface_a2.id: interface_a2} |
|
81
|
|
|
switch_b.interfaces = {interface_b1.id: interface_b1, |
|
82
|
|
|
interface_b2.id: interface_b2} |
|
83
|
|
|
switch_c.interfaces = {interface_c1.id: interface_c1, |
|
84
|
|
|
interface_c2.id: interface_c2} |
|
85
|
|
|
|
|
86
|
|
|
link_1 = get_link_mock(interface_a1, interface_b1) |
|
87
|
|
|
link_2 = get_link_mock(interface_a2, interface_c1) |
|
88
|
|
|
link_3 = get_link_mock(interface_b1, interface_c2) |
|
89
|
|
|
|
|
90
|
|
|
topology = MagicMock() |
|
91
|
|
|
topology.links = {"1": link_1, "2": link_2, "3": link_3} |
|
92
|
|
|
topology.switches = {switch_a.dpid: switch_a, |
|
93
|
|
|
switch_b.dpid: switch_b, |
|
94
|
|
|
switch_c.dpid: switch_c} |
|
95
|
|
|
return topology |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
def get_test_client(controller, napp): |
|
99
|
|
|
"""Return a flask api test client.""" |
|
100
|
|
|
controller.api_server.register_napp_endpoints(napp) |
|
101
|
|
|
return controller.api_server.app.test_client() |
|
102
|
|
|
|