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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
# 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
from unittest 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
self.plugin_context_precommit = None
self.plugin_context_postcommit = 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 _record_context_precommit(self, plugin_context):
self.plugin_context_precommit = plugin_context
def _record_context_postcommit(self, plugin_context):
self.plugin_context_postcommit = plugin_context
def test_create_flow_classifier_driver_manager_called(self):
self.fake_driver_manager.create_flow_classifier_precommit = mock.Mock(
side_effect=self._record_context_precommit)
self.fake_driver_manager.create_flow_classifier_postcommit = mock.Mock(
side_effect=self._record_context_postcommit)
with self.port(
name='test1'
) as port:
with self.flow_classifier(flow_classifier={
'logical_source_port': port['port']['id']
}) as fc:
driver_manager = self.fake_driver_manager
(driver_manager.create_flow_classifier_precommit
.assert_called_once_with(mock.ANY))
(driver_manager.create_flow_classifier_postcommit
.assert_called_once_with(mock.ANY))
self.assertIsInstance(
self.plugin_context_precommit,
fc_ctx.FlowClassifierContext)
self.assertIsInstance(
self.plugin_context_postcommit,
fc_ctx.FlowClassifierContext)
self.assertIn('flow_classifier', fc)
self.assertEqual(
self.plugin_context_precommit.current,
fc['flow_classifier'])
self.assertEqual(
self.plugin_context_postcommit.current,
fc['flow_classifier'])
def test_create_flow_classifier_postcommit_driver_manager_exception(self):
self.fake_driver_manager.create_flow_classifier_postcommit = mock.Mock(
side_effect=fc_exc.FlowClassifierDriverError(
method='create_flow_classifier_postcommit'
)
)
with self.port(
name='test1'
) as port:
self._create_flow_classifier(
self.fmt, {'logical_source_port': port['port']['id']},
expected_res_status=500)
driver_manager = self.fake_driver_manager
(driver_manager.create_flow_classifier_precommit
.assert_called_once_with(mock.ANY))
(driver_manager.create_flow_classifier_postcommit
.assert_called_once_with(mock.ANY))
(driver_manager.delete_flow_classifier
.assert_called_once_with(mock.ANY))
(driver_manager.delete_flow_classifier_precommit
.assert_called_once_with(mock.ANY))
(driver_manager.delete_flow_classifier_postcommit
.assert_called_once_with(mock.ANY))
self._test_list_resources('flow_classifier', [])
def test_create_flow_classifier_precommit_driver_manager_exception(self):
self.fake_driver_manager.create_flow_classifier_precommit = mock.Mock(
side_effect=fc_exc.FlowClassifierDriverError(
method='create_flow_classifier_precommit'
)
)
with self.port(
name='test1'
) as port:
self._test_list_resources('flow_classifier', [])
self._create_flow_classifier(
self.fmt, {'logical_source_port': port['port']['id']},
expected_res_status=500)
self._test_list_resources('flow_classifier', [])
driver_manager = self.fake_driver_manager
(driver_manager.create_flow_classifier_precommit
.assert_called_once_with(mock.ANY))
(driver_manager.create_flow_classifier_postcommit
.assert_not_called())
driver_manager.delete_flow_classifier.assert_not_called()
(driver_manager.delete_flow_classifier_precommit
.assert_not_called())
(driver_manager.delete_flow_classifier_postcommit
.assert_not_called())
self._test_list_resources('flow_classifier', [])
def test_update_flow_classifier_driver_manager_called(self):
self.fake_driver_manager.update_flow_classifier_precommit = mock.Mock(
side_effect=self._record_context_precommit)
self.fake_driver_manager.update_flow_classifier_postcommit = mock.Mock(
side_effect=self._record_context_postcommit)
with self.port(
name='test1'
) as port:
with self.flow_classifier(flow_classifier={
'name': 'test1',
'logical_source_port': port['port']['id']
}) 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_precommit
.assert_called_once_with(mock.ANY))
(driver_manager.update_flow_classifier_postcommit
.assert_called_once_with(mock.ANY))
self.assertIsInstance(
self.plugin_context_precommit,
fc_ctx.FlowClassifierContext)
self.assertIsInstance(self.plugin_context_postcommit,
fc_ctx.FlowClassifierContext)
self.assertIn('flow_classifier', fc)
self.assertIn('flow_classifier', res)
self.assertEqual(self.plugin_context_precommit.current,
res['flow_classifier'])
self.assertEqual(self.plugin_context_postcommit.current,
res['flow_classifier'])
self.assertEqual(self.plugin_context_precommit.original,
fc['flow_classifier'])
self.assertEqual(self.plugin_context_postcommit.original,
fc['flow_classifier'])
def _test_update_flow_classifier_driver_manager_exception(self, updated):
with self.port(
name='test1'
) as port:
with self.flow_classifier(flow_classifier={
'name': 'test1',
'logical_source_port': port['port']['id']
}) 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)
if updated:
updated_flow_classifier['name'] = 'test2'
res = req.get_response(self.ext_api)
self.assertEqual(500, res.status_int)
driver_manager = self.fake_driver_manager
(driver_manager.update_flow_classifier_precommit
.assert_called_once_with(mock.ANY))
if updated:
(driver_manager.update_flow_classifier_postcommit
.assert_called_once_with(mock.ANY))
else:
(driver_manager.update_flow_classifier_postcommit
.assert_not_called())
res = self._list('flow_classifiers')
self.assertIn('flow_classifiers', res)
self.assertCountEqual(
res['flow_classifiers'], [updated_flow_classifier])
def test_update_flow_classifier_precommit_driver_manager_exception(self):
self.fake_driver_manager.update_flow_classifier_precommit = mock.Mock(
side_effect=fc_exc.FlowClassifierDriverError(
method='update_flow_classifier_precommit'
)
)
self._test_update_flow_classifier_driver_manager_exception(False)
def test_update_flow_classifier_postcommit_driver_manager_exception(self):
self.fake_driver_manager.update_flow_classifier_postcommit = mock.Mock(
side_effect=fc_exc.FlowClassifierDriverError(
method='update_flow_classifier_postcommit'
)
)
self._test_update_flow_classifier_driver_manager_exception(True)
def test_delete_flow_classifer_driver_manager_called(self):
self.fake_driver_manager.delete_flow_classifier = mock.Mock(
side_effect=self._record_context)
self.fake_driver_manager.delete_flow_classifier_precommit = mock.Mock(
side_effect=self._record_context_precommit)
self.fake_driver_manager.delete_flow_classifier_postcommit = mock.Mock(
side_effect=self._record_context_postcommit)
with self.port(
name='test1'
) as port:
with self.flow_classifier(
flow_classifier={'logical_source_port': port['port']['id']},
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(204, res.status_int)
driver_manager = self.fake_driver_manager
(driver_manager.delete_flow_classifier
.assert_called_once_with(mock.ANY))
(driver_manager.delete_flow_classifier_precommit
.assert_called_once_with(mock.ANY))
(driver_manager.delete_flow_classifier_postcommit
.assert_called_once_with(mock.ANY))
self.assertIsInstance(
self.plugin_context, fc_ctx.FlowClassifierContext
)
self.assertIsInstance(
self.plugin_context_precommit, fc_ctx.FlowClassifierContext
)
self.assertIsInstance(self.plugin_context_postcommit,
fc_ctx.FlowClassifierContext)
self.assertIn('flow_classifier', fc)
self.assertEqual(
self.plugin_context.current, fc['flow_classifier'])
self.assertEqual(self.plugin_context_precommit.current,
fc['flow_classifier'])
self.assertEqual(self.plugin_context_postcommit.current,
fc['flow_classifier'])
def _test_delete_flow_classifier_driver_manager_exception(self):
with self.port(
name='test1'
) as port:
with self.flow_classifier(flow_classifier={
'name': 'test1',
'logical_source_port': port['port']['id']
}, 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(500, res.status_int)
driver_manager = self.fake_driver_manager
driver_manager.delete_flow_classifier.assert_called_once_with(
mock.ANY
)
self._test_list_resources('flow_classifier', [fc])
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'
)
)
self._test_delete_flow_classifier_driver_manager_exception()
def test_delete_flow_classifier_precommit_driver_manager_exception(self):
self.fake_driver_manager.delete_flow_classifier_precommit = mock.Mock(
side_effect=fc_exc.FlowClassifierDriverError(
method='delete_flow_classifier_precommit'
)
)
self._test_delete_flow_classifier_driver_manager_exception()
|