Debian Codegraph @

cylc/flow/log_diagnosis.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 """Run reference test."""
17
18 from difflib import unified_diff
19 import re
20 import os
21 from typing import TYPE_CHECKING
22
23 from cylc.flow import LOG
24 from cylc.flow.exceptions import WorkflowEventError
25 from cylc.flow.pathutil import get_workflow_test_log_path
26
27 if TYPE_CHECKING:
28 from cylc.flow.scheduler import Scheduler
29
30
31 RE_TRIG = re.compile(r'(^.*? -triggered off \[.*\].*$)')
32
33
34 def run_reftest(schd: 'Scheduler') -> None:
35 """Run reference test at shutdown."""
36 reffilename = schd.config.get_ref_log_name()
37 curfilename = get_workflow_test_log_path(schd.workflow)
38 ref = _load_reflog(reffilename, False)
39 if not ref:
40 raise WorkflowEventError("No triggering events in reference log.")
41 cur = _load_reflog(curfilename, "in flow" not in ref[0])
42 if not cur:
43 raise WorkflowEventError("No triggering events in test log.")
44 if ref == cur:
45 LOG.info('WORKFLOW REFERENCE TEST PASSED')
46 else:
47 exc = WorkflowEventError(
48 'WORKFLOW REFERENCE TEST FAILED\n'
49 'triggering is NOT consistent with the reference log:\n%s\n'
50 % '\n'.join(unified_diff(ref, cur, 'reference', 'this run'))
51 )
52 LOG.exception(exc)
53 raise exc
54
55
56 def _load_reflog(filename, strip_flows):
57 """Reference test: get trigger info from reference log.
58
59 Back-compat for old logs: strip flow nums from each line.
60 """
61 res = []
62 with open(os.path.expandvars(filename), 'r') as reflog:
63 for line in reflog:
64 match = RE_TRIG.search(line)
65 if match:
66 if strip_flows:
67 res.append(re.sub(' in flow .*$', '', match.groups()[0]))
68 else:
69 res.append(match.groups()[0])
70 res.sort()
71 return res