|
1
|
|
|
"""Module to test the KytosGraph in graph.py.""" |
|
2
|
|
|
# from unittest import TestCase |
|
3
|
|
|
# from unittest.mock import Mock |
|
4
|
|
|
# |
|
5
|
|
|
# from graph import KytosGraph |
|
6
|
|
|
# |
|
7
|
|
|
# |
|
8
|
|
|
# class TestKytosGraph(TestCase): |
|
9
|
|
|
# """Class to test KytosGraph class.""" |
|
10
|
|
|
# |
|
11
|
|
|
# def setUp(self): |
|
12
|
|
|
# """Create a custom KytosGraph, nodes and links. |
|
13
|
|
|
# |
|
14
|
|
|
# 1 2 3 4 5 |
|
15
|
|
|
# 0 ---- 0 ---- 0 ----- 0 --- 0 |
|
16
|
|
|
# | | |
|
17
|
|
|
# --------------- |
|
18
|
|
|
# """ |
|
19
|
|
|
# self.graph = KytosGraph() |
|
20
|
|
|
# self.nodes = self.create_custom_nodes() |
|
21
|
|
|
# self.links = self.create_custom_links() |
|
22
|
|
|
# |
|
23
|
|
|
# @staticmethod |
|
24
|
|
|
# def create_custom_nodes(): |
|
25
|
|
|
# """Create custom nodes.""" |
|
26
|
|
|
# nodes = [] |
|
27
|
|
|
# for number in range(1, 6): |
|
28
|
|
|
# device_id = "00:00:00:00:00:00:00:0"+str(number) |
|
29
|
|
|
# ports = [{'number': 65534}, {'number': 1}, |
|
30
|
|
|
# {'number': 2}, {'number': 3}] |
|
31
|
|
|
# node = Mock(device_id=device_id, ports=ports) |
|
32
|
|
|
# nodes.append(node) |
|
33
|
|
|
# return nodes |
|
34
|
|
|
# |
|
35
|
|
|
# def create_custom_links(self): |
|
36
|
|
|
# """Create custom links between nodes.""" |
|
37
|
|
|
# links = [] |
|
38
|
|
|
# for number in range(1, 3): |
|
39
|
|
|
# interface_one = Mock(device=self.nodes[number], port_id=2) |
|
40
|
|
|
# interface_two = Mock(device=self.nodes[number+1], port_id=1) |
|
41
|
|
|
# link = Mock(interface_one=interface_one, |
|
42
|
|
|
# interface_two=interface_two) |
|
43
|
|
|
# links.append(link) |
|
44
|
|
|
# |
|
45
|
|
|
# interface_one = Mock(device=self.nodes[1], port_id=3) |
|
46
|
|
|
# interface_two = Mock(device=self.nodes[3], port_id=3) |
|
47
|
|
|
# shortest_link = Mock(interface_one=interface_one, |
|
48
|
|
|
# interface_two=interface_two) |
|
49
|
|
|
# links.append(shortest_link) |
|
50
|
|
|
# |
|
51
|
|
|
# interface_one = Mock(device=self.nodes[0], port_id=1) |
|
52
|
|
|
# interface_two = Mock(device=self.nodes[1], port_id=1) |
|
53
|
|
|
# host_link = Mock(interface_one=interface_one, |
|
54
|
|
|
# interface_two=interface_two) |
|
55
|
|
|
# links.append(host_link) |
|
56
|
|
|
# |
|
57
|
|
|
# interface_one = Mock(device=self.nodes[3], port_id=1) |
|
58
|
|
|
# interface_two = Mock(device=self.nodes[4], port_id=2) |
|
59
|
|
|
# host_link = Mock(interface_one=interface_one, |
|
60
|
|
|
# interface_two=interface_two) |
|
61
|
|
|
# links.append(host_link) |
|
62
|
|
|
# |
|
63
|
|
|
# return links |
|
64
|
|
|
# |
|
65
|
|
|
# def test_update_nodes(self): |
|
66
|
|
|
# """Test update nodes using custom_nodes.""" |
|
67
|
|
|
# self.graph.update_nodes(self.nodes) |
|
68
|
|
|
# self.assertEqual(self.graph.graph.number_of_nodes(), 5) |
|
69
|
|
|
# |
|
70
|
|
|
# def test_update_links(self): |
|
71
|
|
|
# """Test update links between nodes using custom links.""" |
|
72
|
|
|
# self.graph.update_links(self.links) |
|
73
|
|
|
# self.assertEqual(self.graph.graph.number_of_edges(), 5) |
|
74
|
|
|
# |
|
75
|
|
|
# def test_shortest_path(self): |
|
76
|
|
|
# """Test calculate shortest path with valid source and destination. |
|
77
|
|
|
# |
|
78
|
|
|
# """ |
|
79
|
|
|
# self.graph.update_nodes(self.nodes) |
|
80
|
|
|
# self.graph.update_links(self.links) |
|
81
|
|
|
# path = self.graph.shortest_path('00:00:00:00:00:00:00:01', |
|
82
|
|
|
# '00:00:00:00:00:00:00:05') |
|
83
|
|
|
# expected_path = ['00:00:00:00:00:00:00:01', |
|
84
|
|
|
# '00:00:00:00:00:00:00:02', |
|
85
|
|
|
# '00:00:00:00:00:00:00:04', |
|
86
|
|
|
# '00:00:00:00:00:00:00:05'] |
|
87
|
|
|
# self.assertEqual(path, expected_path) |
|
88
|
|
|
# |
|
89
|
|
|
# def test_unreachable_path(self): |
|
90
|
|
|
# """Test calculate shorts path with invalid destination.""" |
|
91
|
|
|
# self.graph.update_nodes(self.nodes) |
|
92
|
|
|
# self.graph.update_links(self.links) |
|
93
|
|
|
# source = '00:00:00:00:00:00:00:01' |
|
94
|
|
|
# destination = '00:00:00:00:00:00:00:06' |
|
95
|
|
|
# path = self.graph.shortest_path(source, destination) |
|
96
|
|
|
# expected_path = "The shortest path between {} and {} can't |
|
97
|
|
|
# be found." |
|
98
|
|
|
# self.assertEqual(expected_path.format(source, destination), path) |
|
99
|
|
|
|