Skip to content

Commit

Permalink
[CERTTF-303] Handle case of file upload interrupted by the user (#266)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
boukeas authored May 3, 2024
1 parent bf3fe87 commit a6c8def
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cli/testflinger_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions cli/testflinger_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()

Expand Down

0 comments on commit a6c8def

Please sign in to comment.