Test Failed
Pull Request — master (#74)
by
unknown
02:33
created

build.tests.helpers.get_topology_mock()   B

Complexity

Conditions 1

Size

Total Lines 52
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 41
nop 0
dl 0
loc 52
rs 8.896
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"""Module to help to create tests."""
2
3
# pylint: disable=too-many-locals
4
from unittest.mock import MagicMock
5
6
from kytos.core.interface import Interface
7
from kytos.core.link import Link
8
from kytos.core.switch import Switch
9
from kytos.core.common import EntityStatus
10
from kytos.lib.helpers import (get_interface_mock, get_link_mock,
11
                               get_switch_mock)
12
13
14
def get_topology_mock():
15
    """Create a default topology."""
16
    switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
17
    switch_a.status = EntityStatus.UP
18
    switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04)
19
    switch_b.status = EntityStatus.UP
20
    switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01)
21
    switch_c.status = EntityStatus.UP
22
23
    interface_a1 = get_interface_mock("s1-eth1", 1, switch_a)
24
    interface_a1.status = EntityStatus.UP
25
    interface_a2 = get_interface_mock("s1-eth2", 2, switch_a)
26
    interface_a2.status = EntityStatus.UP
27
28
    interface_b1 = get_interface_mock("s2-eth1", 1, switch_b)
29
    interface_b1.status = EntityStatus.UP
30
    interface_b2 = get_interface_mock("s2-eth2", 2, switch_b)
31
    interface_b2.status = EntityStatus.UP
32
33
    interface_c1 = get_interface_mock("s3-eth1", 1, switch_c)
34
    interface_c1.status = EntityStatus.UP
35
    interface_c2 = get_interface_mock("s3-eth2", 2, switch_c)
36
    interface_c2.status = EntityStatus.UP
37
38
    switch_a.interfaces = {
39
        interface_a1.id: interface_a1,
40
        interface_a2.id: interface_a2,
41
    }
42
    switch_b.interfaces = {
43
        interface_b1.id: interface_b1,
44
        interface_b2.id: interface_b2,
45
    }
46
    switch_c.interfaces = {
47
        interface_c1.id: interface_c1,
48
        interface_c2.id: interface_c2,
49
    }
50
51
    link_1 = get_link_mock(interface_a1, interface_b1)
52
    link_1.status = EntityStatus.UP
53
    link_2 = get_link_mock(interface_a2, interface_c1)
54
    link_2.status = EntityStatus.UP
55
    link_3 = get_link_mock(interface_b2, interface_c2)
56
    link_3.status = EntityStatus.UP
57
58
    topology = MagicMock()
59
    topology.links = {"1": link_1, "2": link_2, "3": link_3}
60
    topology.switches = {
61
        switch_a.dpid: switch_a,
62
        switch_b.dpid: switch_b,
63
        switch_c.dpid: switch_c,
64
    }
65
    return topology
66
67
68
def topology_setting():
69
    """Set the default values associated to a real topology."""
70
    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
    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
    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
    return links_to_interfaces, links_to_metadata, switches_to_interface_counts
169
170
171
def get_topology_with_metadata_mock():
172
    """Create a topology with metadata."""
173
    switches = {}
174
    interfaces = {}
175
    links = {}
176
    i = 0
177
178
    (
179
        links_to_interfaces,
180
        links_to_metadata,
181
        switches_to_interface_counts,
182
    ) = topology_setting()
183
184
    for switch in switches_to_interface_counts:
185
        switches[switch] = get_switch_mock(switch)
186
187
    for key, value in switches_to_interface_counts.items():
188
        switches[key].interfaces = {}
189
        for interface in _get_interfaces(value, switches[key]):
190
            switches[key].interfaces[interface.id] = interface
191
            interfaces[interface.id] = interface
192
193
    for interfaces_str in links_to_interfaces:
194
        interface_a = interfaces[interfaces_str[0]]
195
        interface_b = interfaces[interfaces_str[1]]
196
        links[str(i)] = get_link_mock(interface_a, interface_b)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
197
        links[str(i)].metadata = links_to_metadata[i]
198
        i += 1
199
200
    topology = MagicMock()
201
    topology.links = links
202
    topology.switches = switches
203
    return topology
204
205
206
def get_topology_with_metadata():
207
    """Create a topology with metadata."""
208
    switches = {}
209
    interfaces = {}
210
    links = {}
211
212
    (
213
        links_to_interfaces,
214
        links_to_metadata,
215
        switches_to_interface_counts,
216
    ) = topology_setting()
217
218
    for switch in switches_to_interface_counts:
219
        switches[switch] = Switch(switch)
220
221
    for key, value in switches_to_interface_counts.items():
222
        switches[key].interfaces = {}
223
        for i in range(1, value + 1):
224
            str1 = f"{switches[key].dpid}:{i}"
225
            interface = Interface(str1, i, switches[key])
226
            switches[key].update_interface(interface)
227
            interfaces[interface.id] = interface
228
229
    i = 0
230
    for interfaces_str in links_to_interfaces:
231
        interface_a = interfaces[interfaces_str[0]]
232
        interface_b = interfaces[interfaces_str[1]]
233
        links[str(i)] = Link(interface_a, interface_b)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
234
        links[str(i)].metadata = links_to_metadata[i]
235
        i += 1
236
237
    topology = MagicMock()
238
    topology.links = links
239
    topology.switches = switches
240
    return topology
241
242
243
def _get_interfaces(count, switch):
244
    """Add a new interface to the list of interfaces."""
245
    for i in range(1, count + 1):
246
        yield get_interface_mock("", i, switch)
247
248
249
def get_filter_links_fake(links, metadata=True, **metrics):
250
    """Get test links with optional metadata."""
251
    # pylint: disable=unused-argument
252
    filtered_links = ["a", "b", "c"]
253
    filtered_links_without_metadata = filtered_links[0:2]
254
    if not metadata:
255
        return filtered_links_without_metadata
256
    return filtered_links
257
258
259
# pylint: enable=unused-argument
260