Debian Codegraph @

tests/u/plugins/test_pre_configure.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 """"Test the pre_configure entry point."""
17
18 from random import random
19
20 import pytest
21
22 from cylc.flow.exceptions import PluginError
23 from cylc.flow.parsec.exceptions import ParsecError
24 from cylc.flow.parsec.fileparse import process_plugins
25
26
27 class EntryPointWrapper:
28 """Wraps a method to make it look like an entry point."""
29
30 def __init__(self, fcn):
31 self.name = fcn.__name__
32 self.fcn = fcn
33
34 def load(self):
35 return self.fcn
36
37
38 @EntryPointWrapper
39 def pre_configure_basic(*_, **__):
40 """Simple plugin that returns one env var and one template var."""
41 return {
42 'env': {
43 'ANSWER': '42'
44 },
45 'template_variables': {
46 'QUESTION': 'What do you get if you multiply 7 by 6?'
47 }
48 }
49
50
51 @EntryPointWrapper
52 def pre_configure_templating_detected(*_, **__):
53 """Plugin that detects a random templating engine."""
54 return {
55 'templating_detected': str(random())
56 }
57
58
59 @EntryPointWrapper
60 def pre_configure_error(*_, **__):
61 """Plugin that raises an exception."""
62 raise Exception('foo')
63
64
65 def test_pre_configure(monkeypatch):
66 """It should call the plugin."""
67 monkeypatch.setattr(
68 'cylc.flow.plugins.iter_entry_points',
69 lambda namespace: (
70 [pre_configure_basic] if namespace == 'cylc.pre_configure' else []
71 )
72 )
73 extra_vars = process_plugins('/', None)
74 assert extra_vars == {
75 'env': {
76 'ANSWER': '42'
77 },
78 'template_variables': {
79 'QUESTION': 'What do you get if you multiply 7 by 6?'
80 },
81 'templating_detected': None
82 }
83
84
85 def test_pre_configure_duplicate(monkeypatch):
86 """It should error when plugins clash."""
87 monkeypatch.setattr(
88 'cylc.flow.plugins.iter_entry_points',
89 lambda namespace: (
90 [
91 pre_configure_basic,
92 pre_configure_basic,
93 ] if namespace == 'cylc.pre_configure' else []
94 )
95 )
96 with pytest.raises(ParsecError):
97 process_plugins('/', None)
98
99
100 def test_pre_configure_templating_detected(monkeypatch):
101 """It should error when plugins clash (for templating)."""
102 monkeypatch.setattr(
103 'cylc.flow.plugins.iter_entry_points',
104 lambda namespace: (
105 [
106 pre_configure_templating_detected,
107 pre_configure_templating_detected,
108 ] if namespace == 'cylc.pre_configure' else []
109 )
110 )
111 with pytest.raises(ParsecError):
112 process_plugins('/', None)
113
114
115 def test_pre_configure_exception(monkeypatch):
116 """It should wrap plugin errors."""
117 monkeypatch.setattr(
118 'cylc.flow.plugins.iter_entry_points',
119 lambda namespace: (
120 [
121 pre_configure_error,
122 ] if namespace == 'cylc.pre_configure' else []
123 )
124 )
125 with pytest.raises(PluginError) as exc_ctx:
126 process_plugins('/', None)
127 # the context of the original error should be preserved in the raised
128 # exception
129 assert exc_ctx.value.entry_point == 'cylc.pre_configure'
130 assert exc_ctx.value.plugin_name == 'pre_configure_error'
131 assert str(exc_ctx.value.exc) == 'foo'