Skip to content

Commit

Permalink
Give more detail if job submission fails (#285)
Browse files Browse the repository at this point in the history
* Give more detail if job submission fails

* Add unit test for new error output
  • Loading branch information
plars authored Jun 14, 2024
1 parent 7ff73d8 commit 0dabd5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cli/testflinger_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def submit_job_data(self, data: dict):
# This shouldn't happen, so let's get more information
sys.exit(
"Unexpected error status from testflinger "
f"server: {exc.status}"
f"server: [{exc.status}] {exc.msg}"
)
return job_id

Expand Down
5 changes: 3 additions & 2 deletions cli/testflinger_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
class HTTPError(Exception):
"""Exception class for HTTP error codes"""

def __init__(self, status):
def __init__(self, status, msg=""):
super().__init__(status)
self.status = status
self.msg = msg


class Client:
Expand Down Expand Up @@ -86,7 +87,7 @@ def put(self, uri_frag, data, timeout=15):
logger.error("Unable to communicate with specified server.")
sys.exit(1)
if req.status_code != 200:
raise HTTPError(req.status_code)
raise HTTPError(status=req.status_code, msg=req.text)
return req.text

def put_file(self, uri_frag: str, path: Path, timeout: float):
Expand Down
17 changes: 17 additions & 0 deletions cli/testflinger_cli/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ def test_submit(capsys, tmp_path, requests_mock):
assert jobid in std.out


def test_submit_bad_data(tmp_path, requests_mock):
"""Ensure a 422 response from bad data shows the returned errors"""
fake_data = {"badkey": "badvalue"}
testfile = tmp_path / "test.json"
testfile.write_text(json.dumps(fake_data))
# return 422 and "expected error"
requests_mock.post(URL + "/v1/job", status_code=422, text="expected error")
sys.argv = ["", "submit", str(testfile)]
tfcli = testflinger_cli.TestflingerCli()
with pytest.raises(SystemExit) as err:
tfcli.submit()
assert (
"Unexpected error status from testflinger server: [422] expected error"
in err.value.code
)


def test_submit_with_attachments(tmp_path):
"""Make sure jobs with attachments are submitted correctly"""

Expand Down

0 comments on commit 0dabd5f

Please sign in to comment.