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

build.tests.helpers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 6

6 Functions

Rating   Name   Duplication   Size   Complexity  
A get_topology_mock() 0 18 1
A get_switch_mock() 0 5 1
A get_link_mock() 0 7 1
A get_test_client() 0 4 1
A get_controller_mock() 0 6 1
A get_interface_mock() 0 8 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.interface import Interface
7
from kytos.core.link import Link
8
from kytos.core.switch import Switch
9
10
11
def get_controller_mock():
12
    """Return a controller mock."""
13
    options = KytosConfig().options['daemon']
14
    controller = Controller(options)
15
    controller.log = Mock()
16
    return controller
17
18
19
def get_interface_mock(name, port_number, switch):
20
    """Return a interface mock."""
21
    interface = create_autospec(Interface)
22
    interface.id = "{}:{}".format(switch.dpid, port_number)
23
    type(interface).name = name
24
    type(interface).port_number = port_number
25
    type(interface).switch = switch
26
    return interface
27
28
29
def get_link_mock(endpoint_a, endpoint_b):
30
    """Return a link mock."""
31
    link = create_autospec(Link)
32
    type(link).endpoint_a = endpoint_a
33
    type(link).endpoint_b = endpoint_b
34
    type(link).metadata = {"A": "B"}
35
    return link
36
37
38
def get_switch_mock(dpid):
39
    """Return a switch mock."""
40
    switch = create_autospec(Switch)
41
    type(switch).dpid = dpid
42
    return switch
43
44
45
def get_topology_mock():
46
    """Create a default topology."""
47
    switch = get_switch_mock("00:00:00:00:00:00:00:01")
48
    endpoint_a = get_interface_mock("s1-eth1", 1, switch)
49
    endpoint_b = get_interface_mock("s1-eth2", 2, switch)
50
    endpoint_c = get_interface_mock("s1-eth1", 3, switch)
51
    endpoint_d = get_interface_mock("s1-eth2", 4, switch)
52
    link_1 = get_link_mock(endpoint_a, endpoint_b)
53
    link_2 = get_link_mock(endpoint_c, endpoint_d)
54
    switch.interfaces = {endpoint_a.id: endpoint_a,
55
                         endpoint_b.id: endpoint_b,
56
                         endpoint_c.id: endpoint_c,
57
                         endpoint_d.id: endpoint_d}
58
59
    topology = MagicMock()
60
    topology.links = {"1": link_1, "2": link_2}
61
    topology.switches = {"00:00:00:00:00:00:00:01": switch}
62
    return topology
63
64
65
def get_test_client(controller, napp):
66
    """Return a flask api test client."""
67
    controller.api_server.register_napp_endpoints(napp)
68
    return controller.api_server.app.test_client()
69