aboutsummaryrefslogtreecommitdiffstats
path: root/networking_sfc/tests/unit/services/flowclassifier/test_plugin.py
blob: 2c814f85279be248edb95e93128b1c702f4d6e63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright 2015 Futurewei. All rights reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import copy
import mock

from networking_sfc.services.flowclassifier.common import context as fc_ctx
from networking_sfc.services.flowclassifier.common import exceptions as fc_exc
from networking_sfc.tests.unit.db import test_flowclassifier_db

FLOWCLASSIFIER_PLUGIN_KLASS = (
    "networking_sfc.services.flowclassifier."
    "plugin.FlowClassifierPlugin"
)


class FlowClassifierPluginTestCase(
    test_flowclassifier_db.FlowClassifierDbPluginTestCase
):
    def setUp(
        self, core_plugin=None, flowclassifier_plugin=None, ext_mgr=None
    ):
        if not flowclassifier_plugin:
            flowclassifier_plugin = FLOWCLASSIFIER_PLUGIN_KLASS
        self.driver_manager_p = mock.patch(
            'networking_sfc.services.flowclassifier.driver_manager.'
            'FlowClassifierDriverManager'
        )
        self.fake_driver_manager_class = self.driver_manager_p.start()
        self.fake_driver_manager = mock.Mock()
        self.fake_driver_manager_class.return_value = self.fake_driver_manager
        self.plugin_context = None
        super(FlowClassifierPluginTestCase, self).setUp(
            core_plugin=core_plugin,
            flowclassifier_plugin=flowclassifier_plugin,
            ext_mgr=ext_mgr
        )

    def _record_context(self, plugin_context):
        self.plugin_context = plugin_context

    def test_create_flow_classifier_driver_manager_called(self):
        self.fake_driver_manager.create_flow_classifier = mock.Mock(
            side_effect=self._record_context)
        with self.flow_classifier(flow_classifier={}) as fc:
            driver_manager = self.fake_driver_manager
            driver_manager.create_flow_classifier.assert_called_once_with(
                mock.ANY
            )
            self.assertIsInstance(
                self.plugin_context, fc_ctx.FlowClassifierContext
            )
            self.assertIn('flow_classifier', fc)
            self.assertEqual(
                self.plugin_context.current, fc['flow_classifier'])

    def test_create_flow_classifier_driver_manager_exception(self):
        self.fake_driver_manager.create_flow_classifier = mock.Mock(
            side_effect=fc_exc.FlowClassifierDriverError(
                method='create_flow_classifier'
            )
        )
        self._create_flow_classifier(
            self.fmt, {}, expected_res_status=500)
        self._test_list_resources('flow_classifier', [])
        driver_manager = self.fake_driver_manager
        driver_manager.delete_flow_classifier.assert_called_once_with(
            mock.ANY
        )

    def test_update_flow_classifier_driver_manager_called(self):
        self.fake_driver_manager.update_flow_classifier = mock.Mock(
            side_effect=self._record_context)
        with self.flow_classifier(flow_classifier={'name': 'test1'}) as fc:
            req = self.new_update_request(
                'flow_classifiers', {'flow_classifier': {'name': 'test2'}},
                fc['flow_classifier']['id']
            )
            res = self.deserialize(
                self.fmt,
                req.get_response(self.ext_api)
            )
            driver_manager = self.fake_driver_manager
            driver_manager.update_flow_classifier.assert_called_once_with(
                mock.ANY
            )
            self.assertIsInstance(
                self.plugin_context, fc_ctx.FlowClassifierContext
            )
            self.assertIn('flow_classifier', fc)
            self.assertIn('flow_classifier', res)
            self.assertEqual(
                self.plugin_context.current, res['flow_classifier'])
            self.assertEqual(
                self.plugin_context.original, fc['flow_classifier'])

    def test_update_flow_classifier_driver_manager_exception(self):
        self.fake_driver_manager.update_flow_classifier = mock.Mock(
            side_effect=fc_exc.FlowClassifierDriverError(
                method='update_flow_classifier'
            )
        )
        with self.flow_classifier(flow_classifier={
            'name': 'test1'
        }) as fc:
            self.assertIn('flow_classifier', fc)
            original_flow_classifier = fc['flow_classifier']
            req = self.new_update_request(
                'flow_classifiers', {'flow_classifier': {'name': 'test2'}},
                fc['flow_classifier']['id']
            )
            updated_flow_classifier = copy.copy(original_flow_classifier)
            updated_flow_classifier['name'] = 'test2'
            res = req.get_response(self.ext_api)
            self.assertEqual(res.status_int, 500)
            res = self._list('flow_classifiers')
            self.assertIn('flow_classifiers', res)
            self.assertItemsEqual(
                res['flow_classifiers'], [updated_flow_classifier])

    def test_delete_flow_classifer_driver_manager_called(self):
        self.fake_driver_manager.delete_flow_classifier = mock.Mock(
            side_effect=self._record_context)
        with self.flow_classifier(
            flow_classifier={}, do_delete=False
        ) as fc:
            req = self.new_delete_request(
                'flow_classifiers', fc['flow_classifier']['id']
            )
            res = req.get_response(self.ext_api)
            self.assertEqual(res.status_int, 204)
            driver_manager = self.fake_driver_manager
            driver_manager.delete_flow_classifier.assert_called_once_with(
                mock.ANY
            )
            self.assertIsInstance(
                self.plugin_context, fc_ctx.FlowClassifierContext
            )
            self.assertIn('flow_classifier', fc)
            self.assertEqual(
                self.plugin_context.current, fc['flow_classifier'])

    def test_delete_flow_classifier_driver_manager_exception(self):
        self.fake_driver_manager.delete_flow_classifier = mock.Mock(
            side_effect=fc_exc.FlowClassifierDriverError(
                method='delete_flow_classifier'
            )
        )
        with self.flow_classifier(flow_classifier={
            'name': 'test1'
        }, do_delete=False) as fc:
            req = self.new_delete_request(
                'flow_classifiers', fc['flow_classifier']['id']
            )
            res = req.get_response(self.ext_api)
            self.assertEqual(res.status_int, 500)
            self._test_list_resources('flow_classifier', [fc])