Best Python code snippet using tempest_python
test_volume_types.py
Source:test_volume_types.py
1# All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may4# not use this file except in compliance with the License. You may obtain5# a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14import mock15from rally import exceptions as rally_exceptions16from rally_openstack.scenarios.cinder import volume_types17from tests.unit import test18CINDER_V2_PATH = ("rally_openstack.services.storage"19 ".cinder_v2.CinderV2Service")20class CinderVolumeTypesTestCase(test.ScenarioTestCase):21 def setUp(self):22 super(CinderVolumeTypesTestCase, self).setUp()23 patch = mock.patch(24 "rally_openstack.services.storage.block.BlockStorage")25 self.addCleanup(patch.stop)26 self.mock_cinder = patch.start()27 def _get_context(self):28 context = test.get_test_context()29 context.update({30 "admin": {31 "id": "fake_user_id",32 "credential": mock.MagicMock()33 },34 "user": {"id": "fake_user_id",35 "credential": mock.MagicMock()},36 "tenant": {"id": "fake", "name": "fake"}})37 return context38 def test_create_and_get_volume_type(self):39 mock_service = self.mock_cinder.return_value40 scenario = volume_types.CreateAndGetVolumeType(self._get_context())41 description = "rally tests creating types"42 is_public = False43 scenario.run(description=description, is_public=is_public)44 mock_service.create_volume_type.assert_called_once_with(45 description=description, is_public=is_public)46 mock_service.get_volume_type.assert_called_once_with(47 mock_service.create_volume_type.return_value)48 def test_create_and_delete_volume_type(self):49 mock_service = self.mock_cinder.return_value50 scenario = volume_types.CreateAndDeleteVolumeType(self._get_context())51 description = "rally tests creating types"52 is_public = False53 scenario.run(description=description, is_public=is_public)54 mock_service.create_volume_type.assert_called_once_with(55 description=description, is_public=is_public)56 mock_service.delete_volume_type.assert_called_once_with(57 mock_service.create_volume_type.return_value)58 def test_create_and_delete_encryption_type(self):59 mock_service = self.mock_cinder.return_value60 context = self._get_context()61 context.update({62 "volume_types": [{"id": "fake_id",63 "name": "fake_name"}],64 "iteration": 1})65 scenario = volume_types.CreateAndDeleteEncryptionType(66 context)67 # case: create_specs is None68 specs = {69 "provider": "prov",70 "cipher": "cip",71 "key_size": "ks",72 "control_location": "cl"73 }74 scenario.run(create_specs=None, provider="prov", cipher="cip",75 key_size="ks", control_location="cl")76 mock_service.create_encryption_type.assert_called_once_with(77 "fake_id", specs=specs)78 mock_service.delete_encryption_type.assert_called_once_with(79 "fake_id")80 # case: create_specs is not None81 scenario.run(create_specs="fakecreatespecs", provider="prov",82 cipher="cip", key_size="ks", control_location="cl")83 mock_service.create_encryption_type.assert_called_with(84 "fake_id", specs="fakecreatespecs")85 mock_service.delete_encryption_type.assert_called_with(86 "fake_id")87 def test_create_get_and_delete_encryption_type(self):88 mock_service = self.mock_cinder.return_value89 context = self._get_context()90 context.update({91 "volume_types": [{"id": "fake_id",92 "name": "fake_name"}],93 "iteration": 1})94 scenario = volume_types.CreateGetAndDeleteEncryptionType(95 context)96 specs = {97 "provider": "prov",98 "cipher": "cip",99 "key_size": "ks",100 "control_location": "cl"101 }102 scenario.run(provider="prov", cipher="cip",103 key_size="ks", control_location="cl")104 mock_service.create_encryption_type.assert_called_once_with(105 "fake_id", specs=specs)106 mock_service.get_encryption_type.assert_called_once_with(107 "fake_id")108 mock_service.delete_encryption_type.assert_called_once_with(109 "fake_id")110 def test_create_and_list_volume_types(self):111 mock_service = self.mock_cinder.return_value112 fake_type = mock.Mock()113 pool_list = [mock.Mock(), mock.Mock(), fake_type]114 description = "rally tests creating types"115 is_public = False116 scenario = volume_types.CreateAndListVolumeTypes(self._get_context())117 mock_service.create_volume_type.return_value = fake_type118 mock_service.list_types.return_value = pool_list119 scenario.run(description=description, is_public=is_public)120 mock_service.create_volume_type.assert_called_once_with(121 description=description, is_public=is_public)122 mock_service.list_types.assert_called_once_with()123 def test_create_and_list_volume_types_with_fails(self):124 # Negative case: type isn't listed125 mock_service = self.mock_cinder.return_value126 fake_type = mock.Mock()127 pool_list = [mock.Mock(), mock.Mock(), mock.Mock()]128 description = "rally tests creating types"129 is_public = False130 scenario = volume_types.CreateAndListVolumeTypes(self._get_context())131 mock_service.create_volume_type.return_value = fake_type132 mock_service.list_types.return_value = pool_list133 self.assertRaises(rally_exceptions.RallyAssertionError,134 scenario.run,135 description=description, is_public=is_public)136 mock_service.create_volume_type.assert_called_once_with(137 description=description, is_public=is_public)138 mock_service.list_types.assert_called_once_with()139 @mock.patch("%s.create_volume_type" % CINDER_V2_PATH)140 @mock.patch("%s.update_volume_type" % CINDER_V2_PATH)141 def test_create_and_update_volume_type(self, mock_update_volume_type,142 mock_create_volume_type):143 scenario = volume_types.CreateAndUpdateVolumeType(self._get_context())144 fake_type = mock.MagicMock()145 fake_type.name = "any"146 create_description = "test create"147 update_description = "test update"148 mock_create_volume_type.return_value = fake_type149 scenario.run(description=create_description,150 update_description=update_description)151 mock_create_volume_type.assert_called_once_with(152 description=create_description,153 is_public=True)154 mock_update_volume_type.assert_called_once_with(155 fake_type, name="any",156 description=update_description,157 is_public=None)158 def test_create_volume_type_and_encryption_type(self):159 mock_service = self.mock_cinder.return_value160 scenario = volume_types.CreateVolumeTypeAndEncryptionType(161 self._get_context())162 description = "rally tests creating types"163 is_public = False164 # case: create_specs is None165 specs = {166 "provider": "prov",167 "cipher": "cip",168 "key_size": "ks",169 "control_location": "cl"170 }171 scenario.run(create_specs=None, provider="prov", cipher="cip",172 key_size="ks", control_location="cl",173 description=description, is_public=is_public)174 mock_service.create_volume_type.assert_called_once_with(175 description=description, is_public=is_public)176 mock_service.create_encryption_type.assert_called_once_with(177 mock_service.create_volume_type.return_value, specs=specs)178 # case: create_specs is not None179 scenario.run(create_specs="fakecreatespecs", provider="prov",180 cipher="cip", key_size="ks", control_location="cl",181 description=description, is_public=is_public)182 mock_service.create_volume_type.assert_called_with(183 description=description, is_public=is_public)184 mock_service.create_encryption_type.assert_called_with(185 mock_service.create_volume_type.return_value,186 specs="fakecreatespecs")187 def test_create_and_list_encryption_type(self):188 mock_service = self.mock_cinder.return_value189 context = self._get_context()190 context.update({191 "volume_types": [{"id": "fake_id",192 "name": "fake_name"}],193 "iteration": 1})194 scenario = volume_types.CreateAndListEncryptionType(195 context)196 # case: create_specs is None197 specs = {198 "provider": "prov",199 "cipher": "cip",200 "key_size": "ks",201 "control_location": "cl"202 }203 scenario.run(create_specs=None, provider="prov", cipher="cip",204 key_size="ks", control_location="cl",205 search_opts="fakeopts")206 mock_service.create_encryption_type.assert_called_once_with(207 "fake_id", specs=specs)208 mock_service.list_encryption_type.assert_called_once_with(209 "fakeopts")210 # case: create_specs is not None211 scenario.run(create_specs="fakecreatespecs", provider="prov",212 cipher="cip", key_size="ks", control_location="cl",213 search_opts="fakeopts")214 mock_service.create_encryption_type.assert_called_with(215 "fake_id", specs="fakecreatespecs")216 mock_service.list_encryption_type.assert_called_with(217 "fakeopts")218 def test_create_and_set_volume_type_keys(self):219 mock_service = self.mock_cinder.return_value220 volume_type_key = {"volume_backend_name": "LVM_iSCSI"}221 description = "rally tests creating types"222 is_public = False223 scenario = volume_types.CreateAndSetVolumeTypeKeys(224 self._get_context())225 scenario.run(volume_type_key, description=description,226 is_public=is_public)227 mock_service.create_volume_type.assert_called_once_with(228 description=description, is_public=is_public)229 mock_service.set_volume_type_keys.assert_called_once_with(230 mock_service.create_volume_type.return_value,231 metadata=volume_type_key)232 def test_create_and_update_encryption_type(self):233 mock_service = self.mock_cinder.return_value234 context = self._get_context()235 context.update({236 "volume_types": [{"id": "fake_id",237 "name": "fake_name"}],238 "iteration": 1})239 scenario = volume_types.CreateAndUpdateEncryptionType(240 context)241 create_specs = {242 "provider": "create_prov",243 "cipher": "create_cip",244 "key_size": "create_ks",245 "control_location": "create_cl"246 }247 update_specs = {248 "provider": "update_prov",249 "cipher": "update_cip",250 "key_size": "update_ks",251 "control_location": "update_cl"252 }253 scenario.run(create_provider="create_prov", create_cipher="create_cip",254 create_key_size="create_ks",255 create_control_location="create_cl",256 update_provider="update_prov", update_cipher="update_cip",257 update_key_size="update_ks",258 update_control_location="update_cl")259 mock_service.create_encryption_type.assert_called_once_with(260 "fake_id", specs=create_specs)261 mock_service.update_encryption_type.assert_called_once_with(262 "fake_id", specs=update_specs)263 @mock.patch("%s.list_type_access" % CINDER_V2_PATH)264 @mock.patch("%s.add_type_access" % CINDER_V2_PATH)265 @mock.patch("%s.create_volume_type" % CINDER_V2_PATH)266 def test_create_volume_type_add_and_list_type_access(267 self, mock_create_volume_type, mock_add_type_access,268 mock_list_type_access):269 scenario = volume_types.CreateVolumeTypeAddAndListTypeAccess(270 self._get_context())271 fake_type = mock.Mock()272 mock_create_volume_type.return_value = fake_type273 scenario.run(description=None, is_public=False)274 mock_create_volume_type.assert_called_once_with(275 description=None, is_public=False)276 mock_add_type_access.assert_called_once_with(fake_type, project="fake")...
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!!