1
|
|
|
"""Test kytos.core.switch module.""" |
2
|
|
|
import asyncio |
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import Mock |
5
|
|
|
|
6
|
|
|
from kytos.core import Controller |
7
|
|
|
from kytos.core.config import KytosConfig |
8
|
|
|
from kytos.core.interface import Interface |
9
|
|
|
from kytos.core.switch import Switch |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TestSwitch(TestCase): |
13
|
|
|
"""Switch tests.""" |
14
|
|
|
|
15
|
|
|
def setUp(self): |
16
|
|
|
"""Instantiate a controller.""" |
17
|
|
|
|
18
|
|
|
self.loop = asyncio.new_event_loop() |
19
|
|
|
asyncio.set_event_loop(None) |
20
|
|
|
|
21
|
|
|
self.options = KytosConfig().options['daemon'] |
22
|
|
|
self.controller = Controller(self.options, loop=self.loop) |
23
|
|
|
self.controller.log = Mock() |
24
|
|
|
|
25
|
|
|
def test_repr(self): |
26
|
|
|
"""Test repr() output.""" |
27
|
|
|
switch = Switch('some-dpid') |
28
|
|
|
self.assertEqual(repr(switch), "Switch('some-dpid')") |
29
|
|
|
|
30
|
|
|
def tearDown(self): |
31
|
|
|
"""TearDown.""" |
32
|
|
|
self.loop.close() |
33
|
|
|
|
34
|
|
|
def test_switch_vlan_pool_default(self): |
35
|
|
|
"""Test default vlan_pool value.""" |
36
|
|
|
self.assertEqual(self.options.vlan_pool, '{}') |
37
|
|
|
|
38
|
|
|
def test_switch_vlan_pool_options(self): |
39
|
|
|
"""Test switch with the example from kytos.conf.""" |
40
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
41
|
|
|
vlan_pool_json = '{"00:00:00:00:00:00:00:01": ' \ |
42
|
|
|
+ '{"1": [[1, 2], [5, 10]], "4": [[3, 4]]}}' |
43
|
|
|
switch = Switch(dpid) |
44
|
|
|
self.controller.switches[dpid] = switch |
45
|
|
|
self.options.vlan_pool = vlan_pool_json |
46
|
|
|
switch.connection = Mock() |
47
|
|
|
switch.connection.protocol.version = 0x04 |
48
|
|
|
self.controller.get_switch_or_create(dpid, switch.connection) |
49
|
|
|
|
50
|
|
|
port_id = 1 |
51
|
|
|
intf = self.controller.switches[dpid].interfaces[port_id] |
52
|
|
|
tag_values = [tag.value for tag in intf.available_tags] |
53
|
|
|
self.assertEqual(tag_values, [1, 5, 6, 7, 8, 9]) |
54
|
|
|
|
55
|
|
|
port_id = 4 |
56
|
|
|
intf = self.controller.switches[dpid].interfaces[port_id] |
57
|
|
|
tag_values = [tag.value for tag in intf.available_tags] |
58
|
|
|
self.assertEqual(tag_values, [3]) |
59
|
|
|
|
60
|
|
|
# this port number doesn't exist yet. |
61
|
|
|
port_7 = 7 |
62
|
|
|
intf = Interface("test", port_7, switch) |
63
|
|
|
# no attr filters, so should associate as it is |
64
|
|
|
self.controller.switches[dpid].update_interface(intf) |
65
|
|
|
intf_obj = self.controller.switches[dpid].interfaces[port_7] |
66
|
|
|
self.assertEqual(intf_obj, intf) |
67
|
|
|
# assert default vlan_pool range (1, 4096) |
68
|
|
|
tag_values = [tag.value for tag in intf_obj.available_tags] |
69
|
|
|
self.assertEqual(tag_values, list(range(1, 4096))) |
|
|
|
|
70
|
|
|
|