Passed
Pull Request — master (#48)
by Gleyberson
02:13
created

build.tests.helpers.get_link_mock()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
"""Module to help to create tests."""
2
from unittest.mock import MagicMock, Mock, create_autospec
3
4
from kytos.core import Controller
5
from kytos.core.config import KytosConfig
6
from kytos.core.connection import (Connection, ConnectionProtocol,
7
                                   ConnectionState)
8
from kytos.core.interface import Interface
9
from kytos.core.link import Link
10
from kytos.core.switch import Switch
11
12
13
def get_controller_mock():
14
    """Return a controller mock."""
15
    options = KytosConfig().options['daemon']
16
    controller = Controller(options)
17
    controller.log = Mock()
18
    return controller
19
20
21
def get_interface_mock(name, port_number, switch):
22
    """Return a interface mock."""
23
    interface = create_autospec(Interface)
24
    interface.id = "{}:{}".format(switch.dpid, port_number)
25
    interface.name = name
26
    interface.port_number = port_number
27
    interface.switch = switch
28
    interface.address = "00:00:00:00:00:00"
29
    interface.lldp = True
30
    return interface
31
32
33
def get_link_mock(endpoint_a, endpoint_b):
34
    """Return a link mock."""
35
    link = create_autospec(Link)
36
    link.endpoint_a = endpoint_a
37
    link.endpoint_b = endpoint_b
38
    link.metadata = {"A": 0}
39
    return link
40
41
42
def get_switch_mock(dpid):
43
    """Return a switch mock."""
44
    switch = create_autospec(Switch)
45
    switch.dpid = dpid
46
    return switch
47
48
49
def get_connection_mock(of_version, switch, state=ConnectionState.NEW):
50
    """Return a connection mock."""
51
    protocol = create_autospec(ConnectionProtocol)
52
    protocol.version = of_version
53
    connection = create_autospec(Connection)
54
    connection.protocol = protocol
55
    connection.switch = switch
56
    connection.address = "00:00:00:00:00:00"
57
    connection.state = state
58
    return connection
59
60
61
def get_topology_mock():
62
    """Create a default topology."""
63
    switch_a = get_switch_mock("00:00:00:00:00:00:00:01")
64
    switch_b = get_switch_mock("00:00:00:00:00:00:00:02")
65
    switch_c = get_switch_mock("00:00:00:00:00:00:00:03")
66
67
    switch_a.connection = get_connection_mock(0x04, switch_b)
68
    switch_a.connection = get_connection_mock(0x01, switch_c)
69
    switch_b.connection = get_connection_mock(0x04, switch_a)
70
    switch_c.connection = get_connection_mock(0x01, switch_a)
71
72
    interface_a1 = get_interface_mock("s1-eth1", 1, switch_a)
73
    interface_a2 = get_interface_mock("s1-eth2", 2, switch_a)
74
75
    interface_b1 = get_interface_mock("s2-eth1", 1, switch_b)
76
    interface_b2 = get_interface_mock("s2-eth2", 2, switch_b)
77
78
    interface_c1 = get_interface_mock("s3-eth1", 1, switch_c)
79
    interface_c2 = get_interface_mock("s3-eth2", 2, switch_c)
80
81
    switch_a.interfaces = {interface_a1.id: interface_a1,
82
                           interface_a2.id: interface_a2}
83
    switch_b.interfaces = {interface_b1.id: interface_b1,
84
                           interface_b2.id: interface_b2}
85
    switch_c.interfaces = {interface_c1.id: interface_c1,
86
                           interface_c2.id: interface_c2}
87
88
    link_1 = get_link_mock(interface_a1, interface_b1)
89
    link_2 = get_link_mock(interface_a2, interface_c1)
90
    link_3 = get_link_mock(interface_b1, interface_c2)
91
92
    topology = MagicMock()
93
    topology.links = {"1": link_1, "2": link_2, "3": link_3}
94
    topology.switches = {switch_a.dpid: switch_a,
95
                         switch_b.dpid: switch_b,
96
                         switch_c.dpid: switch_c}
97
    return topology
98
99
100
def get_test_client(controller, napp):
101
    """Return a flask api test client."""
102
    controller.api_server.register_napp_endpoints(napp)
103
    return controller.api_server.app.test_client()
104