Best Python code snippet using tempest_python
test_objects.py
Source:test_objects.py
1# Licensed under the Apache License, Version 2.0 (the "License"); you may2# not use this file except in compliance with the License. You may obtain3# a copy of the License at4#5# http://www.apache.org/licenses/LICENSE-2.06#7# Unless required by applicable law or agreed to in writing, software8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the10# License for the specific language governing permissions and limitations11# under the License.12import abc13from tempest.common import tempest_fixtures as fixtures14from tempest.lib.common.utils import data_utils15from tempest.lib import exceptions16from glance_tempest_plugin.tests.rbac.v2 import base as rbac_base17class MetadefV2RbacObjectsTest(rbac_base.RbacMetadefBase):18 def setUp(self):19 # NOTE(pdeore): As we are using global data there is a possibility20 # of invalid results if these tests are executed concurrently, to avoid21 # such conflicts we are using locking to execute metadef tests22 # serially.23 self.useFixture(fixtures.LockFixture('metadef_namespaces'))24 super(MetadefV2RbacObjectsTest, self).setUp()25 @classmethod26 def setup_clients(cls):27 super(MetadefV2RbacObjectsTest, cls).setup_clients()28 if 'project_member' in cls.credentials:29 persona = 'project_member'30 alt_persona = 'project_alt_member'31 elif 'project_reader' in cls.credentials:32 persona = 'project_reader'33 alt_persona = 'project_alt_reader'34 else:35 persona = 'project_admin'36 alt_persona = 'project_alt_admin'37 cls.persona = getattr(cls, 'os_%s' % persona)38 cls.alt_persona = getattr(cls, 'os_%s' % alt_persona)39 cls.project_id = cls.persona.namespace_objects_client.project_id40 cls.alt_project_id = \41 cls.alt_persona.namespace_objects_client.project_id42 cls.objects_client = cls.persona.namespace_objects_client43 def create_objects(self):44 # Create namespace for two different projects45 namespaces = self.create_namespaces()46 client = self.os_project_admin.namespace_objects_client47 namespace_objects = []48 for ns in namespaces:49 if ns['namespace'].startswith(self.project_id):50 client = self.os_project_alt_admin.namespace_objects_client51 object_name = "object_of_%s" % (ns['namespace'])52 namespace_object = client.create_namespace_object(53 ns['namespace'], name=object_name,54 description=data_utils.arbitrary_string())55 obj = {'namespace': ns, 'object': namespace_object}56 namespace_objects.append(obj)57 return namespace_objects58 def assertObjectsList(self, actual_obj, client, owner=None):59 ns = actual_obj['namespace']60 if owner:61 if not (ns['visibility'] == 'public' or62 ns['owner'] == owner):63 self.do_request('list_namespace_objects',64 expected_status=exceptions.NotFound,65 namespace=ns['namespace'],66 client=client)67 else:68 resp = self.do_request('list_namespace_objects',69 expected_status=200,70 namespace=ns['namespace'],71 client=client)72 self.assertIn(actual_obj['object']['name'],73 resp['objects'][0]['name'])74 else:75 resp = self.do_request('list_namespace_objects',76 expected_status=200,77 namespace=ns['namespace'],78 client=client)79 self.assertIn(actual_obj['object']['name'],80 resp['objects'][0]['name'])81class MetadefV2RbacObjectsTemplate(metaclass=abc.ABCMeta):82 @abc.abstractmethod83 def test_create_object(self):84 """Test add_metadef_object policy."""85 pass86 @abc.abstractmethod87 def test_get_object(self):88 """Test get_metadef_object policy."""89 pass90 @abc.abstractmethod91 def test_list_objects(self):92 """Test list_metadef_objects policy."""93 pass94 @abc.abstractmethod95 def test_update_object(self):96 """Test update_metadef_object policy."""97 pass98 @abc.abstractmethod99 def test_delete_object(self):100 """Test delete_metadef_object policy."""101 pass102class ProjectAdminTests(MetadefV2RbacObjectsTest,103 MetadefV2RbacObjectsTemplate):104 credentials = ['project_admin', 'project_alt_admin', 'primary']105 def test_get_object(self):106 ns_objects = self.create_objects()107 # Get all metadef objects with admin role108 for obj in ns_objects:109 resp = self.do_request(110 'show_namespace_object',111 expected_status=200,112 client=self.objects_client,113 namespace=obj['namespace']['namespace'],114 object_name=obj['object']['name'])115 self.assertEqual(obj['object']['name'], resp['name'])116 def test_list_objects(self):117 ns_objects = self.create_objects()118 # list all metadef objects with admin role119 for obj in ns_objects:120 self.assertObjectsList(obj, self.objects_client)121 def test_update_object(self):122 ns_objects = self.create_objects()123 # update all metadef objects with admin role of 'project'124 for obj in ns_objects:125 resp = self.do_request(126 'update_namespace_object',127 expected_status=200,128 namespace=obj['namespace']['namespace'],129 client=self.objects_client,130 object_name=obj['object']['name'],131 name=obj['object']['name'],132 description=data_utils.arbitrary_string(base_text="updated"))133 self.assertNotEqual(obj['object']['description'],134 resp['description'])135 def test_delete_object(self):136 ns_objects = self.create_objects()137 # delete all metadef objects with admin role of 'project'138 for obj in ns_objects:139 self.do_request('delete_namespace_object',140 expected_status=204,141 namespace=obj['namespace']['namespace'],142 object_name=obj['object']['name'],143 client=self.objects_client)144 # Verify the object is deleted successfully145 self.do_request('show_namespace_object',146 expected_status=exceptions.NotFound,147 client=self.objects_client,148 namespace=obj['namespace']['namespace'],149 object_name=obj['object']['name'])150 def test_create_object(self):151 # As this is been covered in other tests for admin role,152 # skipping to test only create objects seperately.153 pass154class ProjectMemberTests(MetadefV2RbacObjectsTest,155 MetadefV2RbacObjectsTemplate):156 credentials = ['project_member', 'project_alt_member', 'project_admin',157 'project_alt_admin', 'primary']158 def test_create_object(self):159 namespaces = self.create_namespaces()160 def assertCreateObjects(namespace, owner, client):161 object_name = "object_of_%s" % (namespace['namespace'])162 expected_status = exceptions.Forbidden163 if (namespace['visibility'] == 'private' and164 namespace['owner'] != owner):165 expected_status = exceptions.NotFound166 self.do_request('create_namespace_object',167 expected_status=expected_status,168 namespace=namespace['namespace'],169 name=object_name,170 client=client)171 # Make sure non admin role of 'project' forbidden to172 # create objects173 for namespace in namespaces:174 assertCreateObjects(namespace, self.project_id,175 self.objects_client)176 def test_get_object(self):177 def assertObjectGet(actual_obj, owner, client):178 ns = actual_obj['namespace']179 expected_status = 200180 if (ns['visibility'] == 'private' and181 ns['owner'] != owner):182 expected_status = exceptions.NotFound183 self.do_request('show_namespace_object',184 expected_status=expected_status,185 namespace=actual_obj['namespace']['namespace'],186 object_name=actual_obj['object']['name'],187 client=client)188 ns_objects = self.create_objects()189 # Get object - member role from 'project' can access all190 # objects of it's own & only objects having public namespace of191 # 'alt_project'192 for obj in ns_objects:193 assertObjectGet(obj, self.project_id, self.objects_client)194 def test_list_objects(self):195 ns_objects = self.create_objects()196 # list objects - member role from 'project' can access all197 # objects of it's own & only objects having public namespace of198 # 'alt_project'199 for obj in ns_objects:200 self.assertObjectsList(obj, self.objects_client, self.project_id)201 def test_update_object(self):202 def assertObjectUpdate(actual_object, owner, client):203 ns = actual_object['namespace']204 expected_status = exceptions.Forbidden205 if (ns['visibility'] == 'private' and206 ns['owner'] != owner):207 expected_status = exceptions.NotFound208 self.do_request('update_namespace_object',209 expected_status=expected_status,210 name=actual_object['object']['name'],211 description=data_utils.arbitrary_string(),212 namespace=actual_object['namespace']['namespace'],213 object_name=actual_object['object']['name'],214 client=client)215 ns_objects = self.create_objects()216 # Make sure non admin role of 'project' not allowed to217 # update objects218 for obj in ns_objects:219 assertObjectUpdate(obj, self.project_id, self.objects_client)220 def test_delete_object(self):221 def assertObjectDelete(actual_obj, owner, client):222 ns = actual_obj['namespace']223 expected_status = exceptions.Forbidden224 if (ns['visibility'] == 'private' and225 ns['owner'] != owner):226 expected_status = exceptions.NotFound227 self.do_request('delete_namespace_object',228 expected_status=expected_status,229 namespace=actual_obj['namespace']['namespace'],230 object_name=actual_obj['object']['name'],231 client=client)232 ns_objects = self.create_objects()233 # Make sure non admin role of 'project' not allowed to234 # delete objects235 for obj in ns_objects:236 assertObjectDelete(obj, self.project_id, self.objects_client)237class ProjectReaderTests(ProjectMemberTests):238 credentials = ['project_reader', 'project_alt_reader', 'project_admin',...
test_images_metadefs_namespace_objects.py
Source:test_images_metadefs_namespace_objects.py
...30 # Create a namespace object31 body = self._create_namespace_object(namespace)32 # Update a namespace object33 up_object_name = data_utils.rand_name('update-object')34 body = self.namespace_objects_client.update_namespace_object(35 namespace['namespace'], body['name'],36 name=up_object_name)37 self.assertEqual(up_object_name, body['name'])38 # Delete a namespace object39 self.namespace_objects_client.delete_namespace_object(40 namespace['namespace'], up_object_name)41 # List namespace objects and validate deletion42 namespace_objects = [43 namespace_object['name'] for namespace_object in44 self.namespace_objects_client.list_namespace_objects(45 namespace['namespace'])['objects']]46 self.assertNotIn(up_object_name, namespace_objects)47 @decorators.idempotent_id('a2a3615e-3b5c-3f6a-a2b1-1ba3574ae738')48 def test_list_meta_namespace_objects(self):...
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!!