Test Failed
Pull Request — master (#53)
by
unknown
02:06
created

build.tests.test_graph3.TestGraph3.test_path9()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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.test_graph import TestKytosGraph
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 TestGraph3(TestKytosGraph):
18
19
    def test_path9(self):
20
        """Tests to see if an illegal path is not in the set of paths that use only edges owned by A."""
21
        #Arrange
22
        self.test_setup()
23
        illegal_path = ['User1', 'User1:1', 'S2:1', 'S2', 'S2:2', 'User2:1', 'User2']
24
        #Act
25
        result = self.get_path_constrained("User1", "User2", 0, ownership = 'A')
26
        #Assert
27
        self.assertNotIn(illegal_path, result[0]["paths"])
28
 
29
    def test_path10(self):
30
        """Tests to see if the edges used in the paths of the result set do not have poor reliability"""
31
        #Arrange
32
        self.test_setup()
33
        reliabilities = []
34
        poor_reliability = 1
35
        key = "reliability"
36
37
        #Act
38
        result = self.get_path_constrained("User1", "User2", 0, reliability = 3)
39
40
        if result:
41
            for path in result[0]["paths"]:
42
                for i in range(1, len(path)):
43
                    endpoint_a = path[i-1]
44
                    endpoint_b = path[i]
45
                    meta_data = self.graph.get_metadata_from_link(endpoint_a, endpoint_b)
46
                    if meta_data and key in meta_data.keys():
47
                        reliabilities.append(meta_data[key])
48
49
        #Assert
50
        self.assertNotIn(poor_reliability, reliabilities)
51
 
52
    def test_path11(self):
53
        """Tests to see if the edges used in the paths from User 1 to User 2 have less than 30 delay."""
54
        #Arrange
55
        self.test_setup()
56
        delays = []
57
        delay_cap = 29
58
        key = "delay"
59
        has_bad_delay = False
60
        
61
        #Act
62
        result = self.get_path_constrained("User1", "User2", 0, delay = delay_cap)
63
64
        if result:
65
            for path in result[0]["paths"]:
66
                for i in range(1, len(path)):
67
                    endpoint_a = path[i-1]
68
                    endpoint_b = path[i]
69
                    meta_data = self.graph.get_metadata_from_link(endpoint_a, endpoint_b)
70
                    if meta_data and key in meta_data.keys():
71
                        delays.append(meta_data[key])
72
73
        #Assert
74
        for delay in delays:
75
            has_bad_delay = delay > delay_cap
76
            self.assertEqual(has_bad_delay, False)
77
 
78
    def test_path12(self):
79
        """Tests to see if the edges used in the paths from User 1 to User 2 have at least 20 bandwidth."""
80
        #Arrange
81
        self.test_setup()
82
        bandwidths = []
83
        bandwidth_floor = 20
84
        key = "bandwidth"
85
        has_bad_bandwidth = False
86
        
87
        #Act
88
        result = self.get_path_constrained("User1", "User2", 0, bandwidth = bandwidth_floor)
89
90
        if result:
91
            for path in result[0]["paths"]:
92
                for i in range(1, len(path)):
93
                    endpoint_a = path[i-1]
94
                    endpoint_b = path[i]
95
                    meta_data = self.graph.get_metadata_from_link(endpoint_a, endpoint_b)
96
                    if meta_data and key in meta_data.keys():
97
                        bandwidths.append(meta_data[key])
98
99
        #Assert
100
        for bandwidth in bandwidths:
101
            has_bad_bandwidth = bandwidth < bandwidth_floor
102
            self.assertEqual(has_bad_bandwidth, False)
103
104
    def test_path13(self):
105
        """Tests to see if the edges used in the paths from User 1 to User 2 have at least 20 bandwidth
106
        and under 30 delay."""
107
        #Arrange
108
        self.test_setup()
109
        bandwidths = []
110
        delays = []
111
        bandwidth_floor = 20
112
        key_a = "bandwidth"
113
        delay_cap = 29
114
        key_b = "delay"
115
        has_bad_bandwidth = False
116
        has_bad_delay = False
117
118
        #Act
119
        result = self.get_path_constrained("User1", "User2", 0, bandwidth = bandwidth_floor, delay = delay_cap)
120
121
        if result:
122
            for path in result[0]["paths"]:
123
                for i in range(1, len(path)):
124
                    endpoint_a = path[i-1]
125
                    endpoint_b = path[i]
126
                    meta_data = self.graph.get_metadata_from_link(endpoint_a, endpoint_b)
127
                    if meta_data and key_a in meta_data.keys():
128
                        bandwidths.append(meta_data[key_a])
129
                    elif meta_data and key_b in meta_data.keys():
130
                        delays.append(meta_data[key_b])
131
        
132
        #Assert
133
        for bandwidth in bandwidths:
134
            has_bad_bandwidth = bandwidth < bandwidth_floor
135
            self.assertEqual(has_bad_bandwidth, False)
136
137
        for delay in delays:
138
            has_bad_delay = delay > delay_cap
139
            self.assertEqual(has_bad_delay, False)
140
141
    @staticmethod
142
    def createLink(interface_a, interface_b, interfaces, links):
143
        compounded = "{}|{}".format(interface_a, interface_b)
144
        final_name = compounded
145
        links[final_name] = Link(interfaces[interface_a], interfaces[interface_b])
146
147
    @staticmethod
148
    def addMetadataToLink(interface_a, interface_b, metrics, links):
149
        compounded = "{}|{}".format(interface_a, interface_b)
150
        links[compounded].extend_metadata(metrics)
151
152
    @staticmethod
153
    def generateTopology():
154
        switches = {}
155
        interfaces = {}
156
        links = {}
157
158
        TestKytosGraph.createSwitch("User1", switches)
159
        TestKytosGraph.addInterfaces(3, switches["User1"], interfaces)
160
161
        TestKytosGraph.createSwitch("S2", switches)
162
        TestKytosGraph.addInterfaces(2, switches["S2"], interfaces)
163
164
        TestKytosGraph.createSwitch("User2", switches)
165
        TestKytosGraph.addInterfaces(3, switches["User2"], interfaces)
166
167
        TestKytosGraph.createSwitch("S4", switches)
168
        TestKytosGraph.addInterfaces(4, switches["S4"], interfaces)
169
170
        TestKytosGraph.createSwitch("S5", switches)
171
        TestKytosGraph.addInterfaces(2, switches["S5"], interfaces)
172
173
        TestGraph3.createLink("User1:1", "S2:1", interfaces, links)
174
        TestGraph3.createLink("User1:2", "S5:1", interfaces, links)
175
        TestGraph3.createLink("User1:3", "S4:1", interfaces, links)
176
        TestGraph3.createLink("S2:2", "User2:1", interfaces, links)
177
        TestGraph3.createLink("User2:2", "S4:2", interfaces, links)
178
        TestGraph3.createLink("S5:2", "S4:3", interfaces, links)
179
        TestGraph3.createLink("User2:3", "S4:4", interfaces, links)
180
181
        TestGraph3.addMetadataToLink("User1:1", "S2:1", {"reliability": 3, "ownership": "B",
182
                                        "delay": 30, "bandwidth": 20}, links)
183
        TestGraph3.addMetadataToLink("User1:2", "S5:1", {"reliability": 1, "ownership": "A",
184
                                        "delay": 5, "bandwidth": 50}, links)
185
        TestGraph3.addMetadataToLink("User1:3", "S4:1", {"reliability": 3, "ownership": "A",
186
                                        "delay": 60, "bandwidth": 10}, links)
187
        TestGraph3.addMetadataToLink("S2:2", "User2:1", {"reliability": 3, "ownership": "B",
188
                                        "delay": 30, "bandwidth": 20}, links)  
189
        TestGraph3.addMetadataToLink("User2:2", "S4:2", {"reliability": 3, "ownership": "B",
190
                                        "delay": 30, "bandwidth": 10}, links)      
191
        TestGraph3.addMetadataToLink("S5:2", "S4:3", {"reliability": 1, "ownership": "A",
192
                                        "delay": 10, "bandwidth": 50}, links)          
193
        TestGraph3.addMetadataToLink("User2:3", "S4:4", {"reliability": 3, "ownership": "A",
194
                                        "delay": 29, "bandwidth": 20}, links)                                                                                                                                                
195
        
196
        return (switches, links)
197