Best Python code snippet using molotov_python
test_quota_per_tenant_ext.py
Source:test_quota_per_tenant_ext.py
1import unittest2import webtest3import mock4from quantum.api import extensions5from quantum.api.v2 import attributes6from quantum.common import config7from quantum import context8from quantum.db import api as db9from quantum import manager10from quantum.openstack.common import cfg11from quantum.plugins.linuxbridge.db import l2network_db_v212from quantum import quota13from quantum.tests.unit import test_api_v214from quantum.tests.unit import test_extensions15TARGET_PLUGIN = ('quantum.plugins.linuxbridge.lb_quantum_plugin'16 '.LinuxBridgePluginV2')17_get_path = test_api_v2._get_path18class QuotaExtensionTestCase(unittest.TestCase):19 def setUp(self):20 if getattr(self, 'testflag', 1) == 1:21 self._setUp1()22 else:23 self._setUp2()24 def _setUp1(self):25 db._ENGINE = None26 db._MAKER = None27 # Ensure 'stale' patched copies of the plugin are never returned28 manager.QuantumManager._instance = None29 # Ensure existing ExtensionManager is not used30 extensions.PluginAwareExtensionManager._instance = None31 # Save the global RESOURCE_ATTRIBUTE_MAP32 self.saved_attr_map = {}33 for resource, attrs in attributes.RESOURCE_ATTRIBUTE_MAP.iteritems():34 self.saved_attr_map[resource] = attrs.copy()35 # Create the default configurations36 args = ['--config-file', test_extensions.etcdir('quantum.conf.test')]37 config.parse(args=args)38 # Update the plugin and extensions path39 cfg.CONF.set_override('core_plugin', TARGET_PLUGIN)40 cfg.CONF.set_override(41 'quota_driver',42 'quantum.extensions._quotav2_driver.DbQuotaDriver',43 group='QUOTAS')44 cfg.CONF.set_override(45 'quota_items',46 ['network', 'subnet', 'port', 'extra1'],47 group='QUOTAS')48 self._plugin_patcher = mock.patch(TARGET_PLUGIN, autospec=True)49 self.plugin = self._plugin_patcher.start()50 # QUOTAS will regester the items in conf when starting51 # extra1 here is added later, so have to do it manually52 quota.QUOTAS.register_resource_by_name('extra1')53 ext_mgr = extensions.PluginAwareExtensionManager.get_instance()54 l2network_db_v2.initialize()55 app = config.load_paste_app('extensions_test_app')56 ext_middleware = extensions.ExtensionMiddleware(app, ext_mgr=ext_mgr)57 self.api = webtest.TestApp(ext_middleware)58 def _setUp2(self):59 db._ENGINE = None60 db._MAKER = None61 # Ensure 'stale' patched copies of the plugin are never returned62 manager.QuantumManager._instance = None63 # Ensure existing ExtensionManager is not used64 extensions.PluginAwareExtensionManager._instance = None65 # Save the global RESOURCE_ATTRIBUTE_MAP66 self.saved_attr_map = {}67 for resource, attrs in attributes.RESOURCE_ATTRIBUTE_MAP.iteritems():68 self.saved_attr_map[resource] = attrs.copy()69 # Create the default configurations70 args = ['--config-file', test_extensions.etcdir('quantum.conf.test')]71 config.parse(args=args)72 # Update the plugin and extensions path73 cfg.CONF.set_override('core_plugin', TARGET_PLUGIN)74 self._plugin_patcher = mock.patch(TARGET_PLUGIN, autospec=True)75 self.plugin = self._plugin_patcher.start()76 ext_mgr = extensions.PluginAwareExtensionManager.get_instance()77 l2network_db_v2.initialize()78 app = config.load_paste_app('extensions_test_app')79 ext_middleware = extensions.ExtensionMiddleware(app, ext_mgr=ext_mgr)80 self.api = webtest.TestApp(ext_middleware)81 def tearDown(self):82 self._plugin_patcher.stop()83 self.api = None84 self.plugin = None85 db._ENGINE = None86 db._MAKER = None87 cfg.CONF.reset()88 # Restore the global RESOURCE_ATTRIBUTE_MAP89 attributes.RESOURCE_ATTRIBUTE_MAP = self.saved_attr_map90 def test_quotas_loaded_right(self):91 res = self.api.get(_get_path('quotas'))92 self.assertEquals(200, res.status_int)93 def test_quotas_defaul_values(self):94 tenant_id = 'tenant_id1'95 env = {'quantum.context': context.Context('', tenant_id)}96 res = self.api.get(_get_path('quotas', id=tenant_id),97 extra_environ=env)98 self.assertEquals(10, res.json['quota']['network'])99 self.assertEquals(10, res.json['quota']['subnet'])100 self.assertEquals(50, res.json['quota']['port'])101 self.assertEquals(-1, res.json['quota']['extra1'])102 def test_show_quotas_with_admin(self):103 tenant_id = 'tenant_id1'104 env = {'quantum.context': context.Context('', tenant_id + '2',105 is_admin=True)}106 res = self.api.get(_get_path('quotas', id=tenant_id),107 extra_environ=env)108 self.assertEquals(200, res.status_int)109 def test_show_quotas_without_admin_forbidden(self):110 tenant_id = 'tenant_id1'111 env = {'quantum.context': context.Context('', tenant_id + '2',112 is_admin=False)}113 res = self.api.get(_get_path('quotas', id=tenant_id),114 extra_environ=env, expect_errors=True)115 self.assertEquals(403, res.status_int)116 def test_update_quotas_without_admin_forbidden(self):117 tenant_id = 'tenant_id1'118 env = {'quantum.context': context.Context('', tenant_id,119 is_admin=False)}120 quotas = {'quota': {'network': 100}}121 res = self.api.put_json(_get_path('quotas', id=tenant_id,122 fmt='json'),123 quotas, extra_environ=env,124 expect_errors=True)125 self.assertEquals(403, res.status_int)126 def test_update_quotas_with_admin(self):127 tenant_id = 'tenant_id1'128 env = {'quantum.context': context.Context('', tenant_id + '2',129 is_admin=True)}130 quotas = {'quota': {'network': 100}}131 res = self.api.put_json(_get_path('quotas', id=tenant_id, fmt='json'),132 quotas, extra_environ=env)133 self.assertEquals(200, res.status_int)134 env2 = {'quantum.context': context.Context('', tenant_id)}135 res = self.api.get(_get_path('quotas', id=tenant_id),136 extra_environ=env2).json137 self.assertEquals(100, res['quota']['network'])138 def test_delete_quotas_with_admin(self):139 tenant_id = 'tenant_id1'140 env = {'quantum.context': context.Context('', tenant_id + '2',141 is_admin=True)}142 res = self.api.delete(_get_path('quotas', id=tenant_id, fmt='json'),143 extra_environ=env)144 self.assertEquals(204, res.status_int)145 def test_delete_quotas_without_admin_forbidden(self):146 tenant_id = 'tenant_id1'147 env = {'quantum.context': context.Context('', tenant_id,148 is_admin=False)}149 res = self.api.delete(_get_path('quotas', id=tenant_id, fmt='json'),150 extra_environ=env, expect_errors=True)151 self.assertEquals(403, res.status_int)152 def test_quotas_loaded_bad(self):153 self.testflag = 2154 try:155 res = self.api.get(_get_path('quotas'), expect_errors=True)156 self.assertEquals(404, res.status_int)157 except Exception:158 pass...
test_garage.py
Source:test_garage.py
...20 LOGGER.info('test log: '+setup1.car_door('whiteroof'))21 assert 'whiteroof' in setup1.car_door('whiteroof')22 # simple setup23 @pytest.fixture(autouse=True)24 def _setup2(self):25 self.setup2 = garage.Garage('wood')26 @pytest.mark.parametrize('paint_color', ['red', 'blue'])27 def test_coloring(self, paint_color):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!