1
|
|
|
"""Interface tests.""" |
2
|
|
|
# pylint: disable=attribute-defined-outside-init |
3
|
1 |
|
import logging |
4
|
1 |
|
import pickle |
5
|
1 |
|
from unittest.mock import MagicMock, Mock |
6
|
|
|
|
7
|
1 |
|
import pytest |
8
|
1 |
|
from pyof.v0x04.common.port import PortFeatures |
9
|
|
|
|
10
|
1 |
|
from kytos.core.common import EntityStatus |
11
|
1 |
|
from kytos.core.exceptions import (KytosSetTagRangeError, |
12
|
|
|
KytosTagsAreNotAvailable, |
13
|
|
|
KytosTagsNotInTagRanges, |
14
|
|
|
KytosTagtypeNotSupported) |
15
|
1 |
|
from kytos.core.interface import TAG, UNI, Interface |
16
|
1 |
|
from kytos.core.switch import Switch |
17
|
|
|
|
18
|
1 |
|
logging.basicConfig(level=logging.CRITICAL) |
19
|
|
|
|
20
|
|
|
|
21
|
1 |
|
class TestTAG(): |
22
|
|
|
"""TAG tests.""" |
23
|
|
|
|
24
|
1 |
|
def setup_method(self): |
25
|
|
|
"""Create TAG object.""" |
26
|
1 |
|
self.tag = TAG('vlan', 123) |
27
|
|
|
|
28
|
1 |
|
async def test_from_dict(self): |
29
|
|
|
"""Test from_dict method.""" |
30
|
1 |
|
tag_dict = {'tag_type': 'vlan_qinq', 'value': 456} |
31
|
1 |
|
tag = self.tag.from_dict(tag_dict) |
32
|
|
|
|
33
|
1 |
|
assert tag.tag_type == 'vlan_qinq' |
34
|
1 |
|
assert tag.value == 456 |
35
|
|
|
|
36
|
1 |
|
async def test_as_dict(self): |
37
|
|
|
"""Test as_dict method.""" |
38
|
1 |
|
assert self.tag.as_dict() == {'tag_type': 'vlan', 'value': 123} |
39
|
|
|
|
40
|
1 |
|
async def test_from_json(self): |
41
|
|
|
"""Test from_json method.""" |
42
|
1 |
|
tag_json = '{"tag_type": "vlan_qinq", "value": 456}' |
43
|
1 |
|
tag = self.tag.from_json(tag_json) |
44
|
|
|
|
45
|
1 |
|
assert tag.tag_type == "vlan_qinq" |
46
|
1 |
|
assert tag.value == 456 |
47
|
|
|
|
48
|
1 |
|
async def test_as_json(self): |
49
|
|
|
"""Test as_json method.""" |
50
|
1 |
|
assert self.tag.as_json() == '{"tag_type": "vlan", "value": 123}' |
51
|
|
|
|
52
|
1 |
|
async def test__repr__(self): |
53
|
|
|
"""Test __repr__ method.""" |
54
|
1 |
|
assert repr(self.tag) == "TAG('vlan', 123)" |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
# pylint: disable=protected-access, too-many-public-methods |
58
|
1 |
|
class TestInterface(): |
59
|
|
|
"""Test Interfaces.""" |
60
|
|
|
|
61
|
1 |
|
def setup_method(self): |
62
|
|
|
"""Create interface object.""" |
63
|
1 |
|
self.iface = self._get_v0x04_iface() |
64
|
|
|
|
65
|
1 |
|
async def test_repr(self): |
66
|
|
|
"""Test repr() output.""" |
67
|
1 |
|
expected = "Interface('name', 42, Switch('dpid'))" |
68
|
1 |
|
assert repr(self.iface) == expected |
69
|
|
|
|
70
|
1 |
|
@staticmethod |
71
|
1 |
|
def _get_v0x04_iface(*args, **kwargs): |
72
|
|
|
"""Create a v0x04 interface object with optional extra arguments.""" |
73
|
1 |
|
switch = Switch('dpid') |
74
|
1 |
|
switch.connection = Mock() |
75
|
1 |
|
switch.connection.protocol.version = 0x04 |
76
|
1 |
|
switch.update_lastseen() |
77
|
1 |
|
return Interface('name', 42, switch, *args, **kwargs) |
78
|
|
|
|
79
|
1 |
|
async def test_speed_feature_none(self): |
80
|
|
|
"""When port's current features is None.""" |
81
|
1 |
|
self.iface.features = None |
82
|
1 |
|
assert self.iface.speed is None |
83
|
1 |
|
assert '' == self.iface.get_hr_speed() |
84
|
|
|
|
85
|
1 |
|
async def test_speed_feature_zero(self): |
86
|
|
|
"""When port's current features is 0. E.g. port 65534.""" |
87
|
1 |
|
self.iface.features = 0 |
88
|
1 |
|
assert self.iface.speed is None |
89
|
1 |
|
assert '' == self.iface.get_hr_speed() |
90
|
|
|
|
91
|
1 |
|
async def test_1_tera_speed(self): |
92
|
|
|
"""1Tb link.""" |
93
|
1 |
|
self.iface.features = PortFeatures.OFPPF_1TB_FD |
94
|
1 |
|
assert 10**12 / 8 == self.iface.speed |
95
|
1 |
|
assert '1 Tbps' == self.iface.get_hr_speed() |
96
|
|
|
|
97
|
1 |
|
async def test_100_giga_speed(self): |
98
|
|
|
"""100Gb link.""" |
99
|
1 |
|
self.iface.features = PortFeatures.OFPPF_100GB_FD |
100
|
1 |
|
assert 100 * 10**9 / 8 == self.iface.speed |
101
|
1 |
|
assert '100 Gbps' == self.iface.get_hr_speed() |
102
|
|
|
|
103
|
1 |
|
async def test_40_giga_speed(self): |
104
|
|
|
"""40Gb link.""" |
105
|
1 |
|
self.iface.features = PortFeatures.OFPPF_40GB_FD |
106
|
1 |
|
assert 40 * 10**9 / 8 == self.iface.speed |
107
|
1 |
|
assert '40 Gbps' == self.iface.get_hr_speed() |
108
|
|
|
|
109
|
1 |
|
async def test_10_giga_speed(self): |
110
|
|
|
"""10Gb link.""" |
111
|
1 |
|
self.iface.features = PortFeatures.OFPPF_10GB_FD |
112
|
1 |
|
assert 10 * 10**9 / 8 == self.iface.speed |
113
|
1 |
|
assert '10 Gbps' == self.iface.get_hr_speed() |
114
|
|
|
|
115
|
1 |
|
async def test_1_giga_speed(self): |
116
|
|
|
"""1Gb link.""" |
117
|
1 |
|
self.iface.features = PortFeatures.OFPPF_1GB_FD |
118
|
1 |
|
assert 10**9 / 8 == self.iface.speed |
119
|
1 |
|
assert '1 Gbps' == self.iface.get_hr_speed() |
120
|
|
|
|
121
|
1 |
|
async def test_100_mega_speed(self): |
122
|
|
|
"""100Mb link.""" |
123
|
1 |
|
self.iface.features = PortFeatures.OFPPF_100MB_FD |
124
|
1 |
|
assert 100 * 10**6 / 8 == self.iface.speed |
125
|
1 |
|
assert '100 Mbps' == self.iface.get_hr_speed() |
126
|
|
|
|
127
|
1 |
|
async def test_10_mega_speed(self): |
128
|
|
|
"""10Mb link.""" |
129
|
1 |
|
self.iface.features = PortFeatures.OFPPF_10MB_FD |
130
|
1 |
|
assert 10 * 10**6 / 8 == self.iface.speed |
131
|
1 |
|
assert '10 Mbps' == self.iface.get_hr_speed() |
132
|
|
|
|
133
|
1 |
|
async def test_speed_setter(self): |
134
|
|
|
"""Should return speed that was set and not features'.""" |
135
|
1 |
|
expected_speed = 12345 |
136
|
1 |
|
self.iface.set_custom_speed(expected_speed) |
137
|
1 |
|
actual_speed = self.iface.speed |
138
|
1 |
|
assert expected_speed == actual_speed |
139
|
|
|
|
140
|
1 |
|
async def test_speed_in_constructor(self): |
141
|
|
|
"""Custom speed should override features'.""" |
142
|
1 |
|
expected_speed = 6789 |
143
|
1 |
|
iface = self._get_v0x04_iface(speed=expected_speed) |
144
|
1 |
|
assert expected_speed == iface.speed |
145
|
|
|
|
146
|
1 |
|
async def test_speed_removing_features(self): |
147
|
|
|
"""Should return custom speed again when features becomes None.""" |
148
|
1 |
|
custom_speed = 101112 |
149
|
1 |
|
of_speed = 10 * 10**6 / 8 |
150
|
1 |
|
iface = self._get_v0x04_iface(speed=custom_speed, |
151
|
|
|
features=PortFeatures.OFPPF_10MB_FD) |
152
|
1 |
|
assert of_speed == iface.speed |
153
|
1 |
|
iface.features = None |
154
|
1 |
|
assert custom_speed == iface.speed |
155
|
|
|
|
156
|
1 |
|
async def test_interface_available_tags_tag_ranges(self): |
157
|
|
|
"""Test available_tags and tag_ranges on Interface class.""" |
158
|
1 |
|
default_available = {'vlan': [[1, 4095]]} |
159
|
1 |
|
default_tag_ranges = {'vlan': [[1, 4095]]} |
160
|
1 |
|
default_special_vlans = {'vlan': ["untagged", "any"]} |
161
|
1 |
|
default_special_tags = {'vlan': ["untagged", "any"]} |
162
|
1 |
|
assert self.iface.available_tags == default_available |
163
|
1 |
|
assert self.iface.tag_ranges == default_tag_ranges |
164
|
1 |
|
assert self.iface.special_available_tags == default_special_vlans |
165
|
1 |
|
assert self.iface.special_tags == default_special_tags |
166
|
|
|
|
167
|
1 |
|
custom_available = {'vlan': [[10, 200], [210, 4095]]} |
168
|
1 |
|
custom_tag_ranges = {'vlan': [[1, 100], [200, 4095]]} |
169
|
1 |
|
custom_special_vlans = {'vlan': ["any"]} |
170
|
1 |
|
custom_special_tags = {'vlan': ["any"]} |
171
|
1 |
|
self.iface.set_available_tags_tag_ranges( |
172
|
|
|
custom_available, custom_tag_ranges, |
173
|
|
|
custom_special_vlans, custom_special_tags |
174
|
|
|
) |
175
|
1 |
|
assert self.iface.available_tags == custom_available |
176
|
1 |
|
assert self.iface.tag_ranges == custom_tag_ranges |
177
|
1 |
|
assert self.iface.special_available_tags == custom_special_vlans |
178
|
1 |
|
assert self.iface.special_tags == custom_special_tags |
179
|
|
|
|
180
|
1 |
|
async def test_interface_is_tag_available(self): |
181
|
|
|
"""Test is_tag_available on Interface class.""" |
182
|
1 |
|
max_range = 4096 |
183
|
1 |
|
for tag in range(1, max_range): |
184
|
1 |
|
next_tag = self.iface.is_tag_available(tag) |
185
|
1 |
|
assert next_tag |
186
|
|
|
|
187
|
|
|
# test lower limit |
188
|
1 |
|
assert self.iface.is_tag_available(0) is False |
189
|
|
|
# test upper limit |
190
|
1 |
|
assert self.iface.is_tag_available(max_range) is False |
191
|
|
|
|
192
|
1 |
|
async def test_interface_use_tags(self, controller): |
193
|
|
|
"""Test all use_tags on Interface class.""" |
194
|
1 |
|
self.iface._notify_interface_tags = MagicMock() |
195
|
1 |
|
tags = [100, 200] |
196
|
|
|
# check use tag for the first time |
197
|
1 |
|
self.iface.use_tags(controller, tags) |
198
|
1 |
|
assert self.iface._notify_interface_tags.call_count == 1 |
199
|
|
|
|
200
|
|
|
# check use tag for the second time |
201
|
1 |
|
with pytest.raises(KytosTagsAreNotAvailable): |
202
|
1 |
|
self.iface.use_tags(controller, tags) |
203
|
1 |
|
assert self.iface._notify_interface_tags.call_count == 1 |
204
|
|
|
|
205
|
|
|
# check use tag after returning the tag to the pool as list |
206
|
1 |
|
self.iface.make_tags_available(controller, [tags]) |
207
|
1 |
|
self.iface.use_tags(controller, [tags]) |
208
|
1 |
|
assert self.iface._notify_interface_tags.call_count == 3 |
209
|
|
|
|
210
|
1 |
|
with pytest.raises(KytosTagsAreNotAvailable): |
211
|
1 |
|
self.iface.use_tags(controller, [tags]) |
212
|
1 |
|
assert self.iface._notify_interface_tags.call_count == 3 |
213
|
|
|
|
214
|
1 |
|
with pytest.raises(KytosTagsAreNotAvailable): |
215
|
1 |
|
self.iface.use_tags(controller, [tags], use_lock=False) |
216
|
1 |
|
assert self.iface._notify_interface_tags.call_count == 3 |
217
|
|
|
|
218
|
1 |
|
self.iface.use_tags(controller, "untagged") |
219
|
1 |
|
assert "untagged" not in self.iface.special_available_tags["vlan"] |
220
|
|
|
|
221
|
1 |
|
async def test_get_endpoint(self): |
222
|
|
|
"""Test get_endpoint method.""" |
223
|
1 |
|
endpoint = ('endpoint', 'time') |
224
|
1 |
|
self.iface.endpoints = [endpoint] |
225
|
|
|
|
226
|
1 |
|
return_endpoint = self.iface.get_endpoint('endpoint') |
227
|
|
|
|
228
|
1 |
|
assert return_endpoint == endpoint |
229
|
|
|
|
230
|
1 |
|
async def test_add_endpoint(self): |
231
|
|
|
"""Test add_endpoint method.""" |
232
|
1 |
|
self.iface.add_endpoint('endpoint') |
233
|
|
|
|
234
|
1 |
|
assert len(self.iface.endpoints) == 1 |
235
|
|
|
|
236
|
1 |
|
async def test_delete_endpoint(self): |
237
|
|
|
"""Test delete_endpoint method.""" |
238
|
1 |
|
endpoint = ('endpoint', 'time') |
239
|
1 |
|
self.iface.endpoints = [endpoint] |
240
|
|
|
|
241
|
1 |
|
self.iface.delete_endpoint('endpoint') |
242
|
|
|
|
243
|
1 |
|
assert len(self.iface.endpoints) == 0 |
244
|
|
|
|
245
|
1 |
|
async def test_update_endpoint(self): |
246
|
|
|
"""Test update_endpoint method.""" |
247
|
1 |
|
endpoint = ('endpoint', 'time') |
248
|
1 |
|
self.iface.endpoints = [endpoint] |
249
|
|
|
|
250
|
1 |
|
self.iface.update_endpoint('endpoint') |
251
|
|
|
|
252
|
1 |
|
assert len(self.iface.endpoints) == 1 |
253
|
|
|
|
254
|
1 |
|
async def test_update_link__none(self): |
255
|
|
|
"""Test update_link method when this interface is not in link |
256
|
|
|
endpoints.""" |
257
|
1 |
|
link = MagicMock() |
258
|
1 |
|
link.endpoint_a = MagicMock() |
259
|
1 |
|
link.endpoint_b = MagicMock() |
260
|
|
|
|
261
|
1 |
|
result = self.iface.update_link(link) |
262
|
|
|
|
263
|
1 |
|
assert result is False |
264
|
|
|
|
265
|
1 |
|
async def test_update_link__endpoint_a(self): |
266
|
|
|
"""Test update_link method when this interface is the endpoint a.""" |
267
|
1 |
|
interface = MagicMock() |
268
|
1 |
|
interface.link = None |
269
|
1 |
|
link = MagicMock() |
270
|
1 |
|
link.endpoint_a = self.iface |
271
|
1 |
|
link.endpoint_b = interface |
272
|
|
|
|
273
|
1 |
|
self.iface.update_link(link) |
274
|
|
|
|
275
|
1 |
|
assert self.iface.link == link |
276
|
1 |
|
assert interface.link == link |
277
|
|
|
|
278
|
1 |
|
async def test_update_link__endpoint_b(self): |
279
|
|
|
"""Test update_link method when this interface is the endpoint b.""" |
280
|
1 |
|
interface = MagicMock() |
281
|
1 |
|
interface.link = None |
282
|
1 |
|
link = MagicMock() |
283
|
1 |
|
link.endpoint_a = interface |
284
|
1 |
|
link.endpoint_b = self.iface |
285
|
|
|
|
286
|
1 |
|
self.iface.update_link(link) |
287
|
|
|
|
288
|
1 |
|
assert self.iface.link == link |
289
|
1 |
|
assert interface.link == link |
290
|
|
|
|
291
|
1 |
|
@staticmethod |
292
|
1 |
|
async def test_pickleable_id() -> None: |
293
|
|
|
"""Test to make sure the id is pickleable.""" |
294
|
1 |
|
switch = Switch("dpid1") |
295
|
1 |
|
interface = Interface("s1-eth1", 1, switch) |
296
|
1 |
|
pickled = pickle.dumps(interface.as_dict()) |
297
|
1 |
|
intf_dict = pickle.loads(pickled) |
298
|
1 |
|
assert intf_dict["id"] == interface.id |
299
|
|
|
|
300
|
1 |
|
async def test_status_funcs(self) -> None: |
301
|
|
|
"""Test status_funcs.""" |
302
|
1 |
|
self.iface.enable() |
303
|
1 |
|
self.iface.activate() |
304
|
1 |
|
assert self.iface.is_active() |
305
|
1 |
|
Interface.register_status_func( |
306
|
|
|
"some_napp_some_func", |
307
|
|
|
lambda iface: EntityStatus.DOWN |
308
|
|
|
) |
309
|
1 |
|
Interface.register_status_reason_func( |
310
|
|
|
"some_napp_some_func", |
311
|
|
|
lambda iface: {'test_status'} |
312
|
|
|
) |
313
|
1 |
|
assert self.iface.status == EntityStatus.DOWN |
314
|
1 |
|
assert self.iface.status_reason == {'test_status'} |
315
|
1 |
|
Interface.register_status_func( |
316
|
|
|
"some_napp_some_func", |
317
|
|
|
lambda iface: None |
318
|
|
|
) |
319
|
1 |
|
Interface.register_status_reason_func( |
320
|
|
|
"some_napp_some_func", |
321
|
|
|
lambda iface: set() |
322
|
|
|
) |
323
|
1 |
|
assert self.iface.status == EntityStatus.UP |
324
|
1 |
|
assert self.iface.status_reason == set() |
325
|
|
|
|
326
|
1 |
|
async def test_default_tag_values(self) -> None: |
327
|
|
|
"""Test default_tag_values property""" |
328
|
1 |
|
expected = { |
329
|
|
|
"vlan": [[1, 4095]], |
330
|
|
|
"vlan_qinq": [[1, 4095]], |
331
|
|
|
"mpls": [[1, 1048575]], |
332
|
|
|
} |
333
|
1 |
|
assert self.iface.default_tag_values == expected |
334
|
|
|
|
335
|
1 |
|
async def test_make_tags_available(self, controller) -> None: |
336
|
|
|
"""Test make_tags_available""" |
337
|
1 |
|
available = {'vlan': [[300, 3000]]} |
338
|
1 |
|
tag_ranges = {'vlan': [[20, 20], [200, 3000]]} |
339
|
1 |
|
special_available_tags = {'vlan': []} |
340
|
1 |
|
special_tags = {'vlan': ["any"]} |
341
|
1 |
|
self.iface._notify_interface_tags = MagicMock() |
342
|
1 |
|
self.iface.set_available_tags_tag_ranges( |
343
|
|
|
available, tag_ranges, |
344
|
|
|
special_available_tags, special_tags |
345
|
|
|
) |
346
|
1 |
|
assert self.iface.available_tags == available |
347
|
1 |
|
assert self.iface.tag_ranges == tag_ranges |
348
|
|
|
|
349
|
1 |
|
with pytest.raises(KytosTagsNotInTagRanges): |
350
|
1 |
|
self.iface.make_tags_available(controller, [1, 20]) |
351
|
1 |
|
assert self.iface._notify_interface_tags.call_count == 0 |
352
|
|
|
|
353
|
1 |
|
with pytest.raises(KytosTagsNotInTagRanges): |
354
|
1 |
|
self.iface.make_tags_available(controller, [[1, 20]]) |
355
|
1 |
|
assert self.iface._notify_interface_tags.call_count == 0 |
356
|
|
|
|
357
|
1 |
|
assert not self.iface.make_tags_available(controller, [250, 280]) |
358
|
1 |
|
assert self.iface._notify_interface_tags.call_count == 1 |
359
|
|
|
|
360
|
1 |
|
with pytest.raises(KytosTagsNotInTagRanges): |
361
|
1 |
|
self.iface.make_tags_available(controller, [1, 1], use_lock=False) |
362
|
|
|
|
363
|
1 |
|
assert self.iface.make_tags_available(controller, 300) == [[300, 300]] |
364
|
|
|
|
365
|
1 |
|
with pytest.raises(KytosTagsNotInTagRanges): |
366
|
1 |
|
self.iface.make_tags_available(controller, "untagged") |
367
|
|
|
|
368
|
1 |
|
assert self.iface.make_tags_available(controller, "any") is None |
369
|
1 |
|
assert "any" in self.iface.special_available_tags["vlan"] |
370
|
1 |
|
assert self.iface.make_tags_available(controller, "any") == "any" |
371
|
|
|
|
372
|
1 |
|
async def test_set_tag_ranges(self, controller) -> None: |
373
|
|
|
"""Test set_tag_ranges""" |
374
|
1 |
|
tag_ranges = [[20, 20], [200, 3000]] |
375
|
1 |
|
with pytest.raises(KytosTagtypeNotSupported): |
376
|
1 |
|
self.iface.set_tag_ranges(tag_ranges, 'vlan_qinq') |
377
|
|
|
|
378
|
1 |
|
self.iface.set_tag_ranges(tag_ranges, 'vlan') |
379
|
1 |
|
self.iface.use_tags(controller, [200, 250]) |
380
|
1 |
|
ava_expected = [[20, 20], [251, 3000]] |
381
|
1 |
|
assert self.iface.tag_ranges['vlan'] == tag_ranges |
382
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
383
|
|
|
|
384
|
1 |
|
tag_ranges = [[20, 20], [400, 1000]] |
385
|
1 |
|
with pytest.raises(KytosSetTagRangeError): |
386
|
1 |
|
self.iface.set_tag_ranges(tag_ranges, 'vlan') |
387
|
|
|
|
388
|
1 |
|
async def test_remove_tag_ranges(self) -> None: |
389
|
|
|
"""Test remove_tag_ranges""" |
390
|
1 |
|
tag_ranges = [[20, 20], [200, 3000]] |
391
|
1 |
|
self.iface.set_tag_ranges(tag_ranges, 'vlan') |
392
|
1 |
|
assert self.iface.tag_ranges['vlan'] == tag_ranges |
393
|
|
|
|
394
|
1 |
|
with pytest.raises(KytosTagtypeNotSupported): |
395
|
1 |
|
self.iface.remove_tag_ranges('vlan_qinq') |
396
|
|
|
|
397
|
1 |
|
self.iface.remove_tag_ranges('vlan') |
398
|
1 |
|
default = [[1, 4095]] |
399
|
1 |
|
assert self.iface.tag_ranges['vlan'] == default |
400
|
|
|
|
401
|
1 |
|
def test_set_special_tags(self) -> None: |
402
|
|
|
"""Test set_special_tags""" |
403
|
1 |
|
self.iface.special_available_tags["vlan"] = ["untagged"] |
404
|
1 |
|
tag_type = "error" |
405
|
1 |
|
special_tags = ["untagged", "any"] |
406
|
1 |
|
with pytest.raises(KytosTagtypeNotSupported): |
407
|
1 |
|
self.iface.set_special_tags(tag_type, special_tags) |
408
|
|
|
|
409
|
1 |
|
tag_type = "vlan" |
410
|
1 |
|
special_tags = ["untagged"] |
411
|
1 |
|
with pytest.raises(KytosSetTagRangeError): |
412
|
1 |
|
self.iface.set_special_tags(tag_type, special_tags) |
413
|
|
|
|
414
|
1 |
|
special_tags = ["any"] |
415
|
1 |
|
self.iface.set_special_tags(tag_type, special_tags) |
416
|
1 |
|
assert self.iface.special_available_tags["vlan"] == [] |
417
|
1 |
|
assert self.iface.special_tags["vlan"] == ["any"] |
418
|
|
|
|
419
|
1 |
|
async def test_remove_tags(self) -> None: |
420
|
|
|
"""Test _remove_tags""" |
421
|
1 |
|
available_tag = [[20, 20], [200, 3000]] |
422
|
1 |
|
tag_ranges = [[1, 4095]] |
423
|
1 |
|
self.iface.set_available_tags_tag_ranges( |
424
|
|
|
{'vlan': available_tag}, |
425
|
|
|
{'vlan': tag_ranges}, |
426
|
|
|
{'vlan': ["untagged", "any"]}, |
427
|
|
|
{'vlan': ["untagged", "any"]} |
428
|
|
|
) |
429
|
1 |
|
ava_expected = [[20, 20], [241, 3000]] |
430
|
1 |
|
assert self.iface._remove_tags([200, 240]) |
431
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
432
|
1 |
|
ava_expected = [[241, 3000]] |
433
|
1 |
|
assert self.iface._remove_tags([20, 20]) |
434
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
435
|
1 |
|
ava_expected = [[241, 250], [400, 3000]] |
436
|
1 |
|
assert self.iface._remove_tags([251, 399]) |
437
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
438
|
1 |
|
assert self.iface._remove_tags([200, 240]) is False |
439
|
1 |
|
ava_expected = [[241, 250], [400, 499]] |
440
|
1 |
|
assert self.iface._remove_tags([500, 3000]) |
441
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
442
|
|
|
|
443
|
1 |
|
async def test_remove_tags_empty(self) -> None: |
444
|
|
|
"""Test _remove_tags when available_tags is empty""" |
445
|
1 |
|
available_tag = [] |
446
|
1 |
|
tag_ranges = [[1, 4095]] |
447
|
1 |
|
parameters = { |
448
|
|
|
"available_tag": {'vlan': available_tag}, |
449
|
|
|
"tag_ranges": {'vlan': tag_ranges}, |
450
|
|
|
"special_available_tags": {'vlan': ["untagged", "any"]}, |
451
|
|
|
"special_tags": {'vlan': ["untagged", "any"]} |
452
|
|
|
} |
453
|
1 |
|
self.iface.set_available_tags_tag_ranges(**parameters) |
454
|
1 |
|
assert self.iface._remove_tags([4, 6]) is False |
455
|
1 |
|
assert self.iface.available_tags['vlan'] == [] |
456
|
|
|
|
457
|
1 |
|
async def test_add_tags(self) -> None: |
458
|
|
|
"""Test _add_tags""" |
459
|
1 |
|
available_tag = [[7, 10], [20, 30]] |
460
|
1 |
|
tag_ranges = [[1, 4095]] |
461
|
1 |
|
parameters = { |
462
|
|
|
"available_tag": {'vlan': available_tag}, |
463
|
|
|
"tag_ranges": {'vlan': tag_ranges}, |
464
|
|
|
"special_available_tags": {'vlan': ["untagged", "any"]}, |
465
|
|
|
"special_tags": {'vlan': ["untagged", "any"]} |
466
|
|
|
} |
467
|
1 |
|
self.iface.set_available_tags_tag_ranges(**parameters) |
468
|
1 |
|
ava_expected = [[4, 10], [20, 30]] |
469
|
1 |
|
assert self.iface._add_tags([4, 6]) |
470
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
471
|
|
|
|
472
|
1 |
|
ava_expected = [[1, 2], [4, 10], [20, 30]] |
473
|
1 |
|
assert self.iface._add_tags([1, 2]) |
474
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
475
|
|
|
|
476
|
1 |
|
ava_expected = [[1, 2], [4, 10], [20, 35]] |
477
|
1 |
|
assert self.iface._add_tags([31, 35]) |
478
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
479
|
|
|
|
480
|
1 |
|
ava_expected = [[1, 2], [4, 10], [20, 35], [90, 90]] |
481
|
1 |
|
assert self.iface._add_tags([90, 90]) |
482
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
483
|
|
|
|
484
|
1 |
|
ava_expected = [[1, 2], [4, 10], [20, 90]] |
485
|
1 |
|
assert self.iface._add_tags([36, 89]) |
486
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
487
|
|
|
|
488
|
1 |
|
ava_expected = [[1, 2], [4, 12], [20, 90]] |
489
|
1 |
|
assert self.iface._add_tags([11, 12]) |
490
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
491
|
|
|
|
492
|
1 |
|
ava_expected = [[1, 2], [4, 12], [17, 90]] |
493
|
1 |
|
assert self.iface._add_tags([17, 19]) |
494
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
495
|
|
|
|
496
|
1 |
|
ava_expected = [[1, 2], [4, 12], [15, 15], [17, 90]] |
497
|
1 |
|
assert self.iface._add_tags([15, 15]) |
498
|
1 |
|
assert self.iface.available_tags['vlan'] == ava_expected |
499
|
|
|
|
500
|
1 |
|
assert self.iface._add_tags([35, 98]) is False |
501
|
|
|
|
502
|
1 |
|
async def test_add_tags_empty(self) -> None: |
503
|
|
|
"""Test _add_tags when available_tags is empty""" |
504
|
1 |
|
available_tag = [] |
505
|
1 |
|
tag_ranges = [[1, 4095]] |
506
|
1 |
|
parameters = { |
507
|
|
|
"available_tag": {'vlan': available_tag}, |
508
|
|
|
"tag_ranges": {'vlan': tag_ranges}, |
509
|
|
|
"special_available_tags": {'vlan': ["untagged", "any"]}, |
510
|
|
|
"special_tags": {'vlan': ["untagged", "any"]} |
511
|
|
|
} |
512
|
1 |
|
self.iface.set_available_tags_tag_ranges(**parameters) |
513
|
1 |
|
assert self.iface._add_tags([4, 6]) |
514
|
1 |
|
assert self.iface.available_tags['vlan'] == [[4, 6]] |
515
|
|
|
|
516
|
1 |
|
async def test_notify_interface_tags(self, controller) -> None: |
517
|
|
|
"""Test _notify_interface_tags""" |
518
|
1 |
|
name = "kytos/core.interface_tags" |
519
|
1 |
|
content = {"interface": self.iface} |
520
|
1 |
|
self.iface._notify_interface_tags(controller) |
521
|
1 |
|
event = controller.buffers.app.put.call_args[0][0] |
522
|
1 |
|
assert event.name == name |
523
|
1 |
|
assert event.content == content |
524
|
|
|
|
525
|
1 |
|
def test_all_tags_available(self) -> None: |
526
|
|
|
"""Test all_tags_available""" |
527
|
1 |
|
self.iface.available_tags = {"vlan": [[1, 1000]]} |
528
|
1 |
|
self.iface.tag_ranges = {"vlan": [[1, 20], [22, 1000]]} |
529
|
1 |
|
self.iface.special_available_tags = {"vlan": []} |
530
|
1 |
|
self.iface.special_tags = {"vlan": ["any"]} |
531
|
1 |
|
assert self.iface.all_tags_available() is False |
532
|
|
|
|
533
|
1 |
|
self.iface.tag_ranges = {"vlan": [[1, 1000]]} |
534
|
1 |
|
self.iface.special_available_tags = {"vlan": ["any"]} |
535
|
1 |
|
assert self.iface.all_tags_available() |
536
|
|
|
|
537
|
|
|
|
538
|
1 |
|
class TestUNI(): |
539
|
|
|
"""UNI tests.""" |
540
|
|
|
|
541
|
1 |
|
def setup_method(self): |
542
|
|
|
"""Create UNI object.""" |
543
|
1 |
|
switch = MagicMock() |
544
|
1 |
|
switch.id = '00:00:00:00:00:00:00:01' |
545
|
1 |
|
interface = Interface('name', 1, switch) |
546
|
1 |
|
user_tag = TAG('vlan', 123) |
547
|
1 |
|
self.uni = UNI(interface, user_tag) |
548
|
|
|
|
549
|
1 |
|
async def test__eq__(self): |
550
|
|
|
"""Test __eq__ method.""" |
551
|
1 |
|
user_tag = TAG('vlan', 456) |
552
|
1 |
|
interface = Interface('name', 2, MagicMock()) |
553
|
1 |
|
other = UNI(interface, user_tag) |
554
|
|
|
|
555
|
1 |
|
assert (self.uni == other) is False |
556
|
|
|
|
557
|
1 |
|
async def test_is_valid(self): |
558
|
|
|
"""Test is_valid method for a valid, invalid and none tag.""" |
559
|
1 |
|
assert self.uni.is_valid() |
560
|
|
|
|
561
|
1 |
|
with pytest.raises(ValueError): |
562
|
1 |
|
TAG("some_type", 123) |
563
|
|
|
|
564
|
1 |
|
self.uni.user_tag = None |
565
|
1 |
|
assert self.uni.is_valid() |
566
|
|
|
|
567
|
1 |
|
async def test_is_reserved_valid_tag(self): |
568
|
|
|
"""Test _is_reserved_valid_tag method for string TAG.""" |
569
|
1 |
|
self.uni.user_tag = TAG("vlan", "untagged") |
570
|
1 |
|
assert self.uni.is_valid() |
571
|
|
|
|
572
|
1 |
|
self.uni.user_tag = TAG("vlan", "any") |
573
|
1 |
|
assert self.uni.is_valid() |
574
|
|
|
|
575
|
1 |
|
self.uni.user_tag = TAG("vlan", "2/4094") |
576
|
1 |
|
assert self.uni.is_valid() is False |
577
|
|
|
|
578
|
1 |
|
async def test_as_dict(self): |
579
|
|
|
"""Test as_dict method.""" |
580
|
1 |
|
expected_dict = {'interface_id': '00:00:00:00:00:00:00:01:1', |
581
|
|
|
'tag': {'tag_type': 'vlan', 'value': 123}} |
582
|
1 |
|
assert self.uni.as_dict() == expected_dict |
583
|
|
|
|
584
|
1 |
|
async def test_as_json(self): |
585
|
|
|
"""Test as_json method.""" |
586
|
1 |
|
expected_json = '{"interface_id": "00:00:00:00:00:00:00:01:1", ' + \ |
587
|
|
|
'"tag": {"tag_type": "vlan", "value": 123}}' |
588
|
|
|
assert self.uni.as_json() == expected_json |
589
|
|
|
|