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

build.tests.helpers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 8

7 Functions

Rating   Name   Duplication   Size   Complexity  
A get_link_mock() 0 7 1
A get_controller_mock() 0 6 1
A get_interface_mock() 0 10 1
A get_topology_mock() 0 32 1
A get_connection_mock() 0 10 1
A get_switch_mock() 0 7 2
A get_test_client() 0 4 1
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, of_version=None):
43
    """Return a switch mock."""
44
    switch = create_autospec(Switch)
45
    switch.dpid = dpid
46
    if of_version:
47
        switch.connection = get_connection_mock(of_version, switch)
48
    return switch
49
50
51
def get_connection_mock(of_version, switch, state=ConnectionState.NEW):
52
    """Return a connection mock."""
53
    protocol = create_autospec(ConnectionProtocol)
54
    protocol.version = of_version
55
    connection = create_autospec(Connection)
56
    connection.protocol = protocol
57
    connection.switch = switch
58
    connection.address = "00:00:00:00:00:00"
59
    connection.state = state
60
    return connection
61
62
63
def get_topology_mock():
64
    """Create a default topology."""
65
    switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
66
    switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04)
67
    switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01)
68
69
    interface_a1 = get_interface_mock("s1-eth1", 1, switch_a)
70
    interface_a2 = get_interface_mock("s1-eth2", 2, switch_a)
71
72
    interface_b1 = get_interface_mock("s2-eth1", 1, switch_b)
73
    interface_b2 = get_interface_mock("s2-eth2", 2, switch_b)
74
75
    interface_c1 = get_interface_mock("s3-eth1", 1, switch_c)
76
    interface_c2 = get_interface_mock("s3-eth2", 2, switch_c)
77
78
    switch_a.interfaces = {interface_a1.id: interface_a1,
79
                           interface_a2.id: interface_a2}
80
    switch_b.interfaces = {interface_b1.id: interface_b1,
81
                           interface_b2.id: interface_b2}
82
    switch_c.interfaces = {interface_c1.id: interface_c1,
83
                           interface_c2.id: interface_c2}
84
85
    link_1 = get_link_mock(interface_a1, interface_b1)
86
    link_2 = get_link_mock(interface_a2, interface_c1)
87
    link_3 = get_link_mock(interface_b1, interface_c2)
88
89
    topology = MagicMock()
90
    topology.links = {"1": link_1, "2": link_2, "3": link_3}
91
    topology.switches = {switch_a.dpid: switch_a,
92
                         switch_b.dpid: switch_b,
93
                         switch_c.dpid: switch_c}
94
    return topology
95
96
97
def get_test_client(controller, napp):
98
    """Return a flask api test client."""
99
    controller.api_server.register_napp_endpoints(napp)
100
    return controller.api_server.app.test_client()
101