Completed
Pull Request — master (#69)
by Gleyberson
02:18
created

build.tests.helpers.unit()   A

Complexity

Conditions 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 11
dl 15
loc 15
rs 9.85
c 0
b 0
f 0
cc 3
nop 1
1
"""Module to help to create tests."""
2
import os
3
from functools import wraps
4
from unittest.mock import MagicMock, Mock
5
6
from pyof.utils import unpack
7
from kytos.core import Controller
8
from kytos.core.config import KytosConfig
9
from kytos.core.connection import Connection, ConnectionState
10
from kytos.core.interface import Interface
11
from kytos.core.switch import Switch
12
13
14
def get_controller_mock():
15
    """Return a controller mock."""
16
    options = KytosConfig().options['daemon']
17
    controller = Controller(options)
18
    controller.log = Mock()
19
    return controller
20
21
22
def get_interface_mock(interface_name, port, *args, **kwargs):
23
    """Return a interface mock."""
24
    switch = get_switch_mock(0x04)
25
    switch.connection = Mock()
26
    iface = Interface(interface_name, port, switch, *args, **kwargs)
27
    return iface
28
29
30
def get_switch_mock(dpid="00:00:00:00:00:00:00:01"):
31
    """Return a switch mock."""
32
    return Switch(dpid)
33
34
35
def get_connection_mock(of_version, target_switch, state=ConnectionState.NEW):
36
    """Return a connection mock."""
37
    connection = Connection(Mock(), Mock(), Mock())
38
    connection.switch = target_switch
39
    connection.state = state
40
    connection.protocol.version = of_version
41
    connection.protocol.unpack = unpack
42
    return connection
43
44
45
def get_kytos_event_mock(**kwargs):
46
    """Return a kytos event mock."""
47
    destination = kwargs.get('destination')
48
    message = kwargs.get('message')
49
    source = kwargs.get('source')
50
51
    event = MagicMock()
52
    event.source = source
53
    event.content = {'destination': destination,
54
                     'message': message}
55
    return event
56
57
58
# pylint: disable=unused-argument
59 View Code Duplication
def unit(size='small'):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
60
    """Handle tokens from requests."""
61
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
62
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
63
64
    def inner_func(func):
65
66
        @wraps(func)
67
        def wrapper(*args, **kwargs):
68
            if env_test_type == 'unit' and size == env_test_size:
69
                return func(*args, **kwargs)
70
            return None
71
        return wrapper
72
73
    return inner_func
74
75
76
# pylint: disable=unused-argument
77 View Code Duplication
def integration(size='small'):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
78
    """Handle tokens from requests."""
79
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
80
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
81
82
    def inner_func(func):
83
84
        @wraps(func)
85
        def wrapper(*args, **kwargs):
86
            if env_test_type == 'integration' and size == env_test_size:
87
                return func(*args, **kwargs)
88
            return None
89
        return wrapper
90
91
    return inner_func
92
93
94
# pylint: disable=unused-argument
95 View Code Duplication
def e2e(size='small'):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
96
    """Handle tokens from requests."""
97
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
98
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
99
100
    def inner_func(func):
101
102
        @wraps(func)
103
        def wrapper(*args, **kwargs):
104
            if env_test_type == 'e2e' and size == env_test_size:
105
                return func(*args, **kwargs)
106
            return None
107
        return wrapper
108
109
    return inner_func
110