Passed
Push — master ( 151596...743f3c )
by Humberto
01:26 queued 12s
created

TestFlowSerializer10.test_to_dict()   A

Complexity

Conditions 1

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 31
rs 9.304
c 0
b 0
f 0
cc 1
nop 1
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
# pylint: disable=protected-access, no-member
9
class TestFlowSerializer10(TestCase):
10
    """Test the FlowSerializer10 class."""
11
12
    def setUp(self):
13
        """Execute steps before each tests."""
14
        self.napp = FlowSerializer10()
15
16
    @patch('napps.kytos.flow_manager.serializers.v0x01.ActionOutput')
17
    @patch('napps.kytos.flow_manager.serializers.v0x01.FlowMod')
18
    def test_from_dict(self, *args):
19
        """Test from_dict method."""
20
        (mock_flow_mod, _) = args
21
        flow_mod = MagicMock()
22
        flow_mod.actions = []
23
        mock_flow_mod.return_value = flow_mod
24
25
        self.napp.flow_attributes = {'flow_attr'}
26
        self.napp.match_attributes = {'match_attr'}
27
        dictionary = {'flow_attr': 'any_data',
28
                      'match': {'match_attr': 123},
29
                      'actions': [{'action_type': 'output',
30
                                   'port': 'controller'}]}
31
        return_flow_mod = self.napp.from_dict(dictionary)
32
33
        self.assertEqual(return_flow_mod.flow_attr, 'any_data')
34
        self.assertEqual(return_flow_mod.match.match_attr, 123)
35
        self.assertEqual(len(return_flow_mod.actions), 1)
36
37
    def test_update_match(self):
38
        """Test _update_match method."""
39
        match = MagicMock()
40
        dictionary = {'attr': 'data'}
41
        self.napp.match_attributes = {'attr'}
42
43
        self.napp._update_match(match, dictionary)
44
45
        self.assertEqual(match.attr, 'data')
46
47
    @patch('napps.kytos.flow_manager.serializers.v0x01.ActionOutput')
48
    @patch('napps.kytos.flow_manager.serializers.v0x01.ActionVlanVid')
49
    def test_actions_from_list(self, *args):
50
        """Test _actions_from_list method."""
51
        (mock_action_vlan_vid, mock_action_output) = args
52
        mock_action_vlan_vid.return_value = 'action_vlan_vid'
53
        mock_action_output.side_effect = ['action_output_controller',
54
                                          'action_output_any']
55
        actions = [{'action_type': 'set_vlan', 'vlan_id': 'id'},
56
                   {'action_type': 'output', 'port': 'controller'},
57
                   {'action_type': 'output', 'port': 'any'},
58
                   {'action_type': 'any'}]
59
60
        return_actions = self.napp._actions_from_list(actions)
61
62
        expected_actions = ['action_vlan_vid', 'action_output_controller',
63
                            'action_output_any']
64
        self.assertEqual(return_actions, expected_actions)
65
66
    def test_to_dict(self):
67
        """Test to_dict method."""
68
        action_1 = MagicMock()
69
        action_1.action_type = 1
70
        action_1.vlan_id.value = 'id'
71
72
        action_2 = MagicMock()
73
        action_2.action_type = 0
74
        action_2.port = 0xfffd
75
76
        action_3 = MagicMock()
77
        action_3.action_type = 0
78
        action_3.port.value = 'port'
79
80
        flow_stats = MagicMock()
81
        flow_stats.actions = [action_1, action_2, action_3]
82
        flow_stats.flow_attr = MagicMock()
83
        flow_stats.match.match_attr = MagicMock()
84
        self.napp.flow_attributes = {'flow_attr'}
85
        self.napp.match_attributes = {'match_attr'}
86
87
        flow_dict = self.napp.to_dict(flow_stats)
88
89
        expected = {'flow_attr': flow_stats.flow_attr.value,
90
                    'match': {'match_attr': flow_stats.match.match_attr.value},
91
                    'actions': [{'action_type': 'set_vlan', 'vlan_id': 'id'},
92
                                {'action_type': 'output',
93
                                 'port': 'controller'},
94
                                {'action_type': 'output', 'port': 'port'}]}
95
96
        self.assertEqual(flow_dict, expected)
97