-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gen3 Workflow cli and tool. (#244)
* Add cli and tool logic * Add unit tests * Add Documentation
- Loading branch information
Showing
12 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import click | ||
|
||
|
||
from gen3.tools.wrap import Gen3Wrap | ||
|
||
|
||
@click.command( | ||
context_settings={"ignore_unknown_options": True, "allow_extra_args": True}, | ||
help="A wrapper command that forwards COMMAND_ARGS as-is after setting the environment variable GEN3_TOKEN", | ||
) | ||
@click.argument("command_args", nargs=-1, type=click.UNPROCESSED) | ||
@click.pass_context | ||
def run(ctx, command_args): | ||
auth = ctx.obj["auth_factory"].get() | ||
gen3Wrap_object = Gen3Wrap(auth, command_args) | ||
gen3Wrap_object.run_command() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import subprocess | ||
|
||
from cdislogging import get_logger | ||
from gen3.auth import Gen3Auth, Gen3AuthError | ||
|
||
logger = get_logger("__name__") | ||
|
||
|
||
class Gen3Wrap: | ||
def __init__(self, auth: Gen3Auth, command_args: tuple): | ||
""" | ||
auth : Gen3Auth instance | ||
command_args: A tuple consisting of all the commands sent to the `gen3 run` tool | ||
""" | ||
self.auth = auth | ||
self.command_args = command_args | ||
|
||
def run_command(self): | ||
""" | ||
Take the command args and run a subprocess with appropriate access token in the env var | ||
""" | ||
cmd = list(self.command_args) | ||
try: | ||
os.environ["GEN3_TOKEN"] = self.auth.get_access_token() | ||
except Gen3AuthError as e: | ||
logger.error(f"ERROR getting Gen3 Access Token:", e) | ||
raise | ||
logger.info( | ||
f"Running the command {self.command_args} with gen3 access token in environment variable" | ||
) | ||
try: | ||
subprocess.run(cmd, stderr=subprocess.STDOUT) | ||
except Exception as e: | ||
logger.error(f"ERROR while running '{cmd}':", e) | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pytest | ||
from unittest.mock import MagicMock, patch | ||
|
||
from gen3.tools.wrap import Gen3Wrap, Gen3Auth, Gen3AuthError | ||
|
||
|
||
@patch.object(Gen3Auth, "get_access_token", MagicMock(return_value="1.2.3")) | ||
def test_gen3_wrap_valid_auth(mock_gen3_auth): | ||
""" | ||
Patch subprocess.run and verify that the appropriate arguments are passed to the method when authentication process is valid. | ||
""" | ||
|
||
test_command_args = ("echo", "Test1", "Test2") | ||
with patch("gen3.tools.wrap.subprocess.run") as mock_subprocess_run: | ||
wrapper_obj = Gen3Wrap(mock_gen3_auth, test_command_args) | ||
wrapper_obj.run_command() | ||
mock_subprocess_run.assert_called_once_with(list(test_command_args), stderr=-2) | ||
|
||
|
||
@patch.object(Gen3Auth, "get_access_token", MagicMock(side_effect=Gen3AuthError())) | ||
def test_gen3_wrap_inavalid_auth(mock_gen3_auth): | ||
""" | ||
Break the authentication process to verify the following: | ||
1. Ensure a Gen3AuthError is raised. | ||
2. Confirm the subprocess is not executed when authentication fails. | ||
""" | ||
|
||
test_command_args = ("echo", "Test1", "Test2") | ||
with pytest.raises(Gen3AuthError): | ||
with patch("gen3.tools.wrap.subprocess.run") as mock_subprocess_run: | ||
wrapper_obj = Gen3Wrap(mock_gen3_auth, test_command_args) | ||
wrapper_obj.run_command() | ||
mock_subprocess_run.assert_not_called() |