Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harmony 1764 add support for specifying data operation using a file #42

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion harmony/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def setup_cli(parser):
parser.add_argument('--harmony-input',
help=('the input data for the action provided by Harmony, required for '
'--harmony-action=invoke'))
parser.add_argument('--harmony-input-file',
help=('the optional path to the input data for the action provided by Harmony'))
parser.add_argument('--harmony-sources',
help=('file path that contains a STAC catalog with items and metadata to '
'be processed by the service. Required for non-deprecated '
Expand Down Expand Up @@ -323,11 +325,16 @@ def run_cli(parser, args, AdapterClass, cfg=None):
if args.harmony_wrap_stdout:
setup_stdout_log_formatting(cfg)

# read in the operation file passed in with --harmony-input-file if any
if bool(args.harmony_input_file):
with open(args.harmony_input_file, 'r') as f:
args.harmony_input = f.read()

if args.harmony_action == 'invoke':
start_time = datetime.datetime.now()
if not bool(args.harmony_input):
parser.error(
'--harmony-input must be provided for --harmony-action=invoke')
'--harmony-input or --harmony-input-file must be provided for --harmony-action=invoke')
elif not bool(args.harmony_sources):
successful = _invoke_deprecated(AdapterClass, args.harmony_input, cfg)
if not successful:
Expand Down
13 changes: 12 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import unittest
from unittest.mock import patch

Expand Down Expand Up @@ -50,8 +51,11 @@ def test_when_passing_nothing_it_returns_false(self, parser):
class TestCliInvokeAction(unittest.TestCase):
def setUp(self):
self.config = harmony.util.config(validate=False)
with open('/tmp/operation.json', 'w') as f:
f.write('{"test": "input"}')

def tearDown(self):
os.remove('/tmp/operation.json')
MockAdapter.messages = []
MockAdapter.errors = []
MockAdapter.cleaned_up = []
Expand All @@ -62,14 +66,21 @@ def test_when_harmony_input_is_not_provided_it_terminates_with_error(self, parse
args = parser.parse_args()
cli.run_cli(parser, args, MockAdapter, self.config)
error_method.assert_called_once_with(
'--harmony-input must be provided for --harmony-action=invoke')
'--harmony-input or --harmony-input-file must be provided for --harmony-action=invoke')

@cli_test('--harmony-action', 'invoke', '--harmony-input', '{"test": "input"}')
def test_when_harmony_input_is_provided_it_creates_and_invokes_an_adapter(self, parser):
args = parser.parse_args()
cli.run_cli(parser, args, MockAdapter, self.config)
self.assertListEqual([{'test': 'input'}], MockAdapter.messages)

@cli_test('--harmony-action', 'invoke', '--harmony-input-file', '/tmp/operation.json')
def test_when_harmony_input_file_is_provided_it_creates_and_invokes_an_adapter(self, parser):
args = parser.parse_args()

cli.run_cli(parser, args, MockAdapter, self.config)
self.assertListEqual([{'test': 'input'}], MockAdapter.messages)

@cli_test('--harmony-action', 'invoke', '--harmony-input', '{"test": "input"}')
def test_when_the_backend_service_doesnt_respond_it_responds_with_an_error(self, parser):
class MockImpl(MockAdapter):
Expand Down
Loading