TestFlow.test_flow_mod()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 16
rs 9.65
c 0
b 0
f 0
cc 3
nop 2
1
"""Tests for high-level Flow of OpenFlow 1.0 and 1.3."""
2
from unittest import TestCase
3
from unittest.mock import MagicMock, patch
4
5
from kytos.lib.helpers import get_connection_mock, get_switch_mock
6
from napps.kytos.of_core.v0x01.flow import Flow as Flow01
7
from napps.kytos.of_core.v0x04.flow import Flow as Flow04
8
9
10
class TestFlowFactory(TestCase):
11
    """Test the FlowFactory class."""
12
13
    def setUp(self):
14
        """Execute steps before each tests.
15
        Set the server_name_url from kytos/of_core
16
        """
17
        self.switch_v0x01 = get_switch_mock("00:00:00:00:00:00:00:01", 0x01)
18
        self.switch_v0x04 = get_switch_mock("00:00:00:00:00:00:00:02", 0x04)
19
        self.switch_v0x01.connection = get_connection_mock(
20
            0x01, get_switch_mock("00:00:00:00:00:00:00:03"))
21
        self.switch_v0x04.connection = get_connection_mock(
22
            0x04, get_switch_mock("00:00:00:00:00:00:00:04"))
23
24
        patch('kytos.core.helpers.run_on_thread', lambda x: x).start()
25
        # pylint: disable=import-outside-toplevel
26
        from napps.kytos.of_core.flow import FlowFactory
27
        self.addCleanup(patch.stopall)
28
29
        self.napp = FlowFactory()
30
31
    @patch('napps.kytos.of_core.flow.v0x01')
32
    @patch('napps.kytos.of_core.flow.v0x04')
33
    def test_from_of_flow_stats(self, *args):
34
        """Test from_of_flow_stats."""
35
        (mock_flow_v0x04, mock_flow_v0x01) = args
36
        mock_stats = MagicMock()
37
38
        self.napp.from_of_flow_stats(mock_stats, self.switch_v0x01)
39
        mock_flow_v0x01.flow.Flow.from_of_flow_stats.assert_called()
40
41
        self.napp.from_of_flow_stats(mock_stats, self.switch_v0x04)
42
        mock_flow_v0x04.flow.Flow.from_of_flow_stats.assert_called()
43
44
45
class TestFlow(TestCase):
46
    """Test OF flow abstraction."""
47
48
    mock_switch = get_switch_mock("00:00:00:00:00:00:00:01")
49
    mock_switch.id = "00:00:00:00:00:00:00:01"
50
    expected = {'switch': mock_switch.id,
51
                'table_id': 1,
52
                'match': {
53
                    'dl_src': '11:22:33:44:55:66'
54
                },
55
                'priority': 2,
56
                'idle_timeout': 3,
57
                'hard_timeout': 4,
58
                'cookie': 5,
59
                'actions': [
60
                    {'action_type': 'set_vlan',
61
                     'vlan_id': 6}],
62
                'stats': {}}
63
64
    @patch('napps.kytos.of_core.flow.v0x01')
65
    @patch('napps.kytos.of_core.flow.v0x04')
66
    @patch('napps.kytos.of_core.flow.json.dumps')
67
    def test_flow_mod(self, *args):
68
        """Convert a dict to flow and vice-versa."""
69
        (mock_json, _, _) = args
70
        dpid = "00:00:00:00:00:00:00:01"
71
        mock_json.return_value = str(self.expected)
72
        for flow_class, version in [(Flow04, 0x01), (Flow04, 0x04)]:
73
            with self.subTest(flow_class=flow_class):
74
                mock_switch = get_switch_mock(dpid, version)
75
                mock_switch.id = dpid
76
                flow = flow_class.from_dict(self.expected, mock_switch)
77
                actual = flow.as_dict()
78
                del actual['id']
79
                self.assertDictEqual(self.expected, actual)
80
81
    @patch('napps.kytos.of_core.flow.FlowBase._as_of_flow_mod')
82
    def test_of_flow_mod(self, mock_flow_mod):
83
        """Test convertion from Flow to OFFlow."""
84
85
        for flow_class in Flow01, Flow04:
86
            with self.subTest(flow_class=flow_class):
87
                flow = flow_class.from_dict(self.expected, self.mock_switch)
88
                flow.as_of_add_flow_mod()
89
                mock_flow_mod.assert_called()
90
91
                flow.as_of_delete_flow_mod()
