Best Python code snippet using tempest_python
test_images.py
Source:test_images.py  
...18from tempest import config19from tempest import exceptions20from tempest import test21CONF = config.CONF22def get_container_and_disk_format():23    a_formats = ['ami', 'ari', 'aki']24    container_format = CONF.image.container_formats[0]25    disk_format = CONF.image.disk_formats[0]26    if container_format in a_formats and container_format != disk_format:27        msg = ("The container format and the disk format don't match. "28               "Contaiter format: %(container)s, Disk format: %(disk)s." %29               {'container': container_format, 'disk': disk_format})30        raise exceptions.InvalidConfiguration(message=msg)31    return container_format, disk_format32class CreateRegisterImagesTest(base.BaseV1ImageTest):33    """Here we test the registration and creation of images."""34    @test.idempotent_id('3027f8e6-3492-4a11-8575-c3293017af4d')35    def test_register_then_upload(self):36        # Register, then upload an image37        properties = {'prop1': 'val1'}38        container_format, disk_format = get_container_and_disk_format()39        body = self.create_image(name='New Name',40                                 container_format=container_format,41                                 disk_format=disk_format,42                                 is_public=False,43                                 properties=properties)44        self.assertIn('id', body)45        image_id = body.get('id')46        self.assertEqual('New Name', body.get('name'))47        self.assertFalse(body.get('is_public'))48        self.assertEqual('queued', body.get('status'))49        for key, val in properties.items():50            self.assertEqual(val, body.get('properties')[key])51        # Now try uploading an image file52        image_file = moves.cStringIO(data_utils.random_bytes())53        body = self.client.update_image(image_id, data=image_file)['image']54        self.assertIn('size', body)55        self.assertEqual(1024, body.get('size'))56    @test.idempotent_id('69da74d9-68a9-404b-9664-ff7164ccb0f5')57    def test_register_remote_image(self):58        # Register a new remote image59        container_format, disk_format = get_container_and_disk_format()60        body = self.create_image(name='New Remote Image',61                                 container_format=container_format,62                                 disk_format=disk_format, is_public=False,63                                 location=CONF.image.http_image,64                                 properties={'key1': 'value1',65                                             'key2': 'value2'})66        self.assertIn('id', body)67        self.assertEqual('New Remote Image', body.get('name'))68        self.assertFalse(body.get('is_public'))69        self.assertEqual('active', body.get('status'))70        properties = body.get('properties')71        self.assertEqual(properties['key1'], 'value1')72        self.assertEqual(properties['key2'], 'value2')73    @test.idempotent_id('6d0e13a7-515b-460c-b91f-9f4793f09816')74    def test_register_http_image(self):75        container_format, disk_format = get_container_and_disk_format()76        body = self.create_image(name='New Http Image',77                                 container_format=container_format,78                                 disk_format=disk_format, is_public=False,79                                 copy_from=CONF.image.http_image)80        self.assertIn('id', body)81        image_id = body.get('id')82        self.assertEqual('New Http Image', body.get('name'))83        self.assertFalse(body.get('is_public'))84        self.client.wait_for_image_status(image_id, 'active')85        self.client.show_image(image_id)86    @test.idempotent_id('05b19d55-140c-40d0-b36b-fafd774d421b')87    def test_register_image_with_min_ram(self):88        # Register an image with min ram89        container_format, disk_format = get_container_and_disk_format()90        properties = {'prop1': 'val1'}91        body = self.create_image(name='New_image_with_min_ram',92                                 container_format=container_format,93                                 disk_format=disk_format,94                                 is_public=False,95                                 min_ram=40,96                                 properties=properties)97        self.assertIn('id', body)98        self.assertEqual('New_image_with_min_ram', body.get('name'))99        self.assertFalse(body.get('is_public'))100        self.assertEqual('queued', body.get('status'))101        self.assertEqual(40, body.get('min_ram'))102        for key, val in properties.items():103            self.assertEqual(val, body.get('properties')[key])104        self.client.delete_image(body['id'])105class ListImagesTest(base.BaseV1ImageTest):106    """Here we test the listing of image information"""107    @classmethod108    def skip_checks(cls):109        super(ListImagesTest, cls).skip_checks()110        if (len(CONF.image.container_formats) < 2111           or len(CONF.image.disk_formats) < 2):112            skip_msg = ("%s skipped as multiple container formats "113                        "or disk formats are not available." % cls.__name__)114            raise cls.skipException(skip_msg)115    @classmethod116    def resource_setup(cls):117        super(ListImagesTest, cls).resource_setup()118        # We add a few images here to test the listing functionality of119        # the images API120        a_formats = ['ami', 'ari', 'aki']121        (cls.container_format,122         cls.container_format_alt) = CONF.image.container_formats[:2]123        cls.disk_format, cls.disk_format_alt = CONF.image.disk_formats[:2]124        if cls.container_format in a_formats:125            cls.disk_format = cls.container_format126        if cls.container_format_alt in a_formats:127            cls.disk_format_alt = cls.container_format_alt128        img1 = cls._create_remote_image('one', cls.container_format,129                                        cls.disk_format)130        img2 = cls._create_remote_image('two', cls.container_format_alt,131                                        cls.disk_format_alt)132        img3 = cls._create_remote_image('dup', cls.container_format,133                                        cls.disk_format)134        img4 = cls._create_remote_image('dup', cls.container_format,135                                        cls.disk_format)136        img5 = cls._create_standard_image('1', cls.container_format_alt,137                                          cls.disk_format_alt, 42)138        img6 = cls._create_standard_image('2', cls.container_format_alt,139                                          cls.disk_format_alt, 142)140        img7 = cls._create_standard_image('33', cls.container_format,141                                          cls.disk_format, 142)142        img8 = cls._create_standard_image('33', cls.container_format,143                                          cls.disk_format, 142)144        cls.created_set = set(cls.created_images)145        # same container format146        cls.same_container_format_set = set((img1, img3, img4, img7, img8))147        # same disk format148        cls.same_disk_format_set = set((img2, img5, img6))149        # 1x with size 42150        cls.size42_set = set((img5,))151        # 3x with size 142152        cls.size142_set = set((img6, img7, img8,))153        # dup named154        cls.dup_set = set((img3, img4))155    @classmethod156    def _create_remote_image(cls, name, container_format, disk_format):157        """Create a new remote image and return newly-registered image-id"""158        name = 'New Remote Image %s' % name159        location = CONF.image.http_image160        image = cls.create_image(name=name,161                                 container_format=container_format,162                                 disk_format=disk_format,163                                 is_public=False,164                                 location=location)165        image_id = image['id']166        return image_id167    @classmethod168    def _create_standard_image(cls, name, container_format,169                               disk_format, size):170        """Create a new standard image and return newly-registered image-id171        Note that the size of the new image is a random number between172        1024 and 4096173        """174        image_file = moves.cStringIO(data_utils.random_bytes(size))175        name = 'New Standard Image %s' % name176        image = cls.create_image(name=name,177                                 container_format=container_format,178                                 disk_format=disk_format,179                                 is_public=False, data=image_file)180        image_id = image['id']181        return image_id182    @test.idempotent_id('246178ab-3b33-4212-9a4b-a7fe8261794d')183    def test_index_no_params(self):184        # Simple test to see all fixture images returned185        images_list = self.client.list_images()['images']186        image_list = map(lambda x: x['id'], images_list)187        for image_id in self.created_images:188            self.assertIn(image_id, image_list)189    @test.idempotent_id('f1755589-63d6-4468-b098-589820eb4031')190    def test_index_disk_format(self):191        images_list = self.client.list_images(192            disk_format=self.disk_format_alt)['images']193        for image in images_list:194            self.assertEqual(image['disk_format'], self.disk_format_alt)195        result_set = set(map(lambda x: x['id'], images_list))196        self.assertTrue(self.same_disk_format_set <= result_set)197        self.assertFalse(self.created_set - self.same_disk_format_set198                         <= result_set)199    @test.idempotent_id('2143655d-96d9-4bec-9188-8674206b4b3b')200    def test_index_container_format(self):201        images_list = self.client.list_images(202            container_format=self.container_format)['images']203        for image in images_list:204            self.assertEqual(image['container_format'], self.container_format)205        result_set = set(map(lambda x: x['id'], images_list))206        self.assertTrue(self.same_container_format_set <= result_set)207        self.assertFalse(self.created_set - self.same_container_format_set208                         <= result_set)209    @test.idempotent_id('feb32ac6-22bb-4a16-afd8-9454bb714b14')210    def test_index_max_size(self):211        images_list = self.client.list_images(size_max=42)['images']212        for image in images_list:213            self.assertTrue(image['size'] <= 42)214        result_set = set(map(lambda x: x['id'], images_list))215        self.assertTrue(self.size42_set <= result_set)216        self.assertFalse(self.created_set - self.size42_set <= result_set)217    @test.idempotent_id('6ffc16d0-4cbf-4401-95c8-4ac63eac34d8')218    def test_index_min_size(self):219        images_list = self.client.list_images(size_min=142)['images']220        for image in images_list:221            self.assertTrue(image['size'] >= 142)222        result_set = set(map(lambda x: x['id'], images_list))223        self.assertTrue(self.size142_set <= result_set)224        self.assertFalse(self.size42_set <= result_set)225    @test.idempotent_id('e5dc26d9-9aa2-48dd-bda5-748e1445da98')226    def test_index_status_active_detail(self):227        images_list = self.client.list_images(detail=True,228                                              status='active',229                                              sort_key='size',230                                              sort_dir='desc')['images']231        top_size = images_list[0]['size']  # We have non-zero sized images232        for image in images_list:233            size = image['size']234            self.assertTrue(size <= top_size)235            top_size = size236            self.assertEqual(image['status'], 'active')237    @test.idempotent_id('097af10a-bae8-4342-bff4-edf89969ed2a')238    def test_index_name(self):239        images_list = self.client.list_images(240            detail=True,241            name='New Remote Image dup')['images']242        result_set = set(map(lambda x: x['id'], images_list))243        for image in images_list:244            self.assertEqual(image['name'], 'New Remote Image dup')245        self.assertTrue(self.dup_set <= result_set)246        self.assertFalse(self.created_set - self.dup_set <= result_set)247class UpdateImageMetaTest(base.BaseV1ImageTest):248    @classmethod249    def resource_setup(cls):250        super(UpdateImageMetaTest, cls).resource_setup()251        container_format, disk_format = get_container_and_disk_format()252        cls.image_id = cls._create_standard_image('1', container_format,253                                                  disk_format, 42)254    @classmethod255    def _create_standard_image(cls, name, container_format,256                               disk_format, size):257        """Create a new standard image and return newly-registered image-id"""258        image_file = moves.cStringIO(data_utils.random_bytes(size))259        name = 'New Standard Image %s' % name260        image = cls.create_image(name=name,261                                 container_format=container_format,262                                 disk_format=disk_format,263                                 is_public=False, data=image_file,264                                 properties={'key1': 'value1'})265        image_id = image['id']...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!!
