Debian Codegraph @

tests/i/scripts/test_cat_log.py

raw
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
23 from cylc.flow.exceptions import InputError
24 from cylc.flow.option_parsers import Options
25 from cylc.flow.scripts.cat_log import (
26 _main as cat_log,
27 get_option_parser as cat_log_gop
28 )
29
30
31 BAD_NAME = "NONEXISTENTWORKFLOWNAME"
32
33
34 @pytest.fixture
35 def brokendir(run_dir):
36 brokendir = (run_dir / BAD_NAME)
37 brokendir.mkdir(exist_ok=True)
38 yield brokendir
39 shutil.rmtree(brokendir)
40
41
42 def test_fail_no_file(flow):
43 """It produces a helpful error if there is no workflow log file.
44 """
45 parser = cat_log_gop()
46 id_ = flow({})
47 with pytest.raises(InputError, match='Log file not found.'):
48 cat_log(parser, Options(parser)(), id_)
49
50
51 def test_fail_rotation_out_of_range(flow):
52 """It produces a helpful error if rotation number > number of log files.
53 """
54 parser = cat_log_gop()
55 id_ = flow({})
56 path = flow.args[1]
57 name = id_.split('/')[-1]
58 logpath = (path / name / 'log/scheduler')
59 logpath.mkdir(parents=True)
60 (logpath / '01-start-01.log').touch()
61
62 with pytest.raises(SystemExit):
63 cat_log(parser, Options(parser)(rotation_num=0), id_)
64
65 msg = r'--rotation 1 invalid \(max value is 0\)'
66
67 with pytest.raises(InputError, match=msg):
68 cat_log(parser, Options(parser)(rotation_num=1), id_)
69
70
71 def test_bad_workflow(run_dir):
72 """Test "cylc cat-log" with bad workflow name."""
73 parser = cat_log_gop()
74 msg = re.compile(
75 fr'^Workflow ID not found: {BAD_NAME}'
76 fr'\n\(Directory not found: {run_dir}/{BAD_NAME}\)$',
77 re.MULTILINE
78 )
79 with pytest.raises(InputError, match=msg):
80 cat_log(parser, Options(parser)(filename='l'), BAD_NAME)
81
82
83 def test_bad_workflow2(run_dir, brokendir, capsys):
84 """Check a non existent file in a valid workflow results in error.
85 """
86 parser = cat_log_gop()
87 with pytest.raises(SystemExit, match='1'):
88 cat_log(
89 parser,
90 Options(parser)(filename='j'),
91 BAD_NAME
92 )
93 msg = (
94 f'File not found: {run_dir}'
95 '/NONEXISTENTWORKFLOWNAME/log/j\n')
96 assert capsys.readouterr().err == msg
97
98
99 def test_bad_task_dir(run_dir, brokendir, capsys):
100 """Check a non existent job log dir in a valid workflow results in error.
101 """
102 parser = cat_log_gop()
103 with pytest.raises(SystemExit, match='1'):
104 cat_log(
105 parser,
106 Options(parser)(mode='list-dir'),
107 BAD_NAME + "//1/foo"
108 )
109 msg = (
110 f'Directory not found: {run_dir}'
111 '/NONEXISTENTWORKFLOWNAME/log/job/1/foo/NN\n')
112 assert capsys.readouterr().err == msg