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

TestSearchResults2.test_path7()   B

Complexity

Conditions 4

Size

Total Lines 63
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 49
nop 1
dl 0
loc 63
rs 8.669
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 test the KytosGraph in graph.py."""
2
from itertools import combinations
3
from unittest import TestCase
4
from unittest.mock import Mock
5
6
import networkx as nx
7
from kytos.core.interface import Interface
8
from kytos.core.link import Link
9
# Core modules to import
10
from kytos.core.switch import Switch
11
12
# module under test
13
from graph import KytosGraph
14
from tests.unit.test_search_results import TestSearchResults
15
16
17
class TestSearchResults2(TestSearchResults):
18
    '''Tests for the graph class.'''
19
20
    def test_path1(self):
21
        """Tests paths between all users using unconstrained path alogrithm."""
22
        self.setup()
23
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
24
        for point_a, point_b in combos:
25
            results = self.get_path(point_a, point_b)
26
            self.assertNotEqual(results, [])
27
28
    def test_path2(self):
29
        """Tests paths between all users using constrained path algorithm,
30
        with no constraints set."""
31
        self.setup()
32
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
33
        for point_a, point_b in combos:
34
            results = self.get_path_constrained(point_a, point_b)
35
            self.assertNotEqual(results, [])
36
37
    def test_path3(self):
38
        """Tests paths between all users using constrained path algorithm,
39
        with the ownership constraint set to B."""
40
        self.setup()
41
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
42
        for point_a, point_b in combos:
43
            results = self.get_path_constrained(
44
                point_a, point_b, 0, ownership="B")
45
            for result in results:
46
                for path in result["paths"]:
47
                    self.assertNotIn("S4:1", path)
48
                    self.assertNotIn("S5:2", path)
49
                    self.assertNotIn("S4:2", path)
50
                    self.assertNotIn("User1:2", path)
51
                    self.assertNotIn("S5:4", path)
52
                    self.assertNotIn("S6:2", path)
53
                    self.assertNotIn("S6:5", path)
54
                    self.assertNotIn("S10:1", path)
55
                    self.assertNotIn("S8:6", path)
56
                    self.assertNotIn("S10:2", path)
57
                    self.assertNotIn("S10:3", path)
58
                    self.assertNotIn("User2:1", path)
59
60
    def test_path4(self):
61
        """Tests paths between all users using constrained path algorithm,
62
        with the reliability constraint set to 3."""
63
        self.setup()
64
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
65
        for point_a, point_b in combos:
66
            results = self.get_path_constrained(
67
                point_a, point_b, 0, reliability=3)
68
            for result in results:
69
                for path in result["paths"]:
70
                    self.assertNotIn("S4:1", path)
71
                    self.assertNotIn("S5:2", path)
72
                    self.assertNotIn("S5:3", path)
73
                    self.assertNotIn("S6:1", path)
74
75
    def test_path5(self):
76
        """Tests paths between all users using constrained path algorithm,
77
        with the bandwidth contraint set to 100."""
78
        self.setup()
79
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
80
        for point_a, point_b in combos:
81
            results = self.get_path_constrained(
82
                point_a, point_b, 0, bandwidth=100)
83
            for result in results:
84
                for path in result["paths"]:
85
                    self.assertNotIn("S3:1", path)
86
                    self.assertNotIn("S5:1", path)
87
                    self.assertNotIn("User1:4", path)
88
                    self.assertNotIn("User4:3", path)
89
90
    def test_path6(self):
91
        """Tests paths between all users using constrained path algorithm,
