From 0dabd5f434bdc5fca4ba8e72b9e51582693f6ba9 Mon Sep 17 00:00:00 2001 From: Paul Larson Date: Fri, 14 Jun 2024 17:45:41 -0400 Subject: [PATCH] Give more detail if job submission fails (#285) * Give more detail if job submission fails * Add unit test for new error output --- cli/testflinger_cli/__init__.py | 2 +- cli/testflinger_cli/client.py | 5 +++-- cli/testflinger_cli/tests/test_cli.py | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cli/testflinger_cli/__init__.py b/cli/testflinger_cli/__init__.py index 2ce3aa94..8dc405b5 100644 --- a/cli/testflinger_cli/__init__.py +++ b/cli/testflinger_cli/__init__.py @@ -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 diff --git a/cli/testflinger_cli/client.py b/cli/testflinger_cli/client.py index f5b28d47..a5e55426 100644 --- a/cli/testflinger_cli/client.py +++ b/cli/testflinger_cli/client.py @@ -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: @@ -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): diff --git a/cli/testflinger_cli/tests/test_cli.py b/cli/testflinger_cli/tests/test_cli.py index ac7ac413..ff03ea32 100644 --- a/cli/testflinger_cli/tests/test_cli.py +++ b/cli/testflinger_cli/tests/test_cli.py @@ -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"""