Skip to content

Commit

Permalink
validate: fail on bad task event handler templates
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewrmshin committed Sep 7, 2017
1 parent 2ed487f commit 4b2b5cd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
24 changes: 23 additions & 1 deletion lib/cylc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class SuiteConfig(object):
"""Class for suite configuration items and derived quantities."""

Q_DEFAULT = 'default'
TASK_EVENT_TMPL_KEYS = (
'event', 'suite', 'point', 'name', 'submit_num', 'id', 'message')

def __init__(self, suite, fpath, template_vars=None,
owner=None, run_mode='live', is_validate=False, strict=False,
Expand Down Expand Up @@ -1373,7 +1375,27 @@ def check_tasks(self):
('deprecated: [runtime][%s][job]shell=%s: '
'use of ksh to run cylc task job file') %
(taskdef.name, job_shell))

# Check custom event handler templates compat with task meta
if taskdef.rtconfig['events']:
subs = dict((key, key) for key in self.TASK_EVENT_TMPL_KEYS)
for key, value in self.cfg['meta'].items():
subs['suite_' + key.lower()] = value
subs.update(taskdef.rtconfig['meta'])
try:
subs['task_url'] = subs.pop('URL')
except KeyError:
pass
for key, values in taskdef.rtconfig['events'].items():
if values and (
key == 'handlers' or key.endswith(' handler')):
for value in values:
try:
value % subs
except (KeyError, ValueError) as exc:
raise SuiteConfigError(
'ERROR: bad task event handler template'
' %s: %s: %s' % (
taskdef.name, value, repr(exc)))
if cylc.flags.verbose:
OUT.info("Checking for defined tasks not used in the graph")
for name in self.cfg['runtime']:
Expand Down
7 changes: 5 additions & 2 deletions tests/events/36-task-event-bad-custom-template.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
#-------------------------------------------------------------------------------
# Test custom task event handler bad template
. "$(dirname "$0")/test_header"
set_test_number 3
set_test_number 4

OPT_SET=
if [[ "${TEST_NAME_BASE}" == *-globalcfg ]]; then
create_test_globalrc '' ''
fi
install_suite "${TEST_NAME_BASE}" "${TEST_NAME_BASE}"
run_ok "${TEST_NAME_BASE}-validate" cylc validate "${SUITE_NAME}"
run_fail "${TEST_NAME_BASE}-validate" cylc validate "${SUITE_NAME}"
cmp_ok "${TEST_NAME_BASE}-validate.stderr" <<'__ERR__'
"ERROR: bad task event handler template t1: echo %(rubbish)s: KeyError('rubbish',)"
__ERR__
suite_run_ok "${TEST_NAME_BASE}-run" \
cylc run --reference-test --debug "${SUITE_NAME}"
LOG="${SUITE_RUN_DIR}/log/suite/log"
Expand Down
55 changes: 55 additions & 0 deletions tests/validate/65-bad-task-event-handler-tmpl.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
# THIS FILE IS PART OF THE CYLC SUITE ENGINE.
# Copyright (C) 2008-2017 NIWA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------
# Test validation fails on bad task event handler templates.
. "$(dirname "$0")/test_header"

set_test_number 4

TEST_NAME="${TEST_NAME_BASE}-bad-key"
cat >'suite.rc' <<'__SUITE_RC__'
[scheduling]
[[dependencies]]
graph=t1
[runtime]
[[t1]]
script=true
[[[events]]]
failed handler = echo %(id)s, echo %(rubbish)s
__SUITE_RC__
run_fail "${TEST_NAME}" cylc validate 'suite.rc'
cmp_ok "${TEST_NAME}.stderr" <<'__ERR__'
"ERROR: bad task event handler template t1: echo %(rubbish)s: KeyError('rubbish',)"
__ERR__

TEST_NAME="${TEST_NAME_BASE}-bad-value"
cat >'suite.rc' <<'__SUITE_RC__'
[scheduling]
[[dependencies]]
graph=t1
[runtime]
[[t1]]
script=true
[[[events]]]
failed handler = echo %(ids
__SUITE_RC__
run_fail "${TEST_NAME}" cylc validate 'suite.rc'
cmp_ok "${TEST_NAME}.stderr" <<'__ERR__'
"ERROR: bad task event handler template t1: echo %(ids: ValueError('incomplete format key',)"
__ERR__

exit

0 comments on commit 4b2b5cd

Please sign in to comment.