Best Python code snippet using tempest_python
test_upload_utils.py
Source:test_upload_utils.py  
...53    def test_safe_kill(self):54        req = unit_test_utils.get_fake_request()55        id = unit_test_utils.UUID156        self.mox.StubOutWithMock(registry, "update_image_metadata")57        registry.update_image_metadata(req.context, id, {'status': 'killed'})58        self.mox.ReplayAll()59        upload_utils.safe_kill(req, id)60        self.mox.VerifyAll()61    def test_safe_kill_with_error(self):62        req = unit_test_utils.get_fake_request()63        id = unit_test_utils.UUID164        self.mox.StubOutWithMock(registry, "update_image_metadata")65        registry.update_image_metadata(req.context,66                                       id,67                                       {'status': 'killed'}68                                       ).AndRaise(Exception())69        self.mox.ReplayAll()70        upload_utils.safe_kill(req, id)71        self.mox.VerifyAll()72    def test_upload_data_to_store(self):73        req = unit_test_utils.get_fake_request()74        location = "file://foo/bar"75        size = 1076        checksum = "checksum"77        image_meta = {'id': unit_test_utils.UUID1,78                      'size': size}79        image_data = "blah"80        notifier = self.mox.CreateMockAnything()81        store = self.mox.CreateMockAnything()82        store.add(83            image_meta['id'],84            mox.IgnoreArg(),85            image_meta['size']).AndReturn((location, size, checksum, {}))86        self.mox.StubOutWithMock(registry, "update_image_metadata")87        update_data = {'checksum': checksum,88                       'size': size}89        registry.update_image_metadata(req.context,90                                       image_meta['id'],91                                       update_data92                                       ).AndReturn(93                                           image_meta.update(update_data))94        self.mox.ReplayAll()95        actual_meta, actual_loc, loc_meta = upload_utils.upload_data_to_store(96            req, image_meta, image_data, store, notifier)97        self.mox.VerifyAll()98        self.assertEqual(actual_loc, location)99        self.assertEqual(actual_meta, image_meta.update(update_data))100    def test_upload_data_to_store_mismatch_size(self):101        req = unit_test_utils.get_fake_request()102        location = "file://foo/bar"103        size = 10104        checksum = "checksum"105        image_meta = {'id': unit_test_utils.UUID1,106                      'size': size + 1}  # Need incorrect size for test107        image_data = "blah"108        notifier = self.mox.CreateMockAnything()109        store = self.mox.CreateMockAnything()110        store.add(111            image_meta['id'],112            mox.IgnoreArg(),113            image_meta['size']).AndReturn((location, size, checksum, {}))114        self.mox.StubOutWithMock(registry, "update_image_metadata")115        update_data = {'checksum': checksum}116        registry.update_image_metadata(req.context,117                                       image_meta['id'],118                                       update_data119                                       ).AndReturn(120                                           image_meta.update(update_data))121        notifier.error('image.upload', mox.IgnoreArg())122        self.mox.ReplayAll()123        self.assertRaises(webob.exc.HTTPBadRequest,124                          upload_utils.upload_data_to_store,125                          req, image_meta, image_data, store, notifier)126        self.mox.VerifyAll()127    def test_upload_data_to_store_mismatch_checksum(self):128        req = unit_test_utils.get_fake_request()129        location = "file://foo/bar"130        size = 10131        checksum = "checksum"132        image_meta = {'id': unit_test_utils.UUID1,133                      'size': size}134        image_data = "blah"135        notifier = self.mox.CreateMockAnything()136        store = self.mox.CreateMockAnything()137        store.add(138            image_meta['id'],139            mox.IgnoreArg(),140            image_meta['size']).AndReturn((location,141                                           size,142                                           checksum + "NOT",143                                           {}))144        self.mox.StubOutWithMock(registry, "update_image_metadata")145        update_data = {'checksum': checksum}146        registry.update_image_metadata(req.context,147                                       image_meta['id'],148                                       update_data).AndReturn(149                                               image_meta.update(update_data))150        notifier.error('image.upload', mox.IgnoreArg())151        self.mox.ReplayAll()152        self.assertRaises(webob.exc.HTTPBadRequest,153                          upload_utils.upload_data_to_store,154                          req, image_meta, image_data, store, notifier)155        self.mox.VerifyAll()156    def _test_upload_data_to_store_exception(self, exc_class, expected_class):157        req = unit_test_utils.get_fake_request()158        location = "file://foo/bar"159        size = 10160        checksum = "checksum"161        image_meta = {'id': unit_test_utils.UUID1,162                      'size': size}163        image_data = "blah"164        notifier = self.mox.CreateMockAnything()165        store = self.mox.CreateMockAnything()166        store.add(167            image_meta['id'],168            mox.IgnoreArg(),169            image_meta['size']).AndRaise(exc_class)170        self.mox.StubOutWithMock(upload_utils, "safe_kill")171        upload_utils.safe_kill(req, image_meta['id'])172        self.mox.ReplayAll()173        self.assertRaises(expected_class,174                          upload_utils.upload_data_to_store,175                          req, image_meta, image_data, store, notifier)176        self.mox.VerifyAll()177    def _test_upload_data_to_store_exception_with_notify(self,178                                                         exc_class,179                                                         expected_class):180        req = unit_test_utils.get_fake_request()181        location = "file://foo/bar"182        size = 10183        checksum = "checksum"184        image_meta = {'id': unit_test_utils.UUID1,185                      'size': size}186        image_data = "blah"187        store = self.mox.CreateMockAnything()188        store.add(189            image_meta['id'],190            mox.IgnoreArg(),191            image_meta['size']).AndRaise(exc_class)192        self.mox.StubOutWithMock(upload_utils, "safe_kill")193        upload_utils.safe_kill(req, image_meta['id'])194        notifier = self.mox.CreateMockAnything()195        notifier.error('image.upload', mox.IgnoreArg())196        self.mox.ReplayAll()197        self.assertRaises(expected_class,198                          upload_utils.upload_data_to_store,199                          req, image_meta, image_data, store, notifier)200        self.mox.VerifyAll()201    def test_upload_data_to_store_duplicate(self):202        self._test_upload_data_to_store_exception_with_notify(203                                        exception.Duplicate,204                                        webob.exc.HTTPConflict)205    def test_upload_data_to_store_forbidden(self):206        self._test_upload_data_to_store_exception_with_notify(207                                        exception.Forbidden,208                                        webob.exc.HTTPForbidden)209    def test_upload_data_to_store_storage_full(self):210        self._test_upload_data_to_store_exception_with_notify(211                                        exception.StorageFull,212                                        webob.exc.HTTPRequestEntityTooLarge)213    def test_upload_data_to_store_storage_write_denied(self):214        self._test_upload_data_to_store_exception_with_notify(215                                        exception.StorageWriteDenied,216                                        webob.exc.HTTPServiceUnavailable)217    def test_upload_data_to_store_size_limit_exceeded(self):218        self._test_upload_data_to_store_exception_with_notify(219                                        exception.ImageSizeLimitExceeded,220                                        webob.exc.HTTPRequestEntityTooLarge)221    def test_upload_data_to_store_http_error(self):222        self._test_upload_data_to_store_exception_with_notify(223                                        webob.exc.HTTPError,224                                        webob.exc.HTTPError)225    def test_upload_data_to_store_client_disconnect(self):226        self._test_upload_data_to_store_exception(227                                        ValueError,228                                        webob.exc.HTTPBadRequest)229    def test_upload_data_to_store_client_disconnect_ioerror(self):230        self._test_upload_data_to_store_exception(231                                        IOError,232                                        webob.exc.HTTPBadRequest)233    def test_upload_data_to_store_exception(self):234        self._test_upload_data_to_store_exception_with_notify(235                                        Exception,236                                        webob.exc.HTTPInternalServerError)237    def test_upload_data_to_store_not_found_after_upload(self):238        req = unit_test_utils.get_fake_request()239        location = "file://foo/bar"240        size = 10241        checksum = "checksum"242        image_meta = {'id': unit_test_utils.UUID1,243                      'size': size}244        image_data = "blah"245        notifier = self.mox.CreateMockAnything()246        store = self.mox.CreateMockAnything()247        store.add(248            image_meta['id'],249            mox.IgnoreArg(),250            image_meta['size']).AndReturn((location, size, checksum, {}))251        self.mox.StubOutWithMock(registry, "update_image_metadata")252        update_data = {'checksum': checksum,253                       'size': size}254        registry.update_image_metadata(req.context,255                                       image_meta['id'],256                                       update_data257                                       ).AndRaise(exception.NotFound)258        self.mox.StubOutWithMock(upload_utils, "initiate_deletion")259        upload_utils.initiate_deletion(req, location, image_meta['id'],260                                       mox.IsA(bool))261        self.mox.StubOutWithMock(upload_utils, "safe_kill")262        upload_utils.safe_kill(req, image_meta['id'])263        notifier.error('image.upload', mox.IgnoreArg())264        self.mox.ReplayAll()265        self.assertRaises(webob.exc.HTTPPreconditionFailed,266                          upload_utils.upload_data_to_store,267                          req, image_meta, image_data, store, notifier)268        self.mox.VerifyAll()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!!
