Skip to content

Commit

Permalink
fix: mixed construction methods (#92)
Browse files Browse the repository at this point in the history
* fix: no notice on config errors

* fixed a typo in config unittests

* double quotes

* fixed incomplete partial application in pipeline

* test for mixed constructor load

* improved test for invalid configuration parameters
  • Loading branch information
thoto authored Sep 1, 2021
1 parent 6ded652 commit a1a3010
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
23 changes: 23 additions & 0 deletions cobald_tests/daemon/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from cobald.daemon.config.mapping import ConfigurationError
from cobald.daemon.core.config import load, COBalDLoader, yaml_constructor
from cobald.controller.linear import LinearController

from ...mock.pool import MockPool

Expand Down Expand Up @@ -79,3 +80,25 @@ def test_load_missing(self):
with pytest.raises(ConfigurationError):
with load(config.name):
assert False

def test_load_mixed_creation(self):
"""Load a YAML config with mixed pipeline step creation methods"""
with NamedTemporaryFile(suffix=".yaml") as config:
with open(config.name, "w") as write_stream:
write_stream.write(
"""
pipeline:
- __type__: cobald.controller.linear.LinearController
low_utilisation: 0.9
high_allocation: 0.9
- !MockPool
"""
)
with load(config.name) as config:
pipeline = next(
content
for plugin, content in config.items()
if plugin.section == "pipeline"
)
assert isinstance(pipeline[0], LinearController)
assert isinstance(pipeline[0].target, MockPool)
13 changes: 9 additions & 4 deletions src/cobald/daemon/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from ..config.python import load_configuration as load_python_configuration
from ..config.mapping import Translator, SectionPlugin
from ...interfaces._partial import Partial


class COBalDLoader(SafeLoader):
Expand Down Expand Up @@ -77,24 +78,25 @@ def load(config_path: str):
:param config_path: path to a configuration file
"""
# we bind the config to _ to keep it alive
# we bind the config to c to keep it alive
if os.path.splitext(config_path)[1] in (".yaml", ".yml"):
add_constructor_plugins(
"cobald.config.yaml_constructors", COBalDLoader # type: ignore
)
config_plugins = load_section_plugins("cobald.config.sections")
_ = load_yaml_configuration(
c = load_yaml_configuration(
config_path,
loader=COBalDLoader, # type: ignore
plugins=config_plugins,
)
elif os.path.splitext(config_path)[1] == ".py":
_ = load_python_configuration(config_path)
c = load_python_configuration(config_path)
else:
raise ValueError(
"Unknown configuration extension: %r" % os.path.splitext(config_path)[1]
)
yield
# yielded value used in tests, runtime does not use configuration result
yield c


@plugin_constraints(required=True)
Expand Down Expand Up @@ -150,5 +152,8 @@ def translate_hierarchy(self, structure, *, where="", **construct_kwargs):
prev_item = self.translate_hierarchy(
item, where="%s[%s]" % (where, index)
)
if isinstance(prev_item, Partial): # got form __type__
prev_item = prev_item.__construct__()
assert not isinstance(prev_item, Partial)
items.append(prev_item)
return list(reversed(items))

0 comments on commit a1a3010

Please sign in to comment.