Skip to content

Commit

Permalink
Non-zero exit code for client on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
milesgranger committed May 7, 2019
1 parent f24cd98 commit c635a44
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
11 changes: 6 additions & 5 deletions gordo_components/cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import typing
import sys
import json
from datetime import datetime
from pprint import pprint
Expand Down Expand Up @@ -108,20 +109,20 @@ def predict(

# Loop over all error messages for each result and log them
click.secho(f"\n{'-' * 20} Summary of failed predictions (if any) {'-' * 20}")
exit_code = 0
for (_name, _df, error_messages) in predictions:
for err_msg in error_messages:
# Any error message indicates we encountered at least one error
exit_code = 1
click.secho(err_msg, fg="red")

# Shall we write the predictions out?
if output_dir is not None:
for (
name,
prediction_df,
_err_msgs,
) in predictions: # [(name: str, predictions: pd.DataFrame), ...]
for (name, prediction_df, _err_msgs) in predictions:
prediction_df.to_csv(
os.path.join(output_dir, f"{name}.csv.gz"), compression="gzip"
)
sys.exit(exit_code)


@click.command("metadata")
Expand Down
44 changes: 44 additions & 0 deletions tests/gordo_components/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import tempfile
import json
import logging
from dateutil.parser import isoparse # type: ignore

import aiohttp
Expand Down Expand Up @@ -331,6 +332,49 @@ def test_client_cli_predict(
)


@pytest.mark.parametrize(
"should_fail,start_date,end_date",
[
(True, "1888-01-01T00:00:00Z", "1888-02-01T01:00:00Z"), # Fail on bad dates
(False, "2016-01-01T00:00:00Z", "2016-01-01T01:00:00Z"), # pass on good dates
],
)
def test_client_cli_predict_non_zero_exit(
should_fail, start_date, end_date, caplog, influxdb, watchman_service
):
"""
Test ability for client to get predictions via CLI
"""
runner = CliRunner()

# Should fail requesting dates which clearly don't exist.
args = [
"client",
"--metadata",
"key,value",
"--project",
tu.GORDO_PROJECT,
"predict",
start_date,
end_date,
]

data_provider = providers.InfluxDataProvider(
measurement=tu.INFLUXDB_MEASUREMENT, value_name="Value", uri=tu.INFLUXDB_URI
)

args.extend(["--data-provider", json.dumps(data_provider.to_dict())])

# Run without any error
with caplog.at_level(logging.CRITICAL):
out = runner.invoke(cli.gordo, args=args)

if should_fail:
assert out.exit_code != 0, f"{out.output}"
else:
assert out.exit_code == 0, f"{out.output}"


@pytest.mark.parametrize(
"config",
(
Expand Down

0 comments on commit c635a44

Please sign in to comment.