92
                mock_flow_mod.assert_called()
93
94
    # pylint: disable = protected-access
95
    def test_as_of_flow_mod(self):
96
        """Test _as_of_flow_mod."""
97
        mock_command = MagicMock()
98
        for flow_class in Flow01, Flow04:
99
            with self.subTest(flow_class=flow_class):
100
                flow_mod = flow_class.from_dict(self.expected,
101
                                                self.mock_switch)
102
                response = flow_mod._as_of_flow_mod(mock_command)
103
                self.assertEqual(response.cookie, self.expected['cookie'])
104
                self.assertEqual(response.idle_timeout,
105
                                 self.expected['idle_timeout'])
106
                self.assertEqual(response.hard_timeout,
107
                                 self.expected['hard_timeout'])
108
109
110
class TestFlowBase(TestCase):
111
    """Test FlowBase Class."""
112
113
    def test__eq__success_with_equal_flows(self):
114
        """Test success case to __eq__ override with equal flows."""
115
        mock_switch = get_switch_mock("00:00:00:00:00:00:00:01")
116
117
        flow_dict = {'switch': mock_switch.id,
118
                     'table_id': 1,
119
                     'match': {
120
                         'dl_src': '11:22:33:44:55:66'
121
                     },
122
                     'priority': 2,
123
                     'idle_timeout': 3,
124
                     'hard_timeout': 4,
125
                     'cookie': 5,
126
                     'actions': [
127
                         {'action_type': 'set_vlan',
128
                          'vlan_id': 6}],
129
                     'stats': {}
130
                     }
131
132
        for flow_class in Flow01, Flow04:
133
            with self.subTest(flow_class=flow_class):
134
                flow_1 = flow_class.from_dict(flow_dict, mock_switch)
135
                flow_2 = flow_class.from_dict(flow_dict, mock_switch)
136
                self.assertEqual(flow_1 == flow_2, True)
137
138
    def test__eq__success_with_different_flows(self):
139
        """Test success case to __eq__ override with different flows."""
140
        mock_switch = get_switch_mock("00:00:00:00:00:00:00:01")
141
142
        flow_dict_1 = {'switch': mock_switch.id,
143
                       'table_id': 1,
144
                       'match': {
145
                           'dl_src': '11:22:33:44:55:66'
146
                       },
147
                       'priority': 2,
148
                       'idle_timeout': 3,
149
                       'hard_timeout': 4,
150
                       'cookie': 5,
151
                       'actions': [
152
                           {'action_type': 'set_vlan',
153
                            'vlan_id': 6}],
154
                       'stats': {}
155
                       }
156
157
        flow_dict_2 = {'switch': mock_switch.id,
158
                       'table_id': 1,
159
                       'match': {
160
                           'dl_src': '11:22:33:44:55:66'
161
                       },
162
                       'priority': 1000,
163
                       'idle_timeout': 3,
164
                       'hard_timeout': 4,
165
                       'cookie': 5,
166
                       'actions': [
167
                           {'action_type': 'set_vlan',
168
                            'vlan_id': 6}],
169
                       'stats': {}
170
                       }
171
172
        for flow_class in Flow01, Flow04:
173
            with self.subTest(flow_class=flow_class):
174
                flow_1 = flow_class.from_dict(flow_dict_1, mock_switch)
175
                flow_2 = flow_class.from_dict(flow_dict_2, mock_switch)
176
                self.assertEqual(flow_1 == flow_2, False)
177
178
    def test__eq__fail(self):
179
        """Test the case where __eq__ receives objects with different types."""
180
        mock_switch = get_switch_mock("00:00:00:00:00:00:00:01")
181
182
        flow_dict = {'switch': mock_switch.id,
183
                     'table_id': 1,
184
                     'match': {
185
                         'dl_src': '11:22:33:44:55:66'
186
                     },
187
                     'priority': 2,
188
                     'idle_timeout': 3,
189
                     'hard_timeout': 4,
190
                     'cookie': 5,
191
                     'actions': [
192
                         {'action_type': 'set_vlan',
193
                          'vlan_id': 6}],
194
                     'stats': {}
195
                     }
196
197
        for flow_class in Flow01, Flow04:
198
            with self.subTest(flow_class=flow_class):
199
                flow_1 = flow_class.from_dict(flow_dict, mock_switch)
200
                flow_2 = "any_string_object"
201
                with self.assertRaises(ValueError):
202
                    return flow_1 == flow_2
203