Adds a FakeNamespace object to teuthology.config that mimics

what an argparse Namespace looks like when teuthology.misc.read_config
is used on it.  This allows us to refactor our commands to use docopt
but still all all the tasks to be passed one big namespace.

Signed-off-by: Andrew Schoen <aschoen@redhat.com>
This commit is contained in:
Andrew Schoen 2014-11-21 11:58:58 -06:00
parent fa35faa6ab
commit 0d5f1249b3
2 changed files with 91 additions and 6 deletions

View File

@ -127,9 +127,49 @@ class JobConfig(YamlConfig):
pass
system_config_path = '/etc/teuthology.yaml'
if not os.path.exists(TeuthologyConfig.yaml_path) and \
os.path.exists(system_config_path):
config = TeuthologyConfig(yaml_path=system_config_path)
else:
config = TeuthologyConfig()
class FakeNamespace(YamlConfig):
""" This class is meant to behave like a argparse Namespace with an attached
teuthology config at the teuthology_config property. It mimics the old
way of doing things with argparse and teuthology.misc.read_config.
We'll use this as a stop-gap as we refactor commands but allow the tasks
to still be passed a single namespace object for the time being.
"""
def __init__(self, config_dict=None, yaml_path=None):
if not yaml_path:
yaml_path = _get_config_path()
if not config_dict:
config_dict = dict()
# teuthology.misc.read_config attaches the teuthology config
# to a teuthology_config attribute of the argparse Namespace
config_dict["teuthology_config"] = TeuthologyConfig(yaml_path)
self._conf = self._clean_config(config_dict)
def _clean_config(self, config_dict):
""" Makes sure that the keys of config_dict are able to be used. For example
the "--" prefix of a docopt dict isn't valid and won't populate correctly.
"""
result = dict()
for key, value in config_dict.iteritems():
new_key = key
if new_key.startswith("--"):
new_key = new_key[2:]
elif new_key.startswith("<") and new_key.endswith(">"):
new_key = new_key[1:-1]
if "-" in new_key:
new_key = new_key.replace("-", "_")
result[new_key] = value
return result
def _get_config_path():
system_config_path = '/etc/teuthology.yaml'
if not os.path.exists(TeuthologyConfig.yaml_path) and \
os.path.exists(system_config_path):
return system_config_path
return TeuthologyConfig.yaml_path
config = TeuthologyConfig(yaml_path=_get_config_path())

View File

@ -86,3 +86,48 @@ class TestTeuthologyConfig(TestYamlConfig):
class TestJobConfig(TestYamlConfig):
def setup(self):
self.test_class = config.JobConfig
class TestFakeNamespace(TestYamlConfig):
def setup(self):
self.test_class = config.FakeNamespace
def test_docopt_dict(self):
""" Tests if a dict in the format that docopt returns can
be parsed correctly.
"""
d = {
"--verbose": True,
"--an-option": "some_option",
"<an_arg>": "the_arg",
"something": "some_thing",
}
conf_obj = self.test_class(d)
assert conf_obj.verbose
assert conf_obj.an_option == "some_option"
assert conf_obj.an_arg == "the_arg"
assert conf_obj.something == "some_thing"
def test_config(self):
""" Tests that a teuthology_config property is automatically added
and that defaults are properly used. However, we won't check all
the defaults.
"""
conf_obj = self.test_class(dict())
assert conf_obj.teuthology_config
assert conf_obj.teuthology_config.archive_base
assert not conf_obj.teuthology_config.automated_scheduling
assert conf_obj.teuthology_config.ceph_git_base_url == 'https://github.com/ceph/'
def test_update(self):
"""
This is slightly different thank TestYamlConfig.update() in that it
only tests what was updated - since to_dict() yields all values,
including defaults.
"""
conf_obj = self.test_class(dict())
conf_obj.foo = 'foo'
conf_obj.bar = 'bar'
conf_obj.update(dict(bar='baz'))
assert conf_obj.foo == 'foo'
assert conf_obj.bar == 'baz'