Test Failed
Pull Request — master (#58)
by
unknown
02:27
created

TestSearchResults.setup()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
"""Module to test the KytosGraph in graph.py."""
2
from unittest import TestCase
3
4
import networkx as nx
5
# Core modules to import
6
from kytos.core.interface import Interface
7
from kytos.core.link import Link
8
from kytos.core.switch import Switch
9
10
# module under test
11
from graph import KytosGraph
12
13
14
class TestSearchResults(TestCase):
15
    """Tests for the graph class."""
16
17
    def setup(self):
18
        """Setup for most tests"""
19
        switches, links = self.generate_topology()
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
        """Return the shortest path"""
28
        results = self.graph.shortest_paths(source, destination)
29
        return results
30
31
    def get_path_constrained(self, source, destination, metrics=None,
32
                             flexible_metrics=None, flexible=None):
33
        """Return the constrained shortest path"""
34
        complete_metrics = KytosGraph.pack_metrics(metrics, flexible_metrics)
35
        return self.graph.constrained_flexible_paths(source, destination,
36
                                                     complete_metrics,
37
                                                     flexible)
38
39
    def test_setup(self):
40
        """Provides information on default test setup"""
41
        self.setup()
42
43
    @ staticmethod
44
    def generate_topology():
45
        """Generates a predetermined topology"""
46
        switches = {}
47
        links = {}
48
        return (switches, links)
49
50
    @ staticmethod
51
    def create_switch(name, switches):
52
        '''Add a new switch to the list of switches'''
53
        switches[name] = Switch(name)
54
55
    @ staticmethod
56
    def add_interfaces(count, switch, interfaces):
57
        '''Add a new interface to the list of interfaces'''
58
        for i in range(1, count + 1):
59
            str1 = "{}:{}".format(switch.dpid, i)
60
            interface = Interface(str1, i, switch)
61
            interfaces[str1] = interface
62
            switch.update_interface(interface)
63
64
    @ staticmethod
65
    def create_link(interface_a, interface_b, interfaces, links):
66
        '''Add a new link between two interfaces into the list of links'''
67
        compounded = "{}|{}".format(interface_a, interface_b)
68
        final_name = compounded
69
        links[final_name] = Link(
70
            interfaces[interface_a], interfaces[interface_b])
71
72
    @ staticmethod
73
    def add_metadata_to_link(interface_a, interface_b, metrics, links):
74
        '''Add metadata to an existing link in the list of links'''
75
        compounded = "{}|{}".format(interface_a, interface_b)
76
        links[compounded].extend_metadata(metrics)
77