From c307c9189e1c5c4b26ae444cb7198ab43b53f609 Mon Sep 17 00:00:00 2001 From: Ian Cooke Date: Thu, 2 Nov 2023 15:11:10 -0400 Subject: [PATCH 1/3] set exit status appropriately, and skip stack trace in CLI mode --- CHANGELOG.md | 3 +++ src/cirrus/plugins/management/commands/manage.py | 11 +++++++++-- src/cirrus/plugins/management/deployment.py | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bd5f01..5ad29d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Return correct execution(`[-1]`), as new Step Function executions are appended to the `executions` list, from the StateDB Item. ([#6](https://github.com/cirrus-geo/cirrus-mgmt/pull/6)) +- enable both `call` CLI to set exit status code based of `Deployment.call`, + and exception raised from `subprocess.check_call` if used as a + library. ([#7](https://github.com/cirrus-geo/cirrus-mgmt/pull/7) ## [v0.1.0] - 2023-08-01 diff --git a/src/cirrus/plugins/management/commands/manage.py b/src/cirrus/plugins/management/commands/manage.py index f1e4685..d0e0147 100644 --- a/src/cirrus/plugins/management/commands/manage.py +++ b/src/cirrus/plugins/management/commands/manage.py @@ -7,7 +7,11 @@ from cirrus.cli.utils import click as utils_click from click_option_group import RequiredMutuallyExclusiveOptionGroup, optgroup -from cirrus.plugins.management.deployment import WORKFLOW_POLL_INTERVAL, Deployment +from cirrus.plugins.management.deployment import ( + WORKFLOW_POLL_INTERVAL, + CalledProcessError, + Deployment, +) from cirrus.plugins.management.utils.click import ( additional_variables, silence_templating_errors, @@ -304,7 +308,10 @@ def _call(ctx, deployment, command, include_user_vars): """Run an executable, in a new process, with the deployment environment vars loaded""" if not command: return - deployment.call(command, include_user_vars=include_user_vars) + try: + deployment.call(command, include_user_vars=include_user_vars) + except CalledProcessError as cpe: + sys.exit(cpe.returncode) @manage.command() diff --git a/src/cirrus/plugins/management/deployment.py b/src/cirrus/plugins/management/deployment.py index 789207f..7074a51 100644 --- a/src/cirrus/plugins/management/deployment.py +++ b/src/cirrus/plugins/management/deployment.py @@ -4,7 +4,7 @@ import os from datetime import datetime, timezone from pathlib import Path -from subprocess import check_call +from subprocess import CalledProcessError, check_call from time import sleep, time import backoff From 4def2540a429cda50350f765ecb39c114c1cfe76 Mon Sep 17 00:00:00 2001 From: Ian Cooke Date: Thu, 2 Nov 2023 15:40:50 -0400 Subject: [PATCH 2/3] add test for call function exit status --- CHANGELOG.md | 2 +- tests/test_manage.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ad29d0..e278629 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Return correct execution(`[-1]`), as new Step Function executions are appended to the `executions` list, from the StateDB Item. ([#6](https://github.com/cirrus-geo/cirrus-mgmt/pull/6)) -- enable both `call` CLI to set exit status code based of `Deployment.call`, +- Enable both `call` CLI to set exit status code based of `Deployment.call`, and exception raised from `subprocess.check_call` if used as a library. ([#7](https://github.com/cirrus-geo/cirrus-mgmt/pull/7) diff --git a/tests/test_manage.py b/tests/test_manage.py index 40d42ba..1c809f6 100644 --- a/tests/test_manage.py +++ b/tests/test_manage.py @@ -101,8 +101,10 @@ def test_manage_refresh(deployment, mock_lambda_get_conf, lambda_env): assert new["environment"] == lambda_env -# @pytest.mark.parametrize("item", ( def test_manage_get_execution_by_payload_id(deployment, basic_payloads, statedb): + """Adds causes two workflow executions, and confirms that the second call to + get_execution_by_payload_id gets a different executionArn value from the first execution. + """ current_env = deepcopy(os.environ) # stash env deployment.set_env() basic_payloads.process() @@ -113,3 +115,8 @@ def test_manage_get_execution_by_payload_id(deployment, basic_payloads, statedb) sfn_exe2 = deployment.get_execution_by_payload_id(pid) assert sfn_exe1["executionArn"] != sfn_exe2["executionArn"] os.environ = current_env # pop stash + + +@pytest.mark.parametrize("command,expect_exit_zero", (("true", True), ("false", False))) +def test_call_cli_return_values(deployment, command, expect_exit_zero): + result = deployment(f"call {command}") From 0967d0a97789719d7fe77a1f56aa9f84d78efcb3 Mon Sep 17 00:00:00 2001 From: Ian Cooke Date: Thu, 2 Nov 2023 15:52:50 -0400 Subject: [PATCH 3/3] ensure test checks exit status --- tests/test_manage.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_manage.py b/tests/test_manage.py index 1c809f6..2aba873 100644 --- a/tests/test_manage.py +++ b/tests/test_manage.py @@ -120,3 +120,4 @@ def test_manage_get_execution_by_payload_id(deployment, basic_payloads, statedb) @pytest.mark.parametrize("command,expect_exit_zero", (("true", True), ("false", False))) def test_call_cli_return_values(deployment, command, expect_exit_zero): result = deployment(f"call {command}") + assert result.exit_code == 0 if expect_exit_zero else result.exit_code != 0