92
        with the delay constraint set to 50."""
93
        self.setup()
94
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
95
        for point_a, point_b in combos:
96
            results = self.get_path_constrained(point_a, point_b, 0, delay=50)
97
            for result in results:
98
                for path in result["paths"]:
99
                    self.assertNotIn("S1:1", path)
100
                    self.assertNotIn("S2:1", path)
101
                    self.assertNotIn("S3:1", path)
102
                    self.assertNotIn("S5:1", path)
103
                    self.assertNotIn("S4:2", path)
104
                    self.assertNotIn("User1:2", path)
105
                    self.assertNotIn("S5:5", path)
106
                    self.assertNotIn("S8:2", path)
107
                    self.assertNotIn("S5:6", path)
108
                    self.assertNotIn("User1:3", path)
109
                    self.assertNotIn("S6:3", path)
110
                    self.assertNotIn("S9:1", path)
111
                    self.assertNotIn("S6:4", path)
112
                    self.assertNotIn("S9:2", path)
113
                    self.assertNotIn("S6:5", path)
114
                    self.assertNotIn("S10:1", path)
115
                    self.assertNotIn("S8:5", path)
116
                    self.assertNotIn("S9:4", path)
117
                    self.assertNotIn("User1:4", path)
118
                    self.assertNotIn("User4:3", path)
119
120
    def test_path7(self):
121
        """Tests paths between all users using constrained path algorithm,
122
        with the delay constraint set to 50, the bandwidth constraint set
123
        to 100, the reliability contraint set to 3, and the ownership
124
        constraint set to 'B' """
125
        self.setup()
126
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
127
        for point_a, point_b in combos:
128
            results = self.get_path_constrained(
129
                point_a, point_b, 0, delay=50, bandwidth=100, reliability=3,
130
                ownership="B")
131
            for result in results:
132
                for path in result["paths"]:
133
                    # delay = 50 checks
134
                    self.assertNotIn("S1:1", path)
135
                    self.assertNotIn("S2:1", path)
136
                    self.assertNotIn("S3:1", path)
137
                    self.assertNotIn("S5:1", path)
138
                    self.assertNotIn("S4:2", path)
139
                    self.assertNotIn("User1:2", path)
140
                    self.assertNotIn("S5:5", path)
141
                    self.assertNotIn("S8:2", path)
142
                    self.assertNotIn("S5:6", path)
143
                    self.assertNotIn("User1:3", path)
144
                    self.assertNotIn("S6:3", path)
145
                    self.assertNotIn("S9:1", path)
146
                    self.assertNotIn("S6:4", path)
147
                    self.assertNotIn("S9:2", path)
148
                    self.assertNotIn("S6:5", path)
149
                    self.assertNotIn("S10:1", path)
150
                    self.assertNotIn("S8:5", path)
151
                    self.assertNotIn("S9:4", path)
152
                    self.assertNotIn("User1:4", path)
153
                    self.assertNotIn("User4:3", path)
154
155
                    # bandwidth = 100 checks
156
157
                    self.assertNotIn("S3:1", path)
158
                    self.assertNotIn("S5:1", path)
159
                    self.assertNotIn("User1:4", path)
160
                    self.assertNotIn("User4:3", path)
161
162
                    # reliability = 3 checks
163
164
                    self.assertNotIn("S4:1", path)
165
                    self.assertNotIn("S5:2", path)
166
                    self.assertNotIn("S5:3", path)
167
                    self.assertNotIn("S6:1", path)
168
169
                    # ownership = "B" checks
170
171
                    self.assertNotIn("S4:1", path)
172
                    self.assertNotIn("S5:2", path)
173
                    self.assertNotIn("S4:2", path)
174
                    self.assertNotIn("User1:2", path)
175
                    self.assertNotIn("S5:4", path)
176
                    self.assertNotIn("S6:2", path)
177
                    self.assertNotIn("S6:5", path)
178
                    self.assertNotIn("S10:1", path)
179
                    self.assertNotIn("S8:6", path)
180
                    self.assertNotIn("S10:2", path)
181
                    self.assertNotIn("S10:3", path)
182
                    self.assertNotIn("User2:1", path)
183
184
    def test_path8(self):
185
        """Tests paths between all users using constrained path algorithm,
186
        with the delay constraint set to 50, the bandwidth constraint
187
        set to 100, the reliability contraint set to 3, and the ownership
188
        constraint set to 'B'
