1 | """Module to help to create tests.""" |
||
2 | |||
3 | # pylint: disable=too-many-locals |
||
4 | 1 | from unittest.mock import MagicMock |
|
5 | |||
6 | 1 | from kytos.core.interface import Interface |
|
7 | 1 | from kytos.core.link import Link |
|
8 | 1 | from kytos.core.switch import Switch |
|
9 | 1 | from kytos.core.common import EntityStatus |
|
10 | 1 | from kytos.lib.helpers import (get_interface_mock, get_link_mock, |
|
11 | get_switch_mock) |
||
12 | |||
13 | |||
14 | 1 | def get_topology_mock(): |
|
15 | """Create a default topology.""" |
||
16 | 1 | switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
17 | 1 | switch_a.status = EntityStatus.UP |
|
18 | 1 | switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) |
|
19 | 1 | switch_b.status = EntityStatus.UP |
|
20 | 1 | switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01) |
|
21 | 1 | switch_c.status = EntityStatus.UP |
|
22 | |||
23 | 1 | interface_a1 = get_interface_mock("s1-eth1", 1, switch_a) |
|
24 | 1 | interface_a1.status = EntityStatus.UP |
|
25 | 1 | interface_a2 = get_interface_mock("s1-eth2", 2, switch_a) |
|
26 | 1 | interface_a2.status = EntityStatus.UP |
|
27 | |||
28 | 1 | interface_b1 = get_interface_mock("s2-eth1", 1, switch_b) |
|
29 | 1 | interface_b1.status = EntityStatus.UP |
|
30 | 1 | interface_b2 = get_interface_mock("s2-eth2", 2, switch_b) |
|
31 | 1 | interface_b2.status = EntityStatus.UP |
|
32 | |||
33 | 1 | interface_c1 = get_interface_mock("s3-eth1", 1, switch_c) |
|
34 | 1 | interface_c1.status = EntityStatus.UP |
|
35 | 1 | interface_c2 = get_interface_mock("s3-eth2", 2, switch_c) |
|
36 | 1 | interface_c2.status = EntityStatus.UP |
|
37 | |||
38 | 1 | switch_a.interfaces = { |
|
39 | interface_a1.id: interface_a1, |
||
40 | interface_a2.id: interface_a2, |
||
41 | } |
||
42 | 1 | switch_b.interfaces = { |
|
43 | interface_b1.id: interface_b1, |
||
44 | interface_b2.id: interface_b2, |
||
45 | } |
||
46 | 1 | switch_c.interfaces = { |
|
47 | interface_c1.id: interface_c1, |
||
48 | interface_c2.id: interface_c2, |
||
49 | } |
||
50 | |||
51 | 1 | link_1 = get_link_mock(interface_a1, interface_b1) |
|
52 | 1 | link_1.status = EntityStatus.UP |
|
53 | 1 | link_2 = get_link_mock(interface_a2, interface_c1) |
|
54 | 1 | link_2.status = EntityStatus.UP |
|
55 | 1 | link_3 = get_link_mock(interface_b2, interface_c2) |
|
56 | 1 | link_3.status = EntityStatus.UP |
|
57 | |||
58 | 1 | topology = MagicMock() |
|
59 | 1 | topology.links = {"1": link_1, "2": link_2, "3": link_3} |
|
60 | 1 | topology.switches = { |
|
61 | switch_a.dpid: switch_a, |
||
62 | switch_b.dpid: switch_b, |
||
63 | switch_c.dpid: switch_c, |
||
64 | } |
||
65 | 1 | return topology |
|
66 | |||
67 | |||
68 | 1 | def topology_setting(): |
|
69 | """Set the default values associated to a real topology.""" |
||
70 | 1 | switches_to_interface_counts = { |
|
71 | "S1": 2, |
||
72 | "S2": 2, |
||
73 | "S3": 6, |
||
74 | "S4": 2, |
||
75 | "S5": 6, |
||
76 | "S6": 5, |
||
77 | "S7": 2, |
||
78 | "S8": 8, |
||
79 | "S9": 4, |
||
80 | "S10": 3, |
||
81 | "S11": 3, |
||
82 | "User1": 4, |
||
83 | "User2": 2, |
||
84 | "User3": 2, |
||
85 | "User4": 3, |
||
86 | } |
||
87 | |||
88 | 1 | links_to_interfaces = [ |
|
89 | ["S1:1", "S2:1"], |
||
90 | ["S1:2", "User1:1"], |
||
91 | ["S2:2", "User4:1"], |
||
92 | ["S3:1", "S5:1"], |
||
93 | ["S3:2", "S7:1"], |
||
94 | ["S3:3", "S8:1"], |
||
95 | ["S3:4", "S11:1"], |
||
96 | ["S3:5", "User3:1"], |
||
97 | ["S3:6", "User4:2"], |
||
98 | ["S4:1", "S5:2"], |
||
99 | ["S4:2", "User1:2"], |
||
100 | ["S5:3", "S6:1"], |
||
101 | ["S5:4", "S6:2"], |
||
102 | ["S5:5", "S8:2"], |
||
103 | ["S5:6", "User1:3"], |
||
104 | ["S6:3", "S9:1"], |
||
105 | ["S6:4", "S9:2"], |
||
106 | ["S6:5", "S10:1"], |
||
107 | ["S7:2", "S8:3"], |
||
108 | ["S8:4", "S9:3"], |
||
109 | ["S8:5", "S9:4"], |
||
110 | ["S8:6", "S10:2"], |
||
111 | ["S8:7", "S11:2"], |
||
112 | ["S8:8", "User3:2"], |
||
113 | ["S10:3", "User2:1"], |
||
114 | ["S11:3", "User2:2"], |
||
115 | ["User1:4", "User4:3"], |
||
116 | ] |
||
117 | |||
118 | 1 | links_to_metadata = [ |
|
119 | {"reliability": 5, "bandwidth": 100, "delay": 105}, |
||
120 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
121 | {"reliability": 5, "bandwidth": 100, "delay": 10}, |
||
122 | {"reliability": 5, "bandwidth": 10, "delay": 112}, |
||
123 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
124 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
125 | {"reliability": 3, "bandwidth": 100, "delay": 6}, |
||
126 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
127 | {"reliability": 5, "bandwidth": 100, "delay": 10}, |
||
128 | { |
||
129 | "reliability": 1, |
||
130 | "bandwidth": 100, |
||
131 | "delay": 30, |
||
132 | "ownership": {"A": {}}, |
||
133 | }, |
||
134 | { |
||
135 | "reliability": 3, |
||
136 | "bandwidth": 100, |
||
137 | "delay": 110, |
||
138 | "ownership": {"A": {}}, |
||
139 | }, |
||
140 | {"reliability": 1, "bandwidth": 100, "delay": 40}, |
||
141 | { |
||
142 | "reliability": 3, |
||
143 | "bandwidth": 100, |
||
144 | "delay": 40, |
||
145 | "ownership": {"A": {}}, |
||
146 | }, |
||
147 | {"reliability": 5, "bandwidth": 100, "delay": 112}, |
||
148 | {"reliability": 3, "bandwidth": 100, "delay": 60}, |
||
149 | {"reliability": 3, "bandwidth": 100, "delay": 60}, |
||
150 | {"reliability": 5, "bandwidth": 100, "delay": 62}, |
||
151 | {"bandwidth": 100, "delay": 108, "ownership": "A"}, |
||
152 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
153 | {"reliability": 3, "bandwidth": 100, "delay": 32}, |
||
154 | {"reliability": 3, "bandwidth": 100, "delay": 110}, |
||
155 | {"reliability": 5, "bandwidth": 100, "ownership": {"A": {}}}, |
||
156 | {"reliability": 3, "bandwidth": 100, "delay": 7}, |
||
157 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
158 | { |
||
159 | "reliability": 3, |
||
160 | "bandwidth": 100, |
||
161 | "delay": 10, |
||
162 | "ownership": {"A": {}}, |
||
163 | }, |
||
164 | {"reliability": 3, "bandwidth": 100, "delay": 6}, |
||
165 | {"reliability": 5, "bandwidth": 10, "delay": 105}, |
||
166 | ] |
||
167 | |||
168 | 1 | return links_to_interfaces, links_to_metadata, switches_to_interface_counts |
|
169 | |||
170 | |||
171 | 1 | def get_topology_with_metadata_mock(): |
|
172 | """Create a topology with metadata.""" |
||
173 | 1 | switches = {} |
|
174 | 1 | interfaces = {} |
|
175 | 1 | links = {} |
|
176 | 1 | i = 0 |
|
177 | |||
178 | 1 | ( |
|
179 | links_to_interfaces, |
||
180 | links_to_metadata, |
||
181 | switches_to_interface_counts, |
||
182 | ) = topology_setting() |
||
183 | |||
184 | 1 | for switch in switches_to_interface_counts: |
|
185 | 1 | switches[switch] = get_switch_mock(switch) |
|
186 | |||
187 | 1 | for key, value in switches_to_interface_counts.items(): |
|
188 | 1 | switches[key].interfaces = {} |
|
189 | 1 | for interface in _get_interfaces(value, switches[key]): |
|
190 | 1 | switches[key].interfaces[interface.id] = interface |
|
191 | 1 | interfaces[interface.id] = interface |
|
192 | |||
193 | 1 | for interfaces_str in links_to_interfaces: |
|
194 | 1 | interface_a = interfaces[interfaces_str[0]] |
|
195 | 1 | interface_b = interfaces[interfaces_str[1]] |
|
196 | 1 | links[str(i)] = get_link_mock(interface_a, interface_b) |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
197 | 1 | links[str(i)].metadata = links_to_metadata[i] |
|
198 | 1 | i += 1 |
|
199 | |||
200 | 1 | topology = MagicMock() |
|
201 | 1 | topology.links = links |
|
202 | 1 | topology.switches = switches |
|
203 | 1 | return topology |
|
204 | |||
205 | |||
206 | 1 | def get_topology_with_metadata(): |
|
207 | """Create a topology with metadata.""" |
||
208 | 1 | switches = {} |
|
209 | 1 | interfaces = {} |
|
210 | 1 | links = {} |
|
211 | |||
212 | 1 | ( |
|
213 | links_to_interfaces, |
||
214 | links_to_metadata, |
||
215 | switches_to_interface_counts, |
||
216 | ) = topology_setting() |
||
217 | |||
218 | 1 | for switch in switches_to_interface_counts: |
|
219 | 1 | switches[switch] = Switch(switch) |
|
220 | |||
221 | 1 | for key, value in switches_to_interface_counts.items(): |
|
222 | 1 | switches[key].interfaces = {} |
|
223 | 1 | for i in range(1, value + 1): |
|
224 | 1 | str1 = f"{switches[key].dpid}:{i}" |
|
225 | 1 | interface = Interface(str1, i, switches[key]) |
|
226 | 1 | switches[key].update_interface(interface) |
|
227 | 1 | interfaces[interface.id] = interface |
|
228 | |||
229 | 1 | i = 0 |
|
230 | 1 | for interfaces_str in links_to_interfaces: |
|
231 | 1 | interface_a = interfaces[interfaces_str[0]] |
|
232 | 1 | interface_b = interfaces[interfaces_str[1]] |
|
233 | 1 | links[str(i)] = Link(interface_a, interface_b) |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
234 | 1 | links[str(i)].metadata = links_to_metadata[i] |
|
235 | 1 | i += 1 |
|
236 | |||
237 | 1 | topology = MagicMock() |
|
238 | 1 | topology.links = links |
|
239 | 1 | topology.switches = switches |
|
240 | 1 | return topology |
|
241 | |||
242 | |||
243 | 1 | def _get_interfaces(count, switch): |
|
244 | """Add a new interface to the list of interfaces.""" |
||
245 | 1 | for i in range(1, count + 1): |
|
246 | 1 | yield get_interface_mock("", i, switch) |
|
247 | |||
248 | |||
249 | 1 | def get_filter_links_fake(links, metadata=True, **metrics): |
|
250 | """Get test links with optional metadata.""" |
||
251 | # pylint: disable=unused-argument |
||
252 | 1 | filtered_links = ["a", "b", "c"] |
|
253 | 1 | filtered_links_without_metadata = filtered_links[0:2] |
|
254 | 1 | if not metadata: |
|
255 | return filtered_links_without_metadata |
||
256 | 1 | return filtered_links |
|
257 | |||
258 | |||
259 | # pylint: enable=unused-argument |
||
260 |