From 4b2b5cd158c29905b2512273b4352c33143947ef Mon Sep 17 00:00:00 2001 From: Matt Shin Date: Mon, 7 Aug 2017 16:26:51 +0100 Subject: [PATCH] validate: fail on bad task event handler templates --- lib/cylc/config.py | 24 +++++++- .../36-task-event-bad-custom-template.t | 7 ++- .../validate/65-bad-task-event-handler-tmpl.t | 55 +++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100755 tests/validate/65-bad-task-event-handler-tmpl.t diff --git a/lib/cylc/config.py b/lib/cylc/config.py index 3f0a1c3d502..ed3e42f99d0 100644 --- a/lib/cylc/config.py +++ b/lib/cylc/config.py @@ -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, @@ -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']: diff --git a/tests/events/36-task-event-bad-custom-template.t b/tests/events/36-task-event-bad-custom-template.t index 2127edb0f69..6b701cd067a 100755 --- a/tests/events/36-task-event-bad-custom-template.t +++ b/tests/events/36-task-event-bad-custom-template.t @@ -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" diff --git a/tests/validate/65-bad-task-event-handler-tmpl.t b/tests/validate/65-bad-task-event-handler-tmpl.t new file mode 100755 index 00000000000..30d2d7a0e99 --- /dev/null +++ b/tests/validate/65-bad-task-event-handler-tmpl.t @@ -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 . +#------------------------------------------------------------------------------- +# 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