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
|
|
|
# Core modules to import |
11
|
|
|
from kytos.core.switch import Switch |
12
|
|
|
from kytos.core.interface import Interface |
13
|
|
|
from kytos.core.link import Link |
14
|
|
|
|
15
|
|
|
class TestSearchResults(TestCase): |
16
|
|
|
|
17
|
|
|
def setup(self): |
18
|
|
|
"""Setup for most tests""" |
19
|
|
|
switches, links = self.generateTopology() |
20
|
|
|
self.graph = KytosGraph() |
21
|
|
|
self.graph.clear() |
22
|
|
|
self.graph.update_nodes(switches) |
23
|
|
|
self.graph.update_links(links) |
24
|
|
|
self.graph.set_path_fun(nx.shortest_simple_paths) |
25
|
|
|
|
26
|
|
|
def get_path(self, source, destination): |
27
|
|
|
results = self.graph.shortest_paths(source,destination) |
28
|
|
|
return results |
29
|
|
|
|
30
|
|
|
def get_path_constrained(self, source, destination, flexible = 0, **metrics): |
31
|
|
|
results = self.graph.constrained_flexible_paths(source, destination,{},metrics,flexible) |
32
|
|
|
return results |
33
|
|
|
|
34
|
|
|
def get_path_constrained2(self, source, destination, metrics, flexible_metrics): |
35
|
|
|
return self.graph.constrained_flexible_paths(source, destination, metrics, flexible_metrics) |
36
|
|
|
|
37
|
|
|
def test_setup(self): |
38
|
|
|
"""Provides information on default test setup""" |
39
|
|
|
self.setup() |
40
|
|
|
|
41
|
|
|
@staticmethod |
42
|
|
|
def generateTopology(): |
43
|
|
|
"""Generates a predetermined topology""" |
44
|
|
|
switches = {} |
45
|
|
|
links = {} |
46
|
|
|
return (switches,links) |
47
|
|
|
|
48
|
|
|
@staticmethod |
49
|
|
|
def createSwitch(name,switches): |
50
|
|
|
switches[name] = Switch(name) |
51
|
|
|
|
52
|
|
|
@staticmethod |
53
|
|
|
def addInterfaces(count,switch,interfaces): |
54
|
|
|
for x in range(1,count + 1): |
55
|
|
|
str1 = "{}:{}".format(switch.dpid,x) |
56
|
|
|
iFace = Interface(str1,x,switch) |
57
|
|
|
interfaces[str1] = iFace |
58
|
|
|
switch.update_interface(iFace) |
59
|
|
|
|
60
|
|
|
@staticmethod |
61
|
|
|
def createLink(interface_a, interface_b, interfaces, links): |
62
|
|
|
compounded = "{}|{}".format(interface_a, interface_b) |
63
|
|
|
final_name = compounded |
64
|
|
|
links[final_name] = Link(interfaces[interface_a], interfaces[interface_b]) |
65
|
|
|
|
66
|
|
|
@staticmethod |
67
|
|
|
def addMetadataToLink(interface_a, interface_b, metrics, links): |
68
|
|
|
compounded = "{}|{}".format(interface_a, interface_b) |
69
|
|
|
links[compounded].extend_metadata(metrics) |
70
|
|
|
|