|
| 1 | +import json |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from traceback import print_exception |
| 5 | + |
| 6 | +import pytest |
| 7 | +from click.testing import CliRunner |
| 8 | + |
| 9 | +from cromshell.__main__ import main_entry as cromshell |
| 10 | + |
| 11 | + |
| 12 | +def run_cromshell_submit( |
| 13 | + wdl: str, json_file: str, local_cromwell_url: str, exit_code: int |
| 14 | +): |
| 15 | + """Run cromshell submit using CliRunner and assert job is successful""" |
| 16 | + |
| 17 | + runner = CliRunner(mix_stderr=False) |
| 18 | + # The absolute path will be passed to the invoke command because |
| 19 | + # the test is being run in temp directory created by CliRunner. |
| 20 | + absolute_wdl = str(Path(wdl).resolve()) |
| 21 | + absolute_json = str(Path(json_file).resolve()) |
| 22 | + with runner.isolated_filesystem(): |
| 23 | + result = runner.invoke( |
| 24 | + cromshell, |
| 25 | + [ |
| 26 | + "--cromwell_url", |
| 27 | + local_cromwell_url, |
| 28 | + "--hide_logo", |
| 29 | + "submit", |
| 30 | + absolute_wdl, |
| 31 | + absolute_json, |
| 32 | + ], |
| 33 | + ) |
| 34 | + assert result.exit_code == exit_code, ( |
| 35 | + f"\nSTDOUT:\n{result.stdout}" |
| 36 | + f"\nSTDERR:\n{result.stderr}" |
| 37 | + f"\nExceptions:\n{result.exception}" |
| 38 | + f"\n{print_exception(*result.exc_info)}" |
| 39 | + ) |
| 40 | + return result |
| 41 | + |
| 42 | + |
| 43 | +def workflow_id_in_txt_db(result, local_workflow_database_tsv: Path): |
| 44 | + """Get workflow id and assert it is listed in the local workflow database tsv""" |
| 45 | + |
| 46 | + stdout_substring_formatted = json.loads(result.stdout) |
| 47 | + test_workflow_id = stdout_substring_formatted["id"] |
| 48 | + |
| 49 | + # check if workflow id is in database tsv |
| 50 | + with open(local_workflow_database_tsv, "r") as file1: |
| 51 | + |
| 52 | + # read file content |
| 53 | + readfile = file1.read() |
| 54 | + # checking condition for string found or not |
| 55 | + assert ( |
| 56 | + test_workflow_id in readfile |
| 57 | + ), "Workflow ID was not found in /all.workflow.database.tsv" |
| 58 | + |
| 59 | + |
| 60 | +class TestSubmit: |
| 61 | + @pytest.mark.dependency(name="test_submit") |
| 62 | + @pytest.mark.parametrize( |
| 63 | + "wdl, json_file, exit_code", |
| 64 | + [ |
| 65 | + ("tests/workflows/helloWorld.wdl", "tests/workflows/helloWorld.json", 0), |
| 66 | + ("tests/workflows/helloWorld.wdl", "tests/workflows/not_valid.json", 1), |
| 67 | + ("tests/workflows/not_valid.wdl", "tests/workflows/helloWorld.json", 1), |
| 68 | + ], |
| 69 | + ) |
| 70 | + def test_submit( |
| 71 | + self, |
| 72 | + local_cromwell_url: str, |
| 73 | + wdl: str, |
| 74 | + json_file: str, |
| 75 | + exit_code: int, |
| 76 | + local_hidden_cromshell_folder: Path, |
| 77 | + local_workflow_database_tsv: Path, |
| 78 | + ): |
| 79 | + # Run cromshell submit |
| 80 | + result = run_cromshell_submit( |
| 81 | + wdl=wdl, |
| 82 | + json_file=json_file, |
| 83 | + exit_code=exit_code, |
| 84 | + local_cromwell_url=local_cromwell_url, |
| 85 | + ) |
| 86 | + |
| 87 | + # If submission passed check workflow id in database tsv |
| 88 | + if exit_code == 0: |
| 89 | + workflow_id_in_txt_db( |
| 90 | + result=result, local_workflow_database_tsv=local_workflow_database_tsv |
| 91 | + ) |
| 92 | + |
| 93 | + @pytest.mark.dependency(depends=["test_submit"]) |
| 94 | + def test_submit_cromshell_folders_created( |
| 95 | + self, local_server_folder, local_hidden_cromshell_folder |
| 96 | + ): |
| 97 | + # Created hidden cromshell directory |
| 98 | + assert Path.exists(local_hidden_cromshell_folder) |
| 99 | + # Created a directory to hold function input files, using server name |
| 100 | + assert Path.exists(local_server_folder) |
| 101 | + |
| 102 | + @pytest.fixture |
| 103 | + def local_cromwell_url(self): |
| 104 | + return "http://localhost:8000" |
| 105 | + |
| 106 | + @pytest.fixture |
| 107 | + def local_hidden_cromshell_folder(self): |
| 108 | + return Path(os.environ.get("CROMSHELL_CONFIG")).joinpath(".cromshell") |
| 109 | + |
| 110 | + @pytest.fixture |
| 111 | + def local_workflow_database_tsv(self, local_hidden_cromshell_folder): |
| 112 | + return Path(local_hidden_cromshell_folder).joinpath("all.workflow.database.tsv") |
| 113 | + |
| 114 | + @pytest.fixture |
| 115 | + def local_server_folder(self, local_hidden_cromshell_folder, local_cromwell_url): |
| 116 | + stripped_cromwell_url = local_cromwell_url.replace("http://", "") |
| 117 | + return Path(local_hidden_cromshell_folder).joinpath(stripped_cromwell_url) |
0 commit comments