"""
return str(self)
- def get(self, key, default=None):
- """
- A function that acts like dict.get(). Will first check self._conf, then
- _defaults for the given key.
-
- :param key: The key to fetch
- :returns: The value for the given key, or the default kwarg if not found
- """
- result = self.__getattr__(key)
- if not result:
- return default
- return result
-
def __str__(self):
return yaml.safe_dump(self._conf, default_flow_style=False).strip()
return result
+ def __getattr__(self, name):
+ """
+ We need to modify this for FakeNamespace so that getattr() will
+ work correctly on a FakeNamespace instance.
+ """
+ result = self._conf.get(name, self._defaults.get(name))
+ if result is None:
+ raise AttributeError
+ return self._conf.get(name, self._defaults.get(name))
+
def _get_config_path():
system_config_path = '/etc/teuthology.yaml'
+import pytest
+
from .. import config
del conf_obj.foo
assert conf_obj.foo is None
- def test_get(self):
- conf_obj = self.test_class()
- conf_obj.foo = "bar"
- assert conf_obj.get("foo") == "bar"
- assert conf_obj.get("not_there", "default") == "default"
-
def test_assignment(self):
conf_obj = self.test_class()
conf_obj["foo"] = "bar"
assert not conf_obj.teuthology_config.automated_scheduling
assert conf_obj.teuthology_config.ceph_git_base_url == 'https://github.com/ceph/'
assert conf_obj.teuthology_config["ceph_git_base_url"] == 'https://github.com/ceph/'
+
+ def test_getattr(self):
+ conf_obj = self.test_class.from_dict({"foo": "bar"})
+ result = getattr(conf_obj, "not_there", "default")
+ assert result == "default"
+ result = getattr(conf_obj, "foo")
+ assert result == "bar"
+
+ def test_delattr(self):
+ conf_obj = self.test_class()
+ conf_obj.foo = 'bar'
+ assert conf_obj.foo == 'bar'
+ del conf_obj.foo
+ with pytest.raises(AttributeError):
+ conf_obj.foo