| 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 |
|
| 17 |
import pytest |
| 18 |
from typing import Any, Dict |
| 19 |
|
| 20 |
from cylc.flow.cycling.integer import IntegerPoint |
| 21 |
from cylc.flow.cycling.iso8601 import ISO8601Point |
| 22 |
from cylc.flow.id import TaskTokens |
| 23 |
from cylc.flow.scheduler import Scheduler |
| 24 |
|
| 25 |
|
| 26 |
# Define here to ensure test doesn't just mirror code: |
| 27 |
KGO = { |
| 28 |
'live': { |
| 29 |
'flow_nums': '[1]', |
| 30 |
'is_manual_submit': 0, |
| 31 |
'try_num': 1, |
| 32 |
'submit_status': 0, |
| 33 |
'run_signal': None, |
| 34 |
'run_status': 0, |
| 35 |
# capture_live_submissions fixture submits jobs in sim mode |
| 36 |
'platform_name': 'simulation', |
| 37 |
'job_runner_name': 'simulation', |
| 38 |
'job_id': None, |
| 39 |
}, |
| 40 |
'skip': { |
| 41 |
'flow_nums': '[1]', |
| 42 |
'is_manual_submit': 0, |
| 43 |
'try_num': 1, |
| 44 |
'submit_status': 0, |
| 45 |
'run_signal': None, |
| 46 |
'run_status': 0, |
| 47 |
'platform_name': 'skip', |
| 48 |
'job_runner_name': 'skip', |
| 49 |
'job_id': None, |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
def not_time(data: Dict[str, Any]): |
| 55 |
"""Filter out fields containing times to reduce risk of |
| 56 |
flakiness""" |
| 57 |
return {k: v for k, v in data.items() if 'time' not in k} |
| 58 |
|
| 59 |
|
| 60 |
@pytest.fixture |
| 61 |
def submit_and_check_db(): |
| 62 |
"""Wraps up testing that we want to do repeatedly in |
| 63 |
test_db_task_jobs. |
| 64 |
""" |
| 65 |
def _inner(schd): |
| 66 |
# Submit task jobs: |
| 67 |
schd.submit_task_jobs(schd.pool.get_tasks()) |
| 68 |
# Make sure that db changes are enacted: |
| 69 |
schd.workflow_db_mgr.process_queued_ops() |
| 70 |
|
| 71 |
for mode, kgo in KGO.items(): |
| 72 |
task_jobs = schd.workflow_db_mgr.pub_dao.select_task_job(1, mode) |
| 73 |
|
| 74 |
# Check all non-datetime items against KGO: |
| 75 |
assert not_time(task_jobs) == kgo, ( |
| 76 |
f'Mode {mode}: incorrect db entries.') |
| 77 |
|
| 78 |
# Check that timestamps have been created: |
| 79 |
for timestamp in [ |
| 80 |
'time_submit', 'time_submit_exit', 'time_run', 'time_run_exit' |
| 81 |
]: |
| 82 |
assert task_jobs[timestamp] is not None |
| 83 |
return _inner |
| 84 |
|
| 85 |
|
| 86 |
async def test_db_task_jobs( |
| 87 |
flow, scheduler, start, capture_live_submissions, |
| 88 |
submit_and_check_db |
| 89 |
): |
| 90 |
"""Ensure that task job data is added to the database correctly |
| 91 |
for each run mode. |
| 92 |
""" |
| 93 |
schd: Scheduler = scheduler( |
| 94 |
flow({ |
| 95 |
'scheduling': { |
| 96 |
'graph': { |
| 97 |
'R1': ' & '.join(KGO) |
| 98 |
} |
| 99 |
}, |
| 100 |
'runtime': { |
| 101 |
mode: {'run mode': mode} for mode in KGO |
| 102 |
}, |
| 103 |
}), |
| 104 |
run_mode='live' |
| 105 |
) |
| 106 |
async with start(schd): |
| 107 |
# Reference all task proxies so we can examine them |
| 108 |
# at the end of the test: |
| 109 |
itask_skip = schd.pool.get_task(IntegerPoint('1'), 'skip') |
| 110 |
itask_live = schd.pool.get_task(IntegerPoint('1'), 'live') |
| 111 |
|
| 112 |
submit_and_check_db(schd) |
| 113 |
|
| 114 |
# Set outputs to failed: |
| 115 |
schd.pool.set_prereqs_and_outputs( |
| 116 |
{TaskTokens('*', 'root')}, ['failed'], [], [] |
| 117 |
) |
| 118 |
|
| 119 |
submit_and_check_db(schd) |
| 120 |
|
| 121 |
# capture_live_submissions fixture submits jobs in sim mode |
| 122 |
assert itask_live.run_mode.value == 'simulation' |
| 123 |
assert itask_skip.run_mode.value == 'skip' |
| 124 |
|
| 125 |
|
| 126 |
async def test_db_task_states( |
| 127 |
one_conf, flow, scheduler, start |
| 128 |
): |
| 129 |
"""Test that tasks will have the same information entered into the task |
| 130 |
state database whichever mode is used. |
| 131 |
""" |
| 132 |
conf = one_conf |
| 133 |
conf['runtime'] = {'one': {'run mode': 'skip'}} |
| 134 |
schd = scheduler(flow(conf)) |
| 135 |
async with start(schd): |
| 136 |
schd.submit_task_jobs(schd.pool.get_tasks()) |
| 137 |
schd.workflow_db_mgr.process_queued_ops() |
| 138 |
result = schd.workflow_db_mgr.pri_dao.connect().execute( |
| 139 |
'SELECT * FROM task_states').fetchone() |
| 140 |
|
| 141 |
# Submit number has been added to the table: |
| 142 |
assert result[5] == 1 |
| 143 |
# time_created added to the table |
| 144 |
assert result[3] |
| 145 |
|
| 146 |
|
| 147 |
async def test_mean_task_time( |
| 148 |
flow, scheduler, start, complete, capture_live_submissions |
| 149 |
): |
| 150 |
"""Non-live tasks are not added to the list of task times, |
| 151 |
so skipping tasks will not affect how long Cylc expects tasks to run. |
| 152 |
""" |
| 153 |
schd = scheduler(flow({ |
| 154 |
'scheduling': { |
| 155 |
'initial cycle point': '1000', |
| 156 |
'final cycle point': '1002', |
| 157 |
'graph': {'P1Y': 'foo'}} |
| 158 |
}), run_mode='live') |
| 159 |
|
| 160 |
async with start(schd): |
| 161 |
itask = schd.pool.get_task(ISO8601Point('10000101T0000Z'), 'foo') |
| 162 |
assert list(itask.tdef.elapsed_times) == [] |
| 163 |
|
| 164 |
# Make the task run in skip mode at one cycle: |
| 165 |
schd.broadcast_mgr.put_broadcast( |
| 166 |
['1000'], ['foo'], [{'run mode': 'skip'}]) |
| 167 |
|
| 168 |
# Fake adding some other examples of the task: |
| 169 |
itask.tdef.elapsed_times.extend([133.0, 132.4]) |
| 170 |
|
| 171 |
# Submit two tasks: |
| 172 |
schd.submit_task_jobs([itask]) |
| 173 |
|
| 174 |
# Ensure that the skipped task has succeeded, and that the |
| 175 |
# number of items in the elapsed_times has not changed. |
| 176 |
assert itask.state.status == 'succeeded' |
| 177 |
assert len(itask.tdef.elapsed_times) == 2 |