1
|
|
|
"""Module to help to create tests.""" |
2
|
|
|
from unittest.mock import MagicMock, Mock |
3
|
|
|
|
4
|
|
|
from pyof.utils import unpack |
5
|
|
|
from kytos.core import Controller |
6
|
|
|
from kytos.core.config import KytosConfig |
7
|
|
|
from kytos.core.connection import Connection, ConnectionState |
8
|
|
|
from kytos.core.interface import Interface |
9
|
|
|
from kytos.core.switch import Switch |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def get_controller_mock(): |
13
|
|
|
"""Return a controller mock.""" |
14
|
|
|
options = KytosConfig().options['daemon'] |
15
|
|
|
controller = Controller(options) |
16
|
|
|
controller.log = Mock() |
17
|
|
|
return controller |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def get_interface_mock(interface_name, port, *args, **kwargs): |
21
|
|
|
"""Return a interface mock.""" |
22
|
|
|
switch = get_switch_mock(0x04) |
23
|
|
|
switch.connection = Mock() |
24
|
|
|
iface = Interface(interface_name, port, switch, *args, **kwargs) |
25
|
|
|
return iface |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def get_switch_mock(dpid="00:00:00:00:00:00:00:01"): |
29
|
|
|
"""Return a switch mock.""" |
30
|
|
|
return Switch(dpid) |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def get_connection_mock(of_version, target_switch, state=ConnectionState.NEW): |
34
|
|
|
"""Return a connection mock.""" |
35
|
|
|
connection = Connection(Mock(), Mock(), Mock()) |
36
|
|
|
connection.switch = target_switch |
37
|
|
|
connection.state = state |
38
|
|
|
connection.protocol.version = of_version |
39
|
|
|
connection.protocol.unpack = unpack |
40
|
|
|
return connection |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def get_kytos_event_mock(**kwargs): |
44
|
|
|
"""Return a kytos event mock.""" |
45
|
|
|
destination = kwargs.get('destination') |
46
|
|
|
message = kwargs.get('message') |
47
|
|
|
source = kwargs.get('source') |
48
|
|
|
|
49
|
|
|
event = MagicMock() |
50
|
|
|
event.source = source |
51
|
|
|
event.content = {'destination': destination, |
52
|
|
|
'message': message} |
53
|
|
|
return event |
54
|
|
|
|