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

build.tests.unit.test_results_metadata   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 122
dl 0
loc 163
rs 9.92
c 0
b 0
f 0
wmc 31

4 Methods

Rating   Name   Duplication   Size   Complexity  
B TestResultsMetadata.generate_topology() 0 60 1
C TestResultsMetadata.test_path3() 0 21 10
B TestResultsMetadata.test_path2() 0 21 7
B TestResultsMetadata.test_path1() 0 20 6
1
"""Module to test the KytosGraph in graph.py."""
2
3
# module under test
4
from tests.unit.test_results import TestResults
5
6
7
class TestResultsMetadata(TestResults):
8
    """Tests for the graph class."""
9
10
    def test_path1(self):
11
        """Tests to see if the edges used in the paths of the result set
12
        do not have poor reliability"""
13
        reliabilities = []
14
        requirements = {"reliability": 3}
15
        poor_reliability = 1
16
17
        result = self.get_path_constrained("User1", "User2", base=requirements)
18
19
        if result:
20
            for path in result[0]["paths"]:
21
                for i in range(1, len(path)):
22
                    endpoint_a = path[i - 1]
23
                    endpoint_b = path[i]
24
                    meta_data = self.graph.get_metadata_from_link(
25
                        endpoint_a, endpoint_b)
26
                    if meta_data and "reliability" in meta_data.keys():
27
                        reliabilities.append(meta_data["reliability"])
28
29
        self.assertNotIn(poor_reliability, reliabilities)
30
31
    def test_path2(self):
32
        """Tests to see if the edges used in the paths from User 1 to User 2
33
        have less than 30 delay."""
34
        delays = []
35
        requirements = {"delay": 29}
36
37
        result = self.get_path_constrained(
38
            "User1", "User2", base=requirements)
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(
46
                        endpoint_a, endpoint_b)
47
                    if meta_data and "delay" in meta_data.keys():
48
                        delays.append(meta_data["delay"])
49
50
        for delay in delays:
51
            self.assertEqual(delay > requirements["delay"], False)
52
53
    def test_path3(self):
54
        """Tests to see if the edges used in the paths from User 1 to User 2
55
        have at least 20 bandwidth."""
56
        bandwidths = []
57
        requirements = {"bandwidth": 20}
58
59
        result = self.get_path_constrained(
60
            "User1", "User2", base=requirements)
61
62
        if result:
63
            for path in result[0]["paths"]:
64
                for i in range(1, len(path)):
65
                    endpoint_a = path[i - 1]
66
                    endpoint_b = path[i]
67
                    meta_data = self.graph.get_metadata_from_link(
68
                        endpoint_a, endpoint_b)
69
                    if meta_data and "bandwidth" in meta_data.keys():
70
                        bandwidths.append(meta_data["bandwidth"])
71
72
        for bandwidth in bandwidths:
73
            self.assertEqual(bandwidth < requirements["bandwidth"], False)
74
75
    def test_path3(self):
76
        """Tests to see if the edges used in the paths from User 1 to User 2
77
        have at least 20 bandwidth and under 30 delay."""
78
        bandwidths = []
79
        delays = []
80
        requirements = {"bandwidth": 20, "delay": 29}
81
82
        result = self.get_path_constrained(
83
            "User1", "User2", base=requirements)
84
85
        if result:
86
            for path in result[0]["paths"]:
87
                for i in range(1, len(path)):
88
                    endpoint_a = path[i - 1]
89
                    endpoint_b = path[i]
90
                    meta_data = self.graph.get_metadata_from_link(
91
                        endpoint_a, endpoint_b)
92
                    if meta_data and "bandwidth" in meta_data.keys():
93
                        bandwidths.append(meta_data["bandwidth"])
94
                    elif meta_data and "delay" in meta_data.keys():
95
                        delays.append(meta_data["delay"])
96
97
        for bandwidth in bandwidths:
98
            self.assertEqual(bandwidth < requirements["bandwidth"], False)
99
100
        for delay in delays:
101
            self.assertEqual(delay > requirements["delay"], False)
102
103
    @staticmethod
104
    def generate_topology():
105
        """Generates a predetermined topology"""
106
        switches = {}
107
        interfaces = {}
108
        links = {}
109
110
        TestResults.create_switch("User1", switches)
111
        TestResults.add_interfaces(3, switches["User1"], interfaces)
112
113
        TestResults.create_switch("S2", switches)
114
        TestResults.add_interfaces(2, switches["S2"], interfaces)
115
116
        TestResults.create_switch("User2", switches)
117
        TestResults.add_interfaces(3, switches["User2"], interfaces)
118
119
        TestResults.create_switch("S4", switches)
120
        TestResults.add_interfaces(4, switches["S4"], interfaces)
121
122
        TestResults.create_switch("S5", switches)
123
        TestResults.add_interfaces(2, switches["S5"], interfaces)
124
125
        TestResults.create_link("User1:1", "S2:1", interfaces, links)
126
        TestResults.create_link("User1:2", "S5:1", interfaces, links)
127
        TestResults.create_link("User1:3", "S4:1", interfaces, links)
128
        TestResults.create_link("S2:2", "User2:1", interfaces, links)
129
        TestResults.create_link("User2:2", "S4:2", interfaces, links)
130
        TestResults.create_link("S5:2", "S4:3", interfaces, links)
131
        TestResults.create_link("User2:3", "S4:4", interfaces, links)
132
133
        TestResults.add_metadata_to_link(
134
            "User1:1", "S2:1", {
135
                "reliability": 3, "ownership": "B", "delay": 30,
136
                "bandwidth": 20}, links)
137
        TestResults.add_metadata_to_link(
138
            "User1:2", "S5:1", {
139
                "reliability": 1, "ownership": "A", "delay": 5,
140
                "bandwidth": 50}, links)
141
        TestResults.add_metadata_to_link(
142
            "User1:3", "S4:1", {
143
                "reliability": 3, "ownership": "A", "delay": 60,
144
                "bandwidth": 10}, links)
145
        TestResults.add_metadata_to_link(
146
            "S2:2", "User2:1", {
147
                "reliability": 3, "ownership": "B", "delay": 30,
148
                "bandwidth": 20}, links)
149
        TestResults.add_metadata_to_link(
150
            "User2:2", "S4:2", {
151
                "reliability": 3, "ownership": "B", "delay": 30,
152
                "bandwidth": 10}, links)
153
        TestResults.add_metadata_to_link(
154
            "S5:2", "S4:3", {
155
                "reliability": 1, "ownership": "A", "delay": 10,
156
                "bandwidth": 50}, links)
157
        TestResults.add_metadata_to_link(
158
            "User2:3", "S4:4", {
159
                "reliability": 3, "ownership": "A", "delay": 29,
160
                "bandwidth": 20}, links)
161
162
        return (switches, links)
163