Completed
Pull Request — master (#58)
by
unknown
02:36
created

build.tests.unit.test_search_results3   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 32
eloc 145
dl 0
loc 204
rs 9.84
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B TestSearchResults3.test_path11() 0 28 7
A TestSearchResults3.test_path9() 0 11 1
B TestSearchResults3.test_path12() 0 28 7
C TestSearchResults3.test_path13() 0 38 10
B TestSearchResults3.test_path10() 0 24 6
B TestSearchResults3.generate_topology() 0 60 1
1
"""Module to test the KytosGraph in graph.py."""
2
3
# module under test
4
from tests.unit.test_search_results import TestSearchResults
5
6
7
class TestSearchResults3(TestSearchResults):
8
    """Tests for the graph class."""
9
10
    def test_path9(self):
11
        """Tests to see if an illegal path is not in the set of paths that
12
        use only edges owned by A."""
13
        # Arrange
14
        self.test_setup()
15
        illegal_path = ['User1', 'User1:1', 'S2:1',
16
                        'S2', 'S2:2', 'User2:1', 'User2']
17
        # Act
18
        result = self.get_path_constrained("User1", "User2", 0, ownership='A')
19
        # Assert
20
        self.assertNotIn(illegal_path, result[0]["paths"])
21
22
    def test_path10(self):
23
        """Tests to see if the edges used in the paths of the result set
24
        do not have poor reliability"""
25
        # Arrange
26
        self.test_setup()
27
        reliabilities = []
28
        poor_reliability = 1
29
        key = "reliability"
30
31
        # Act
32
        result = self.get_path_constrained("User1", "User2", 0, reliability=3)
33
34
        if result:
35
            for path in result[0]["paths"]:
36
                for i in range(1, len(path)):
37
                    endpoint_a = path[i - 1]
38
                    endpoint_b = path[i]
39
                    meta_data = self.graph.get_metadata_from_link(
40
                        endpoint_a, endpoint_b)
41
                    if meta_data and key in meta_data.keys():
42
                        reliabilities.append(meta_data[key])
43
44
        # Assert
45
        self.assertNotIn(poor_reliability, reliabilities)
46
47
    def test_path11(self):
48
        """Tests to see if the edges used in the paths from User 1 to User 2
49
        have less than 30 delay."""
50
        # Arrange
51
        self.test_setup()
52
        delays = []
53
        delay_cap = 29
54
        key = "delay"
55
        has_bad_delay = False
56
57
        # Act
58
        result = self.get_path_constrained(
59
            "User1", "User2", 0, delay=delay_cap)
60
61
        if result:
62
            for path in result[0]["paths"]:
63
                for i in range(1, len(path)):
64
                    endpoint_a = path[i - 1]
65
                    endpoint_b = path[i]
66
                    meta_data = self.graph.get_metadata_from_link(
67
                        endpoint_a, endpoint_b)
68
                    if meta_data and key in meta_data.keys():
69
                        delays.append(meta_data[key])
70
71
        # Assert
72
        for delay in delays:
73
            has_bad_delay = delay > delay_cap
74
            self.assertEqual(has_bad_delay, False)
75
76
    def test_path12(self):
77
        """Tests to see if the edges used in the paths from User 1 to User 2
78
        have at least 20 bandwidth."""
79
        # Arrange
80
        self.test_setup()
81
        bandwidths = []
82
        bandwidth_floor = 20
83
        key = "bandwidth"
84
        has_bad_bandwidth = False
85
86
        # Act
87
        result = self.get_path_constrained(
88
            "User1", "User2", 0, bandwidth=bandwidth_floor)
89
90
        if result:
91
            for path in result[0]["paths"]:
92
                for i in range(1, len(path)):
93
                    endpoint_a = path[i - 1]
94
                    endpoint_b = path[i]
95
                    meta_data = self.graph.get_metadata_from_link(
96
                        endpoint_a, endpoint_b)
97
                    if meta_data and key in meta_data.keys():
98
                        bandwidths.append(meta_data[key])
99
100
        # Assert
101
        for bandwidth in bandwidths:
102
            has_bad_bandwidth = bandwidth < bandwidth_floor
103
            self.assertEqual(has_bad_bandwidth, False)
104
105
    def test_path13(self):
106
        """Tests to see if the edges used in the paths from User 1 to User 2
107
        have at least 20 bandwidth and under 30 delay."""
108
        # Arrange
109
        self.test_setup()
110
        bandwidths = []
111
        delays = []
112
        bandwidth_floor = 20
113
        key_a = "bandwidth"
114
        delay_cap = 29
115
        key_b = "delay"
116
        has_bad_bandwidth = False
117
        has_bad_delay = False
118
119
        # Act
120
        result = self.get_path_constrained(
121
            "User1", "User2", 0, bandwidth=bandwidth_floor, delay=delay_cap)
122
123
        if result:
124
            for path in result[0]["paths"]:
125
                for i in range(1, len(path)):
126
                    endpoint_a = path[i - 1]
127
                    endpoint_b = path[i]
128
                    meta_data = self.graph.get_metadata_from_link(
129
                        endpoint_a, endpoint_b)
130
                    if meta_data and key_a in meta_data.keys():
131
                        bandwidths.append(meta_data[key_a])
132
                    elif meta_data and key_b in meta_data.keys():
133
                        delays.append(meta_data[key_b])
134
135
        # Assert
136
        for bandwidth in bandwidths:
137
            has_bad_bandwidth = bandwidth < bandwidth_floor
138
            self.assertEqual(has_bad_bandwidth, False)
139
140
        for delay in delays:
141
            has_bad_delay = delay > delay_cap
142
            self.assertEqual(has_bad_delay, False)
143
144
    @staticmethod
145
    def generate_topology():
146
        """Generates a predetermined topology"""
147
        switches = {}
148
        interfaces = {}
149
        links = {}
150
151
        TestSearchResults.create_switch("User1", switches)
152
        TestSearchResults.add_interfaces(3, switches["User1"], interfaces)
153
154
        TestSearchResults.create_switch("S2", switches)
155
        TestSearchResults.add_interfaces(2, switches["S2"], interfaces)
156
157
        TestSearchResults.create_switch("User2", switches)
158
        TestSearchResults.add_interfaces(3, switches["User2"], interfaces)
159
160
        TestSearchResults.create_switch("S4", switches)
161
        TestSearchResults.add_interfaces(4, switches["S4"], interfaces)
162
163
        TestSearchResults.create_switch("S5", switches)
164
        TestSearchResults.add_interfaces(2, switches["S5"], interfaces)
165
166
        TestSearchResults.create_link("User1:1", "S2:1", interfaces, links)
167
        TestSearchResults.create_link("User1:2", "S5:1", interfaces, links)
168
        TestSearchResults.create_link("User1:3", "S4:1", interfaces, links)
169
        TestSearchResults.create_link("S2:2", "User2:1", interfaces, links)
170
        TestSearchResults.create_link("User2:2", "S4:2", interfaces, links)
171
        TestSearchResults.create_link("S5:2", "S4:3", interfaces, links)
172
        TestSearchResults.create_link("User2:3", "S4:4", interfaces, links)
173
174
        TestSearchResults.add_metadata_to_link(
175
            "User1:1", "S2:1", {
176
                "reliability": 3, "ownership": "B", "delay": 30,
177
                "bandwidth": 20}, links)
178
        TestSearchResults.add_metadata_to_link(
179
            "User1:2", "S5:1", {
180
                "reliability": 1, "ownership": "A", "delay": 5,
181
                "bandwidth": 50}, links)
182
        TestSearchResults.add_metadata_to_link(
183
            "User1:3", "S4:1", {
184
                "reliability": 3, "ownership": "A", "delay": 60,
185
                "bandwidth": 10}, links)
186
        TestSearchResults.add_metadata_to_link(
187
            "S2:2", "User2:1", {
188
                "reliability": 3, "ownership": "B", "delay": 30,
189
                "bandwidth": 20}, links)
190
        TestSearchResults.add_metadata_to_link(
191
            "User2:2", "S4:2", {
192
                "reliability": 3, "ownership": "B", "delay": 30,
193
                "bandwidth": 10}, links)
194
        TestSearchResults.add_metadata_to_link(
195
            "S5:2", "S4:3", {
196
                "reliability": 1, "ownership": "A", "delay": 10,
197
                "bandwidth": 50}, links)
198
        TestSearchResults.add_metadata_to_link(
199
            "User2:3", "S4:4", {
200
                "reliability": 3, "ownership": "A", "delay": 29,
201
                "bandwidth": 20}, links)
202
203
        return (switches, links)
204