Passed
Push — master ( cf72e6...2ad18d )
by Humberto
06:56 queued 03:57
created

kytos.lib.helpers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 81
rs 10
c 0
b 0
f 0
ccs 55
cts 55
cp 1
wmc 8

7 Functions

Rating   Name   Duplication   Size   Complexity  
A get_connection_mock() 0 11 1
A get_link_mock() 0 7 1
A get_interface_mock() 0 10 1
A get_kytos_event_mock() 0 9 1
A get_switch_mock() 0 8 2
A get_controller_mock() 0 6 1
A get_test_client() 0 4 1
1
"""Module with utilities to create tests."""
2 1
from unittest.mock import Mock, create_autospec
3
4 1
from kytos.core import Controller
5 1
from kytos.core.config import KytosConfig
6 1
from kytos.core.connection import (Connection, ConnectionProtocol,
7
                                   ConnectionState)
8 1
from kytos.core.events import KytosEvent
9 1
from kytos.core.interface import Interface
10 1
from kytos.core.link import Link
11 1
from kytos.core.switch import Switch
12
13
14 1
def get_controller_mock(loop=None):
15
    """Return a controller mock."""
16 1
    options = KytosConfig().options['daemon']
17 1
    controller = Controller(options, loop=loop)
18 1
    controller.log = Mock()
19 1
    return controller
20
21
22 1
def get_interface_mock(name, port_number, switch, address="00:00:00:00:00:00"):
23
    """Return a interface mock."""
24 1
    interface = create_autospec(Interface)
25 1
    interface.id = "{}:{}".format(switch.dpid, port_number)
26 1
    interface.name = name
27 1
    interface.port_number = port_number
28 1
    interface.switch = switch
29 1
    interface.address = address
30 1
    interface.lldp = True
31 1
    return interface
32
33
34 1
def get_link_mock(endpoint_a, endpoint_b):
35
    """Return a link mock."""
36 1
    link = create_autospec(Link)
37 1
    link.endpoint_a = endpoint_a
38 1
    link.endpoint_b = endpoint_b
39 1
    link.metadata = {"A": 0}
40 1
    return link
41
42
43 1
def get_switch_mock(dpid, of_version=None):
44
    """Return a switch mock."""
45 1
    switch = create_autospec(Switch)
46 1
    switch.dpid = dpid
47 1
    if of_version:
48 1
        switch.ofp_version = '0x0' + str(of_version)
49 1
        switch.connection = get_connection_mock(of_version, switch)
50 1
    return switch
51
52
53 1
def get_connection_mock(of_version, switch, address="00:00:00:00:00:00",
54
                        state=ConnectionState.NEW):
55
    """Return a connection mock."""
56 1
    protocol = create_autospec(ConnectionProtocol)
57 1
    protocol.version = of_version
58 1
    connection = create_autospec(Connection)
59 1
    connection.protocol = protocol
60 1
    connection.switch = switch
61 1
    connection.address = address
62 1
    connection.state = state
63 1
    return connection
64
65
66 1
def get_kytos_event_mock(name, content):
67
    """Return a kytos event mock."""
68 1
    event = create_autospec(KytosEvent)
69 1
    event.name = name
70 1
    event.content = content
71 1
    event.message = content.get('message')
72 1
    event.destination = content.get('destination')
73 1
    event.source = content.get('source')
74 1
    return event
75
76
77 1
def get_test_client(controller, napp):
78
    """Return a flask api test client."""
79 1
    controller.api_server.register_napp_endpoints(napp)
80
    return controller.api_server.app.test_client()
81