|
| 1 | +# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 | +# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +"""Integration test for the cat log script. |
| 17 | +""" |
| 18 | + |
| 19 | +import pytest |
| 20 | +import re |
| 21 | +import shutil |
| 22 | +from types import SimpleNamespace |
| 23 | + |
| 24 | +from cylc.flow.exceptions import InputError |
| 25 | +from cylc.flow.option_parsers import Options |
| 26 | +from cylc.flow.scripts.cat_log import ( |
| 27 | + _main as cat_log, |
| 28 | + get_option_parser as cat_log_gop |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +BAD_NAME = "NONEXISTENTWORKFLOWNAME" |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def brokendir(run_dir): |
| 37 | + brokendir = (run_dir / BAD_NAME) |
| 38 | + brokendir.mkdir(exist_ok=True) |
| 39 | + yield brokendir |
| 40 | + shutil.rmtree(brokendir) |
| 41 | + |
| 42 | + |
| 43 | +def test_fail_no_file(flow): |
| 44 | + """It produces a helpful error if there is no workflow log file. |
| 45 | + """ |
| 46 | + parser = cat_log_gop() |
| 47 | + id_ = flow({}) |
| 48 | + with pytest.raises(InputError, match='Log file not found.'): |
| 49 | + cat_log(parser, Options(parser)(), id_) |
| 50 | + |
| 51 | + |
| 52 | +def test_fail_rotation_out_of_range(flow): |
| 53 | + """It produces a helpful error if rotation number > number of log files. |
| 54 | + """ |
| 55 | + parser = cat_log_gop() |
| 56 | + id_ = flow({}) |
| 57 | + path = flow.args[1] |
| 58 | + name = id_.split('/')[-1] |
| 59 | + logpath = (path / name / 'log/scheduler') |
| 60 | + logpath.mkdir(parents=True) |
| 61 | + (logpath / '01-start-01.log').touch() |
| 62 | + |
| 63 | + with pytest.raises(SystemExit): |
| 64 | + cat_log(parser, Options(parser)(rotation_num=0), id_) |
| 65 | + |
| 66 | + msg = r'--rotation 1 invalid \(max value is 0\)' |
| 67 | + |
| 68 | + with pytest.raises(InputError, match=msg): |
| 69 | + cat_log(parser, Options(parser)(rotation_num=1), id_) |
| 70 | + |
| 71 | + |
| 72 | +def test_bad_workflow(run_dir): |
| 73 | + """Test "cylc cat-log" with bad workflow name.""" |
| 74 | + parser = cat_log_gop() |
| 75 | + msg = re.compile( |
| 76 | + fr'^Workflow ID not found: {BAD_NAME}' |
| 77 | + fr'\n\(Directory not found: {run_dir}/{BAD_NAME}\)$', |
| 78 | + re.MULTILINE |
| 79 | + ) |
| 80 | + with pytest.raises(InputError, match=msg): |
| 81 | + cat_log(parser, Options(parser)(filename='l'), BAD_NAME) |
| 82 | + |
| 83 | + |
| 84 | +def test_bad_workflow2(run_dir, brokendir, capsys): |
| 85 | + """Check a non existent file in a valid workflow results in error. |
| 86 | + """ |
| 87 | + parser = cat_log_gop() |
| 88 | + with pytest.raises(SystemExit, match='1'): |
| 89 | + cat_log( |
| 90 | + parser, |
| 91 | + Options(parser)(filename='j'), |
| 92 | + BAD_NAME |
| 93 | + ) |
| 94 | + msg = ( |
| 95 | + f'file not found: {run_dir}' |
| 96 | + '/NONEXISTENTWORKFLOWNAME/log/j\n') |
| 97 | + assert capsys.readouterr().err == msg |
0 commit comments