1
|
|
|
"""Module to help to create tests.""" |
2
|
|
|
from unittest.mock import Mock |
3
|
|
|
|
4
|
|
|
from kytos.core import Controller |
5
|
|
|
from kytos.core.common import EntityStatus |
6
|
|
|
from kytos.core.config import KytosConfig |
7
|
|
|
from kytos.core.interface import TAG, UNI, Interface |
8
|
|
|
from kytos.core.link import Link |
9
|
|
|
from kytos.core.switch import Switch |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def get_controller_mock(): |
13
|
|
|
"""Return a controller mock.""" |
14
|
|
|
options = KytosConfig().options["daemon"] |
15
|
|
|
controller = Controller(options) |
16
|
|
|
controller.log = Mock() |
17
|
|
|
return controller |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def get_link_mocked(**kwargs): |
21
|
|
|
"""Return a link mocked. |
22
|
|
|
|
23
|
|
|
Args: |
24
|
|
|
link_dict: Python dict returned after call link.as_dict() |
25
|
|
|
""" |
26
|
|
|
switch_a = kwargs.get("switch_a", Switch("00:00:00:00:00:01")) |
27
|
|
|
switch_b = kwargs.get("switch_b", Switch("00:00:00:00:00:02")) |
28
|
|
|
|
29
|
|
|
endpoint_a = Interface( |
30
|
|
|
kwargs.get("endpoint_a_name", "eth0"), |
31
|
|
|
kwargs.get("endpoint_a_port", 1), |
32
|
|
|
switch_a, |
33
|
|
|
) |
34
|
|
|
endpoint_b = Interface( |
35
|
|
|
kwargs.get("endpoint_b_name", "eth1"), |
36
|
|
|
kwargs.get("endpoint_b_port", 2), |
37
|
|
|
switch_b, |
38
|
|
|
) |
39
|
|
|
link = Mock(spec=Link, endpoint_a=endpoint_a, endpoint_b=endpoint_b) |
40
|
|
|
link.endpoint_a.link = link |
41
|
|
|
link.endpoint_b.link = link |
42
|
|
|
link.as_dict.return_value = kwargs.get( |
43
|
|
|
"link_dict", {"id": kwargs.get("link_id", 1)} |
44
|
|
|
) |
45
|
|
|
|
46
|
|
|
link.status = kwargs.get("status", EntityStatus.DOWN) |
47
|
|
|
|
48
|
|
|
metadata = kwargs.get("metadata", {}) |
49
|
|
|
|
50
|
|
|
def side_effect(key): |
51
|
|
|
"""Mock Link get metadata.""" |
52
|
|
|
return Mock(value=metadata.get(key)) |
53
|
|
|
|
54
|
|
|
link.get_metadata = Mock(side_effect=side_effect) |
55
|
|
|
|
56
|
|
|
return link |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
def get_mocked_requests(_): |
60
|
|
|
"""Mock requests.get.""" |
61
|
|
|
return MockResponse( |
62
|
|
|
{ |
63
|
|
|
"links": { |
64
|
|
|
"abc": {"active": False, "enabled": True}, |
65
|
|
|
"def": {"active": True, "enabled": True}, |
66
|
|
|
} |
67
|
|
|
}, |
68
|
|
|
200, |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def get_uni_mocked(**kwargs): |
73
|
|
|
"""Create an uni mocked. |
74
|
|
|
|
75
|
|
|
Args: |
76
|
|
|
interface_name(str): Interface name. Defaults to "eth1". |
77
|
|
|
interface_port(int): Interface pror. Defaults to 1. |
78
|
|
|
tag_type(int): Type of a tag. Defaults to 1. |
79
|
|
|
tag_value(int): Value of a tag. Defaults to 81 |
80
|
|
|
is_valid(bool): Value returned by is_valid method. |
81
|
|
|
Defaults to False. |
82
|
|
|
""" |
83
|
|
|
interface_name = kwargs.get("interface_name", "eth1") |
84
|
|
|
interface_port = kwargs.get("interface_port", 1) |
85
|
|
|
tag_type = kwargs.get("tag_type", 1) |
86
|
|
|
tag_value = kwargs.get("tag_value", 81) |
87
|
|
|
is_valid = kwargs.get("is_valid", False) |
88
|
|
|
switch = Mock(spec=Switch) |
89
|
|
|
switch.id = kwargs.get("switch_id", "custom_switch_id") |
90
|
|
|
switch.dpid = kwargs.get("switch_dpid", "custom_switch_dpid") |
91
|
|
|
interface = Interface(interface_name, interface_port, switch) |
92
|
|
|
tag = TAG(tag_type, tag_value) |
93
|
|
|
uni = Mock(spec=UNI, interface=interface, user_tag=tag) |
94
|
|
|
uni.is_valid.return_value = is_valid |
95
|
|
|
uni.as_dict.return_value = { |
96
|
|
|
"interface_id": f"switch_mock:{interface_port}", |
97
|
|
|
"tag": tag.as_dict(), |
98
|
|
|
} |
99
|
|
|
return uni |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
class MockResponse: |
103
|
|
|
""" |
104
|
|
|
Mock a requests response object. |
105
|
|
|
|
106
|
|
|
Just define a function and add the patch decorator to the test. |
107
|
|
|
Example: |
108
|
|
|
def mocked_requests_get(*args, **kwargs): |
109
|
|
|
return MockResponse({}, 200) |
110
|
|
|
@patch('requests.get', side_effect=mocked_requests_get) |
111
|
|
|
|
112
|
|
|
""" |
113
|
|
|
|
114
|
|
|
def __init__(self, json_data, status_code): |
115
|
|
|
"""Create mock response object with parameters. |
116
|
|
|
|
117
|
|
|
Args: |
118
|
|
|
json_data: JSON response content |
119
|
|
|
status_code: HTTP status code. |
120
|
|
|
""" |
121
|
|
|
self.json_data = json_data |
122
|
|
|
self.status_code = status_code |
123
|
|
|
|
124
|
|
|
def json(self): |
125
|
|
|
"""Return the response json data.""" |
126
|
|
|
return self.json_data |
127
|
|
|
|
128
|
|
|
def __str__(self): |
129
|
|
|
return self.__class__.__name__ |
130
|
|
|
|