Completed
Pull Request — master (#81)
by Gleyberson
03:00
created

TestFlowSerializer10.setUp()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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