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

TestSearchResults2._add_metadata_to_links()   B

Complexity

Conditions 1

Size

Total Lines 60
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

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