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

TestSearchResults3.test_path12()   B

Complexity

Conditions 7

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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