From a6c8def86bf872049ac2ce9f8d9218bc3103af09 Mon Sep 17 00:00:00 2001 From: George Boukeas Date: Fri, 3 May 2024 15:11:26 +0100 Subject: [PATCH] [CERTTF-303] Handle case of file upload interrupted by the user (#266) * fix(cli): catch Ctrl-C during attachment upload and cancel job * feat(cli): make error logging more explicit * fix(cli): specify mimetype when uploading attachments archive --- cli/testflinger_cli/__init__.py | 7 +++++++ cli/testflinger_cli/client.py | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cli/testflinger_cli/__init__.py b/cli/testflinger_cli/__init__.py index ba938a1b..2ce3aa94 100644 --- a/cli/testflinger_cli/__init__.py +++ b/cli/testflinger_cli/__init__.py @@ -382,10 +382,12 @@ def submit(self): with tempfile.NamedTemporaryFile(suffix="tar.gz") as archive: archive_path = Path(archive.name) # create attachments archive prior to job submission + logger.info("Packing attachments into %s", archive_path) self.pack_attachments(archive_path, attachments_data) # submit job, followed by the submission of the archive job_id = self.submit_job_data(job_dict) try: + logger.info("Submitting attachments for %s", job_id) self.submit_job_attachments(job_id, path=archive_path) except AttachmentError: self.cancel(job_id) @@ -443,6 +445,11 @@ def submit_job_attachments(self, job_id: str, path: Path): for _ in range(tries): try: self.client.post_attachment(job_id, path, timeout=timeout) + except KeyboardInterrupt as error: + raise AttachmentError( + f"Unable to submit attachment archive for {job_id}: " + f"attachment upload was cancelled by the user" + ) from error except requests.HTTPError as error: # we can't recover from these errors, give up without retrying if error.response.status_code == 400: diff --git a/cli/testflinger_cli/client.py b/cli/testflinger_cli/client.py index 199bba23..f5b28d47 100644 --- a/cli/testflinger_cli/client.py +++ b/cli/testflinger_cli/client.py @@ -102,7 +102,7 @@ def put_file(self, uri_frag: str, path: Path, timeout: float): uri = urllib.parse.urljoin(self.server, uri_frag) with open(path, "rb") as file: try: - files = {"file": (path.name, file)} + files = {"file": (path.name, file, "application/x-gzip")} response = requests.post(uri, files=files, timeout=timeout) except requests.exceptions.ConnectTimeout: logger.error( @@ -115,8 +115,8 @@ def put_file(self, uri_frag: str, path: Path, timeout: float): "in the alloted amount of time" ) raise - except requests.exceptions.ConnectionError: - logger.error("A connection error occured") + except requests.exceptions.ConnectionError as error: + logger.error("A connection error occured %s", error) raise response.raise_for_status()