| 1 | import json |
| 2 | import logging |
| 3 | import os.path |
| 4 | import shlex |
| 5 | import tempfile |
| 6 | |
| 7 | import pbcommand.common_options as CU |
| 8 | from pbcommand.cli.core import pacbio_args_runner |
| 9 | from pbcommand.cli import get_default_argparser |
| 10 | from pbcommand.utils import setup_log |
| 11 | |
| 12 | log = logging.getLogger(__name__) |
| 13 | |
| 14 | |
| 15 | def args_runner(*args, **kwargs): |
| 16 | log.info("Running args: {a}".format(a=args)) |
| 17 | return 0 |
| 18 | |
| 19 | |
| 20 | def _example_parser(): |
| 21 | p = get_default_argparser("1.0.0", "Example Mock Parser") |
| 22 | p = CU.add_log_debug_option(p) |
| 23 | p.add_argument('example_file', type=str, help="No testing of existence") |
| 24 | return p |
| 25 | |
| 26 | |
| 27 | def _example_main(cmdline_args): |
| 28 | """Example func for testing.""" |
| 29 | p = _example_parser() |
| 30 | argv = shlex.split(cmdline_args) |
| 31 | rcode = pacbio_args_runner(argv, p, args_runner, log, setup_log) |
| 32 | return rcode |
| 33 | |
| 34 | |
| 35 | def args_runner_fail(*args, **kwargs): |
| 36 | raise RuntimeError("Oops, something crashed") |
| 37 | |
| 38 | |
| 39 | def _example_main_fail(cmdline_args): |
| 40 | """Example func for testing.""" |
| 41 | p = _example_parser() |
| 42 | argv = shlex.split(cmdline_args) |
| 43 | rcode = pacbio_args_runner(argv, p, args_runner_fail, log, setup_log) |
| 44 | return rcode |
| 45 | |
| 46 | |
| 47 | class SimpleTest: |
| 48 | |
| 49 | def setup_method(self, method): |
| 50 | tmpdir = tempfile.mkdtemp(suffix="cromwell-executions") |
| 51 | self._cwd = os.getcwd() |
| 52 | os.chdir(tmpdir) |
| 53 | |
| 54 | def teardown_method(self, method): |
| 55 | os.chdir(self._cwd) |
| 56 | |
| 57 | def test_01(self): |
| 58 | args = "--debug /path/to/my_fake_file.txt" |
| 59 | rcode = _example_main(args) |
| 60 | assert rcode == 0 |
| 61 | assert not os.path.isfile("alarms.json") |
| 62 | |
| 63 | def test_dump_alarm_on_error(self): |
| 64 | args = "--debug /path/to/my_fake_file.txt" |
| 65 | os.environ["SMRT_PIPELINE_BUNDLE_DIR"] = "true" |
| 66 | rcode = _example_main_fail(args) |
| 67 | assert rcode == 2 |
| 68 | assert os.path.isfile("alarms.json") |
| 69 | with open("alarms.json", "r") as json_in: |
| 70 | d = json.loads(json_in.read())[0] |
| 71 | assert d["severity"] == "ERROR" |
| 72 | with open("task-report.json", "r") as rpt_in: |
| 73 | d = json.loads(rpt_in.read()) |
| 74 | a = {a["id"]: a["value"] for a in d["attributes"]} |
| 75 | assert a["workflow_task.exit_code"] == 2 |
| 76 | assert a["workflow_task.nproc"] == 1 |