# Copyright (c) 2012 OpenStack Foundation. # # 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 logging import mock from quantum import context from quantum.manager import QuantumManager from quantum.plugins.cisco.common import cisco_constants as const from quantum.plugins.cisco.db import network_db_v2 # noqa from quantum.tests.unit import test_db_plugin LOG = logging.getLogger(__name__) class CiscoNetworkPluginV2TestCase(test_db_plugin.QuantumDbPluginV2TestCase): _plugin_name = 'quantum.plugins.cisco.network_plugin.PluginV2' def setUp(self): # Use a mock netconf client mock_ncclient = mock.Mock() self.patch_obj = mock.patch.dict('sys.modules', {'ncclient': mock_ncclient}) self.patch_obj.start() super(CiscoNetworkPluginV2TestCase, self).setUp(self._plugin_name) self.port_create_status = 'DOWN' self.addCleanup(self.patch_obj.stop) def _get_plugin_ref(self): plugin_obj = QuantumManager.get_plugin() if getattr(plugin_obj, "_master"): plugin_ref = plugin_obj else: plugin_ref = getattr(plugin_obj, "_model").\ _plugins[const.VSWITCH_PLUGIN] return plugin_ref class TestCiscoBasicGet(CiscoNetworkPluginV2TestCase, test_db_plugin.TestBasicGet): pass class TestCiscoV2HTTPResponse(CiscoNetworkPluginV2TestCase, test_db_plugin.TestV2HTTPResponse): pass class TestCiscoPortsV2(CiscoNetworkPluginV2TestCase, test_db_plugin.TestPortsV2): def test_create_ports_bulk_emulated_plugin_failure(self): real_has_attr = hasattr #ensures the API choose the emulation code path def fakehasattr(item, attr): if attr.endswith('__native_bulk_support'): return False return real_has_attr(item, attr) with mock.patch('__builtin__.hasattr', new=fakehasattr): plugin_ref = self._get_plugin_ref() orig = plugin_ref.create_port with mock.patch.object(plugin_ref, 'create_port') as patched_plugin: def side_effect(*args, **kwargs): return self._do_side_effect(patched_plugin, orig, *args, **kwargs) patched_plugin.side_effect = side_effect with self.network() as net: res = self._create_port_bulk(self.fmt, 2, net['network']['id'], 'test', True) # We expect a 500 as we injected a fault in the plugin self._validate_behavior_on_bulk_failure(res, 'ports', 500) def test_create_ports_bulk_native_plugin_failure(self): if self._skip_native_bulk: self.skipTest("Plugin does not support native bulk port create") ctx = context.get_admin_context() with self.network() as net: plugin_ref = self._get_plugin_ref() orig = plugin_ref.create_port with mock.patch.object(plugin_ref, 'create_port') as patched_plugin: def side_effect(*args, **kwargs): return self._do_side_effect(patched_plugin, orig, *args, **kwargs) patched_plugin.side_effect = side_effect res = self._create_port_bulk(self.fmt, 2, net['network']['id'], 'test', True, context=ctx) # We expect a 500 as we injected a fault in the plugin self._validate_behavior_on_bulk_failure(res, 'ports', 500) class TestCiscoNetworksV2(CiscoNetworkPluginV2TestCase, test_db_plugin.TestNetworksV2): def test_create_networks_bulk_emulated_plugin_failure(self): real_has_attr = hasattr def fakehasattr(item, attr): if attr.endswith('__native_bulk_support'): return False return real_has_attr(item, attr) plugin_ref = self._get_plugin_ref() orig = plugin_ref.create_network #ensures the API choose the emulation code path with mock.patch('__builtin__.hasattr', new=fakehasattr): with mock.patch.object(plugin_ref, 'create_network') as patched_plugin: def side_effect(*args, **kwargs): return self._do_side_effect(patched_plugin, orig, *args, **kwargs) patched_plugin.side_effect = side_effect res = self._create_network_bulk(self.fmt, 2, 'test', True) LOG.debug("response is %s" % res) # We expect a 500 as we injected a fault in the plugin self._validate_behavior_on_bulk_failure(res, 'networks', 500) def test_create_networks_bulk_native_plugin_failure(self): if self._skip_native_bulk: self.skipTest("Plugin does not support native bulk network create") plugin_ref = self._get_plugin_ref() orig = plugin_ref.create_network with mock.patch.object(plugin_ref, 'create_network') as patched_plugin: def side_effect(*args, **kwargs): return self._do_side_effect(patched_plugin, orig, *args, **kwargs) patched_plugin.side_effect = side_effect res = self._create_network_bulk(self.fmt, 2, 'test', True) # We expect a 500 as we injected a fault in the plugin self._validate_behavior_on_bulk_failure(res, 'networks', 500) class TestCiscoSubnetsV2(CiscoNetworkPluginV2TestCase, test_db_plugin.TestSubnetsV2): def test_create_subnets_bulk_emulated_plugin_failure(self): real_has_attr = hasattr #ensures the API choose the emulation code path def fakehasattr(item, attr): if attr.endswith('__native_bulk_support'): return False return real_has_attr(item, attr) with mock.patch('__builtin__.hasattr', new=fakehasattr): plugin_ref = self._get_plugin_ref() orig = plugin_ref.create_subnet with mock.patch.object(plugin_ref, 'create_subnet') as patched_plugin: def side_effect(*args, **kwargs): self._do_side_effect(patched_plugin, orig, *args, **kwargs) patched_plugin.side_effect = side_effect with self.network() as net: res = self._create_subnet_bulk(self.fmt, 2, net['network']['id'], 'test') # We expect a 500 as we injected a fault in the plugin self._validate_behavior_on_bulk_failure(res, 'subnets', 500) def test_create_subnets_bulk_native_plugin_failure(self): if self._skip_native_bulk: self.skipTest("Plugin does not support native bulk subnet create") plugin_ref = self._get_plugin_ref() orig = plugin_ref.create_subnet with mock.patch.object(plugin_ref, 'create_subnet') as patched_plugin: def side_effect(*args, **kwargs): return self._do_side_effect(patched_plugin, orig, *args, **kwargs) patched_plugin.side_effect = side_effect with self.network() as net: res = self._create_subnet_bulk(self.fmt, 2, net['network']['id'], 'test') # We expect a 500 as we injected a fault in the plugin self._validate_behavior_on_bulk_failure(res, 'subnets', 500) class TestCiscoPortsV2XML(TestCiscoPortsV2): fmt = 'xml' class TestCiscoNetworksV2XML(TestCiscoNetworksV2): fmt = 'xml' class TestCiscoSubnetsV2XML(TestCiscoSubnetsV2): fmt = 'xml'