Best Python code snippet using tempest_python
test_images_rbac.py
Source:test_images_rbac.py
...64 try:65 rbac_utils.switch_role(self, switchToRbacRole=True)66 # Try uploading an image file67 image_file = moves.cStringIO(data_utils.random_bytes())68 self.client.store_image_file(body['id'], image_file)69 except ValueError as e:70 '''ValueError is being thrown when role doesn't have permission to71 upload file when creating an image'''72 LOG.info("ValueError is being thrown when role doesn't have "73 "permission to upload file when creating an image")74 raise rbac_exceptions.RbacActionFailed(e)75 finally:76 rbac_utils.switch_role(self, switchToRbacRole=False)77 @rbac_rule_validation.action(component="Image", service="glance",78 rule="delete_image")79 @test.idempotent_id('3b5c341e-645b-11e6-ac4f-080027d0d606')80 def test_delete_image(self):81 """82 Delete created image83 """84 try:85 image_name = data_utils.rand_name('image')86 body = self.client.create_image(name=image_name,87 container_format='bare',88 disk_format='raw',89 visibility='public')90 image_id = body.get('id')91 # Toggle role and delete created image92 rbac_utils.switch_role(self, switchToRbacRole=True)93 self.client.delete_image(image_id)94 self.client.wait_for_resource_deletion(image_id)95 finally:96 rbac_utils.switch_role(self, switchToRbacRole=False)97 images = self.client.list_images()['images']98 images = [image['id'] for image in images]99 if image_id in images:100 self.client.delete_image(image_id)101 self.client.wait_for_resource_deletion(image_id)102 @rbac_rule_validation.action(component="Image", service="glance",103 rule="get_image")104 @test.idempotent_id('3085c7c6-645b-11e6-ac4f-080027d0d606')105 def test_show_image(self):106 """107 Get created image108 """109 try:110 image_name = data_utils.rand_name('image')111 body = self.client.create_image(name=image_name,112 container_format='bare',113 disk_format='raw',114 visibility='private')115 image_id = body.get('id')116 self.addCleanup(self.client.delete_image, image_id)117 # Toggle role and get created image118 rbac_utils.switch_role(self, switchToRbacRole=True)119 self.client.show_image(image_id)120 finally:121 rbac_utils.switch_role(self, switchToRbacRole=False)122 @rbac_rule_validation.action(component="Image", service="glance",123 rule="get_images")124 @test.idempotent_id('bf1a4e94-645b-11e6-ac4f-080027d0d606')125 def test_list_images(self):126 """127 List all the images128 """129 try:130 # Toggle role and get created image131 rbac_utils.switch_role(self, switchToRbacRole=True)132 self.client.list_images()133 finally:134 rbac_utils.switch_role(self, switchToRbacRole=False)135 @rbac_rule_validation.action(component="Image", service="glance",136 rule="modify_image")137 @test.idempotent_id('32ecf48c-645e-11e6-ac4f-080027d0d606')138 def test_update_image(self):139 """140 Update given images141 """142 try:143 image_name = data_utils.rand_name('image')144 body = self.client.create_image(name=image_name,145 container_format='bare',146 disk_format='raw',147 visibility='private')148 image_id = body.get('id')149 self.addCleanup(self.client.delete_image, image_id)150 # Now try uploading an image file151 image_file = moves.cStringIO(data_utils.random_bytes())152 self.client.store_image_file(image_id, image_file)153 # Toggle role and update created image154 rbac_utils.switch_role(self, switchToRbacRole=True)155 new_image_name = data_utils.rand_name('new-image')156 body = self.client.update_image(image_id, [157 dict(replace='/name', value=new_image_name)])158 finally:159 rbac_utils.switch_role(self, switchToRbacRole=False)160 @rbac_rule_validation.action(component="Image", service="glance",161 rule="publicize_image")162 @test.idempotent_id('0ea4809c-6461-11e6-ac4f-080027d0d606')163 def test_publicize_image(self):164 """165 Publicize Image166 """167 try:168 image_name = data_utils.rand_name('image')169 rbac_utils.switch_role(self, switchToRbacRole=True)170 self.create_image(name=image_name,171 container_format='bare',172 disk_format='raw',173 visibility='public')174 finally:175 rbac_utils.switch_role(self, switchToRbacRole=False)176 @rbac_rule_validation.action(component="Image", service="glance",177 rule="deactivate")178 @test.idempotent_id('b488458c-65df-11e6-9947-080027824017')179 def test_deactivate_image(self):180 """181 Deactivate Image Test182 """183 try:184 uuid = '00000000-1111-2222-3333-444455556666'185 image_name = data_utils.rand_name('image')186 body = self.create_image(name=image_name,187 container_format='bare',188 disk_format='raw',189 visibility='private',190 ramdisk_id=uuid)191 image_id = body.get('id')192 # Now try uploading an image file193 image_file = moves.cStringIO(data_utils.random_bytes())194 self.client.store_image_file(image_id=image_id, data=image_file)195 # Toggling role and deacivate image196 rbac_utils.switch_role(self, switchToRbacRole=True)197 self.client.deactivate_image(image_id)198 finally:199 rbac_utils.switch_role(self, switchToRbacRole=False)200 @rbac_rule_validation.action(component="Image", service="glance",201 rule="reactivate")202 @test.idempotent_id('d3fa28b8-65df-11e6-9947-080027824017')203 def test_reactivate_image(self):204 """205 Reactivate Image Test206 """207 try:208 uuid = '00000000-1111-2222-3333-444455556666'209 image_name = data_utils.rand_name('image')210 body = self.create_image(name=image_name,211 container_format='bare',212 disk_format='raw',213 visibility='private',214 ramdisk_id=uuid)215 # Now try uploading an image file216 image_id = body.get('id')217 image_file = moves.cStringIO(data_utils.random_bytes())218 self.client.store_image_file(image_id=image_id, data=image_file)219 # Toggling role and reactivate image220 rbac_utils.switch_role(self, switchToRbacRole=True)221 self.client.reactivate_image(image_id)222 finally:...
select_music_images.py
Source:select_music_images.py
...44 cur.execute(sql, (artist_id, link, width, height, ok))45 id_ = cur.fetchone()[0]46 conn.commit()47 return id_48def store_image_file(conn, image_id, image):49 sql = "INSERT INTO image_files(image_id, image) VALUES(%s,%s);"50 cur = conn.cursor()51 cur.execute(sql, (image_id, image))52 conn.commit()53 #return cur.lastrowid54 return55def get_google_images(name):56 print(f"**************Google Custom Search Engine Request for {name} **************")57 http = httplib2.Http()58 service = discovery.build('customsearch', 'v1',59 developerKey=google_api_key, http=http)60 z = service.cse().list(q=f'"{name}" music', searchType='image', imgType='face', #alternative type is photo61 imgSize='XXLARGE', num=10, 62 cx='007924195092800608279:0o2y8a3v-kw').execute() 63 images = []64 for data in z.get('items', []): 65 image=Image(data['link'],66 data['image']['width'],67 data['image']['height'],68 True) # is image OK - really not necessary anymore69 images.append(image)70 return images 71def get_page(topic):72 try:73 page = wikipedia.page(topic, auto_suggest=False) # I changed auto_suggest = False to the default (I changed page function in wikipedia.py74 except Exception as e:75 print(f"Couldn't find {topic} wikipedia: {e}")76 return77 return page78def get_all_wikipedia_image_uris(page):79 uri_list = list()80 for uri in page.images: 81 pos = uri.rfind('.')82 if uri[pos:].lower() in [".jpg", ".jpeg"]:83 uri_list.append(uri)84 return uri_list85if __name__ == "__main__":86 if len(sys.argv) == 1:87 print("You need to supply a name!")88 sys.exit(1)89 sql = "SELECT id,name FROM artists WHERE LOWER(name)=LOWER(%s);"90 cur.execute(sql, (sys.argv[1],))91 row = cur.fetchone()92 if row:93 artist_id, name = row94 else:95 print(f"Can't find {sys.argv[1]}!!")96 sys.exit(1)97 print(f"\x1b[1m{artist_id} {name}\x1b[0m\n")98 cur.execute("SELECT pg_size_pretty(pg_total_relation_size('image_files'));")99 pre_size = cur.fetchone()[0]100 response = input("Do you want to look at the images currently in the database? ")101 if response[0].lower() == 'y':102 cur.execute(f"SELECT id,link,width,height FROM images WHERE artist_id={artist_id}")103 rows = cur.fetchall()104 for row in rows:105 image_id, link, width, height = row106 print() # line feed107 cur.execute("SELECT image FROM image_files WHERE image_id=%s", (image_id,))108 row = cur.fetchone()109 if row:110 show_image(BytesIO(row[0]))111 print()112 response = input("This image is being stored as bytes in the database - do you want to KEEP it? ")113 if response[0].lower() != 'y':114 cur.execute("DELETE FROM image_files WHERE image_id=%s", (image_id,))115 cur.execute("DELETE FROM images WHERE id=%s", (image_id,))116 conn.commit()117 else:118 display_image(link)119 print()120 response = input("This image is NOT being stored as bytes yet - do you want to KEEP it? ")121 if response[0].lower() == 'y':122 f = generate_image(link, sonos_image_size, sonos_image_size)123 image = f.getvalue()124 store_image_file(conn, image_id, image)125 else: 126 cur.execute("DELETE FROM images WHERE link=%s", (link,))127 conn.commit()128 response = input("Do you want to look for images in the wikipedia? ")129 if response[0].lower() == 'y':130 wiki_page = get_page(name)131 if wiki_page:132 uris = get_all_wikipedia_image_uris(wiki_page)133 if not uris:134 print(f"Could not find any images for {name} on their wiki page!")135 for uri in uris:136 display_image(uri, erase=False) # don't change size of image137 cur.execute("SELECT COUNT(1) FROM images WHERE link=%s;", (uri,))138 row = cur.fetchone()139 if row[0] == 1:140 print("We already have that url in the database!")141 continue142 response = input("Do you want to put this image into the database? ")143 if response[0].lower() == 'y':144 f,width,height = generate_image2(uri, sonos_image_size, sonos_image_size)145 image_id = store_image(conn, artist_id, uri, width, height, True)146 image = f.getvalue()147 store_image_file(conn, image_id, image)148 print("We stored the image data and the image file!")149 else:150 print(f"Could not find {name} page!")151 response = input("Do you want to do a google search for images? ")152 if response[0].lower() == 'y':153 images = get_google_images(name)154 if images:155 for image in images:156 result = display_image(image.link, erase=False) # don't change size of image157 if not result:158 continue159 cur.execute("SELECT COUNT(1) FROM images WHERE link=%s;", (image.link,))160 row = cur.fetchone()161 if row[0] == 1:162 print("We already have that url in the database!")163 continue164 response = input("Do you want to use this image? ")165 if response[0].lower() == 'y':166 image_id = store_image(conn, artist_id, image.link, image.width, image.height, True)167 f = generate_image(image.link, sonos_image_size, sonos_image_size)168 image = f.getvalue()169 store_image_file(conn, image_id, image)170 print("We stored the image data and the image file!")171 else: 172 print(f"Could not find any google images for {name}!")173 174 cur.execute("SELECT pg_size_pretty(pg_total_relation_size('image_files'));")175 post_size = cur.fetchone()[0]...
test_store_image_file_negative.py
Source:test_store_image_file_negative.py
...33 1) Using a previously created image, store image file with invalid34 content type35 2) Verify response code is 41536 """37 response = self.images_client.store_image_file(38 self.image.id_, self.test_file,39 content_type="invalid_content_type")40 self.assertEqual(response.status_code, 415)41 @tags(type='negative', regression='true', skipable='true')42 def test_store_image_file_with_blank_image_id(self):43 """44 @summary: Store image file with blank image id45 1) Store image file with blank image id46 2) Verify response code is 40447 """48 response = self.images_client.store_image_file(49 image_id="", file_data=self.test_file)50 self.assertEqual(response.status_code, 404)51 @tags(type='negative', regression='true', skipable='true')52 def test_store_image_file_with_invalid_image_id(self):53 """54 @summary: Store image file with invalid image id55 1) Store image file with invalid image id56 2) Verify response code is 40457 """58 response = self.images_client.store_image_file(59 image_id="invalid_id", file_data=self.test_file)60 self.assertEqual(response.status_code, 404)61 @tags(type='negative', regression='true', skipable='true')62 def test_store_image_file_with_duplicate_data(self):63 """64 @summary: Store image file with duplicate data65 1) Using a previously created image, store image file66 2) Verify response code is 20467 3) Get the image68 4) Verify that the response contains an image entity with the correct69 properties70 5) Try to store image file with duplicate data71 6) Verify response code is 40972 """73 response = self.images_client.store_image_file(74 self.image.id_, self.test_file)75 self.assertEqual(response.status_code, 204)76 response = self.images_client.get_image(image_id=self.image.id_)77 self.assertEqual(response.status_code, 200)78 updated_image = response.entity79 self.assertEqual(updated_image.id_, self.image.id_)80 self.assertEqual(updated_image.status, ImageStatus.ACTIVE)81 self.assertEqual(updated_image.size, len(self.test_file))82 self.assertIsNotNone(updated_image.checksum)83 response = self.images_client.store_image_file(84 self.image.id_, self.test_file)...
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!!