189
190
        Tests conducted with flexibility enabled"""
191
        self.setup()
192
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
193
        for point_a, point_b in combos:
194
            results = self.get_path_constrained(
195
                point_a, point_b, 4, delay=50, bandwidth=100, reliability=3,
196
                ownership="B")
197
            for result in results:
198
                # delay = 50 checks
199 View Code Duplication
                if "delay" in result["metrics"]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
200
                    for path in result["paths"]:
201
                        self.assertNotIn("S1:1", path)
202
                        self.assertNotIn("S2:1", path)
203
                        self.assertNotIn("S3:1", path)
204
                        self.assertNotIn("S5:1", path)
205
                        self.assertNotIn("S4:2", path)
206
                        self.assertNotIn("User1:2", path)
207
                        self.assertNotIn("S5:5", path)
208
                        self.assertNotIn("S8:2", path)
209
                        self.assertNotIn("S5:6", path)
210
                        self.assertNotIn("User1:3", path)
211
                        self.assertNotIn("S6:3", path)
212
                        self.assertNotIn("S9:1", path)
213
                        self.assertNotIn("S6:4", path)
214
                        self.assertNotIn("S9:2", path)
215
                        self.assertNotIn("S6:5", path)
216
                        self.assertNotIn("S10:1", path)
217
                        self.assertNotIn("S8:5", path)
218
                        self.assertNotIn("S9:4", path)
219
                        self.assertNotIn("User1:4", path)
220
                        self.assertNotIn("User4:3", path)
221
222
                # bandwidth = 100 checks
223
                if "bandwidth" in result["metrics"]:
224
                    for path in result["paths"]:
225
                        self.assertNotIn("S3:1", path)
226
                        self.assertNotIn("S5:1", path)
227
                        self.assertNotIn("User1:4", path)
228
                        self.assertNotIn("User4:3", path)
229
230
                # reliability = 3 checks
231
                if "reliability" in result["metrics"]:
232
                    for path in result["paths"]:
233
                        self.assertNotIn("S4:1", path)
234
                        self.assertNotIn("S5:2", path)
235
                        self.assertNotIn("S5:3", path)
236
                        self.assertNotIn("S6:1", path)
237
238
                # ownership = "B" checks
239
                if "ownership" in result["metrics"]:
240
                    for path in result["paths"]:
241
                        self.assertNotIn("S4:1", path)
242
                        self.assertNotIn("S5:2", path)
243
                        self.assertNotIn("S4:2", path)
244
                        self.assertNotIn("User1:2", path)
245
                        self.assertNotIn("S5:4", path)
246
                        self.assertNotIn("S6:2", path)
247
                        self.assertNotIn("S6:5", path)
248
                        self.assertNotIn("S10:1", path)
249
                        self.assertNotIn("S8:6", path)
250
                        self.assertNotIn("S10:2", path)
251
                        self.assertNotIn("S10:3", path)
252
                        self.assertNotIn("User2:1", path)
253
254
    def test_path9(self):
255
        """Tests paths between all users using constrained path algorithm,
256
        with the delay constraint set to 50, the bandwidth constraint
257
        set to 100, the reliability contraint set to 3, and the ownership
258
        constraint set to 'B'
