build.tests.helpers   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 5

3 Functions

Rating   Name   Duplication   Size   Complexity  
A get_controller_mock() 0 6 1
A get_link_mocked() 0 28 1
A get_uni_mocked() 0 28 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MockResponse.__init__() 0 9 1
A MockResponse.json() 0 3 1
1
"""Module to help to create tests."""
2
from unittest.mock import Mock
3
4
from kytos.core import Controller
5
from kytos.core.common import EntityStatus
6
from kytos.core.config import KytosConfig
7
from kytos.core.interface import TAG, UNI, Interface
8
from kytos.core.link import Link
9
from kytos.core.switch import Switch
10
11
12
def get_controller_mock():
13
    """Return a controller mock."""
14
    options = KytosConfig().options['daemon']
15
    controller = Controller(options)
16
    controller.log = Mock()
17
    return controller
18
19
20
def get_link_mocked(**kwargs):
21
    """Return a link mocked.
22
23
    Args:
24
        link_dict: Python dict returned after call link.as_dict()
25
    """
26
    switch_a = kwargs.get('switch_a', Switch('00:00:00:00:00:01'))
27
    switch_b = kwargs.get('switch_b', Switch('00:00:00:00:00:02'))
28
29
    endpoint_a = Interface(kwargs.get('endpoint_a_name', 'eth0'),
30
                           kwargs.get('endpoint_a_port', 1), switch_a)
31
    endpoint_b = Interface(kwargs.get('endpoint_b_name', 'eth1'),
32
                           kwargs.get('endpoint_b_port', 2), switch_b)
33
    link = Mock(spec=Link, endpoint_a=endpoint_a, endpoint_b=endpoint_b)
34
    link.as_dict.return_value = kwargs.get('link_dict',
35
                                           {'id': kwargs.get('link_id', 1)})
36
37
    link.status = kwargs.get('status', EntityStatus.DOWN)
38
39
    metadata = kwargs.get("metadata", {})
40
41
    def side_effect(key):
42
        """Mock Link get metadata."""
43
        return Mock(value=metadata.get(key))
44
45
    link.get_metadata = Mock(side_effect=side_effect)
46
47
    return link
48
49
50
def get_uni_mocked(**kwargs):
51
    """Create an uni mocked.
52
53
    Args:
54
        interface_name(str): Interface name. Defaults to "eth1".
55
        interface_port(int): Interface pror. Defaults to 1.
56
        tag_type(int): Type of a tag. Defaults to 1.
57
        tag_value(int): Value of a tag. Defaults to 81
58
        is_valid(bool): Value returned by is_valid method.
59
                        Defaults to False.
60
    """
61
    interface_name = kwargs.get("interface_name", "eth1")
62
    interface_port = kwargs.get("interface_port", 1)
63
    tag_type = kwargs.get("tag_type", 1)
64
    tag_value = kwargs.get("tag_value", 81)
65
    is_valid = kwargs.get("is_valid", False)
66
    switch = Mock(spec=Switch)
67
    switch.id = kwargs.get("switch_id", "custom_switch_id")
68
    switch.dpid = kwargs.get("switch_dpid", "custom_switch_dpid")
69
    interface = Interface(interface_name, interface_port, switch)
70
    tag = TAG(tag_type, tag_value)
71
    uni = Mock(spec=UNI, interface=interface, user_tag=tag)
72
    uni.is_valid.return_value = is_valid
73
    uni.as_dict.return_value = {
74
        "interface_id": f'switch_mock:{interface_port}',
75
        "tag": tag.as_dict()
76
    }
77
    return uni
78
79
80
class MockResponse:
81
    """Mock a requests response object.
82
83
    Just define a function and add the patch decorator to the test.
84
85
    Example:
86
    def mocked_requests_get(*args, **kwargs):
87
        return MockResponse({}, 200)
88
89
    @patch('requests.get', side_effect=mocked_requests_get)
90
    """
91
92
    def __init__(self, json_data, status_code):
93
        """Create mock response object with parameters.
94
95
        Args:
96
            json_data: JSON response content
97
            status_code: HTTP status code.
98
        """
99
        self.json_data = json_data
100
        self.status_code = status_code
101
102
    def json(self):
103
        """Return the response json data."""
104
        return self.json_data
105