From b419843ee7da6a52581ec7276bd47f4a7fce2d95 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Tue, 1 Dec 2015 14:29:14 -0700 Subject: [PATCH] Drop misc.read_config() Replace with config.set_config_attr(). This is mainly for backward-compatibility in tasks contained in ceph-qa-suite. Signed-off-by: Zack Cerza --- teuthology/config.py | 27 ++++++++++++------- teuthology/lock.py | 3 ++- teuthology/misc.py | 17 ------------ teuthology/openstack/__init__.py | 3 ++- .../openstack/test/openstack-integration.py | 4 ++- teuthology/openstack/test/test_openstack.py | 5 ++-- teuthology/test/test_config.py | 23 +++++++++++++--- teuthology/worker.py | 4 +-- 8 files changed, 48 insertions(+), 38 deletions(-) diff --git a/teuthology/config.py b/teuthology/config.py index a9365a609..8f3e2ea00 100644 --- a/teuthology/config.py +++ b/teuthology/config.py @@ -178,29 +178,23 @@ class TeuthologyConfig(YamlConfig): return (self.ceph_git_url or self.ceph_git_base_url + 'ceph.git') + class JobConfig(YamlConfig): pass class FakeNamespace(YamlConfig): """ - This class is meant to behave like a argparse Namespace. It mimics the old - way of doing things with argparse and teuthology.misc.read_config. + This class is meant to behave like a argparse Namespace 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() + def __init__(self, config_dict=None): if not config_dict: config_dict = dict() self._conf = self._clean_config(config_dict) - # avoiding circular imports - from .misc import read_config - # teuthology.misc.read_config attaches the teuthology config - # to a teuthology_config key. - read_config(self) + set_config_attr(self) def _clean_config(self, config_dict): """ @@ -234,6 +228,12 @@ class FakeNamespace(YamlConfig): return self._defaults[name] raise AttributeError(name) + def __setattr__(self, name, value): + if name == 'teuthology_config': + object.__setattr__(self, name, value) + else: + super(FakeNamespace, self).__setattr__(name, value) + def __repr__(self): return repr(self._conf) @@ -241,6 +241,13 @@ class FakeNamespace(YamlConfig): return str(self._conf) +def set_config_attr(obj): + """ + Set obj.teuthology_config, mimicking the old behavior of misc.read_config + """ + obj.teuthology_config = config + + def _get_config_path(): system_config_path = '/etc/teuthology.yaml' if not os.path.exists(TeuthologyConfig.yaml_path) and \ diff --git a/teuthology/lock.py b/teuthology/lock.py index 7eddd2cd3..081a6bd48 100644 --- a/teuthology/lock.py +++ b/teuthology/lock.py @@ -14,6 +14,7 @@ import teuthology from . import misc from . import provision from .config import config +from .config import set_config_attr from .contextutil import safe_while from .lockstatus import get_status @@ -184,7 +185,7 @@ def main(ctx): if ctx.verbose: teuthology.log.setLevel(logging.DEBUG) - misc.read_config(ctx) + set_config_attr(ctx) ret = 0 user = ctx.owner diff --git a/teuthology/misc.py b/teuthology/misc.py index f4043c768..9ea7a421c 100644 --- a/teuthology/misc.py +++ b/teuthology/misc.py @@ -1000,23 +1000,6 @@ def get_user(): return getpass.getuser() + '@' + socket.gethostname() -def read_config(ctx): - """ - read the default teuthology yaml configuration file. - """ - ctx.teuthology_config = {} - filename = os.path.join(os.environ['HOME'], '.teuthology.yaml') - - if not os.path.exists(filename): - log.debug("%s not found", filename) - return - - with file(filename) as f: - g = yaml.safe_load_all(f) - for new in g: - ctx.teuthology_config.update(new) - - def get_mon_names(ctx): """ :returns: a list of monitor names diff --git a/teuthology/openstack/__init__.py b/teuthology/openstack/__init__.py index 729862857..974bb6ae0 100644 --- a/teuthology/openstack/__init__.py +++ b/teuthology/openstack/__init__.py @@ -38,6 +38,7 @@ from subprocess import CalledProcessError from teuthology.contextutil import safe_while from teuthology.config import config as teuth_config +from teuthology.config import set_config_attr from teuthology.orchestra import connection from teuthology import misc @@ -421,7 +422,7 @@ class TeuthologyOpenStack(OpenStack): Entry point implementing the teuthology-openstack command. """ self.setup_logs() - misc.read_config(self.args) + set_config_attr(self.args) self.key_filename = self.args.key_filename self.verify_openstack() ip = self.setup() diff --git a/teuthology/openstack/test/openstack-integration.py b/teuthology/openstack/test/openstack-integration.py index 8fe041295..64ec9feb0 100644 --- a/teuthology/openstack/test/openstack-integration.py +++ b/teuthology/openstack/test/openstack-integration.py @@ -39,13 +39,15 @@ import scripts.schedule import scripts.lock import scripts.suite from teuthology.config import config as teuth_config +from teuthology.config import set_config_attr + class Integration(object): @classmethod def setup_class(self): teuthology.log.setLevel(logging.DEBUG) - teuthology.misc.read_config(argparse.Namespace()) + set_config_attr(argparse.Namespace()) self.teardown_class() @classmethod diff --git a/teuthology/openstack/test/test_openstack.py b/teuthology/openstack/test/test_openstack.py index 32fecb5b7..c46287be3 100644 --- a/teuthology/openstack/test/test_openstack.py +++ b/teuthology/openstack/test/test_openstack.py @@ -31,6 +31,7 @@ from mock import patch import teuthology from teuthology import misc +from teuthology.config import set_config_attr from teuthology.openstack import TeuthologyOpenStack, OpenStack, OpenStackInstance import scripts.openstack @@ -214,7 +215,7 @@ class TestTeuthologyOpenStack(object): pytest.skip('no OS_AUTH_URL environment variable') teuthology.log.setLevel(logging.DEBUG) - teuthology.misc.read_config(argparse.Namespace()) + set_config_attr(argparse.Namespace()) ip = TeuthologyOpenStack.create_floating_ip() if ip: @@ -223,7 +224,7 @@ class TestTeuthologyOpenStack(object): self.can_create_floating_ips = True else: self.can_create_floating_ips = False - + def setup(self): self.key_filename = tempfile.mktemp() self.key_name = 'teuthology-test' diff --git a/teuthology/test/test_config.py b/teuthology/test/test_config.py index fee44bf1f..30c324919 100644 --- a/teuthology/test/test_config.py +++ b/teuthology/test/test_config.py @@ -133,14 +133,12 @@ class TestFakeNamespace(TestYamlConfig): def test_config(self): """ Tests that a teuthology_config property is automatically added - by misc.read_config. + to the conf_obj """ conf_obj = self.test_class(dict(foo="bar")) assert conf_obj["foo"] == "bar" assert conf_obj.foo == "bar" - # teuthology_config needs to be a dict because all - # of the tasks expect it to be - assert isinstance(conf_obj.teuthology_config, dict) + assert conf_obj.teuthology_config.get("fake key") is None def test_getattr(self): conf_obj = self.test_class.from_dict({"foo": "bar"}) @@ -165,3 +163,20 @@ class TestFakeNamespace(TestYamlConfig): in_str = "foo: bar" conf_obj = self.test_class.from_str(in_str) assert conf_obj.to_str() == "{'foo': 'bar'}" + + def test_multiple_access(self): + """ + Test that config.config and FakeNamespace.teuthology_config reflect + each others' modifications + """ + in_str = "foo: bar" + conf_obj = self.test_class.from_str(in_str) + assert config.config.get('test_key_1') is None + assert conf_obj.teuthology_config.get('test_key_1') is None + config.config.test_key_1 = 'test value' + assert conf_obj.teuthology_config['test_key_1'] == 'test value' + + assert config.config.get('test_key_2') is None + assert conf_obj.teuthology_config.get('test_key_2') is None + conf_obj.teuthology_config['test_key_2'] = 'test value' + assert config.config['test_key_2'] == 'test value' diff --git a/teuthology/worker.py b/teuthology/worker.py index 48befffcd..9ea1c7746 100644 --- a/teuthology/worker.py +++ b/teuthology/worker.py @@ -13,9 +13,9 @@ from . import beanstalk from . import report from . import safepath from .config import config as teuth_config +from .config import set_config_attr from .exceptions import BranchNotFoundError from .kill import kill_job -from .misc import read_config from .repo_utils import fetch_qa_suite, fetch_teuthology log = logging.getLogger(__name__) @@ -73,7 +73,7 @@ def main(ctx): else: teuth_config.archive_base = ctx.archive_dir - read_config(ctx) + set_config_attr(ctx) connection = beanstalk.connect() beanstalk.watch_tube(connection, ctx.tube) -- 2.47.3