def test_unregistered_snap_must_raise_exception(self): class MockResponse: status_code = 404 def json(self): return dict( error_list=[{ "code": "resource-not-found", "message": "Snap not found for name=basic", }]) self.fake_precheck.mock.side_effect = StoreUploadError( "basic", MockResponse()) raised = self.assertRaises( storeapi.errors.StoreUploadError, self.run_command, ["upload-metadata", self.snap_file], ) self.assertThat( str(raised), Contains( "This snap is not registered. Register the snap and try again." ), )
def test_upload_unregistered_snap_must_ask(self): class MockResponse: status_code = 404 def json(self): return dict( error_list=[{ "code": "resource-not-found", "message": "Snap not found for name=basic", }]) self.fake_store_upload_precheck.mock.side_effect = [ StoreUploadError("basic", MockResponse()), None, ] result = self.run_command(["upload", self.snap_file], input="y\n") self.assertThat(result.exit_code, Equals(0)) self.assertThat( result.output, Contains( "You are required to register this snap before continuing. "), ) self.fake_store_register.mock.assert_called_once_with("basic", is_private=False, series="16", store_id=None)
def test_push_with_updown_error(self): # We really don't know of a reason why this would fail # aside from a 5xx style error on the server. class MockResponse: text = "stub error" reason = "stub reason" self.fake_store_upload.mock.side_effect = StoreUploadError( MockResponse()) self.assertRaises(storeapi.errors.StoreUploadError, self.run_command, ["push", self.snap_file])
def upload_files(binary_filename, updown_client): """Upload a binary file to the Store. Submit a file to the Store upload service and return the corresponding upload_id. """ try: binary_file_size = os.path.getsize(binary_filename) binary_file = open(binary_filename, 'rb') encoder = MultipartEncoder(fields={ 'binary': ('filename', binary_file, 'application/octet-stream') }) # Create a progress bar that looks like: Uploading foo [== ] 50% progress_bar = ProgressBar(widgets=[ 'Uploading {} '.format(binary_filename), Bar(marker='=', left='[', right=']'), ' ', Percentage() ], maxval=os.path.getsize(binary_filename)) progress_bar.start() # Print a newline so the progress bar has some breathing room. logger.info('') # Create a monitor for this upload, so that progress can be displayed monitor = MultipartEncoderMonitor( encoder, functools.partial(_update_progress_bar, progress_bar, binary_file_size)) # Begin upload response = updown_client.upload(monitor) # Make sure progress bar shows 100% complete progress_bar.finish() except Exception as err: raise RuntimeError('An unexpected error was found while uploading ' 'files: {!r}.'.format(err)) finally: # Close the open file binary_file.close() if not response.ok: raise StoreUploadError(response) response_data = response.json() return { 'upload_id': response_data['upload_id'], 'binary_filesize': binary_file_size, 'source_uploaded': False, }
def test_push_with_updown_error(self): # We really don't know of a reason why this would fail # aside from a 5xx style error on the server. class MockResponse: text = 'stub error' reason = 'stub reason' patcher = mock.patch.object(storeapi.StoreClient, 'upload') mock_upload = patcher.start() self.addCleanup(patcher.stop) mock_upload.side_effect = StoreUploadError(MockResponse()) self.assertRaises(storeapi.errors.StoreUploadError, self.run_command, ['push', self.snap_file])
def upload_files(binary_filename, updown_client): """Upload a binary file to the Store. Submit a file to the Store upload service and return the corresponding upload_id. """ try: binary_file_size = os.path.getsize(binary_filename) binary_file = open(binary_filename, "rb") encoder = MultipartEncoder(fields={ "binary": ("filename", binary_file, "application/octet-stream") }) # Create a progress bar that looks like: Uploading foo [== ] 50% progress_bar = ProgressBar( widgets=[ "Pushing {} ".format(os.path.basename(binary_filename)), Bar(marker="=", left="[", right="]"), " ", Percentage(), ], maxval=os.path.getsize(binary_filename), ) progress_bar.start() # Create a monitor for this upload, so that progress can be displayed monitor = MultipartEncoderMonitor( encoder, functools.partial(_update_progress_bar, progress_bar, binary_file_size), ) # Begin upload response = updown_client.upload(monitor) # Make sure progress bar shows 100% complete progress_bar.finish() finally: # Close the open file binary_file.close() if not response.ok: raise StoreUploadError(response) response_data = response.json() return { "upload_id": response_data["upload_id"], "binary_filesize": binary_file_size, "source_uploaded": False, }
def test_push_with_updown_error(self): # We really don't know of a reason why this would fail # aside from a 5xx style error on the server. self.useFixture(fixture_setup.FakeTerminal()) class MockResponse: text = 'stub error' reason = 'stub reason' patcher = mock.patch.object(storeapi.StoreClient, 'upload') mock_upload = patcher.start() self.addCleanup(patcher.stop) mock_upload.side_effect = StoreUploadError(MockResponse()) # Create a snap main(['init']) main(['snap']) snap_file = glob.glob('*.snap')[0] self.assertRaises(SystemExit, main, ['push', snap_file])