|
| 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" |
0 commit comments