Debian Codegraph @

tests/unit/parsec/test_upgrade.py

raw
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 unittest
18
19 import pytest
20
21 from cylc.flow.parsec.upgrade import upgrader, converter
22 from cylc.flow.parsec.exceptions import UpgradeError
23 from cylc.flow.parsec.OrderedDict import OrderedDict
24
25
26 def test_simple():
27 """A quick test of overall functionality."""
28 cfg = {
29 'item one': 1,
30 'item two': 'move me up',
31 'section A': {
32 'abc': 5,
33 'cde': 'foo',
34 'gah': 'bar'
35 },
36 'hostnames': {
37 'host 1': {
38 'work dir': '/a/b/c',
39 'running dir': '/a/b/c/d'
40 },
41 'host 2': {
42 'work dir': '/x/b/c',
43 'running dir': '/x/b/c/d'
44 },
45 }
46 }
47 x2 = converter(lambda x: 2 * x, 'value x 2')
48
49 upg = upgrader(cfg, 'test file')
50 # successive upgrades are incremental - at least until I think of a
51 # good way to remember what items have already been translated...
52 upg.deprecate('1.3', ['item one'], ['item ONE'], x2)
53 upg.deprecate('1.3', ['section A'], ['Heading A'])
54 # NOTE change to new item keys here!
55 upg.deprecate('1.3', ['Heading A', 'cde'], ['Heading A', 'CDE'])
56 upg.deprecate(
57 '1.4', ['Heading A', 'abc'], ['Heading A', 'abc'], cvtr=x2,
58 silent=True)
59 upg.deprecate(
60 '1.4.1', ['item two'], ['Heading A', 'item two'], silent=True)
61 upg.deprecate('1.5', ['hostnames'], ['hosts'])
62 upg.deprecate(
63 '1.5',
64 ['hosts', '__MANY__', 'running dir'], ['hosts', '__MANY__', 'run dir'])
65 # obsolete() but with a custom message - `[Heading A]gah` will be deleted:
66 upg.deprecate(
67 '1.3', ['Heading A', 'gah'], None,
68 cvtr=converter(lambda x: x, 'Yaba daba do'))
69
70 upg.upgrade()
71
72 assert cfg == {
73 'item ONE': 2,
74 'Heading A': {
75 'CDE': 'foo',
76 'abc': 10,
77 'item two': 'move me up'
78 },
79 'hosts': {
80 'host 1': {
81 'work dir': '/a/b/c',
82 'run dir': '/a/b/c/d'
83 },
84 'host 2': {
85 'work dir': '/x/b/c',
86 'run dir': '/x/b/c/d'
87 }
88 }
89 }
90
91
92 def test_conflicting_items():
93 cfg = {
94 'item one': 1,
95 'item two': 2,
96 }
97
98 def get_upgrader():
99 upg = upgrader(cfg, 'test file')
100 upg.deprecate('1.3', ['item one'], ['item two'])
101 return upg
102
103 # specifying both the old and new variants of a config should result in an
104 # error
105 upg = get_upgrader()
106 with pytest.raises(UpgradeError):
107 upg.upgrade()
108
109 # unless the new config is unset
110 cfg['item two'] = None
111 upg = get_upgrader()
112 upg.upgrade()
113
114
115 class TestUpgrade(unittest.TestCase):
116
117 def setUp(self):
118 self.cfg = OrderedDict()
119 self.cfg['section'] = OrderedDict()
120 self.cfg['section']['a'] = '1'
121 self.cfg['section']['b'] = '2'
122 self.u = upgrader(self.cfg, "1.0 to 2.0")
123
124 def test_converter(self):
125 def callback(i):
126 return 1 + i
127
128 c = converter(callback=callback, descr="My callback")
129 self.assertEqual("My callback", c.describe())
130 self.assertEqual(2, c.convert(1))
131
132 def test_constructor(self):
133 self.assertEqual(self.cfg, self.u.cfg)
134 self.assertEqual("1.0 to 2.0", self.u.descr)
135
136 def test_deprecate(self):
137 # b is being deprecated; use c instead
138 self.u.deprecate('entry', ['section', 'b'],
139 ['section', 'c'])
140 self.assertTrue('entry' in self.u.upgrades)
141
142 def callback(i):
143 return 10 * i
144
145 c = converter(callback=callback, descr="My callback")
146 self.u.deprecate(vn='entry',
147 oldkeys=[1, 2, 3],
148 newkeys=[10, 20, 30],
149 cvtr=c)
150
151 # assert the key exists before deprecation
152 self.assertTrue('b' in self.cfg['section'])
153 self.assertFalse('c' in self.cfg['section'])
154 self.u.upgrade()
155 # assert the key exists before deprecation
156 self.assertFalse('b' in self.cfg['section'])
157 self.assertTrue('c' in self.cfg['section'])
158
159 def test_obsolete(self):
160 # b is obsolete, so the value is omitted
161 self.u.obsolete(vn='entry', oldkeys=['section', 'b'], silent=True)
162 self.assertTrue('entry' in self.u.upgrades)
163 self.assertEqual(True, self.u.upgrades['entry'][0]['silent'])
164
165 self.assertTrue('b' in self.cfg['section'])
166 self.u.upgrade()
167 self.assertFalse('b' in self.cfg['section'])
168
169 self.u.obsolete(
170 vn='entry',
171 oldkeys=['section', 'b'],
172 silent=False)
173 self.assertEqual(True, self.u.upgrades['entry'][0]['silent'])
174 self.assertEqual(False, self.u.upgrades['entry'][1]['silent'])
175
176 self.u.obsolete(vn='whocalled?', oldkeys=['section', 'b'], silent=True)
177
178 def test_get_item(self):
179 for keys, results in [
180 (['section', 'a'], '1'),
181 (['section', 'b'], '2'),
182 (['section'], {'a': '1', 'b': '2'})
183 ]:
184 item = self.u.get_item(keys)
185 self.assertEqual(results, item)
186
187 def test_put_item(self):
188 for keys, value in [
189 (['section', 'a'], '100'),
190 (['section', 'b'], '200'),
191 (['special', 'c'], '3'),
192 ]:
193 self.u.put_item(keys, value)
194 self.assertEqual(self.u.get_item(keys), value)
195
196 def test_expand_not_many(self):
197 upg = {
198 'new': None,
199 'cvt': None,
200 'silent': True,
201 'is_section': False,
202 'old': [
203 ]
204 }
205 self.assertEqual([upg], self.u.expand(upg))
206
207 def test_expand_too_many(self):
208 upg = {
209 'new': None,
210 'cvt': None,
211 'silent': True,
212 'is_section': True,
213 'old': [
214 'section', '__MANY__', '__MANY__'
215 ]
216 }
217 with self.assertRaises(UpgradeError) as cm:
218 self.u.expand(upg)
219 self.assertTrue('Multiple simultaneous __MANY__ not supported' in
220 str(cm.exception))
221
222 def test_expand_deprecate_many_mismatch(self):
223 upg = {
224 'new': [
225 'section', '__MANY__'
226 ],
227 'cvt': None,
228 'silent': True,
229 'is_section': False,
230 'old': [
231 'section', '__MANY__', 'b'
232 ]
233 }
234 with self.assertRaises(UpgradeError) as cm:
235 self.u.expand(upg)
236 self.assertEqual('__MANY__ mismatch', str(cm.exception))
237
238 def test_expand_deprecate(self):
239
240 def callback(i):
241 return i
242
243 c = converter(callback=callback, descr="My callback")
244 upg = {
245 'new': [
246 'section', '__MANY__', 'e'
247 ],
248 'cvt': c,
249 'silent': True,
250 'is_section': False,
251 'old': [
252 'section', '__MANY__', 'c'
253 ]
254 }
255 self.u.upgrade()
256 expanded = self.u.expand(upg)
257 self.assertEqual(2, len(expanded))
258 self.assertEqual(['section', 'a', 'e'], expanded[0]['new'])
259
260 def test_expand_obsolete(self):
261 upg = {
262 'new': None,
263 'cvt': None,
264 'silent': True,
265 'is_section': False,
266 'old': [
267 'section', '__MANY__', 'a'
268 ]
269 }
270 self.cfg['__MANY__'] = OrderedDict()
271 self.cfg['__MANY__']['name'] = 'Arthur'
272 self.u.obsolete('entry', ['section', '__MANY__'])
273 self.u.upgrade()
274 expanded = self.u.expand(upg)
275 self.assertEqual(1, len(expanded))
276 self.assertTrue(expanded[0]['new'] is None)
277
278
279 def test_template_in_converter_description(caplog, capsys):
280 """Before and after values are available to the conversion descriptor"""
281 cfg = {'old': 42}
282 u = upgrader(cfg, 'Whateva')
283 u.deprecate(
284 '2.0.0', ['old'], ['new'],
285 cvtr=converter(lambda x: x + 20, '{old} -> {new}'),
286 silent=False,
287 )
288 u.upgrade()
289 assert cfg == {'new': 62}
290 assert '42 -> 62' in caplog.records[1].message