Passed
Pull Request — master (#114)
by Rogerio
03:38
created

build.tests.helpers.get_paths_mock()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
"""Module to help to create tests."""
2
from unittest.mock import Mock
3
4
from kytos.core import Controller
5
from kytos.core.config import KytosConfig
6
from kytos.core.interface import TAG, UNI, Interface
7
from kytos.core.switch import Switch
8
from kytos.core.link import Link
9
from kytos.core.common import EntityStatus
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.id = kwargs.get('link_id', 1)
35
    link.status = kwargs.get('status', EntityStatus.DOWN)
36
    link.active = kwargs.get('active', False)
37
    link.as_dict.return_value = kwargs.get('link_dict',
38
                                           {'id': link.id,
39
                                            'active': link.active})
40
41
    metadata = kwargs.get("metadata", {})
42
43
    def side_effect(key):
44
        """Mock Link get metadata."""
45
        return Mock(value=metadata.get(key))
46
47
    link.get_metadata = Mock(side_effect=side_effect)
48
49
    return link
50
51
52
def get_uni_mocked(**kwargs):
53
    """Create an uni mocked.
54
55
    Args:
56
        interface_name(str): Interface name. Defaults to "eth1".
57
        interface_port(int): Interface pror. Defaults to 1.
58
        tag_type(int): Type of a tag. Defaults to 1.
59
        tag_value(int): Value of a tag. Defaults to 81
60
        is_valid(bool): Value returned by is_valid method.
61
                        Defaults to False.
62
    """
63
    interface_name = kwargs.get("interface_name", "eth1")
64
    interface_port = kwargs.get("interface_port", 1)
65
    tag_type = kwargs.get("tag_type", 1)
66
    tag_value = kwargs.get("tag_value", 81)
67
    is_valid = kwargs.get("is_valid", False)
68
    switch = Mock(spec=Switch)
69
    switch.id = kwargs.get("switch_id", "custom_switch_id")
70
    switch.dpid = kwargs.get("switch_dpid", "custom_switch_dpid")
71
    interface = Interface(interface_name, interface_port, switch)
72
    tag = TAG(tag_type, tag_value)
73
    uni = Mock(spec=UNI, interface=interface, user_tag=tag)
74
    uni.is_valid.return_value = is_valid
75
    uni.as_dict.return_value = {
76
        "interface_id": f'switch_mock:{interface_port}',
77
        "tag": tag.as_dict()
78
    }
79
    return uni
80
81
82
class MockResponse:
83
    """Mock a requests response object.
84
85
    Just define a function and add the patch decorator to the test.
86
87
    Example:
88
    def mocked_requests_get(*args, **kwargs):
89
        return MockResponse({}, 200)
90
91
    @patch('requests.get', side_effect=mocked_requests_get)
92
    """
93
94
    def __init__(self, json_data, status_code):
95
        """Create mock response object with parameters.
96
97
        Args:
98
            json_data: JSON response content
99
            status_code: HTTP status code.
100
        """
101
        self.json_data = json_data
102
        self.status_code = status_code
103
104
    def json(self):
105
        """Return the response json data."""
106
        return self.json_data
107