class TestUploadAPI(unittest.TestCase): def setUp(self): self.api = UploadAPI(mock.MagicMock()) def test_import_upload_with_override_config(self): ret = self.api.import_upload('upload_id', 'repo_id', 'unit_type_id', unit_key={}, unit_metadata={}, override_config={'mask-id': 'test-mask-id'}) expected_body = { 'upload_id': 'upload_id', 'unit_type_id': 'unit_type_id', 'unit_key': {}, 'unit_metadata': {}, 'override_config': {'mask-id': 'test-mask-id'}, } self.api.server.POST.assert_called_once_with('/v2/repositories/%s/actions/import_upload/' % 'repo_id', expected_body) self.assertEqual(ret, self.api.server.POST.return_value) def test_import_upload(self): ret = self.api.import_upload('upload_id', 'repo_id', 'unit_type_id', unit_key={}, unit_metadata={}) expected_body = { 'upload_id': 'upload_id', 'unit_type_id': 'unit_type_id', 'unit_key': {}, 'unit_metadata': {}, 'override_config': None, } self.api.server.POST.assert_called_once_with('/v2/repositories/%s/actions/import_upload/' % 'repo_id', expected_body) self.assertEqual(ret, self.api.server.POST.return_value)
def upload(self, path, repo, environment): pulp = UploadAPI(self.connection) unit_key, unit_metadata = self.generate_upload_data(path) name = os.path.basename(path) repo_id = "{0}-{1}".format(repo, environment) ################################################################ # Initialize upload ################################################################ response = pulp.initialize_upload() if response.response_code == Constants.PULP_POST_CREATED: Log.log_debug("Initialized upload process for %s" % name) else: raise SystemError("Failed to initialize upload process for %s" % name) upload_id = response.response_body['upload_id'] ################################################################ # Upload chunks w/ Constants.UPLOAD_AT_ONCE size ################################################################ size = os.path.getsize(path) rpm_fd = open(path, 'rb') total_seeked = 0 rpm_fd.seek(0) while total_seeked < size: chunk = rpm_fd.read(Constants.UPLOAD_AT_ONCE) last_offset = total_seeked total_seeked += len(chunk) Log.log_notice("Seeked %s data... (total seeked: %s)" % (len(chunk), total_seeked)) response = pulp.upload_segment(upload_id, last_offset, chunk) if response.response_code is not Constants.PULP_PUT_OK: Log.log_debug("Failed to upload %s" % name) raise SystemError("Failed to upload %s" % name) ################################################################ # Import upload ################################################################ response = pulp.import_upload(upload_id, repo_id, 'rpm', unit_key, unit_metadata) if response.response_code not in [ Constants.PULP_POST_OK, Constants.PULP_POST_ACCEPTED ]: Log.log_error("Failed to import upload for %s" % name) raise SystemError("Failed to import upload for %s" % name) Log.log_debug("RPM upload %s complete" % name) ################################################################ # Finalize upload by cleaning up request on server ################################################################ response = pulp.delete_upload(upload_id) if response.response_code != Constants.PULP_DELETE_OK: Log.log_error("Failed to clean up upload for %s" % name) raise SystemError("Failed to clean up upload for %s" % name) ################################################################ # Publish the repo ################################################################ pulp_repo = PulpRepo(self.connection) pulp_repo.publish(name=repo, environment=environment) ################################################################ # FIN ################################################################ Log.log_info("successfully uploaded %s" % name)
def upload(self, path, repo, environment): pulp = UploadAPI(self.connection) unit_key, unit_metadata = self.generate_upload_data(path) name = os.path.basename(path) repo_id = "{0}-{1}".format(repo, environment) ################################################################ # Initialize upload ################################################################ response = pulp.initialize_upload() if response.response_code == Constants.PULP_POST_CREATED: Log.log_debug("Initialized upload process for %s" % name) else: raise SystemError("Failed to initialize upload process for %s" % name) upload_id = response.response_body['upload_id'] ################################################################ # Upload chunks w/ Constants.UPLOAD_AT_ONCE size ################################################################ size = os.path.getsize(path) rpm_fd = open(path, 'rb') total_seeked = 0 rpm_fd.seek(0) while total_seeked < size: chunk = rpm_fd.read(Constants.UPLOAD_AT_ONCE) last_offset = total_seeked total_seeked += len(chunk) Log.log_notice("Seeked %s data... (total seeked: %s)" % (len(chunk), total_seeked)) response = pulp.upload_segment(upload_id, last_offset, chunk) if response.response_code is not Constants.PULP_PUT_OK: Log.log_debug("Failed to upload %s" % name) raise SystemError("Failed to upload %s" % name) ################################################################ # Import upload ################################################################ response = pulp.import_upload(upload_id, repo_id, 'rpm', unit_key, unit_metadata) if response.response_code not in [Constants.PULP_POST_OK, Constants.PULP_POST_ACCEPTED]: Log.log_error("Failed to import upload for %s" % name) raise SystemError("Failed to import upload for %s" % name) Log.log_debug("RPM upload %s complete" % name) ################################################################ # Finalize upload by cleaning up request on server ################################################################ response = pulp.delete_upload(upload_id) if response.response_code != Constants.PULP_DELETE_OK: Log.log_error("Failed to clean up upload for %s" % name) raise SystemError("Failed to clean up upload for %s" % name) ################################################################ # Publish the repo ################################################################ pulp_repo = PulpRepo(self.connection) pulp_repo.publish(name=repo, environment=environment) ################################################################ # FIN ################################################################ Log.log_info("successfully uploaded %s" % name)