1
|
|
|
"""Module to help to create tests.""" |
2
|
|
|
import sys |
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import Mock, patch |
5
|
|
|
|
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_link_mocked(**kwargs): |
13
|
|
|
"""Return a link mocked. |
14
|
|
|
|
15
|
|
|
Args: |
16
|
|
|
link_dict: Python dict returned after call link.as_dict() |
17
|
|
|
""" |
18
|
|
|
switch = Mock(spec=Switch) |
19
|
|
|
endpoint_a = Interface(kwargs.get('endpoint_a_name', 'eth0'), |
20
|
|
|
kwargs.get('endpoint_a_port', 1), switch) |
21
|
|
|
endpoint_b = Interface(kwargs.get('endpoint_b_name', 'eth1'), |
22
|
|
|
kwargs.get('endpoint_b_port', 2), switch) |
23
|
|
|
link = Mock(spec=Link, endpoint_a=endpoint_a, endpoint_b=endpoint_b) |
24
|
|
|
link.as_dict.return_value = kwargs.get('link_dict', {"id": "link_id"}) |
25
|
|
|
link.status = kwargs.get('status', EntityStatus.DOWN) |
26
|
|
|
|
27
|
|
|
metadata = kwargs.get("metadata", {}) |
28
|
|
|
|
29
|
|
|
def side_effect(key): |
30
|
|
|
return Mock(value=metadata.get(key)) |
31
|
|
|
|
32
|
|
|
link.get_metadata = Mock(side_effect=side_effect) |
33
|
|
|
|
34
|
|
|
return link |
35
|
|
|
|
36
|
|
|
def get_uni_mocked(**kwargs): |
37
|
|
|
"""Create an uni mocked. |
38
|
|
|
|
39
|
|
|
Args: |
40
|
|
|
interface_name(str): Interface name. Defaults to "eth1". |
41
|
|
|
interface_port(int): Interface pror. Defaults to 1. |
42
|
|
|
tag_type(int): Type of a tag. Defaults to 1. |
43
|
|
|
tag_value(int): Value of a tag. Defaults to 81 |
44
|
|
|
is_valid(bool): Value returned by is_valid method. |
45
|
|
|
Defaults to False. |
46
|
|
|
""" |
47
|
|
|
interface_name = kwargs.get("interface_name", "eth1") |
48
|
|
|
interface_port = kwargs.get("interface_port", 1) |
49
|
|
|
tag_type = kwargs.get("tag_type", 1) |
50
|
|
|
tag_value = kwargs.get("tag_value", 81) |
51
|
|
|
is_valid = kwargs.get("is_valid", False) |
52
|
|
|
switch = Mock(spec=Switch) |
53
|
|
|
switch.id = kwargs.get("switch_id", "custom_switch_id") |
54
|
|
|
interface = Interface(interface_name, interface_port, switch) |
55
|
|
|
tag = TAG(tag_type, tag_value) |
56
|
|
|
uni = Mock(spec=UNI, interface=interface, user_tag=tag) |
57
|
|
|
uni.is_valid.return_value = is_valid |
58
|
|
|
uni.as_dict.return_value = { |
59
|
|
|
"interface_id": f'switch_mock:{interface_port}', |
60
|
|
|
"tag": tag.as_dict() |
61
|
|
|
} |
62
|
|
|
return uni |
63
|
|
|
|