|
1
|
|
|
"""Test utils.""" |
|
2
|
1 |
|
import pytest |
|
3
|
1 |
|
from napps.kytos.telemetry_int.utils import ( |
|
4
|
|
|
get_evc_unis, |
|
5
|
|
|
get_id_from_cookie, |
|
6
|
|
|
get_new_cookie, |
|
7
|
|
|
has_int_enabled, |
|
8
|
|
|
is_intra_switch_evc, |
|
9
|
|
|
set_instructions_from_actions, |
|
10
|
|
|
set_new_cookie, |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
1 |
|
@pytest.mark.parametrize( |
|
15
|
|
|
"flow,expected", |
|
16
|
|
|
[ |
|
17
|
|
|
( |
|
18
|
|
|
{ |
|
19
|
|
|
"flow": { |
|
20
|
|
|
"priority": 100, |
|
21
|
|
|
"match": {"in_port": 100}, |
|
22
|
|
|
"actions": [{"action_type": "output", "port": 1}], |
|
23
|
|
|
} |
|
24
|
|
|
}, |
|
25
|
|
|
{ |
|
26
|
|
|
"flow": { |
|
27
|
|
|
"priority": 100, |
|
28
|
|
|
"match": {"in_port": 100}, |
|
29
|
|
|
"instructions": [ |
|
30
|
|
|
{ |
|
31
|
|
|
"instruction_type": "apply_actions", |
|
32
|
|
|
"actions": [{"action_type": "output", "port": 1}], |
|
33
|
|
|
} |
|
34
|
|
|
], |
|
35
|
|
|
} |
|
36
|
|
|
}, |
|
37
|
|
|
), |
|
38
|
|
|
( |
|
39
|
|
|
{ |
|
40
|
|
|
"flow": { |
|
41
|
|
|
"priority": 100, |
|
42
|
|
|
"match": {"in_port": 100}, |
|
43
|
|
|
"instructions": [ |
|
44
|
|
|
{ |
|
45
|
|
|
"instruction_type": "apply_actions", |
|
46
|
|
|
"actions": [{"action_type": "output", "port": 1}], |
|
47
|
|
|
} |
|
48
|
|
|
], |
|
49
|
|
|
} |
|
50
|
|
|
}, |
|
51
|
|
|
{ |
|
52
|
|
|
"flow": { |
|
53
|
|
|
"priority": 100, |
|
54
|
|
|
"match": {"in_port": 100}, |
|
55
|
|
|
"instructions": [ |
|
56
|
|
|
{ |
|
57
|
|
|
"instruction_type": "apply_actions", |
|
58
|
|
|
"actions": [{"action_type": "output", "port": 1}], |
|
59
|
|
|
} |
|
60
|
|
|
], |
|
61
|
|
|
} |
|
62
|
|
|
}, |
|
63
|
|
|
), |
|
64
|
|
|
], |
|
65
|
|
|
) |
|
66
|
1 |
|
def test_instructions_from_actions(flow, expected) -> None: |
|
67
|
|
|
"""Test instructions from actions.""" |
|
68
|
1 |
|
assert set_instructions_from_actions(flow) == expected |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
1 |
|
@pytest.mark.parametrize( |
|
72
|
|
|
"cookie,expected", |
|
73
|
|
|
[ |
|
74
|
|
|
(0xAA3766C105686749, 0xA83766C105686749), |
|
75
|
|
|
(0xAACBEE9338673946, 0xA8CBEE9338673946), |
|
76
|
|
|
], |
|
77
|
|
|
) |
|
78
|
1 |
|
def test_get_new_cookie(cookie, expected) -> None: |
|
79
|
|
|
"""test get_new_cookie.""" |
|
80
|
1 |
|
assert get_new_cookie(cookie) == expected |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
1 |
|
@pytest.mark.parametrize( |
|
84
|
|
|
"cookie,expected_evc_id", |
|
85
|
|
|
[ |
|
86
|
|
|
(0xAA3766C105686749, "3766c105686749"), |
|
87
|
|
|
(0xAACBEE9338673946, "cbee9338673946"), |
|
88
|
|
|
], |
|
89
|
|
|
) |
|
90
|
1 |
|
def test_get_id_from_cookie(cookie, expected_evc_id) -> None: |
|
91
|
|
|
"""test get_id_from_cookie.""" |
|
92
|
1 |
|
assert get_id_from_cookie(cookie) == expected_evc_id |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
1 |
|
def test_set_new_cookie() -> None: |
|
96
|
|
|
"""Test set_new_cookie.""" |
|
97
|
1 |
|
flow = {"flow": {"cookie": 0xAA3766C105686749}} |
|
98
|
1 |
|
set_new_cookie(flow) |
|
99
|
1 |
|
assert flow["flow"]["cookie"] == 0xA83766C105686749 |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
1 |
|
@pytest.mark.parametrize( |
|
103
|
|
|
"evc_dict,expected", |
|
104
|
|
|
[ |
|
105
|
|
|
({"metadata": {"telemetry": {"enabled": True}}}, True), |
|
106
|
|
|
({"metadata": {"telemetry": {"enabled": False}}}, False), |
|
107
|
|
|
({"metadata": {}}, False), |
|
108
|
|
|
], |
|
109
|
|
|
) |
|
110
|
1 |
|
def test_has_int_enabled(evc_dict, expected) -> None: |
|
111
|
|
|
"""test has_int_enabled.""" |
|
112
|
1 |
|
assert has_int_enabled(evc_dict) == expected |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
1 |
|
def test_get_evc_unis() -> None: |
|
116
|
|
|
"""test get_evc_unis.""" |
|
117
|
1 |
|
evc = { |
|
118
|
|
|
"uni_a": { |
|
119
|
|
|
"tag": {"tag_type": 1, "value": 200}, |
|
120
|
|
|
"interface_id": "00:00:00:00:00:00:00:01:1", |
|
121
|
|
|
}, |
|
122
|
|
|
"uni_z": { |
|
123
|
|
|
"tag": {"tag_type": 1, "value": 200}, |
|
124
|
|
|
"interface_id": "00:00:00:00:00:00:00:01:2", |
|
125
|
|
|
}, |
|
126
|
|
|
} |
|
127
|
1 |
|
uni_a, uni_z = get_evc_unis(evc) |
|
128
|
1 |
|
assert uni_a["interface_id"] == evc["uni_a"]["interface_id"] |
|
129
|
1 |
|
assert uni_a["port_number"] == 1 |
|
130
|
1 |
|
assert uni_a["switch"] == "00:00:00:00:00:00:00:01" |
|
131
|
|
|
|
|
132
|
1 |
|
assert uni_z["interface_id"] == evc["uni_z"]["interface_id"] |
|
133
|
1 |
|
assert uni_z["port_number"] == 2 |
|
134
|
1 |
|
assert uni_z["switch"] == "00:00:00:00:00:00:00:01" |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
1 |
|
def test_is_intra_switch_evc() -> None: |
|
138
|
|
|
"""test is_instra_switch_evc.""" |
|
139
|
1 |
|
evc = { |
|
140
|
|
|
"uni_a": { |
|
141
|
|
|
"tag": {"tag_type": 1, "value": 200}, |
|
142
|
|
|
"interface_id": "00:00:00:00:00:00:00:01:1", |
|
143
|
|
|
}, |
|
144
|
|
|
"uni_z": { |
|
145
|
|
|
"tag": {"tag_type": 1, "value": 200}, |
|
146
|
|
|
"interface_id": "00:00:00:00:00:00:00:01:2", |
|
147
|
|
|
}, |
|
148
|
|
|
} |
|
149
|
1 |
|
assert is_intra_switch_evc(evc) |
|
150
|
1 |
|
evc["uni_a"]["interface_id"] = "00:00:00:00:00:00:00:02:1" |
|
151
|
|
|
assert not is_intra_switch_evc(evc) |
|
152
|
|
|
|