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

get_topology_with_metadata_mock()   B

Complexity

Conditions 5

Size

Total Lines 77
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 69
nop 0
dl 0
loc 77
rs 7.5478
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
from unittest.mock import MagicMock
3
4
from kytos.lib.helpers import (get_interface_mock, get_link_mock,
5
                               get_switch_mock)
6
7
8
def get_topology_mock():
9
    """Create a default topology."""
10
    switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
11
    switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04)
12
    switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01)
13
14
    interface_a1 = get_interface_mock("s1-eth1", 1, switch_a)
15
    interface_a2 = get_interface_mock("s1-eth2", 2, switch_a)
16
17
    interface_b1 = get_interface_mock("s2-eth1", 1, switch_b)
18
    interface_b2 = get_interface_mock("s2-eth2", 2, switch_b)
19
20
    interface_c1 = get_interface_mock("s3-eth1", 1, switch_c)
21
    interface_c2 = get_interface_mock("s3-eth2", 2, switch_c)
22
23
    switch_a.interfaces = {interface_a1.id: interface_a1,
24
                           interface_a2.id: interface_a2}
25
    switch_b.interfaces = {interface_b1.id: interface_b1,
26
                           interface_b2.id: interface_b2}
27
    switch_c.interfaces = {interface_c1.id: interface_c1,
28
                           interface_c2.id: interface_c2}
29
30
    link_1 = get_link_mock(interface_a1, interface_b1)
31
    link_2 = get_link_mock(interface_a2, interface_c1)
32
    link_3 = get_link_mock(interface_b2, interface_c2)
33
34
    topology = MagicMock()
35
    topology.links = {"1": link_1, "2": link_2, "3": link_3}
36
    topology.switches = {switch_a.dpid: switch_a,
37
                         switch_b.dpid: switch_b,
38
                         switch_c.dpid: switch_c}
39
    return topology
40
41
42
def get_topology_with_metadata_mock():
43
    """Create a topology with metadata."""
44
    switches = {}
45
    interfaces = {}
46
    links = {}
47
    i = 0
48
49
    switches_to_interface_counts = {"S1": 2, "S2": 2, "S3": 6, "S4": 2,
50
                                    "S5": 6, "S6": 5, "S7": 2, "S8": 8,
51
                                    "S9": 4, "S10": 3, "S11": 3,
52
                                    "User1": 4, "User2": 2,
53
                                    "User3": 2, "User4": 3}
54
55
    links_to_interfaces = [["S1:1", "S2:1"], ["S1:2", "User1:1"],
56
                           ["S2:2", "User4:1"], ["S3:1", "S5:1"],
57
                           ["S3:2", "S7:1"], ["S3:3", "S8:1"],
58
                           ["S3:4", "S11:1"],
59
                           ["S3:5", "User3:1"], ["S3:6", "User4:2"],
60
                           ["S4:1", "S5:2"], ["S4:2", "User1:2"],
61
                           ["S5:3", "S6:1"],
62
                           ["S5:4", "S6:2"], ["S5:5", "S8:2"],
63
                           ["S5:6", "User1:3"], ["S6:3", "S9:1"],
64
                           ["S6:4", "S9:2"], ["S6:5", "S10:1"],
65
                           ["S7:2", "S8:3"],
66
                           ["S8:4", "S9:3"], ["S8:5", "S9:4"],
67
                           ["S8:6", "S10:2"],
68
                           ["S8:7", "S11:2"], ["S8:8", "User3:2"],
69
                           ["S10:3", "User2:1"], ["S11:3", "User2:2"],
70
                           ["User1:4", "User4:3"]]
71
72
    links_to_metadata = [
73
        {"reliability": 5, "bandwidth": 100, "delay": 105},
74
        {"reliability": 5, "bandwidth": 100, "delay": 1},
75
        {"reliability": 5, "bandwidth": 100, "delay": 10},
76
        {"reliability": 5, "bandwidth": 10, "delay": 112},
77
        {"reliability": 5, "bandwidth": 100, "delay": 1},
78
        {"reliability": 5, "bandwidth": 100, "delay": 1},
79
        {"reliability": 3, "bandwidth": 100, "delay": 6},
80
        {"reliability": 5, "bandwidth": 100, "delay": 1},
81
        {"reliability": 5, "bandwidth": 100, "delay": 10},
82
        {"reliability": 1, "bandwidth": 100, "delay": 30, "ownership": "A"},
83
        {"reliability": 3, "bandwidth": 100, "delay": 110, "ownership": "A"},
84
        {"reliability": 1, "bandwidth": 100, "delay": 40},
85
        {"reliability": 3, "bandwidth": 100, "delay": 40, "ownership": "A"},
86
        {"reliability": 5, "bandwidth": 100, "delay": 112},
87
        {"reliability": 3, "bandwidth": 100, "delay": 60},
88
        {"reliability": 3, "bandwidth": 100, "delay": 60},
89
        {"reliability": 5, "bandwidth": 100, "delay": 62},
90
        {"bandwidth": 100, "delay": 108, "ownership": "A"},
91
        {"reliability": 5, "bandwidth": 100, "delay": 1},
92
        {"reliability": 3, "bandwidth": 100, "delay": 32},
93
        {"reliability": 3, "bandwidth": 100, "delay": 110},
94
        {"reliability": 5, "bandwidth": 100, "ownership": "A"},
95
        {"reliability": 3, "bandwidth": 100, "delay": 7},
96
        {"reliability": 5, "bandwidth": 100, "delay": 1},
97
        {"reliability": 3, "bandwidth": 100, "delay": 10, "ownership": "A"},
98
        {"reliability": 3, "bandwidth": 100, "delay": 6},
99
        {"reliability": 5, "bandwidth": 10, "delay": 105}]
100
101
    for switch in switches_to_interface_counts.keys():
102
        switches[switch] = get_switch_mock(switch)
103
104
    for key, value in switches_to_interface_counts.items():
105
        switches[key].interfaces = {}
106
        for interface in _get_interfaces(value, switches[key]):
107
            switches[key].interfaces[interface.id] = interface
108
            interfaces[interface.id] = interface
109
110
    for interfaces in links_to_interfaces:
111
        links[str(i)] = get_link_mock(interfaces[0], interfaces[1])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
112
        links[str(i)].metadata = links_to_metadata[i]
113
        i += 1
114
115
    topology = MagicMock()
116
    topology.links = links
117
    topology.switches = switches
118
    return topology
119
120
121
def _get_interfaces(count, switch):
122
    '''Add a new interface to the list of interfaces'''
123
    for i in range(1, count + 1):
124
        yield get_interface_mock("", i, switch)
125
126
127
test = get_topology_with_metadata_mock()
128
129
print(list((i.endpoint_a, i.endpoint_b, i.metadata)
130
           for i in test.links.values()))
131
132
print("---------------------------------")
133
134
print(list((j.dpid, list(k.id for k in j.interfaces.values()))
135
           for j in test.switches.values()))
136