| 1 |
# Copyright (C) 2015 Midokura SARL. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import contextlib |
| 17 |
|
| 18 |
import testtools |
| 19 |
from unittest import mock |
| 20 |
|
| 21 |
from neutron_lib import constants |
| 22 |
from neutron_lib import context |
| 23 |
from neutron_lib.exceptions import taas as taas_exc |
| 24 |
from neutron_lib import rpc as n_rpc |
| 25 |
from neutron_lib.utils import net as n_utils |
| 26 |
from oslo_config import cfg |
| 27 |
from oslo_utils import uuidutils |
| 28 |
|
| 29 |
from neutron.tests.unit import testlib_api |
| 30 |
|
| 31 |
import neutron_taas.db.taas_db # noqa |
| 32 |
from neutron_taas.services.taas.service_drivers import taas_agent_api |
| 33 |
from neutron_taas.services.taas.service_drivers import taas_rpc |
| 34 |
from neutron_taas.services.taas import taas_plugin |
| 35 |
|
| 36 |
|
| 37 |
class DummyError(Exception): |
| 38 |
pass |
| 39 |
|
| 40 |
|
| 41 |
class TestTaasPlugin(testlib_api.SqlTestCase): |
| 42 |
def setUp(self): |
| 43 |
super().setUp() |
| 44 |
mock.patch.object(n_rpc, 'Connection', spec=object).start() |
| 45 |
mock.patch.object(taas_agent_api, |
| 46 |
'TaasAgentApi', spec=object).start() |
| 47 |
self.driver = mock.MagicMock() |
| 48 |
mock.patch('neutron.services.service_base.load_drivers', |
| 49 |
return_value=({'dummy_provider': self.driver}, |
| 50 |
'dummy_provider')).start() |
| 51 |
mock.patch('neutron.db.servicetype_db.ServiceTypeManager.get_instance', |
| 52 |
return_value=mock.MagicMock()).start() |
| 53 |
self._plugin = taas_plugin.TaasPlugin() |
| 54 |
self._context = context.get_admin_context() |
| 55 |
self.taas_cbs = taas_rpc.TaasCallbacks(self.driver, self._plugin) |
| 56 |
|
| 57 |
self._project_id = self._tenant_id = 'tenant-X' |
| 58 |
self._network_id = uuidutils.generate_uuid() |
| 59 |
self._host_id = 'host-A' |
| 60 |
self._port_id = uuidutils.generate_uuid() |
| 61 |
self._port_details = { |
| 62 |
'tenant_id': self._tenant_id, |
| 63 |
'binding:host_id': self._host_id, |
| 64 |
'mac_address': n_utils.get_random_mac( |
| 65 |
'fa:16:3e:00:00:00'.split(':')), |
| 66 |
} |
| 67 |
self._tap_service = { |
| 68 |
'tenant_id': self._tenant_id, |
| 69 |
'name': 'MyTap', |
| 70 |
'description': 'This is my tap service', |
| 71 |
'port_id': self._port_id, |
| 72 |
'project_id': self._project_id, |
| 73 |
} |
| 74 |
self.vlan_filter = "1-5,9,18,27-30,99-108,4000-4095" |
| 75 |
self._tap_flow = { |
| 76 |
'description': 'This is my tap flow', |
| 77 |
'direction': 'BOTH', |
| 78 |
'name': 'MyTapFlow', |
| 79 |
'source_port': self._port_id, |
| 80 |
'tenant_id': self._tenant_id, |
| 81 |
'project_id': self._project_id, |
| 82 |
'vlan_filter': self.vlan_filter, |
| 83 |
} |
| 84 |
|
| 85 |
@contextlib.contextmanager |
| 86 |
def tap_service(self): |
| 87 |
req = { |
| 88 |
'tap_service': self._tap_service, |
| 89 |
} |
| 90 |
with mock.patch.object(self._plugin, 'get_port_details', |
| 91 |
return_value=self._port_details): |
| 92 |
self._plugin.create_tap_service(self._context, req) |
| 93 |
self._tap_service['id'] = mock.ANY |
| 94 |
self._tap_service['status'] = constants.DOWN |
| 95 |
|
| 96 |
self.driver.assert_has_calls([ |
| 97 |
mock.call.create_tap_service_precommit(mock.ANY), |
| 98 |
mock.call.create_tap_service_postcommit(mock.ANY), |
| 99 |
]) |
| 100 |
pre_args = self.driver.create_tap_service_precommit.call_args[0][0] |
| 101 |
self.assertEqual(self._context, pre_args._plugin_context) |
| 102 |
self.assertEqual(self._tap_service, pre_args.tap_service) |
| 103 |
post_args = self.driver.create_tap_service_postcommit.call_args[0][0] |
| 104 |
self.assertEqual(self._context, post_args._plugin_context) |
| 105 |
self.assertEqual(self._tap_service, post_args.tap_service) |
| 106 |
self.taas_cbs.set_tap_service_status( |
| 107 |
self._context, |
| 108 |
{'id': pre_args.tap_service['id']}, |
| 109 |
constants.ACTIVE, "dummyHost") |
| 110 |
self._tap_service['status'] = constants.ACTIVE |
| 111 |
yield self._plugin.get_tap_service(self._context, |
| 112 |
pre_args.tap_service['id']) |
| 113 |
|
| 114 |
@contextlib.contextmanager |
| 115 |
def tap_flow(self, tap_service, tenant_id=None): |
| 116 |
self._tap_flow['tap_service_id'] = tap_service |
| 117 |
if tenant_id is not None: |
| 118 |
self._tap_flow['tenant_id'] = tenant_id |
| 119 |
req = { |
| 120 |
'tap_flow': self._tap_flow, |
| 121 |
} |
| 122 |
with mock.patch.object(self._plugin, 'get_port_details', |
| 123 |
return_value=self._port_details): |
| 124 |
self._plugin.create_tap_flow(self._context, req) |
| 125 |
self._tap_flow['id'] = mock.ANY |
| 126 |
self._tap_flow['status'] = constants.DOWN |
| 127 |
self._tap_service['id'] = mock.ANY |
| 128 |
self._tap_flow['vlan_filter'] = mock.ANY |
| 129 |
|
| 130 |
self.driver.assert_has_calls([ |
| 131 |
mock.call.create_tap_flow_precommit(mock.ANY), |
| 132 |
mock.call.create_tap_flow_postcommit(mock.ANY), |
| 133 |
]) |
| 134 |
pre_args = self.driver.create_tap_flow_precommit.call_args[0][0] |
| 135 |
self.assertEqual(self._context, pre_args._plugin_context) |
| 136 |
self.assertEqual(self._tap_flow, pre_args.tap_flow) |
| 137 |
post_args = self.driver.create_tap_flow_postcommit.call_args[0][0] |
| 138 |
self.assertEqual(self._context, post_args._plugin_context) |
| 139 |
self.assertEqual(self._tap_flow, post_args.tap_flow) |
| 140 |
self.taas_cbs.set_tap_flow_status( |
| 141 |
self._context, |
| 142 |
{'id': pre_args.tap_flow['id']}, |
| 143 |
constants.ACTIVE, "dummyHost") |
| 144 |
self._tap_flow['status'] = constants.ACTIVE |
| 145 |
yield self._plugin.get_tap_flow(self._context, |
| 146 |
pre_args.tap_flow['id']) |
| 147 |
|
| 148 |
def test_create_tap_service(self): |
| 149 |
with self.tap_service(): |
| 150 |
pass |
| 151 |
|
| 152 |
def test_verify_taas_id_reused(self): |
| 153 |
# make small range id |
| 154 |
cfg.CONF.set_override("vlan_range_start", 1, group="taas") |
| 155 |
cfg.CONF.set_override("vlan_range_end", 3, group="taas") |
| 156 |
with self.tap_service() as ts_1, self.tap_service() as ts_2, \ |
| 157 |
self.tap_service() as ts_3, self.tap_service() as ts_4: |
| 158 |
ts_id_1 = ts_1['id'] |
| 159 |
ts_id_2 = ts_2['id'] |
| 160 |
ts_id_3 = ts_3['id'] |
| 161 |
tap_id_assoc_1 = self._plugin.create_tap_id_association( |
| 162 |
self._context, ts_id_1) |
| 163 |
tap_id_assoc_2 = self._plugin.create_tap_id_association( |
| 164 |
self._context, ts_id_2) |
| 165 |
self.assertEqual({1, 2}, {tap_id_assoc_1['taas_id'], |
| 166 |
tap_id_assoc_2['taas_id']}) |
| 167 |
with testtools.ExpectedException(taas_exc.TapServiceLimitReached): |
| 168 |
self._plugin.create_tap_id_association( |
| 169 |
self._context, |
| 170 |
ts_4['id'] |
| 171 |
) |
| 172 |
# free an tap_id and verify could reallocate same taas id |
| 173 |
self._plugin.delete_tap_service(self._context, ts_id_1) |
| 174 |
self.taas_cbs.set_tap_service_status(self._context, |
| 175 |
{'id': ts_id_1}, |
| 176 |
constants.INACTIVE, |
| 177 |
"dummyHost") |
| 178 |
tap_id_assoc_3 = self._plugin.create_tap_id_association( |
| 179 |
self._context, ts_id_3) |
| 180 |
self.assertEqual({1, 2}, {tap_id_assoc_3['taas_id'], |
| 181 |
tap_id_assoc_2['taas_id']}) |
| 182 |
|
| 183 |
def test_create_tap_service_wrong_tenant_id(self): |
| 184 |
self._port_details['tenant_id'] = 'other-tenant' |
| 185 |
with testtools.ExpectedException(taas_exc.PortDoesNotBelongToTenant), \ |
| 186 |
self.tap_service(): |
| 187 |
pass |
| 188 |
self.assertEqual([], self.driver.mock_calls) |
| 189 |
|
| 190 |
def test_create_tap_service_reach_limit(self): |
| 191 |
# TODO(Yoichiro):Need to move this test to taas_rpc test |
| 192 |
pass |
| 193 |
|
| 194 |
def test_create_tap_service_failed_on_service_driver(self): |
| 195 |
attr = {'create_tap_service_postcommit.side_effect': DummyError} |
| 196 |
self.driver.configure_mock(**attr) |
| 197 |
with testtools.ExpectedException(DummyError): |
| 198 |
req = { |
| 199 |
'tap_service': self._tap_service, |
| 200 |
} |
| 201 |
with mock.patch.object(self._plugin, 'get_port_details', |
| 202 |
return_value=self._port_details): |
| 203 |
self._plugin.create_tap_service(self._context, req) |
| 204 |
|
| 205 |
def test_delete_tap_service(self): |
| 206 |
with self.tap_service() as ts: |
| 207 |
self._plugin.delete_tap_service(self._context, ts['id']) |
| 208 |
self._tap_service['id'] = ts['id'] |
| 209 |
self.driver.assert_has_calls([ |
| 210 |
mock.call.delete_tap_service_precommit(mock.ANY), |
| 211 |
]) |
| 212 |
self._tap_service['status'] = constants.PENDING_DELETE |
| 213 |
pre_args = self.driver.delete_tap_service_precommit.call_args[0][0] |
| 214 |
self.assertEqual(self._context, pre_args._plugin_context) |
| 215 |
self.assertEqual(self._tap_service, pre_args.tap_service) |
| 216 |
self.taas_cbs.set_tap_service_status(self._context, |
| 217 |
{'id': self._tap_service['id']}, |
| 218 |
constants.INACTIVE, |
| 219 |
"dummyHost") |
| 220 |
|
| 221 |
def test_delete_tap_service_with_flow(self): |
| 222 |
with self.tap_service() as ts, \ |
| 223 |
self.tap_flow(tap_service=ts['id']) as tf: |
| 224 |
self._plugin.delete_tap_service(self._context, ts['id']) |
| 225 |
self._tap_service['id'] = ts['id'] |
| 226 |
self._tap_flow['id'] = tf['id'] |
| 227 |
self.driver.assert_has_calls([ |
| 228 |
mock.call.delete_tap_flow_precommit(mock.ANY), |
| 229 |
mock.call.delete_tap_service_precommit(mock.ANY), |
| 230 |
]) |
| 231 |
self._tap_service['status'] = constants.PENDING_DELETE |
| 232 |
self._tap_flow['status'] = constants.PENDING_DELETE |
| 233 |
pre_args = self.driver.delete_tap_flow_precommit.call_args[0][0] |
| 234 |
self.assertEqual(self._context, pre_args._plugin_context) |
| 235 |
self.assertEqual(self._tap_flow, pre_args.tap_flow) |
| 236 |
pre_args = self.driver.delete_tap_service_precommit.call_args[0][0] |
| 237 |
self.assertEqual(self._context, pre_args._plugin_context) |
| 238 |
self.assertEqual(self._tap_service, pre_args.tap_service) |
| 239 |
self.taas_cbs.set_tap_flow_status(self._context, |
| 240 |
{'id': self._tap_flow['id']}, |
| 241 |
constants.INACTIVE, |
| 242 |
"dummyHost") |
| 243 |
self.taas_cbs.set_tap_service_status(self._context, |
| 244 |
{'id': self._tap_service['id']}, |
| 245 |
constants.INACTIVE, |
| 246 |
"dummyHost") |
| 247 |
|
| 248 |
def test_delete_tap_service_non_existent(self): |
| 249 |
with testtools.ExpectedException(taas_exc.TapServiceNotFound): |
| 250 |
self._plugin.delete_tap_service(self._context, 'non-existent') |
| 251 |
|
| 252 |
def test_create_tap_flow(self): |
| 253 |
with self.tap_service() as ts, self.tap_flow(tap_service=ts['id']): |
| 254 |
pass |
| 255 |
|
| 256 |
def test_create_tap_flow_wrong_tenant_id(self): |
| 257 |
with self.tap_service() as ts, \ |
| 258 |
testtools.ExpectedException( |
| 259 |
taas_exc.TapServiceNotBelongToTenant), \ |
| 260 |
self.tap_flow(tap_service=ts['id'], tenant_id='other-tenant'): |
| 261 |
pass |
| 262 |
|
| 263 |
def test_create_tap_flow_failed_on_service_driver(self): |
| 264 |
with self.tap_service() as ts: |
| 265 |
attr = {'create_tap_flow_postcommit.side_effect': DummyError} |
| 266 |
self.driver.configure_mock(**attr) |
| 267 |
with testtools.ExpectedException(DummyError): |
| 268 |
self._tap_flow['tap_service_id'] = ts['id'] |
| 269 |
req = { |
| 270 |
'tap_flow': self._tap_flow, |
| 271 |
} |
| 272 |
with mock.patch.object(self._plugin, 'get_port_details', |
| 273 |
return_value=self._port_details): |
| 274 |
self._plugin.create_tap_flow(self._context, req) |
| 275 |
|
| 276 |
def test_delete_tap_flow(self): |
| 277 |
with self.tap_service() as ts, \ |
| 278 |
self.tap_flow(tap_service=ts['id']) as tf: |
| 279 |
self._plugin.delete_tap_flow(self._context, tf['id']) |
| 280 |
self._tap_flow['id'] = tf['id'] |
| 281 |
self.driver.assert_has_calls([ |
| 282 |
mock.call.delete_tap_flow_precommit(mock.ANY), |
| 283 |
]) |
| 284 |
self._tap_flow['status'] = constants.PENDING_DELETE |
| 285 |
pre_args = self.driver.delete_tap_flow_precommit.call_args[0][0] |
| 286 |
self.assertEqual(self._context, pre_args._plugin_context) |
| 287 |
self.assertEqual(self._tap_flow, pre_args.tap_flow) |
| 288 |
self.taas_cbs.set_tap_flow_status(self._context, |
| 289 |
{'id': self._tap_flow['id']}, |
| 290 |
constants.INACTIVE, |
| 291 |
"dummyHost") |