From be3e27767301b822d2ca63bb3c029b604cea919c Mon Sep 17 00:00:00 2001
From: Tom Coleman <15375218+ColemanTom@users.noreply.github.com>
Date: Wed, 6 Dec 2023 14:45:02 +1100
Subject: [PATCH] Add xtrigger CYLC_PYTHONPATH functional test
Ensures that xtrggers in your CYLC_PYTHONPATH
are respected above entry_point xtriggers.
---
.../xtriggers/04-respect-cylc-pythonpath.t | 47 +++++++++++++++++++
.../04-respect-cylc-pythonpath/dir/echo.py | 21 +++++++++
.../04-respect-cylc-pythonpath/flow.cylc | 15 ++++++
3 files changed, 83 insertions(+)
create mode 100644 tests/functional/xtriggers/04-respect-cylc-pythonpath.t
create mode 100644 tests/functional/xtriggers/04-respect-cylc-pythonpath/dir/echo.py
create mode 100644 tests/functional/xtriggers/04-respect-cylc-pythonpath/flow.cylc
diff --git a/tests/functional/xtriggers/04-respect-cylc-pythonpath.t b/tests/functional/xtriggers/04-respect-cylc-pythonpath.t
new file mode 100644
index 00000000000..f35ece93b3e
--- /dev/null
+++ b/tests/functional/xtriggers/04-respect-cylc-pythonpath.t
@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
+# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
+#
+# 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 persistence of xtrigger results across restart. A cycling task depends
+# on a non cycle-point dependent custom xtrigger called "faker". In the first
+# cycle point the xtrigger succeeds and returns a result, then a task shuts
+# the workflow down. Then we replace the custom xtrigger function with one that
+# will fail if called again - which should not happen because the original
+# result should be remembered (as this xtrigger is not cycle point dependent).
+# Also test the correct result is broadcast to the dependent task before and
+# after workflow restart.
+
+. "$(dirname "$0")/test_header"
+set_test_number 3
+
+install_workflow "${TEST_NAME_BASE}" "${TEST_NAME_BASE}"
+
+# Install the succeeding xtrigger function.
+export CYLC_PYTHONPATH=${WORKFLOW_RUN_DIR}/dir:${CYLC_PYTHONPATH:-}
+
+# Validate the test workflow.
+run_ok "${TEST_NAME_BASE}-val" cylc validate --debug "${WORKFLOW_NAME}"
+
+# Run the first cycle, till auto shutdown by task.
+TEST_NAME="${TEST_NAME_BASE}-run"
+workflow_run_ok "${TEST_NAME}" cylc play --no-detach --debug "${WORKFLOW_NAME}"
+
+# Check the broadcast result of xtrigger.
+cylc cat-log "${WORKFLOW_NAME}" >'scheduler.log.out'
+grep_ok "echo overridden, args=('the_args',)" 'scheduler.log.out'
+
+purge
diff --git a/tests/functional/xtriggers/04-respect-cylc-pythonpath/dir/echo.py b/tests/functional/xtriggers/04-respect-cylc-pythonpath/dir/echo.py
new file mode 100644
index 00000000000..1c38f3ff920
--- /dev/null
+++ b/tests/functional/xtriggers/04-respect-cylc-pythonpath/dir/echo.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
+# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
+#
+# 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 .
+
+
+def echo(*args, **kwargs):
+ print(f"echo overridden, args={args}")
+ return (True, {})
diff --git a/tests/functional/xtriggers/04-respect-cylc-pythonpath/flow.cylc b/tests/functional/xtriggers/04-respect-cylc-pythonpath/flow.cylc
new file mode 100644
index 00000000000..1eb552d84ac
--- /dev/null
+++ b/tests/functional/xtriggers/04-respect-cylc-pythonpath/flow.cylc
@@ -0,0 +1,15 @@
+[scheduler]
+ cycle point format = %Y
+[scheduling]
+ initial cycle point = 2010
+ final cycle point = 2011
+ [[xtriggers]]
+ x1 = echo("the_args")
+ [[graph]]
+ R1 = "@x1 => foo => shutdown"
+ P1Y = "@x1 => foo"
+[runtime]
+ [[foo]]
+ script = true
+ [[shutdown]]
+ script = "cylc shutdown $CYLC_WORKFLOW_ID"