|
1 | 1 | from tempfile import NamedTemporaryFile |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +import copy |
4 | 5 |
|
5 | 6 | from cobald.daemon.config.mapping import ConfigurationError |
6 | 7 | from cobald.daemon.core.config import load, COBalDLoader, yaml_constructor |
|
13 | 14 | COBalDLoader.add_constructor(tag="!MockPool", constructor=yaml_constructor(MockPool)) |
14 | 15 |
|
15 | 16 |
|
| 17 | +# Helpers for testing lazy/eager YAML evaluation |
| 18 | +# Since YAML defaults to lazy evaluation, the arguments available during evaluation |
| 19 | +# are not necessarily complete. |
| 20 | +class TagTracker: |
| 21 | + """Helper to track the arguments supplied to YAML !Tags""" |
| 22 | + |
| 23 | + def __init__(self, *args, **kwargs): |
| 24 | + # the state of arguments *during* YAML evaluation |
| 25 | + self.orig_args = copy.deepcopy(args) |
| 26 | + self.orig_kwargs = copy.deepcopy(kwargs) |
| 27 | + # the state of arguments *after* YAML evaluation |
| 28 | + self.final_args = args |
| 29 | + self.final_kwargs = kwargs |
| 30 | + |
| 31 | + |
| 32 | +COBalDLoader.add_constructor( |
| 33 | + tag="!TagTrackerEager", constructor=yaml_constructor(TagTracker, eager=True) |
| 34 | +) |
| 35 | +COBalDLoader.add_constructor( |
| 36 | + tag="!TagTrackerLazy", constructor=yaml_constructor(TagTracker, eager=False) |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +def get_config_section(config: dict, section: str): |
| 41 | + return next( |
| 42 | + content for plugin, content in config.items() if plugin.section == section |
| 43 | + ) |
| 44 | + |
| 45 | + |
16 | 46 | class TestYamlConfig: |
17 | 47 | def test_load(self): |
18 | 48 | """Load a valid YAML config""" |
@@ -95,10 +125,131 @@ def test_load_mixed_creation(self): |
95 | 125 | """ |
96 | 126 | ) |
97 | 127 | with load(config.name) as config: |
98 | | - pipeline = next( |
99 | | - content |
100 | | - for plugin, content in config.items() |
101 | | - if plugin.section == "pipeline" |
102 | | - ) |
| 128 | + pipeline = get_config_section(config, "pipeline") |
103 | 129 | assert isinstance(pipeline[0], LinearController) |
104 | 130 | assert isinstance(pipeline[0].target, MockPool) |
| 131 | + |
| 132 | + def test_load_tags_substructure(self): |
| 133 | + """Load !Tags with substructure""" |
| 134 | + with NamedTemporaryFile(suffix=".yaml") as config: |
| 135 | + with open(config.name, "w") as write_stream: |
| 136 | + write_stream.write( |
| 137 | + """ |
| 138 | + pipeline: |
| 139 | + - !MockPool |
| 140 | + __config_test: |
| 141 | + tagged: !TagTrackerEager |
| 142 | + host: 127.0.0.1 |
| 143 | + port: 1234 |
| 144 | + algorithm: HS256 |
| 145 | + users: |
| 146 | + - user_name: tardis |
| 147 | + scopes: |
| 148 | + - user:read |
| 149 | + """ |
| 150 | + ) |
| 151 | + with load(config.name) as config: |
| 152 | + tagged = get_config_section(config, "__config_test")["tagged"] |
| 153 | + assert isinstance(tagged, TagTracker) |
| 154 | + assert tagged.final_kwargs["host"] == "127.0.0.1" |
| 155 | + assert tagged.final_kwargs["port"] == 1234 |
| 156 | + assert tagged.final_kwargs["algorithm"] == "HS256" |
| 157 | + assert tagged.final_kwargs["users"][0]["user_name"] == "tardis" |
| 158 | + assert tagged.final_kwargs["users"][0]["scopes"] == ["user:read"] |
| 159 | + |
| 160 | + def test_load_tags_eager(self): |
| 161 | + """Load !Tags with substructure, immediately using them""" |
| 162 | + with NamedTemporaryFile(suffix=".yaml") as config: |
| 163 | + with open(config.name, "w") as write_stream: |
| 164 | + write_stream.write( |
| 165 | + """ |
| 166 | + pipeline: |
| 167 | + - !MockPool |
| 168 | + __config_test: |
| 169 | + tagged: !TagTrackerEager |
| 170 | + top: "top level value" |
| 171 | + nested: |
| 172 | + - leaf: "leaf level value" |
| 173 | + """ |
| 174 | + ) |
| 175 | + with load(config.name) as config: |
| 176 | + tagged = get_config_section(config, "__config_test")["tagged"] |
| 177 | + assert isinstance(tagged, TagTracker) |
| 178 | + # eager loading => all data should exist immediately |
| 179 | + assert tagged.orig_kwargs["top"] == "top level value" |
| 180 | + assert tagged.orig_kwargs["nested"] == [{"leaf": "leaf level value"}] |
| 181 | + assert tagged.orig_kwargs == tagged.final_kwargs |
| 182 | + |
| 183 | + def test_load_tags_lazy(self): |
| 184 | + """Load !Tags with substructure, lazily using them""" |
| 185 | + with NamedTemporaryFile(suffix=".yaml") as config: |
| 186 | + with open(config.name, "w") as write_stream: |
| 187 | + write_stream.write( |
| 188 | + """ |
| 189 | + pipeline: |
| 190 | + - !MockPool |
| 191 | + __config_test: |
| 192 | + tagged: !TagTrackerLazy |
| 193 | + top: "top level value" |
| 194 | + nested: |
| 195 | + - leaf: "leaf level value" |
| 196 | + """ |
| 197 | + ) |
| 198 | + with load(config.name) as config: |
| 199 | + tagged = get_config_section(config, "__config_test")["tagged"] |
| 200 | + assert isinstance(tagged, TagTracker) |
| 201 | + # eager loading => only some data should exist immediately... |
| 202 | + assert tagged.orig_kwargs["top"] == "top level value" |
| 203 | + assert tagged.orig_kwargs["nested"] == [] |
| 204 | + # ...but should be there in the end |
| 205 | + assert tagged.final_kwargs["nested"] == [{"leaf": "leaf level value"}] |
| 206 | + |
| 207 | + def test_load_tags_nested(self): |
| 208 | + """Load !Tags with nested !Tags""" |
| 209 | + with NamedTemporaryFile(suffix=".yaml") as config: |
| 210 | + with open(config.name, "w") as write_stream: |
| 211 | + write_stream.write( |
| 212 | + """ |
| 213 | + pipeline: |
| 214 | + - !MockPool |
| 215 | + __config_test: |
| 216 | + top_eager: !TagTrackerEager |
| 217 | + nested: |
| 218 | + - leaf: "leaf level value" |
| 219 | + - leaf_lazy: !TagTrackerLazy |
| 220 | + nested: |
| 221 | + - leaf: "leaf level value" |
| 222 | + """ |
| 223 | + ) |
| 224 | + with load(config.name) as config: |
| 225 | + top_eager = get_config_section(config, "__config_test")["top_eager"] |
| 226 | + # eager tags are evaluated eagerly |
| 227 | + assert top_eager.orig_kwargs["nested"][0] == { |
| 228 | + "leaf": "leaf level value" |
| 229 | + } |
| 230 | + leaf_lazy = top_eager.orig_kwargs["nested"][1]["leaf_lazy"] |
| 231 | + # eagerness overrides laziness |
| 232 | + assert leaf_lazy.orig_kwargs["nested"] == [{"leaf": "leaf level value"}] |
| 233 | + |
| 234 | + def test_load_tag_settings(self): |
| 235 | + """Load !Tags with decorator settings""" |
| 236 | + # __yaml_tag_test is provided by the cobald package |
| 237 | + with NamedTemporaryFile(suffix=".yaml") as config: |
| 238 | + with open(config.name, "w") as write_stream: |
| 239 | + write_stream.write( |
| 240 | + """ |
| 241 | + pipeline: |
| 242 | + - !MockPool |
| 243 | + __config_test: |
| 244 | + settings_tag: !__yaml_tag_test |
| 245 | + top: "top level value" |
| 246 | + nested: |
| 247 | + - leaf: "leaf level value" |
| 248 | + """ |
| 249 | + ) |
| 250 | + with load(config.name) as config: |
| 251 | + section = get_config_section(config, "__config_test") |
| 252 | + args, kwargs = section["settings_tag"] |
| 253 | + assert args == () |
| 254 | + assert kwargs["top"] == "top level value" |
| 255 | + assert kwargs["nested"] == [{"leaf": "leaf level value"}] |
0 commit comments