Test Failed
Push — master ( 0d7af6...292dca )
by Beraldo
04:07 queued 01:46
created

build.tests.helpers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 6

4 Functions

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

2 Methods

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