1
|
|
|
"""Test Flow serializer for OF 1.0 methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch |
4
|
|
|
|
5
|
|
|
from napps.kytos.flow_manager.serializers.v0x01 import FlowSerializer10 |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestFlowSerializer10(TestCase): |
9
|
|
|
"""Test the FlowSerializer10 class.""" |
10
|
|
|
|
11
|
|
|
def setUp(self): |
12
|
|
|
"""Execute steps before each tests.""" |
13
|
|
|
self.napp = FlowSerializer10() |
14
|
|
|
|
15
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x01.FlowSerializer10.' |
16
|
|
|
'_actions_from_list') |
17
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x01.FlowSerializer10.' |
18
|
|
|
'_update_match') |
19
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x01.FlowMod') |
20
|
|
|
def test_from_dict(self, *args): |
21
|
|
|
"""Test from_dict method.""" |
22
|
|
|
(mock_flow_mod, mock_update_match, mock_actions_from_list) = args |
23
|
|
|
flow_mod = MagicMock() |
24
|
|
|
mock_flow_mod.return_value = flow_mod |
25
|
|
|
mock_actions_from_list.return_value = 'return_actions' |
26
|
|
|
|
27
|
|
|
self.napp.flow_attributes = {'any'} |
28
|
|
|
dictionary = {'any': 'any_data', |
29
|
|
|
'match': 'match_data', |
30
|
|
|
'actions': 'actions_data'} |
31
|
|
|
return_flow_mod = self.napp.from_dict(dictionary) |
32
|
|
|
|
33
|
|
|
self.assertEqual(return_flow_mod.any, 'any_data') |
34
|
|
|
mock_update_match.assert_called_with(return_flow_mod.match, |
35
|
|
|
'match_data') |
36
|
|
|
mock_actions_from_list.assert_called_with('actions_data') |
37
|
|
|
return_flow_mod.actions.extend.assert_called_with('return_actions') |
38
|
|
|
|
39
|
|
|
def test_update_match(self): |
40
|
|
|
"""Test _update_match method.""" |
41
|
|
|
match = MagicMock() |
42
|
|
|
dictionary = {'attr': 'data'} |
43
|
|
|
self.napp.match_attributes = {'attr'} |
44
|
|
|
|
45
|
|
|
self.napp._update_match(match, dictionary) |
46
|
|
|
|
47
|
|
|
self.assertEqual(match.attr, 'data') |
48
|
|
|
|
49
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x01.ActionOutput') |
50
|
|
|
@patch('napps.kytos.flow_manager.serializers.v0x01.ActionVlanVid') |
51
|
|
|
def test_actions_from_list(self, *args): |
52
|
|
|
"""Test _actions_from_list method.""" |
53
|
|
|
(mock_action_vlan_vid, mock_action_output) = args |
54
|
|
|
mock_action_vlan_vid.return_value = 'action_vlan_vid' |
55
|
|
|
mock_action_output.side_effect = ['action_output_controller', |
56
|
|
|
'action_output_any'] |
57
|
|
|
actions = [{'action_type': 'set_vlan', 'vlan_id': 'id'}, |
58
|
|
|
{'action_type': 'output', 'port': 'controller'}, |
59
|
|
|
{'action_type': 'output', 'port': 'any'}, |
60
|
|
|
{'action_type': 'any'}] |
61
|
|
|
|
62
|
|
|
return_actions = self.napp._actions_from_list(actions) |
63
|
|
|
|
64
|
|
|
expected_actions = ['action_vlan_vid', 'action_output_controller', |
65
|
|
|
'action_output_any'] |
66
|
|
|
self.assertEqual(return_actions, expected_actions) |
67
|
|
|
|
68
|
|
|
def test_to_dict(self): |
69
|
|
|
"""Test to_dict method.""" |
70
|
|
|
action_1 = MagicMock() |
71
|
|
|
action_1.action_type = 1 |
72
|
|
|
action_1.vlan_id.value = 'id' |
73
|
|
|
|
74
|
|
|
action_2 = MagicMock() |
75
|
|
|
action_2.action_type = 0 |
76
|
|
|
action_2.port = 0xfffd |
77
|
|
|
|
78
|
|
|
action_3 = MagicMock() |
79
|
|
|
action_3.action_type = 0 |
80
|
|
|
action_3.port.value = 'port' |
81
|
|
|
|
82
|
|
|
flow_stats = MagicMock() |
83
|
|
|
flow_stats.actions = [action_1, action_2, action_3] |
84
|
|
|
flow_stats.flow_attr = MagicMock() |
85
|
|
|
flow_stats.match.match_attr = MagicMock() |
86
|
|
|
self.napp.flow_attributes = {'flow_attr'} |
87
|
|
|
self.napp.match_attributes = {'match_attr'} |
88
|
|
|
|
89
|
|
|
flow_dict = self.napp.to_dict(flow_stats) |
90
|
|
|
|
91
|
|
|
expected = {'flow_attr': flow_stats.flow_attr.value, |
92
|
|
|
'match': {'match_attr': flow_stats.match.match_attr.value}, |
93
|
|
|
'actions': [{'action_type': 'set_vlan', 'vlan_id': 'id'}, |
94
|
|
|
{'action_type': 'output', |
95
|
|
|
'port': 'controller'}, |
96
|
|
|
{'action_type': 'output', 'port': 'port'}]} |
97
|
|
|
|
98
|
|
|
self.assertEqual(flow_dict, expected) |
99
|
|
|
|