Skip to content

Commit 20d3416

Browse files
authored
Add test cases for CLI. (broadinstitute#101)
* Add test cases for CLI. * Update setup.py file. * Try a different approach. * I'm going nuts ... Github Actions does not care scm_version
1 parent 23c6b2c commit 20d3416

File tree

4 files changed

+233
-3
lines changed

4 files changed

+233
-3
lines changed

cromwell_tools/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
__version__ = get_distribution(__name__).version
1111
except DistributionNotFound:
1212
# package is not installed
13-
pass
13+
__version__ = "Unknown Ver."
1414

1515
# By using the below import statement when you call import cromwell_tools you get:
1616
# cromwell_tools.api.status

cromwell_tools/cli.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from cromwell_tools.cromwell_api import CromwellAPI
44
from cromwell_tools.cromwell_auth import CromwellAuth
55
from cromwell_tools.diag import task_runtime
6-
from cromwell_tools import __version__
6+
from cromwell_tools import __version__ as cromwell_tools_version
77
import sys
88

99

@@ -24,7 +24,10 @@ def parser(arguments=None):
2424

2525
# Check the installed version of Cromwell-tools
2626
main_parser.add_argument(
27-
'-V', '--version', action='version', version=f'%(prog)s {__version__}'
27+
'-V',
28+
'--version',
29+
action='version',
30+
version=f'%(prog)s {cromwell_tools_version}',
2831
)
2932

3033
subparsers = main_parser.add_subparsers(help='sub-command help', dest='command')

cromwell_tools/tests/test_cli.py

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
import pytest
2+
from cromwell_tools.cli import parser as cli_parser
3+
import tempfile
4+
import os
5+
import six
6+
import json
7+
from cromwell_tools.cromwell_auth import CromwellAuth
8+
9+
10+
six.add_move(six.MovedModule('mock', 'mock', 'unittest.mock'))
11+
from six.moves import mock # noqa
12+
13+
14+
@pytest.fixture(scope="module")
15+
def username_password_auth():
16+
return [
17+
"--username",
18+
"fake-user",
19+
"--password",
20+
"fake-pwd",
21+
"--url",
22+
"https://fake-cromwell",
23+
]
24+
25+
26+
@pytest.fixture(scope="module")
27+
def no_auth():
28+
return ["--url", "https://fake-cromwell"]
29+
30+
31+
@pytest.fixture(scope="module")
32+
def service_account_auth():
33+
temp_dir = tempfile.mkdtemp()
34+
service_account_key = os.path.join(temp_dir, 'fake_key.json')
35+
fake_svc_info = {"token_uri": "foo", "client_email": "bar", "private_key": "baz"}
36+
with open(service_account_key, 'w') as f:
37+
json.dump(fake_svc_info, f)
38+
return [
39+
"--service-account-key",
40+
service_account_key,
41+
"--url",
42+
"https://fake-cromwell",
43+
]
44+
45+
46+
@pytest.fixture(scope="module")
47+
def secret_file_auth():
48+
temp_dir = tempfile.mkdtemp()
49+
secrets_file = os.path.join(temp_dir, 'fake_secrets.json')
50+
auth_params = {
51+
"url": "https://fake-cromwell",
52+
"username": "fake-user",
53+
"password": "fake-pwd",
54+
}
55+
with open(secrets_file, 'w') as f:
56+
json.dump(auth_params, f)
57+
return ["--secrets-file", secrets_file]
58+
59+
60+
def test_cli_print_version_info():
61+
"""Make sure the CLI prints version info properly"""
62+
user_inputs = ["-V"]
63+
with pytest.raises(SystemExit) as pytest_wrapped_exit:
64+
cli_parser(user_inputs)
65+
assert pytest_wrapped_exit.type == SystemExit
66+
assert pytest_wrapped_exit.value.code == 0
67+
68+
69+
def test_cli_command_raise_value_error_when_no_creds_provided():
70+
"""Make sure the CLI raise exception about the auth when no creds provided."""
71+
user_inputs = ["submit", "--wdl-file", "fake.wdl", "--inputs-files", "fake.json"]
72+
with pytest.raises(ValueError):
73+
command, args = cli_parser(user_inputs)
74+
75+
76+
def test_cli_command_works_with_username_password_auth(username_password_auth):
77+
"""Use the submit command as an example to prove CLI works with u/p auth."""
78+
user_inputs = [
79+
"submit",
80+
"--wdl-file",
81+
"fake.wdl",
82+
"--inputs-files",
83+
"fake.json",
84+
] + username_password_auth
85+
command, args = cli_parser(user_inputs)
86+
87+
88+
def test_cli_command_works_with_no_auth(no_auth):
89+
"""Use the submit command as an example to prove CLI works with u/p auth."""
90+
user_inputs = [
91+
"submit",
92+
"--wdl-file",
93+
"fake.wdl",
94+
"--inputs-files",
95+
"fake.json",
96+
] + no_auth
97+
command, args = cli_parser(user_inputs)
98+
99+
100+
@mock.patch('cromwell_tools.cromwell_auth.CromwellAuth.from_service_account_key_file')
101+
def test_cli_command_works_with_service_account_auth(mock_header, service_account_auth):
102+
"""Use the submit command as an example to prove CLI works with u/p auth."""
103+
expected_auth = CromwellAuth(
104+
url="https://fake-cromwell",
105+
header={"Authorization": "bearer fake_token"},
106+
auth=None,
107+
)
108+
mock_header.return_value = expected_auth
109+
user_inputs = [
110+
"submit",
111+
"--wdl-file",
112+
"fake.wdl",
113+
"--inputs-files",
114+
"fake.json",
115+
] + service_account_auth
116+
command, args = cli_parser(user_inputs)
117+
118+
119+
def test_cli_command_works_with_secrets_file_auth(secret_file_auth):
120+
"""Use the submit command as an example to prove CLI works with u/p auth."""
121+
user_inputs = [
122+
"submit",
123+
"--wdl-file",
124+
"fake.wdl",
125+
"--inputs-files",
126+
"fake.json",
127+
] + secret_file_auth
128+
command, args = cli_parser(user_inputs)
129+
130+
131+
def test_cli_submit_command(no_auth):
132+
"""Test the submit command (with no-auth for simplicity)."""
133+
user_inputs = [
134+
"submit",
135+
"--wdl-file",
136+
"fake.wdl",
137+
"--inputs-files",
138+
"fake.json",
139+
] + no_auth
140+
command, args = cli_parser(user_inputs)
141+
assert command.__name__ == "submit"
142+
assert args['wdl_file'] == "fake.wdl"
143+
assert "fake.json" in args['inputs_files']
144+
145+
146+
def test_cli_wait_command(no_auth):
147+
"""Test the wait command (with no-auth for simplicity)."""
148+
user_inputs = [
149+
"wait",
150+
"--poll-interval-seconds",
151+
"10",
152+
"00000000-0000-0000-0000-000000000000",
153+
"00000000-0000-0000-0000-000000000000",
154+
] + no_auth
155+
command, args = cli_parser(user_inputs)
156+
assert command.__name__ == "wait"
157+
assert "00000000-0000-0000-0000-000000000000" in args["workflow_ids"]
158+
159+
160+
def test_cli_status_command(no_auth):
161+
"""Test the status command (with no-auth for simplicity)."""
162+
user_inputs = ["status", "--uuid", "00000000-0000-0000-0000-000000000000"] + no_auth
163+
command, args = cli_parser(user_inputs)
164+
assert command.__name__ == "status"
165+
assert args["uuid"] == "00000000-0000-0000-0000-000000000000"
166+
167+
168+
def test_cli_abort_command(no_auth):
169+
"""Test the abort command (with no-auth for simplicity)."""
170+
user_inputs = ["abort", "--uuid", "00000000-0000-0000-0000-000000000000"] + no_auth
171+
command, args = cli_parser(user_inputs)
172+
assert command.__name__ == "abort"
173+
assert args["uuid"] == "00000000-0000-0000-0000-000000000000"
174+
175+
176+
def test_cli_release_hold_command(no_auth):
177+
"""Test the release hold command (with no-auth for simplicity)."""
178+
user_inputs = [
179+
"release_hold",
180+
"--uuid",
181+
"00000000-0000-0000-0000-000000000000",
182+
] + no_auth
183+
command, args = cli_parser(user_inputs)
184+
assert command.__name__ == "release_hold"
185+
assert args["uuid"] == "00000000-0000-0000-0000-000000000000"
186+
187+
188+
def test_cli_metadata_command(no_auth):
189+
"""Test the metadata command (with no-auth for simplicity)."""
190+
user_inputs = [
191+
"metadata",
192+
"--uuid",
193+
"00000000-0000-0000-0000-000000000000",
194+
"--includeKey",
195+
"jobId",
196+
] + no_auth
197+
command, args = cli_parser(user_inputs)
198+
assert command.__name__ == "metadata"
199+
assert args["uuid"] == "00000000-0000-0000-0000-000000000000"
200+
assert "jobId" in args["includeKey"]
201+
202+
203+
def test_cli_query_command(no_auth):
204+
"""Test the query command (with no-auth for simplicity)."""
205+
# Not Implemented yet
206+
assert True
207+
208+
209+
def test_cli_health_command(no_auth):
210+
"""Test the health command (with no-auth for simplicity)."""
211+
user_inputs = ["health"] + no_auth
212+
command, args = cli_parser(user_inputs)
213+
assert command.__name__ == "health"
214+
215+
216+
def test_cli_task_runtime_command(no_auth):
217+
"""Test the task_runtime command (with no-auth for simplicity)."""
218+
user_inputs = [
219+
"task_runtime",
220+
"--uuid",
221+
"00000000-0000-0000-0000-000000000000",
222+
] + no_auth
223+
command, args = cli_parser(user_inputs)
224+
assert command.__name__ == "run" # task_runtime's entrypoint is run()
225+
assert args["uuid"] == "00000000-0000-0000-0000-000000000000"

setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
'six>=1.11.0',
1717
'google-auth>=1.6.1,<2',
1818
'setuptools_scm>=3.1.0,<4',
19+
'google-api-python-client>=1.7,<2',
20+
'python-dateutil>=2.8,<3',
1921
]
2022

2123
extras_require = {

0 commit comments

Comments
 (0)