| 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 |
from pathlib import Path |
| 18 |
import pytest |
| 19 |
from pytest import param |
| 20 |
import tempfile |
| 21 |
import unittest |
| 22 |
|
| 23 |
from cylc.flow import __version__ as cylc_version |
| 24 |
from cylc.flow.exceptions import ServiceFileError, InputError |
| 25 |
from cylc.flow.rundb import CylcWorkflowDAO |
| 26 |
from cylc.flow.templatevars import ( |
| 27 |
get_template_vars_from_db, |
| 28 |
load_template_vars |
| 29 |
) |
| 30 |
from cylc.flow.workflow_files import WorkflowFiles |
| 31 |
|
| 32 |
|
| 33 |
class TestTemplatevars(unittest.TestCase): |
| 34 |
|
| 35 |
def test_load_template_vars_no_params(self): |
| 36 |
self.assertFalse(load_template_vars()) |
| 37 |
|
| 38 |
def test_load_template_vars_from_string(self): |
| 39 |
pairs = [ |
| 40 |
"name='John'", |
| 41 |
"type='Human'", |
| 42 |
"age='12'" |
| 43 |
] |
| 44 |
expected = { |
| 45 |
"name": "John", |
| 46 |
"type": "Human", |
| 47 |
"age": "12" |
| 48 |
} |
| 49 |
self.assertEqual(expected, load_template_vars(template_vars=pairs)) |
| 50 |
|
| 51 |
def test_load_template_vars_from_file(self): |
| 52 |
with tempfile.NamedTemporaryFile() as tf: |
| 53 |
tf.write(""" |
| 54 |
name='John' |
| 55 |
type='Human' |
| 56 |
# a comment |
| 57 |
# type=Test |
| 58 |
age='12' |
| 59 |
""".encode()) |
| 60 |
tf.flush() |
| 61 |
expected = { |
| 62 |
"name": "John", |
| 63 |
"type": "Human", |
| 64 |
"age": "12" |
| 65 |
} |
| 66 |
self.assertEqual( |
| 67 |
expected, load_template_vars(template_vars=None, |
| 68 |
template_vars_file=tf.name)) |
| 69 |
|
| 70 |
def test_load_template_vars_from_string_and_file_1(self): |
| 71 |
"""Text pair variables take precedence over file.""" |
| 72 |
pairs = [ |
| 73 |
"name='John'", |
| 74 |
"age='12'" |
| 75 |
] |
| 76 |
with tempfile.NamedTemporaryFile() as tf: |
| 77 |
tf.write(""" |
| 78 |
name='Mariah' |
| 79 |
type='Human' |
| 80 |
# a comment |
| 81 |
# type=Test |
| 82 |
age='70' |
| 83 |
""".encode()) |
| 84 |
tf.flush() |
| 85 |
expected = { |
| 86 |
"name": "John", |
| 87 |
"type": "Human", |
| 88 |
"age": "12" |
| 89 |
} |
| 90 |
self.assertEqual( |
| 91 |
expected, load_template_vars(template_vars=pairs, |
| 92 |
template_vars_file=tf.name)) |
| 93 |
|
| 94 |
def test_load_template_vars_from_string_and_file_2(self): |
| 95 |
"""Text pair variables take precedence over file.""" |
| 96 |
pairs = [ |
| 97 |
"str='str'", |
| 98 |
"int=12", |
| 99 |
"float=12.3", |
| 100 |
"bool=True", |
| 101 |
"none=None" |
| 102 |
] |
| 103 |
expected = { |
| 104 |
'str': 'str', |
| 105 |
'int': 12, |
| 106 |
'float': 12.3, |
| 107 |
'bool': True, |
| 108 |
'none': None |
| 109 |
} |
| 110 |
self.assertEqual(expected, load_template_vars(template_vars=pairs)) |
| 111 |
|
| 112 |
|
| 113 |
@pytest.fixture(scope='module') |
| 114 |
def _setup_db(tmp_path_factory): |
| 115 |
tmp_path: Path = tmp_path_factory.mktemp('test_get_old_tvars') |
| 116 |
logfolder = tmp_path / WorkflowFiles.LogDir.DIRNAME |
| 117 |
logfolder.mkdir() |
| 118 |
db_path = logfolder / WorkflowFiles.LogDir.DB |
| 119 |
with CylcWorkflowDAO(db_path, create_tables=True) as dao: |
| 120 |
dao.connect().execute( |
| 121 |
r''' |
| 122 |
INSERT INTO workflow_params |
| 123 |
VALUES |
| 124 |
(?, ?) |
| 125 |
''', |
| 126 |
("cylc_version", cylc_version), |
| 127 |
) |
| 128 |
dao.connect().executemany( |
| 129 |
r''' |
| 130 |
INSERT INTO workflow_template_vars |
| 131 |
VALUES |
| 132 |
(?, ?) |
| 133 |
''', |
| 134 |
[ |
| 135 |
("FOO", "42"), |
| 136 |
("BAR", "'hello world'"), |
| 137 |
("BAZ", "'foo', 'bar', 48"), |
| 138 |
("QUX", "['foo', 'bar', 21]"), |
| 139 |
], |
| 140 |
) |
| 141 |
dao.connect().commit() |
| 142 |
yield get_template_vars_from_db(tmp_path) |
| 143 |
|
| 144 |
|
| 145 |
@pytest.mark.parametrize( |
| 146 |
'key, expect', |
| 147 |
( |
| 148 |
('FOO', 42), |
| 149 |
('BAR', 'hello world'), |
| 150 |
('BAZ', ('foo', 'bar', 48)), |
| 151 |
('QUX', ['foo', 'bar', 21]) |
| 152 |
) |
| 153 |
) |
| 154 |
def test_get_old_tvars(key, expect, _setup_db): |
| 155 |
"""It can extract a variety of items from a workflow database. |
| 156 |
""" |
| 157 |
assert _setup_db[key] == expect |
| 158 |
|
| 159 |
|
| 160 |
def test_get_old_tvars_fails_if_cylc_7_db(tmp_path): |
| 161 |
"""get_template_vars_from_db fails with error if db file is not a valid |
| 162 |
Cylc 8 DB. |
| 163 |
""" |
| 164 |
dbfile = tmp_path / WorkflowFiles.LogDir.DIRNAME / WorkflowFiles.LogDir.DB |
| 165 |
dbfile.parent.mkdir() |
| 166 |
dbfile.touch() |
| 167 |
with pytest.raises(ServiceFileError, match='database is incompatible'): |
| 168 |
get_template_vars_from_db(tmp_path) |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.parametrize( |
| 172 |
'tvars_lists, expect', |
| 173 |
( |
| 174 |
param( |
| 175 |
['FOO=a,b,c'], {'FOO': ['a', 'b', 'c']}, |
| 176 |
id='basic' |
| 177 |
), |
| 178 |
param( |
| 179 |
['FOO=a,"b,b",c'], {'FOO': ['a', 'b,b', 'c']}, |
| 180 |
id='contains-quotes' |
| 181 |
), |
| 182 |
) |
| 183 |
) |
| 184 |
def test_load_var_lists(tvars_lists, expect): |
| 185 |
""" |
| 186 |
load_template_vars should parse template vars lists and dump these |
| 187 |
into template vars. |
| 188 |
""" |
| 189 |
result = load_template_vars(templatevars_lists=tvars_lists) |
| 190 |
assert result == expect |
| 191 |
|
| 192 |
|
| 193 |
def test_load_var_lists_warns_overwriting(caplog): |
| 194 |
"""It wars user if same tvar name used twice.""" |
| 195 |
load_template_vars(template_vars=["BAZ='HEY'", "BAZ='THERE'"]) |
| 196 |
assert 'BAZ' in caplog.records[-1].message |
| 197 |
load_template_vars(templatevars_lists=['FOO=a,b', 'FOO=b,c']) |
| 198 |
assert 'FOO' in caplog.records[-1].message |
| 199 |
|
| 200 |
|
| 201 |
def test_load_var_lists_fails(): |
| 202 |
""" |
| 203 |
load_template_vars should fail if the same var is set using -s and -z |
| 204 |
""" |
| 205 |
errmsg = r"FOO=a and FOO=\['a', 'b'\]" |
| 206 |
with pytest.raises(InputError, match=errmsg): |
| 207 |
load_template_vars( |
| 208 |
template_vars=['FOO="a"'], |
| 209 |
templatevars_lists=['FOO=a,b'] |
| 210 |
) |
| 211 |
|
| 212 |
|
| 213 |
def test_load_vars_invalid_value_pairs(): |
| 214 |
with pytest.raises(InputError, match="FOO\n.*qux\n.*BAZ\n.*QUX"): |
| 215 |
load_template_vars( |
| 216 |
template_vars=['FOO', 'qux'], |
| 217 |
templatevars_lists=['BAZ', 'QUX'] |
| 218 |
) |
| 219 |
|
| 220 |
|
| 221 |
def test_load_template_vars_ValueError_fm_file(monkeypatch): |
| 222 |
from io import StringIO |
| 223 |
handle = StringIO('Hello') |
| 224 |
monkeypatch.setattr('builtins.open', lambda *_: handle) |
| 225 |
with pytest.raises(InputError, match='Hello'): |
| 226 |
load_template_vars(template_vars_file='foo') |