]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
modified __getattr__ for FakeNamespace so that it raises AttributeError on a missing...
authorAndrew Schoen <aschoen@redhat.com>
Tue, 2 Dec 2014 21:27:09 +0000 (15:27 -0600)
committerAndrew Schoen <aschoen@redhat.com>
Sat, 6 Dec 2014 19:46:47 +0000 (13:46 -0600)
Signed-off-by: Andrew Schoen <aschoen@redhat.com>
teuthology/config.py
teuthology/test/test_config.py

index 3091b4b7d20fced6d138a9f487ab65d1d30483b7..9623cd385e0e24d797ef04d335ed8d1e099b1640 100644 (file)
@@ -80,19 +80,6 @@ class YamlConfig(collections.MutableMapping):
         """
         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()
 
@@ -198,6 +185,16 @@ class FakeNamespace(YamlConfig):
 
         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'
index 8bb8c149a03786beb469c8651dd7c93403ce6e02..a176179927325a0907920db80fccd86950861467 100644 (file)
@@ -1,3 +1,5 @@
+import pytest
+
 from .. import config
 
 
@@ -56,12 +58,6 @@ class TestYamlConfig(object):
         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"
@@ -134,3 +130,18 @@ class TestFakeNamespace(TestYamlConfig):
         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