Best Python code snippet using tempest_python
test_image_caching.py
Source:test_image_caching.py  
...58        image_file = io.BytesIO(file_content)59        self.client.store_image_file(image['id'], image_file)60        image = self.client.show_image(image['id'])61        return image62    def _assertCheckQueues(self, queued_images):63        for image in self.cached_info:64            if self.cached_info[image] == 'queued':65                self.assertIn(image, queued_images)66    def _assertCheckCache(self, cached_images):67        cached_list = []68        for image in cached_images:69            cached_list.append(image['image_id'])70        for image in self.cached_info:71            if self.cached_info[image] == 'cached':72                self.assertIn(image, cached_list)73    @decorators.idempotent_id('4bf6adba-2f9f-47e9-a6d5-37f21ad4387c')74    def test_image_caching_cycle(self):75        """Test image cache APIs"""76        # Ensure that non-admin user is not allowed to perform caching77        # operations78        self.assertRaises(lib_exc.Forbidden,79                          self.os_primary.image_cache_client.list_cache)80        # Check there is nothing is queued for cached by us81        output = self.os_admin.image_cache_client.list_cache()82        self._assertCheckQueues(output['queued_images'])83        self._assertCheckCache(output['cached_images'])84        # Non-existing image should raise NotFound exception85        self.assertRaises(lib_exc.NotFound,86                          self.os_admin.image_cache_client.cache_queue,87                          'non-existing-image-id')88        # Verify that we can not use queued image for queueing89        image = self.image_create_and_upload(name='queued', upload=False)90        self.assertRaises(lib_exc.BadRequest,91                          self.os_admin.image_cache_client.cache_queue,92                          image['id'])93        # Create one image94        image = self.image_create_and_upload(name='first',95                                             container_format='bare',96                                             disk_format='raw',97                                             visibility='private')98        self.assertEqual('active', image['status'])99        # Queue image for caching100        self.os_admin.image_cache_client.cache_queue(image['id'])101        self.cached_info[image['id']] = 'queued'102        # Verify that we have 1 image for queueing and 0 for caching103        output = self.os_admin.image_cache_client.list_cache()104        self._assertCheckQueues(output['queued_images'])105        self._assertCheckCache(output['cached_images'])106        # Wait for image caching107        LOG.info("Waiting for image %s to get cached", image['id'])108        caching = waiters.wait_for_caching(109            self.client,110            self.os_admin.image_cache_client,111            image['id'])112        self.cached_info[image['id']] = 'cached'113        # verify that we have image in cache and not in queued114        self._assertCheckQueues(caching['queued_images'])115        self._assertCheckCache(caching['cached_images'])116        # Verify that we can delete images from caching and queueing with117        # api call.118        self.os_admin.image_cache_client.cache_clear()119        output = self.os_admin.image_cache_client.list_cache()120        self.assertEqual(0, len(output['queued_images']))121        self.assertEqual(0, len(output['cached_images']))122        # Verify that invalid header value for target returns 400 response123        self.assertRaises(lib_exc.BadRequest,124                          self.os_admin.image_cache_client.cache_clear,125                          target="invalid")126        # Remove all data from local information...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!!
