Completed
Pull Request — master (#58)
by
unknown
02:26
created

TestSearchResults3.test_path11()   B

Complexity

Conditions 7

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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