| 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 |
"""Compatibility tests for handling old workflow databases.""" |
| 18 |
|
| 19 |
from functools import partial |
| 20 |
from unittest.mock import Mock |
| 21 |
import pytest |
| 22 |
import sqlite3 |
| 23 |
|
| 24 |
from cylc.flow.exceptions import CylcError, ServiceFileError |
| 25 |
from cylc.flow.task_pool import TaskPool |
| 26 |
from cylc.flow.workflow_db_mgr import ( |
| 27 |
CylcWorkflowDAO, |
| 28 |
WorkflowDatabaseManager, |
| 29 |
) |
| 30 |
from cylc.flow.dbstatecheck import CylcWorkflowDBChecker |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture |
| 34 |
def _setup_db(tmp_path): |
| 35 |
"""Fixture to create old DB.""" |
| 36 |
def _inner(stmts, db_file_name='sql.db'): |
| 37 |
db_file = tmp_path / db_file_name |
| 38 |
db_file.parent.mkdir(parents=True, exist_ok=True) |
| 39 |
# Note: cannot use CylcWorkflowDAO here as creating outdated DB |
| 40 |
conn = sqlite3.connect(str(db_file)) |
| 41 |
conn.execute(( |
| 42 |
r'CREATE TABLE task_states(name TEXT, cycle TEXT, flow_nums TEXT,' |
| 43 |
r' time_created TEXT, time_updated TEXT, submit_num INTEGER,' |
| 44 |
r' status TEXT, flow_wait INTEGER, is_manual_submit INTEGER,' |
| 45 |
r' PRIMARY KEY(name, cycle, flow_nums));') |
| 46 |
) |
| 47 |
conn.execute(( |
| 48 |
r'CREATE TABLE task_jobs(cycle TEXT, name TEXT,' |
| 49 |
r' submit_num INTEGER, is_manual_submit INTEGER,' |
| 50 |
r' try_num INTEGER, time_submit TEXT, time_submit_exit TEXT,' |
| 51 |
r' submit_status INTEGER, time_run TEXT, time_run_exit TEXT,' |
| 52 |
r' run_signal TEXT, run_status INTEGER, platform_name TEXT,' |
| 53 |
r' job_runner_name TEXT, job_id TEXT,' |
| 54 |
r' PRIMARY KEY(cycle, name, submit_num));' |
| 55 |
)) |
| 56 |
for stmt, values in stmts: |
| 57 |
conn.execute(stmt, values) |
| 58 |
conn.execute(( |
| 59 |
r"INSERT INTO task_jobs VALUES" |
| 60 |
r" ('10090101T0000Z', 'foo', 1, 0, 1, '2022-12-05T14:46:06Z'," |
| 61 |
r" '2022-12-05T14:46:07Z', 0, '2022-12-05T14:46:10Z'," |
| 62 |
r" '2022-12-05T14:46:39Z', '', 0, 'localhost', 'background'," |
| 63 |
r" 4377)" |
| 64 |
)) |
| 65 |
conn.commit() |
| 66 |
conn.close() |
| 67 |
return db_file |
| 68 |
return _inner |
| 69 |
|
| 70 |
|
| 71 |
def test_upgrade_pre_810_fails_on_multiple_flows(_setup_db): |
| 72 |
stmts = [( |
| 73 |
r'INSERT INTO task_states VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', |
| 74 |
( |
| 75 |
'foo', |
| 76 |
'10050101T0000Z', |
| 77 |
'[1, 3]', |
| 78 |
'2022-12-05T14:46:33Z', |
| 79 |
'2022-12-05T14:46:40Z', |
| 80 |
1, |
| 81 |
'succeeded', |
| 82 |
0, |
| 83 |
0, |
| 84 |
), |
| 85 |
)] |
| 86 |
db_file_name = _setup_db(stmts) |
| 87 |
with CylcWorkflowDAO(db_file_name) as dao, pytest.raises( |
| 88 |
CylcError, |
| 89 |
match='^Cannot .* 8.0.x to 8.1.0 .* used.$' |
| 90 |
): |
| 91 |
WorkflowDatabaseManager.upgrade_pre_810(dao) |
| 92 |
|
| 93 |
|
| 94 |
def test_upgrade_pre_810_pass_on_single_flow(_setup_db): |
| 95 |
stmts = [( |
| 96 |
r'INSERT INTO task_states VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', |
| 97 |
( |
| 98 |
'foo', |
| 99 |
'10050101T0000Z', |
| 100 |
'[1]', |
| 101 |
'2022-12-05T14:46:33Z', |
| 102 |
'2022-12-05T14:46:40Z', |
| 103 |
1, |
| 104 |
'succeeded', |
| 105 |
0, |
| 106 |
0, |
| 107 |
), |
| 108 |
)] |
| 109 |
db_file_name = _setup_db(stmts) |
| 110 |
with CylcWorkflowDAO(db_file_name) as dao: |
| 111 |
WorkflowDatabaseManager.upgrade_pre_810(dao) |
| 112 |
result = dao.connect().execute( |
| 113 |
'SELECT DISTINCT flow_nums FROM task_jobs;' |
| 114 |
).fetchall()[0][0] |
| 115 |
assert result == '[1]' |
| 116 |
|
| 117 |
|
| 118 |
def test_check_workflow_db_compat(_setup_db, capsys): |
| 119 |
"""method can pick private or public db to check. |
| 120 |
""" |
| 121 |
# Create public and private databases with different cylc versions: |
| 122 |
create = r'CREATE TABLE workflow_params(key TEXT, value TEXT)' |
| 123 |
insert = r'INSERT INTO workflow_params VALUES (?, ?)' |
| 124 |
pri_path = _setup_db( |
| 125 |
[ |
| 126 |
(create, tuple()), |
| 127 |
(insert, ('cylc_version', '7.99.99')), |
| 128 |
], |
| 129 |
db_file_name='private/db', |
| 130 |
) |
| 131 |
pub_path = _setup_db( |
| 132 |
[ |
| 133 |
(create, tuple()), |
| 134 |
(insert, ('cylc_version', '7.99.98')), |
| 135 |
], |
| 136 |
db_file_name='public/db' |
| 137 |
) |
| 138 |
|
| 139 |
with pytest.raises(ServiceFileError, match='99.98'): |
| 140 |
WorkflowDatabaseManager.check_db_compatibility(pub_path) |
| 141 |
|
| 142 |
with pytest.raises(ServiceFileError, match='99.99'): |
| 143 |
WorkflowDatabaseManager.check_db_compatibility(pri_path) |
| 144 |
|
| 145 |
|
| 146 |
def test_cylc_7_db_wflow_params_table(_setup_db): |
| 147 |
"""Test back-compat needed by workflow state xtrigger for Cylc 7 DBs.""" |
| 148 |
ptformat = "CCYY" |
| 149 |
create = r'CREATE TABLE suite_params(key TEXT, value TEXT)' |
| 150 |
insert = r'INSERT INTO suite_params VALUES (?, ?)' |
| 151 |
db_file_name = _setup_db([ |
| 152 |
(create, tuple()), |
| 153 |
(insert, ('cycle_point_format', ptformat)), |
| 154 |
]) |
| 155 |
with CylcWorkflowDBChecker('foo', 'bar', db_path=db_file_name) as checker: |
| 156 |
with pytest.raises( |
| 157 |
sqlite3.OperationalError, match="no such table: workflow_params" |
| 158 |
): |
| 159 |
checker._get_db_point_format() |
| 160 |
|
| 161 |
assert checker.db_point_fmt == ptformat |
| 162 |
|
| 163 |
|
| 164 |
def test_pre_830_task_action_timers(_setup_db): |
| 165 |
"""Test back compat for task_action_timers table. |
| 166 |
|
| 167 |
Before 8.3.0, TaskEventMailContext had an extra field "ctx_type" at |
| 168 |
index 1. TaskPool.load_db_task_action_timers() should be able to |
| 169 |
discard this field. |
| 170 |
""" |
| 171 |
stmts = [ |
| 172 |
( |
| 173 |
r''' |
| 174 |
CREATE TABLE task_action_timers( |
| 175 |
cycle TEXT, name TEXT, ctx_key TEXT, ctx TEXT, delays TEXT, |
| 176 |
num INTEGER, delay TEXT, timeout TEXT, |
| 177 |
PRIMARY KEY(cycle, name, ctx_key) |
| 178 |
); |
| 179 |
''', |
| 180 |
tuple(), |
| 181 |
), |
| 182 |
( |
| 183 |
r'INSERT INTO task_action_timers VALUES (?, ?, ?, ?, ?, ?, ?, ?)', |
| 184 |
( |
| 185 |
'1', |
| 186 |
'foo', |
| 187 |
'[["event-mail", "failed"], 9]', |
| 188 |
( |
| 189 |
'["TaskEventMailContext", ["event-mail", "event-mail",' |
| 190 |
' "[email protected]", "jfaden"]]' |
| 191 |
), |
| 192 |
'[0.0]', |
| 193 |
1, |
| 194 |
'0.0', |
| 195 |
'1709229449.61275', |
| 196 |
) |
| 197 |
), |
| 198 |
( |
| 199 |
r'INSERT INTO task_action_timers VALUES (?, ?, ?, ?, ?, ?, ?, ?)', |
| 200 |
( |
| 201 |
'1', |
| 202 |
'foo', |
| 203 |
'["try_timers", "execution-retry"]', |
| 204 |
None, |
| 205 |
'[94608000.0]', |
| 206 |
1, |
| 207 |
None, |
| 208 |
None, |
| 209 |
), |
| 210 |
), |
| 211 |
] |
| 212 |
db_file = _setup_db(stmts) |
| 213 |
mock_pool = Mock() |
| 214 |
load_db_task_action_timers = partial( |
| 215 |
TaskPool.load_db_task_action_timers, mock_pool |
| 216 |
) |
| 217 |
with CylcWorkflowDAO(db_file, create_tables=True) as dao: |
| 218 |
dao.select_task_action_timers(load_db_task_action_timers) |