259
260
        Tests conducted with all but ownership flexible"""
261
        self.setup()
262
        combos = combinations(["User1", "User2", "User3", "User4"], 2)
263
        for point_a, point_b in combos:
264
            results = self.get_path_constrained2(point_a, point_b,
265
                                                 {"ownership": "B"}, {
266
                                                     "delay": 50,
267
                                                     "bandwidth": 100,
268
                                                     "reliability": 3})
269
            for result in results:
270
                # delay = 50 checks
271 View Code Duplication
                if "delay" in result["metrics"]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
272
                    for path in result["paths"]:
273
                        self.assertNotIn("S1:1", path)
274
                        self.assertNotIn("S2:1", path)
275
                        self.assertNotIn("S3:1", path)
276
                        self.assertNotIn("S5:1", path)
277
                        self.assertNotIn("S4:2", path)
278
                        self.assertNotIn("User1:2", path)
279
                        self.assertNotIn("S5:5", path)
280
                        self.assertNotIn("S8:2", path)
281
                        self.assertNotIn("S5:6", path)
282
                        self.assertNotIn("User1:3", path)
283
                        self.assertNotIn("S6:3", path)
284
                        self.assertNotIn("S9:1", path)
285
                        self.assertNotIn("S6:4", path)
286
                        self.assertNotIn("S9:2", path)
287
                        self.assertNotIn("S6:5", path)
288
                        self.assertNotIn("S10:1", path)
289
                        self.assertNotIn("S8:5", path)
290
                        self.assertNotIn("S9:4", path)
291
                        self.assertNotIn("User1:4", path)
292
                        self.assertNotIn("User4:3", path)
293
294
                # bandwidth = 100 checks
295
                if "bandwidth" in result["metrics"]:
296
                    for path in result["paths"]:
297
                        self.assertNotIn("S3:1", path)
298
                        self.assertNotIn("S5:1", path)
299
                        self.assertNotIn("User1:4", path)
300
                        self.assertNotIn("User4:3", path)
301
302
                # reliability = 3 checks
303
                if "reliability" in result["metrics"]:
304
                    for path in result["paths"]:
305
                        self.assertNotIn("S4:1", path)
306
                        self.assertNotIn("S5:2", path)
307
                        self.assertNotIn("S5:3", path)
308
                        self.assertNotIn("S6:1", path)
309
310
                # ownership = "B" checks
311
                self.assertIn("ownership", result["metrics"])
312
                for path in result["paths"]:
313
                    self.assertNotIn("S4:1", path)
314
                    self.assertNotIn("S5:2", path)
315
                    self.assertNotIn("S4:2", path)
316
                    self.assertNotIn("User1:2", path)
317
                    self.assertNotIn("S5:4", path)
318
                    self.assertNotIn("S6:2", path)
319
                    self.assertNotIn("S6:5", path)
320
                    self.assertNotIn("S10:1", path)
321
                    self.assertNotIn("S8:6", path)
322
                    self.assertNotIn("S10:2", path)
323
                    self.assertNotIn("S10:3", path)
324
                    self.assertNotIn("User2:1", path)
325
326
    def test_path10(self):
327
        """Tests that TypeError is generated by get_path_constrained
