Best Python code snippet using tempest_python
test_images_oneserver.py
Source:test_images_oneserver.py
...78 # Return an error while trying to delete another tenant's image79 self.servers_client.wait_for_server_status(self.server['id'], 'ACTIVE')80 snapshot_name = rand_name('test-snap-')81 resp, body = self.client.create_image(self.server['id'], snapshot_name)82 image_id = parse_image_id(resp['location'])83 self.image_ids.append(image_id)84 self.client.wait_for_image_resp_code(image_id, 200)85 self.client.wait_for_image_status(image_id, 'ACTIVE')86 # Delete image87 self.assertRaises(exceptions.NotFound,88 self.alt_client.delete_image, image_id)89 @attr(type='smoke')90 @testtools.skipUnless(compute.CREATE_IMAGE_ENABLED,91 'Environment unable to create images.')92 def test_create_delete_image(self):93 # Create a new image94 name = rand_name('image')95 meta = {'image_type': 'test'}96 resp, body = self.client.create_image(self.server['id'], name, meta)97 self.assertEqual(202, resp.status)98 image_id = parse_image_id(resp['location'])99 self.client.wait_for_image_resp_code(image_id, 200)100 self.client.wait_for_image_status(image_id, 'ACTIVE')101 # Verify the image was created correctly102 resp, image = self.client.get_image(image_id)103 self.assertEqual(name, image['name'])104 self.assertEqual('test', image['metadata']['image_type'])105 # Verify minRAM and minDisk values are the same as the original image106 resp, original_image = self.client.get_image(self.image_ref)107 self.assertEqual(original_image['minRam'], image['minRam'])108 self.assertEqual(original_image['minDisk'], image['minDisk'])109 @attr(type='negative')110 @testtools.skipUnless(compute.MULTI_USER,111 'Need multiple users for this test.')112 def test_create_image_for_server_in_another_tenant(self):113 # Creating image of another tenant's server should be return error114 snapshot_name = rand_name('test-snap-')115 self.assertRaises(exceptions.NotFound, self.alt_client.create_image,116 self.server['id'], snapshot_name)117 @attr(type='negative')118 def test_create_second_image_when_first_image_is_being_saved(self):119 # Disallow creating another image when first image is being saved120 # Create first snapshot121 snapshot_name = rand_name('test-snap-')122 resp, body = self.client.create_image(self.server['id'],123 snapshot_name)124 self.assertEqual(202, resp.status)125 image_id = parse_image_id(resp['location'])126 self.image_ids.append(image_id)127 # Create second snapshot128 alt_snapshot_name = rand_name('test-snap-')129 self.assertRaises(exceptions.Duplicate, self.client.create_image,130 self.server['id'], alt_snapshot_name)131 self.client.wait_for_image_status(image_id, 'ACTIVE')132 @attr(type='negative')133 def test_create_image_specify_name_over_256_chars(self):134 # Return an error if snapshot name over 256 characters is passed135 snapshot_name = rand_name('a' * 260)136 self.assertRaises(exceptions.BadRequest, self.client.create_image,137 self.server['id'], snapshot_name)138 @attr(type='negative')139 def test_delete_image_that_is_not_yet_active(self):140 # Return an error while trying to delete an image what is creating141 snapshot_name = rand_name('test-snap-')142 resp, body = self.client.create_image(self.server['id'], snapshot_name)143 self.assertEqual(202, resp.status)144 image_id = parse_image_id(resp['location'])145 self.image_ids.append(image_id)146 # Do not wait, attempt to delete the image, ensure it's successful147 resp, body = self.client.delete_image(image_id)148 self.assertEqual('204', resp['status'])149 self.image_ids.remove(image_id)150 self.assertRaises(exceptions.NotFound, self.client.get_image, image_id)151class ImagesOneServerTestXML(ImagesOneServerTestJSON):...
util.py
Source:util.py
...66 )67 return resource(68 credential, subscription_id=parameters["subscriptionId"], **parameters69 )70def parse_image_id(image_id):71 """Extract publisher, offer, sku and optional version from image_id.72 The image_id is expected to be a string in the following73 format: Canonical:UbuntuServer:19.10-DAILY[:latest]74 Args:75 image_id: string, The image id76 Returns77 Dict with publisher, offer and sku and optional version keys.78 """79 match = re.match(RE_AZURE_IMAGE_ID, image_id)80 if not match:81 # Snapshot image ids do not follow the publisher:offer:sku pattern82 return {}83 return match.groupdict()84def get_resource_group_name_from_id(resource_id):85 """Retrieve the resource group name of a resource.86 Args:87 resource_id: string, the resource id88 Returns:89 A string representing the resource group90 """91 return resource_id.split("/")[4]92def get_resource_name_from_id(resource_id):93 """Retrieve the name of a resource.94 Args:95 resource_id: string, the resource id96 Returns:97 A string representing the resource name98 """99 return resource_id.split("/")[-1]100def get_image_reference_params(image_id):101 """Return the correct parameter for image reference based on image id.102 Verify if the image id is associated with a current image found103 on Azure Marketplace or a custom image, for example, created through104 a snapshot process. Depending on the image id format, we can105 differentiate if we should create image parameters for a106 Marketplace image or a custom image.107 Args:108 image_id: string, Represents a image to be used when provisioning109 a virtual machine110 Returns:111 A dict representing the image reference parameters that will be112 used to provision a virtual machine113 """114 img_dict = parse_image_id(image_id)115 if img_dict.get("publisher") == "Canonical":116 # If the image id starts with 'Canonical", we know that it is a117 # marketplace image, and to we must reference it using the118 # combination of publisher, offer, sku and version info119 img_dict.update({"version": "latest"})120 return img_dict121 # Custom images can be directly referenced by their id122 return {"id": image_id}123def is_pro_image(image_id, registered_image):124 """Verify if the image id represents a pro image.125 Check the image id string for patterns found only on126 pro images. However, snapshot images do not have pro127 information on ther image id. We are enconding that128 information on the registed_image dict, which represents129 the base image that created the snapshot. Therefore,130 we fail at looking in the image id string, we look it up131 at the registered_image dict.132 Args:133 image_id: string, Represents a image to be used when provisioning134 a virtual machine135 registered_image: dict, Represents the base image used for creating136 the image referenced by image_id. This will only137 happen for snapshot images.138 Returns:139 A boolean indicating if the image is pro image140 """141 offer = ""142 img_dict = parse_image_id(image_id)143 if img_dict.get("publisher") == "Canonical":144 offer = img_dict["offer"]145 elif registered_image is not None:146 offer = registered_image["offer"] or ""147 return bool("-pro-" in offer)148def get_plan_params(image_id, registered_image):149 """Return the correct parameter for plan based on pro image id.150 Args:151 image_id: string, Represents a image to be used when provisioning152 a virtual machine153 registered_image: dict, Represents the base image used for creating154 the image referenced by image_id. This will only155 happen for snapshot images.156 Returns:157 A dict representing the plan parameters that will be158 used to provision a virtual machine159 """160 if registered_image is not None:161 return {162 "name": registered_image["sku"],163 "product": registered_image["offer"],164 "publisher": "canonical",165 }166 img_dict = parse_image_id(image_id)167 return {168 "name": img_dict.get("sku"),169 "product": img_dict.get("offer"),170 "publisher": img_dict.get("publisher", "").lower(),...
test_load_image.py
Source:test_load_image.py
...11 ],12)13def test_parse_image_id_valid(truth_file, correct_image_id, correct_attribute_id):14 image_pair = load_image.ImagePair(truth_file=pathlib.Path(truth_file))15 image_pair.parse_image_id()16 assert image_pair.image_id == correct_image_id17 assert image_pair.attribute_id == correct_attribute_id18@pytest.mark.parametrize('truth_file', ['/LICENSE.txt', '/ISIC_123456.png'])19def test_parse_image_id_invalid(truth_file):20 image_pair = load_image.ImagePair(truth_file=pathlib.Path(truth_file))21 with pytest.raises(Exception):22 image_pair.parse_image_id()23@pytest.mark.parametrize(24 'test_image_name', ['binary.png', 'monochrome.png', 'monochrome_png_noext', 'monochrome.jpg']25)26def test_load_segmentation_image_valid(test_images_path, test_image_name):27 image_path = test_images_path / test_image_name28 np_image = load_image.load_segmentation_image(image_path)29 assert np_image is not None30@pytest.mark.parametrize('test_image_name', ['empty.png', 'random_bytes.png', 'rgb.png'])31def test_load_segmentation_image_invalid(test_images_path, test_image_name):32 image_path = test_images_path / test_image_name33 with pytest.raises(ScoreError):...
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!!