build.tests.helpers.get_connection_mock()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 3
1
"""Module to help to create tests."""
2
from unittest.mock import MagicMock, Mock
3
4
from pyof.utils import unpack
5
6
from kytos.core import Controller
7
from kytos.core.config import KytosConfig
8
from kytos.core.connection import Connection, ConnectionState
9
from kytos.core.interface import Interface
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(interface_name, port, *args, **kwargs):
22
    """Return a interface mock."""
23
    switch = get_switch_mock(0x04)
24
    switch.connection = Mock()
25
    iface = Interface(interface_name, port, switch, *args, **kwargs)
26
    return iface
27
28
29
def get_switch_mock(dpid="00:00:00:00:00:00:00:01"):
30
    """Return a switch mock."""
31
    return Switch(dpid)
32
33
34
def get_connection_mock(of_version, target_switch, state=ConnectionState.NEW):
35
    """Return a connection mock."""
36
    connection = Connection(Mock(), Mock(), Mock())
37
    connection.switch = target_switch
38
    connection.state = state
39
    connection.protocol.version = of_version
40
    connection.protocol.unpack = unpack
41
    return connection
42
43
44
def get_kytos_event_mock(**kwargs):
45
    """Return a kytos event mock."""
46
    destination = kwargs.get('destination')
47
    message = kwargs.get('message')
48
    source = kwargs.get('source')
49
50
    event = MagicMock()
51
    event.source = source
52
    event.content = {'destination': destination,
53
                     'message': message}
54
    return event
55