328
329
        Tests with ownership using an int type rather than string"""
330
        self.setup()
331
        with self.assertRaises(TypeError):
332
            self.get_path_constrained2(
333
                "User1", "User2", {"ownership": 1}, None)
334
335
    @staticmethod
336
    def generate_topology():
337
        """Generates a predetermined topology"""
338
        switches = {}
339
        interfaces = {}
340
        links = {}
341
342
        TestSearchResults.create_switch("S1", switches)
343
        TestSearchResults.add_interfaces(2, switches["S1"], interfaces)
344
345
        TestSearchResults.create_switch("S2", switches)
346
        TestSearchResults.add_interfaces(2, switches["S2"], interfaces)
347
348
        TestSearchResults.create_switch("S3", switches)
349
        TestSearchResults.add_interfaces(6, switches["S3"], interfaces)
350
351
        TestSearchResults.create_switch("S4", switches)
352
        TestSearchResults.add_interfaces(2, switches["S4"], interfaces)
353
354
        TestSearchResults.create_switch("S5", switches)
355
        TestSearchResults.add_interfaces(6, switches["S5"], interfaces)
356
357
        TestSearchResults.create_switch("S6", switches)
358
        TestSearchResults.add_interfaces(5, switches["S6"], interfaces)
359
360
        TestSearchResults.create_switch("S7", switches)
361
        TestSearchResults.add_interfaces(2, switches["S7"], interfaces)
362
363
        TestSearchResults.create_switch("S8", switches)
364
        TestSearchResults.add_interfaces(8, switches["S8"], interfaces)
365
366
        TestSearchResults.create_switch("S9", switches)
367
        TestSearchResults.add_interfaces(4, switches["S9"], interfaces)
368
369
        TestSearchResults.create_switch("S10", switches)
370
        TestSearchResults.add_interfaces(3, switches["S10"], interfaces)
371
372
        TestSearchResults.create_switch("S11", switches)
373
        TestSearchResults.add_interfaces(3, switches["S11"], interfaces)
374
375
        TestSearchResults.create_switch("User1", switches)
376
        TestSearchResults.add_interfaces(4, switches["User1"], interfaces)
377
378
        TestSearchResults.create_switch("User2", switches)
379
        TestSearchResults.add_interfaces(2, switches["User2"], interfaces)
380
381
        TestSearchResults.create_switch("User3", switches)
382
        TestSearchResults.add_interfaces(2, switches["User3"], interfaces)
383
384
        TestSearchResults.create_switch("User4", switches)
385
        TestSearchResults.add_interfaces(3, switches["User4"], interfaces)
386
387
        links["S1:1<->S2:1"] = Link(interfaces["S1:1"], interfaces["S2:1"])
388
        links["S1:1<->S2:1"].extend_metadata(
389
            {"reliability": 5, "bandwidth": 100, "delay": 105})
390
391
        links["S1:2<->User1:1"] = Link(interfaces["S1:2"],
392
                                       interfaces["User1:1"])
393
        links["S1:2<->User1:1"].extend_metadata(
394
            {"reliability": 5, "bandwidth": 100, "delay": 1})
395
396
        links["S2:2<->User4:1"] = Link(interfaces["S2:2"],
397
                                       interfaces["User4:1"])
398
        links["S2:2<->User4:1"].extend_metadata(
399
            {"reliability": 5, "bandwidth": 100, "delay": 10})
400
401
        links["S3:1<->S5:1"] = Link(interfaces["S3:1"], interfaces["S5:1"])
402
        links["S3:1<->S5:1"].extend_metadata(
403
            {"reliability": 5, "bandwidth": 10, "delay": 112})
404
405
        links["S3:2<->S7:1"] = Link(interfaces["S3:2"], interfaces["S7:1"])
406
        links["S3:2<->S7:1"].extend_metadata(
407
            {"reliability": 5, "bandwidth": 100, "delay": 1})
408
409
        links["S3:3<->S8:1"] = Link(interfaces["S3:3"], interfaces["S8:1"])
410
        links["S3:3<->S8:1"].extend_metadata(
411
            {"reliability": 5, "bandwidth": 100, "delay": 1})
412
413
        links["S3:4<->S11:1"] = Link(interfaces["S3:4"], interfaces["S11:1"])
414
        links["S3:4<->S11:1"].extend_metadata(
415
            {"reliability": 3, "bandwidth": 100, "delay": 6})
416
417
        links["S3:5<->User3:1"] = Link(interfaces["S3:5"],
418
                                       interfaces["User3:1"])
419
        links["S3:5<->User3:1"].extend_metadata(
420
            {"reliability": 5, "bandwidth": 100, "delay": 1})
421
422
        links["S3:6<->User4:2"] = Link(interfaces["S3:6"],
423
                                       interfaces["User4:2"])
424
        links["S3:6<->User4:2"].extend_metadata(
425
            {"reliability": 5, "bandwidth": 100, "delay": 10})
426
427
        links["S4:1<->S5:2"] = Link(interfaces["S4:1"], interfaces["S5:2"])
428
        links["S4:1<->S5:2"].extend_metadata(
429
            {"reliability": 1, "bandwidth": 100, "delay": 30,
430
             "ownership": "A"})
431
432
        links["S4:2<->User1:2"] = Link(interfaces["S4:2"],
433
                                       interfaces["User1:2"])
434
        links["S4:2<->User1:2"].extend_metadata(
435
            {"reliability": 3, "bandwidth": 100, "delay": 110,
436
             "ownership": "A"})
437
438
        links["S5:3<->S6:1"] = Link(interfaces["S5:3"], interfaces["S6:1"])
439
        links["S5:3<->S6:1"].extend_metadata(
440
            {"reliability": 1, "bandwidth": 100, "delay": 40})
441
442
        links["S5:4<->S6:2"] = Link(interfaces["S5:4"], interfaces["S6:2"])
443
        links["S5:4<->S6:2"].extend_metadata(
444
            {"reliability": 3, "bandwidth": 100, "delay": 40,
445
             "ownership": "A"})
446
447
        links["S5:5<->S8:2"] = Link(interfaces["S5:5"], interfaces["S8:2"])
448
        links["S5:5<->S8:2"].extend_metadata(
449
            {"reliability": 5, "bandwidth": 100, "delay": 112})
450
451
        links["S5:6<->User1:3"] = Link(interfaces["S5:6"],
452
                                       interfaces["User1:3"])
453
        links["S5:6<->User1:3"].extend_metadata(
454
            {"reliability": 3, "bandwidth": 100, "delay": 60})
455
456
        links["S6:3<->S9:1"] = Link(interfaces["S6:3"], interfaces["S9:1"])
457
        links["S6:3<->S9:1"].extend_metadata(
458
            {"reliability": 3, "bandwidth": 100, "delay": 60})
459
460
        links["S6:4<->S9:2"] = Link(interfaces["S6:4"], interfaces["S9:2"])
461
        links["S6:4<->S9:2"].extend_metadata(
462
            {"reliability": 5, "bandwidth": 100, "delay": 62})
463
464
        links["S6:5<->S10:1"] = Link(interfaces["S6:5"], interfaces["S10:1"])
465
        links["S6:5<->S10:1"].extend_metadata(
466
            {"bandwidth": 100, "delay": 108, "ownership": "A"})
467
468
        links["S7:2<->S8:3"] = Link(interfaces["S7:2"], interfaces["S8:3"])
469
        links["S7:2<->S8:3"].extend_metadata(
470
            {"reliability": 5, "bandwidth": 100, "delay": 1})
471
472
        links["S8:4<->S9:3"] = Link(interfaces["S8:4"], interfaces["S9:3"])
473
        links["S8:4<->S9:3"].extend_metadata(
474
            {"reliability": 3, "bandwidth": 100, "delay": 32})
475
476
        links["S8:5<->S9:4"] = Link(interfaces["S8:5"], interfaces["S9:4"])
477
        links["S8:5<->S9:4"].extend_metadata(
478
            {"reliability": 3, "bandwidth": 100, "delay": 110})
479
480
        links["S8:6<->S10:2"] = Link(interfaces["S8:6"], interfaces["S10:2"])
481
        links["S8:6<->S10:2"].extend_metadata(
482
            {"reliability": 5, "bandwidth": 100, "ownership": "A"})
483
484
        links["S8:7<->S11:2"] = Link(interfaces["S8:7"], interfaces["S11:2"])
485
        links["S8:7<->S11:2"].extend_metadata(
486
            {"reliability": 3, "bandwidth": 100, "delay": 7})
487
488
        links["S8:8<->User3:2"] = Link(interfaces["S8:8"],
489
                                       interfaces["User3:2"])
490
        links["S8:8<->User3:2"].extend_metadata(
491
            {"reliability": 5, "bandwidth": 100, "delay": 1})
492
493
        links["S10:3<->User2:1"] = Link(interfaces["S10:3"],
494
                                        interfaces["User2:1"])
495
        links["S10:3<->User2:1"].extend_metadata(
496
            {"reliability": 3, "bandwidth": 100, "delay": 10,
497
             "ownership": "A"})
498
499
        links["S11:3<->User2:2"] = Link(interfaces["S11:3"],
500
                                        interfaces["User2:2"])
501
        links["S11:3<->User2:2"].extend_metadata(
502
            {"reliability": 3, "bandwidth": 100, "delay": 6})
503
504
        links["User1:4<->User4:3"] = Link(interfaces["User1:4"],
505
                                          interfaces["User4:3"])
506
        links["User1:4<->User4:3"].extend_metadata(
507
            {"reliability": 5, "bandwidth": 10, "delay": 105})
508
509
        return (switches, links)
510