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
|
|
|
|
7
|
|
|
# module under test |
8
|
|
|
from graph import KytosGraph |
9
|
|
|
|
10
|
|
|
from tests.unit.test_search_results import TestSearchResults |
11
|
|
|
|
12
|
|
|
# Core modules to import |
13
|
|
|
from kytos.core.switch import Switch |
14
|
|
|
from kytos.core.interface import Interface |
15
|
|
|
from kytos.core.link import Link |
16
|
|
|
|
17
|
|
|
class TestSearchResults1(TestSearchResults): |
18
|
|
|
|
19
|
|
|
def test_path1(self): |
20
|
|
|
"""Tests a simple, definetly possible path""" |
21
|
|
|
self.setup() |
22
|
|
|
results = self.get_path("S1","S2") |
23
|
|
|
self.assertNotEqual(results, []) |
24
|
|
|
|
25
|
|
|
def test_constrained_path1(self): |
26
|
|
|
"""Tests a simple, definetly possible path""" |
27
|
|
|
self.setup() |
28
|
|
|
results = self.get_path_constrained("S1","S2") |
29
|
|
|
self.assertNotEqual(results, []) |
30
|
|
|
|
31
|
|
|
def test_path2(self): |
32
|
|
|
"""Tests a simple, impossible path""" |
33
|
|
|
self.setup() |
34
|
|
|
results = self.get_path("S1","S4") |
35
|
|
|
self.assertEqual(results, []) |
36
|
|
|
|
37
|
|
|
def test_constrained_path2(self): |
38
|
|
|
"""Tests a simple, impossible path""" |
39
|
|
|
self.setup() |
40
|
|
|
results = self.get_path_constrained("S1","S4") |
41
|
|
|
self.assertEqual(results, []) |
42
|
|
|
|
43
|
|
|
def test_path3(self): |
44
|
|
|
"""Tests a path to self""" |
45
|
|
|
self.setup() |
46
|
|
|
results = self.get_path("S4","S4") |
47
|
|
|
self.assertNotEqual(results, []) |
48
|
|
|
|
49
|
|
|
def test_constrained_path3(self): |
50
|
|
|
"""Tests a path to self""" |
51
|
|
|
self.setup() |
52
|
|
|
results = self.get_path_constrained("S4","S4") |
53
|
|
|
self.assertNotEqual(results, []) |
54
|
|
|
|
55
|
|
|
def test_path4(self): |
56
|
|
|
"""Tests a path to self again""" |
57
|
|
|
self.setup() |
58
|
|
|
results = self.get_path("S1","S1") |
59
|
|
|
self.assertNotEqual(results, []) |
60
|
|
|
|
61
|
|
|
def test_constrained_path4(self): |
62
|
|
|
"""Tests a path to self again""" |
63
|
|
|
self.setup() |
64
|
|
|
results = self.get_path_constrained("S1","S1") |
65
|
|
|
self.assertNotEqual(results, []) |
66
|
|
|
|
67
|
|
|
def test_constrained_path5(self): |
68
|
|
|
"""Tests constrained path""" |
69
|
|
|
self.setup() |
70
|
|
|
results = self.get_path_constrained("S1","S3", 0, bandwidth = 50) |
71
|
|
|
for result in results: |
72
|
|
|
self.assertNotIn(['S1', 'S1:2', 'S3:2', 'S3'], result["paths"]) |
73
|
|
|
|
74
|
|
|
def test_constrained_path6(self): |
75
|
|
|
"""Tests constrained path""" |
76
|
|
|
self.setup() |
77
|
|
|
results = self.get_path_constrained("S1","S2", 0, ownership = "red") |
78
|
|
|
for result in results: |
79
|
|
|
self.assertNotIn(['S1', 'S1:2', 'S3:2', 'S3', 'S3:1', 'S2:2', 'S2'],result["paths"]) |
80
|
|
|
|
81
|
|
|
def test_constrained_path7(self): |
82
|
|
|
"""Tests constrained path""" |
83
|
|
|
self.setup() |
84
|
|
|
results = self.get_path_constrained("S1","S2", 0, ownership = "blue") |
85
|
|
|
for result in results: |
86
|
|
|
self.assertNotIn(['S1', 'S1:1', 'S2:1', 'S2'],result["paths"]) |
87
|
|
|
|
88
|
|
|
def test_constrained_path8(self): |
89
|
|
|
"""Tests constrained path, to self AGAIN""" |
90
|
|
|
self.setup() |
91
|
|
|
results = self.get_path_constrained("S5","S5", 0, ownership = "blue") |
92
|
|
|
for result in results: |
93
|
|
|
self.assertNotEqual([],result["paths"]) |
94
|
|
|
self.assertIn(['S5'],result["paths"]) |
95
|
|
|
|
96
|
|
|
def test_constrained_path9(self): |
97
|
|
|
"""Tests constrained path""" |
98
|
|
|
self.setup() |
99
|
|
|
results = self.get_path_constrained("S1","S2", 1, ownership = "blue") |
100
|
|
|
for result in results: |
101
|
|
|
self.assertNotIn(['S1', 'S1:1', 'S2:1', 'S2'],result["paths"]) |
102
|
|
|
|
103
|
|
|
@staticmethod |
104
|
|
|
def generateTopology(): |
105
|
|
|
"""Generates a predetermined topology""" |
106
|
|
|
switches = {} |
107
|
|
|
interfaces = {} |
108
|
|
|
links = {} |
109
|
|
|
|
110
|
|
|
TestSearchResults.createSwitch("S1",switches) |
111
|
|
|
TestSearchResults.addInterfaces(2, switches["S1"], interfaces) |
112
|
|
|
|
113
|
|
|
TestSearchResults.createSwitch("S2",switches) |
114
|
|
|
TestSearchResults.addInterfaces(3, switches["S2"], interfaces) |
115
|
|
|
|
116
|
|
|
TestSearchResults.createSwitch("S3",switches) |
117
|
|
|
TestSearchResults.addInterfaces(2, switches["S3"], interfaces) |
118
|
|
|
|
119
|
|
|
TestSearchResults.createSwitch("S4",switches) |
120
|
|
|
TestSearchResults.addInterfaces(2, switches["S4"], interfaces) |
121
|
|
|
|
122
|
|
|
TestSearchResults.createSwitch("S5",switches) |
123
|
|
|
|
124
|
|
|
links["S1:1<->S2:1"] = Link(interfaces["S1:1"], interfaces["S2:1"]) |
125
|
|
|
links["S1:1<->S2:1"].extend_metadata({"bandwidth":50,"ownership":"red"}) |
126
|
|
|
|
127
|
|
|
links["S3:1<->S2:2"] = Link(interfaces["S3:1"], interfaces["S2:2"]) |
128
|
|
|
links["S3:1<->S2:2"].extend_metadata({"bandwidth":51,"ownership":"blue"}) |
129
|
|
|
|
130
|
|
|
links["S1:2<->S3:2"] = Link(interfaces["S1:2"], interfaces["S3:2"]) |
131
|
|
|
links["S1:2<->S3:2"].extend_metadata({"bandwidth":49,"ownership":"blue"}) |
132
|
|
|
|
133
|
|
|
return (switches,links) |
134
|
|
|
|
135
|
|
|
|