Skip to content

Commit

Permalink
Merge pull request #42 from nasa/harmony-1764-2
Browse files Browse the repository at this point in the history
Harmony 1764 add support for specifying data operation using a file
  • Loading branch information
indiejames authored May 20, 2024
2 parents d3fb928 + 77a3675 commit 7dc166c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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

0 comments on commit 7dc166c

Please sign in to comment.