From 45a1a564c05b04330a7a58110508bc824bebd50d Mon Sep 17 00:00:00 2001 From: Sergey Vilgelm Date: Wed, 3 Jul 2013 11:28:05 +0400 Subject: [PATCH] Rename cinder.flags to cinder.common.config Replace FLAGS with cfg.CONF Rename modules fake_flags to conf_fixture, test_flags to test_conf, declare_flags to declare_conf, runtime_flags to runtime_conf Renamed cinder.flags, because exactly the same was done in the glance and nova Fixes: bug #1182037 Change-Id: I3424d0a401b3ef7a3254d3e913263554361a52ff --- bin/cinder-all | 12 ++- bin/cinder-api | 12 ++- bin/cinder-backup | 13 ++- bin/cinder-clear-rabbit-queues | 19 +++-- bin/cinder-manage | 32 +++---- bin/cinder-scheduler | 14 +++- bin/cinder-volume | 19 +++-- bin/cinder-volume-usage-audit | 12 ++- cinder/{flags.py => common/config.py} | 30 ++----- cinder/test.py | 42 +++++----- cinder/tests/conf_fixture.py | 48 +++++++++++ .../{declare_flags.py => declare_conf.py} | 2 +- .../{runtime_flags.py => runtime_conf.py} | 2 +- cinder/tests/test_conf.py | 84 +++++++++++++++++++ cinder/tests/test_flags.py | 83 ------------------ cinder/tests/test_volume.py | 4 +- cinder/tests/test_volume_types.py | 6 +- cinder/transfer/__init__.py | 10 ++- cinder/volume/__init__.py | 10 ++- doc/source/devref/cinder.rst | 36 ++------ doc/source/devref/unit_tests.rst | 4 +- 21 files changed, 285 insertions(+), 209 deletions(-) rename cinder/{flags.py => common/config.py} (92%) create mode 100644 cinder/tests/conf_fixture.py rename cinder/tests/{declare_flags.py => declare_conf.py} (92%) rename cinder/tests/{runtime_flags.py => runtime_conf.py} (98%) create mode 100644 cinder/tests/test_conf.py delete mode 100644 cinder/tests/test_flags.py diff --git a/bin/cinder-all b/bin/cinder-all index aab9a9da312..8e19ab5f489 100755 --- a/bin/cinder-all +++ b/bin/cinder-all @@ -27,12 +27,15 @@ continue attempting to launch the rest of the services. """ + import eventlet + eventlet.monkey_patch() import os import sys +from oslo.config import cfg possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, @@ -43,14 +46,19 @@ if os.path.exists(os.path.join(possible_topdir, "cinder", "__init__.py")): from cinder.openstack.common import gettextutils gettextutils.install('cinder') -from cinder import flags +from cinder.common import config # Need to register global_opts from cinder.openstack.common import log as logging from cinder import service from cinder import utils +from cinder import version + + +CONF = cfg.CONF if __name__ == '__main__': - flags.parse_args(sys.argv) + CONF(sys.argv[1:], project='cinder', + version=version.version_string()) logging.setup("cinder") LOG = logging.getLogger('cinder.all') diff --git a/bin/cinder-api b/bin/cinder-api index 22a9f741e69..7005d24cecf 100755 --- a/bin/cinder-api +++ b/bin/cinder-api @@ -24,11 +24,13 @@ # eventlet is updated/released to fix the root issue import eventlet + eventlet.monkey_patch() import os import sys +from oslo.config import cfg possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, @@ -39,13 +41,19 @@ if os.path.exists(os.path.join(possible_topdir, "cinder", "__init__.py")): from cinder.openstack.common import gettextutils gettextutils.install('cinder') -from cinder import flags +from cinder.common import config # Need to register global_opts from cinder.openstack.common import log as logging from cinder import service from cinder import utils +from cinder import version + + +CONF = cfg.CONF + if __name__ == '__main__': - flags.parse_args(sys.argv) + CONF(sys.argv[1:], project='cinder', + version=version.version_string()) logging.setup("cinder") utils.monkey_patch() server = service.WSGIService('osapi_volume') diff --git a/bin/cinder-backup b/bin/cinder-backup index 71f83006a29..5f2a47587cd 100755 --- a/bin/cinder-backup +++ b/bin/cinder-backup @@ -17,6 +17,7 @@ """Starter script for Cinder Volume Backup.""" + import os import sys @@ -24,6 +25,8 @@ import eventlet eventlet.monkey_patch() +from oslo.config import cfg + # If ../cinder/__init__.py exists, add ../ to Python search path, so that # it will override what happens to be installed in /usr/(local/)lib/python... possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), @@ -35,13 +38,19 @@ if os.path.exists(os.path.join(possible_topdir, 'cinder', '__init__.py')): from cinder.openstack.common import gettextutils gettextutils.install('cinder') -from cinder import flags +from cinder.common import config # Need to register global_opts from cinder.openstack.common import log as logging from cinder import service from cinder import utils +from cinder import version + + +CONF = cfg.CONF + if __name__ == '__main__': - flags.parse_args(sys.argv) + CONF(sys.argv[1:], project='cinder', + version=version.version_string()) logging.setup("cinder") utils.monkey_patch() launcher = service.ProcessLauncher() diff --git a/bin/cinder-clear-rabbit-queues b/bin/cinder-clear-rabbit-queues index 684a56578ae..52f6dfc94d0 100755 --- a/bin/cinder-clear-rabbit-queues +++ b/bin/cinder-clear-rabbit-queues @@ -28,6 +28,8 @@ import os import sys import time +from oslo.config import cfg + # If ../cinder/__init__.py exists, add ../ to Python search path, so that # it will override what happens to be installed in /usr/(local/)lib/python... POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), @@ -39,21 +41,21 @@ if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'cinder', '__init__.py')): from cinder.openstack.common import gettextutils gettextutils.install('cinder') -from oslo.config import cfg - +from cinder.common import config # Need to register global_opts from cinder import context from cinder import exception -from cinder import flags from cinder.openstack.common import log as logging from cinder.openstack.common import rpc +from cinder import version + delete_exchange_opt = \ cfg.BoolOpt('delete_exchange', default=False, help='delete cinder exchange too.') -FLAGS = flags.FLAGS -FLAGS.register_cli_opt(delete_exchange_opt) +CONF = cfg.CONF +CONF.register_cli_opt(delete_exchange_opt) def delete_exchange(exch): @@ -69,8 +71,9 @@ def delete_queues(queues): x.queue_delete(q) if __name__ == '__main__': - args = flags.parse_args(sys.argv) + args = CONF(sys.argv[1:], project='cinder', + version=version.version_string()) logging.setup("cinder") delete_queues(args[1:]) - if FLAGS.delete_exchange: - delete_exchange(FLAGS.control_exchange) + if CONF.delete_exchange: + delete_exchange(CONF.control_exchange) diff --git a/bin/cinder-manage b/bin/cinder-manage index 62f96dfdc11..6c8dd3e3953 100755 --- a/bin/cinder-manage +++ b/bin/cinder-manage @@ -54,10 +54,12 @@ CLI interface for cinder management. """ + import os import sys import uuid +from oslo.config import cfg from sqlalchemy import create_engine, MetaData, Table from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker @@ -74,20 +76,19 @@ if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'cinder', '__init__.py')): from cinder.openstack.common import gettextutils gettextutils.install('cinder') -from oslo.config import cfg - +from cinder.common import config # Need to register global_opts from cinder import context from cinder import db from cinder.db import migration from cinder import exception -from cinder import flags from cinder.openstack.common import log as logging from cinder.openstack.common import rpc from cinder.openstack.common import uuidutils from cinder import utils from cinder import version -FLAGS = flags.FLAGS + +CONF = cfg.CONF # Decorators for actions @@ -269,7 +270,7 @@ class VolumeCommands(object): return rpc.cast(ctxt, - rpc.queue_get_for(ctxt, FLAGS.volume_topic, host), + rpc.queue_get_for(ctxt, CONF.volume_topic, host), {"method": "delete_volume", "args": {"volume_id": volume['id']}}) @@ -288,7 +289,7 @@ class VolumeCommands(object): instance = db.instance_get(ctxt, volume['instance_id']) host = instance['host'] rpc.cast(ctxt, - rpc.queue_get_for(ctxt, FLAGS.compute_topic, host), + rpc.queue_get_for(ctxt, CONF.compute_topic, host), {"method": "attach_volume", "args": {"instance_id": instance['id'], "volume_id": volume['id'], @@ -450,7 +451,7 @@ class ConfigCommands(object): pass def list(self): - for key, value in FLAGS.iteritems(): + for key, value in CONF.iteritems(): if value is not None: print '%s = %s' % (key, value) @@ -461,10 +462,10 @@ class GetLogCommands(object): def errors(self): """Get all of the errors from the log files.""" error_found = 0 - if FLAGS.log_dir: - logs = [x for x in os.listdir(FLAGS.log_dir) if x.endswith('.log')] + if CONF.log_dir: + logs = [x for x in os.listdir(CONF.log_dir) if x.endswith('.log')] for file in logs: - log_file = os.path.join(FLAGS.log_dir, file) + log_file = os.path.join(CONF.log_dir, file) lines = [line.strip() for line in open(log_file, "r")] lines.reverse() print_name = 0 @@ -638,14 +639,14 @@ def fetch_func_args(func): fn_args = [] for args, kwargs in getattr(func, 'args', []): arg = get_arg_string(args[0]) - fn_args.append(getattr(FLAGS.category, arg)) + fn_args.append(getattr(CONF.category, arg)) return fn_args def main(): """Parse options and call the appropriate class/method.""" - FLAGS.register_cli_opt(category_opt) + CONF.register_cli_opt(category_opt) script_name = sys.argv[0] if len(sys.argv) < 2: print(_("\nOpenStack Cinder version: %(version)s\n") % @@ -657,10 +658,11 @@ def main(): sys.exit(2) try: - flags.parse_args(sys.argv) + CONF(sys.argv[1:], project='cinder', + version=version.version_string()) logging.setup("cinder") except cfg.ConfigFilesNotFoundError: - cfgfile = FLAGS.config_file[-1] if FLAGS.config_file else None + cfgfile = CONF.config_file[-1] if CONF.config_file else None if cfgfile and not os.access(cfgfile, os.R_OK): st = os.stat(cfgfile) print _("Could not read %s. Re-running with sudo") % cfgfile @@ -672,7 +674,7 @@ def main(): print _('Please re-run cinder-manage as root.') sys.exit(2) - fn = FLAGS.category.action_fn + fn = CONF.category.action_fn fn_args = fetch_func_args(fn) fn(*fn_args) diff --git a/bin/cinder-scheduler b/bin/cinder-scheduler index 28edd8bbf90..0a4c7a0d0c5 100755 --- a/bin/cinder-scheduler +++ b/bin/cinder-scheduler @@ -19,12 +19,16 @@ """Starter script for Cinder Scheduler.""" + import eventlet + eventlet.monkey_patch() import os import sys +from oslo.config import cfg + # If ../cinder/__init__.py exists, add ../ to Python search path, so that # it will override what happens to be installed in /usr/(local/)lib/python... possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), @@ -36,13 +40,19 @@ if os.path.exists(os.path.join(possible_topdir, 'cinder', '__init__.py')): from cinder.openstack.common import gettextutils gettextutils.install('cinder') -from cinder import flags +from cinder.common import config # Need to register global_opts from cinder.openstack.common import log as logging from cinder import service from cinder import utils +from cinder import version + + +CONF = cfg.CONF + if __name__ == '__main__': - flags.parse_args(sys.argv) + CONF(sys.argv[1:], project='cinder', + version=version.version_string()) logging.setup("cinder") utils.monkey_patch() server = service.Service.create(binary='cinder-scheduler') diff --git a/bin/cinder-volume b/bin/cinder-volume index 46d5da24b2f..e9f9d1ca3f5 100755 --- a/bin/cinder-volume +++ b/bin/cinder-volume @@ -20,11 +20,14 @@ """Starter script for Cinder Volume.""" import eventlet + eventlet.monkey_patch() import os import sys +from oslo.config import cfg + # If ../cinder/__init__.py exists, add ../ to Python search path, so that # it will override what happens to be installed in /usr/(local/)lib/python... possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), @@ -36,21 +39,25 @@ if os.path.exists(os.path.join(possible_topdir, 'cinder', '__init__.py')): from cinder.openstack.common import gettextutils gettextutils.install('cinder') -from cinder import flags +from cinder.common import config # Need to register global_opts from cinder.openstack.common import log as logging from cinder import service from cinder import utils +from cinder import version + + +CONF = cfg.CONF -FLAGS = flags.FLAGS if __name__ == '__main__': - flags.parse_args(sys.argv) + CONF(sys.argv[1:], project='cinder', + version=version.version_string()) logging.setup("cinder") utils.monkey_patch() launcher = service.ProcessLauncher() - if FLAGS.enabled_backends: - for backend in FLAGS.enabled_backends: - host = "%s@%s" % (FLAGS.host, backend) + if CONF.enabled_backends: + for backend in CONF.enabled_backends: + host = "%s@%s" % (CONF.host, backend) server = service.Service.create(host=host, service_name=backend) launcher.launch_server(server) diff --git a/bin/cinder-volume-usage-audit b/bin/cinder-volume-usage-audit index b3ba69e66cb..8df06c6c0dc 100755 --- a/bin/cinder-volume-usage-audit +++ b/bin/cinder-volume-usage-audit @@ -34,10 +34,13 @@ Jan 1 through Dec 31 of the previous year. """ + import os import sys import traceback +from oslo.config import cfg + # If ../cinder/__init__.py exists, add ../ to Python search path, so that # it will override what happens to be installed in /usr/(local/)lib/python... POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), @@ -49,20 +52,23 @@ if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'cinder', '__init__.py')): from cinder.openstack.common import gettextutils gettextutils.install('cinder') +from cinder.common import config # Need to register global_opts from cinder import context from cinder import db -from cinder import flags from cinder.openstack.common import log as logging from cinder.openstack.common import rpc from cinder import utils +from cinder import version import cinder.volume.utils -FLAGS = flags.FLAGS +CONF = cfg.CONF + if __name__ == '__main__': admin_context = context.get_admin_context() - flags.parse_args(sys.argv) + CONF(sys.argv[1:], project='cinder', + version=version.version_string()) logging.setup("cinder") begin, end = utils.last_completed_audit_period() print _("Starting volume usage audit") diff --git a/cinder/flags.py b/cinder/common/config.py similarity index 92% rename from cinder/flags.py rename to cinder/common/config.py index f2db0ed4ca5..0273f8661bc 100644 --- a/cinder/flags.py +++ b/cinder/common/config.py @@ -28,30 +28,11 @@ stepping stone. import os import socket -import sys from oslo.config import cfg -from cinder import version -FLAGS = cfg.CONF - - -def parse_args(argv, default_config_files=None): - FLAGS(argv[1:], project='cinder', - version=version.version_string(), - default_config_files=default_config_files) - - -class UnrecognizedFlag(Exception): - pass - - -def DECLARE(name, module_string, flag_values=FLAGS): - if module_string not in sys.modules: - __import__(module_string, globals(), locals()) - if name not in flag_values: - raise UnrecognizedFlag('%s not defined by %s' % (name, module_string)) +CONF = cfg.CONF def _get_my_ip(): @@ -83,7 +64,8 @@ core_opts = [ help='File name for the paste.deploy config for cinder-api'), cfg.StrOpt('pybasedir', default=os.path.abspath(os.path.join(os.path.dirname(__file__), - '../')), + '..', + '..')), help='Directory where the cinder python module is installed'), cfg.StrOpt('bindir', default='$pybasedir/bin', @@ -95,8 +77,8 @@ core_opts = [ debug_opts = [ ] -FLAGS.register_cli_opts(core_opts) -FLAGS.register_cli_opts(debug_opts) +CONF.register_cli_opts(core_opts) +CONF.register_cli_opts(debug_opts) global_opts = [ cfg.StrOpt('my_ip', @@ -230,4 +212,4 @@ global_opts = [ default='cinder.transfer.api.API', help='The full class name of the volume transfer API class'), ] -FLAGS.register_opts(global_opts) +CONF.register_opts(global_opts) diff --git a/cinder/test.py b/cinder/test.py index 59d3bacdb2d..5239d13d6db 100644 --- a/cinder/test.py +++ b/cinder/test.py @@ -18,11 +18,12 @@ """Base classes for our unit tests. -Allows overriding of flags for use of fakes, and some black magic for +Allows overriding of CONF for use of fakes, and some black magic for inline callbacks. """ + import functools import os import shutil @@ -34,13 +35,14 @@ from oslo.config import cfg import stubout import testtools +from cinder.common import config # Need to register global_opts from cinder.db import migration -from cinder import flags from cinder.openstack.common.db.sqlalchemy import session from cinder.openstack.common import log as logging from cinder.openstack.common import timeutils from cinder import service -from cinder.tests import fake_flags +from cinder.tests import conf_fixture + test_opts = [ cfg.StrOpt('sqlite_clean_db', @@ -50,8 +52,8 @@ test_opts = [ default=True, help='should we use everything for testing'), ] -FLAGS = flags.FLAGS -FLAGS.register_opts(test_opts) +CONF = cfg.CONF +CONF.register_opts(test_opts) LOG = logging.getLogger(__name__) @@ -77,7 +79,7 @@ class Database(fixtures.Fixture): if db_migrate.db_version() > db_migrate.INIT_VERSION: return else: - testdb = os.path.join(FLAGS.state_path, sqlite_db) + testdb = os.path.join(CONF.state_path, sqlite_db) if os.path.exists(testdb): return db_migrate.db_sync() @@ -87,7 +89,7 @@ class Database(fixtures.Fixture): self._DB = "".join(line for line in conn.connection.iterdump()) self.engine.dispose() else: - cleandb = os.path.join(FLAGS.state_path, sqlite_clean_db) + cleandb = os.path.join(CONF.state_path, sqlite_clean_db) shutil.copyfile(testdb, cleandb) def setUp(self): @@ -99,8 +101,8 @@ class Database(fixtures.Fixture): self.addCleanup(self.engine.dispose) else: shutil.copyfile( - os.path.join(FLAGS.state_path, self.sqlite_clean_db), - os.path.join(FLAGS.state_path, self.sqlite_db)) + os.path.join(CONF.state_path, self.sqlite_clean_db), + os.path.join(CONF.state_path, self.sqlite_db)) class TestCase(testtools.TestCase): @@ -132,32 +134,32 @@ class TestCase(testtools.TestCase): self.log_fixture = self.useFixture(fixtures.FakeLogger()) - fake_flags.set_defaults(FLAGS) - flags.parse_args([], default_config_files=[]) + conf_fixture.set_defaults(CONF) + CONF([], default_config_files=[]) # NOTE(vish): We need a better method for creating fixtures for tests # now that we have some required db setup for the system # to work properly. self.start = timeutils.utcnow() - FLAGS.set_default('connection', 'sqlite://', 'database') - FLAGS.set_default('sqlite_synchronous', False) + CONF.set_default('connection', 'sqlite://', 'database') + CONF.set_default('sqlite_synchronous', False) self.log_fixture = self.useFixture(fixtures.FakeLogger()) global _DB_CACHE if not _DB_CACHE: _DB_CACHE = Database(session, migration, - sql_connection=FLAGS.database.connection, - sqlite_db=FLAGS.sqlite_db, - sqlite_clean_db=FLAGS.sqlite_clean_db) + sql_connection=CONF.database.connection, + sqlite_db=CONF.sqlite_db, + sqlite_clean_db=CONF.sqlite_clean_db) self.useFixture(_DB_CACHE) # emulate some of the mox stuff, we can't use the metaclass # because it screws with our generators self.mox = mox.Mox() self.stubs = stubout.StubOutForTesting() - self.addCleanup(FLAGS.reset) + self.addCleanup(CONF.reset) self.addCleanup(self.mox.UnsetStubs) self.addCleanup(self.stubs.UnsetAll) self.addCleanup(self.stubs.SmartUnsetAll) @@ -165,7 +167,7 @@ class TestCase(testtools.TestCase): self.injected = [] self._services = [] - FLAGS.set_override('fatal_exception_format_errors', True) + CONF.set_override('fatal_exception_format_errors', True) def tearDown(self): """Runs after each test method to tear down test environment.""" @@ -192,9 +194,9 @@ class TestCase(testtools.TestCase): super(TestCase, self).tearDown() def flags(self, **kw): - """Override flag variables for a test.""" + """Override CONF variables for a test.""" for k, v in kw.iteritems(): - FLAGS.set_override(k, v) + CONF.set_override(k, v) def start_service(self, name, host=None, **kwargs): host = host and host or uuid.uuid4().hex diff --git a/cinder/tests/conf_fixture.py b/cinder/tests/conf_fixture.py new file mode 100644 index 00000000000..54ce228bcdc --- /dev/null +++ b/cinder/tests/conf_fixture.py @@ -0,0 +1,48 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from oslo.config import cfg + + +CONF = cfg.CONF + +CONF.import_opt('iscsi_num_targets', 'cinder.volume.drivers.lvm') +CONF.import_opt('policy_file', 'cinder.policy') +CONF.import_opt('volume_driver', 'cinder.volume.manager') +CONF.import_opt('xiv_proxy', 'cinder.volume.drivers.xiv') +CONF.import_opt('backup_service', 'cinder.backup.manager') + +def_vol_type = 'fake_vol_type' + + +def set_defaults(conf): + conf.set_default('default_volume_type', def_vol_type) + conf.set_default('volume_driver', + 'cinder.tests.fake_driver.FakeISCSIDriver') + conf.set_default('iscsi_helper', 'fake') + conf.set_default('connection_type', 'fake') + conf.set_default('fake_rabbit', True) + conf.set_default('rpc_backend', 'cinder.openstack.common.rpc.impl_fake') + conf.set_default('iscsi_num_targets', 8) + conf.set_default('verbose', True) + conf.set_default('connection', 'sqlite://', group='database') + conf.set_default('sqlite_synchronous', False) + conf.set_default('policy_file', 'cinder/tests/policy.json') + conf.set_default('xiv_proxy', 'cinder.tests.test_xiv.XIVFakeProxyDriver') + conf.set_default('backup_service', 'cinder.tests.backup.fake_service') diff --git a/cinder/tests/declare_flags.py b/cinder/tests/declare_conf.py similarity index 92% rename from cinder/tests/declare_flags.py rename to cinder/tests/declare_conf.py index 0eacd85d966..b9dc1ab2e73 100644 --- a/cinder/tests/declare_flags.py +++ b/cinder/tests/declare_conf.py @@ -21,4 +21,4 @@ from oslo.config import cfg CONF = cfg.CONF -CONF.register_opt(cfg.IntOpt('answer', default=42, help='test flag')) +CONF.register_opt(cfg.IntOpt('answer', default=42, help='test conf')) diff --git a/cinder/tests/runtime_flags.py b/cinder/tests/runtime_conf.py similarity index 98% rename from cinder/tests/runtime_flags.py rename to cinder/tests/runtime_conf.py index ca90e269490..7a9cfb6aaa0 100644 --- a/cinder/tests/runtime_flags.py +++ b/cinder/tests/runtime_conf.py @@ -21,4 +21,4 @@ from oslo.config import cfg CONF = cfg.CONF -CONF.register_opt(cfg.IntOpt('runtime_answer', default=54, help='test flag')) +CONF.register_opt(cfg.IntOpt('runtime_answer', default=54, help='test conf')) diff --git a/cinder/tests/test_conf.py b/cinder/tests/test_conf.py new file mode 100644 index 00000000000..766d1024030 --- /dev/null +++ b/cinder/tests/test_conf.py @@ -0,0 +1,84 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# Copyright 2011 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from oslo.config import cfg + +from cinder import test + + +CONF = cfg.CONF +CONF.register_opt(cfg.StrOpt('conf_unittest', + default='foo', + help='for testing purposes only')) + + +class ConfigTestCase(test.TestCase): + + def setUp(self): + super(ConfigTestCase, self).setUp() + + def test_declare(self): + self.assert_('answer' not in CONF) + CONF.import_opt('answer', 'cinder.tests.declare_conf') + self.assert_('answer' in CONF) + self.assertEqual(CONF.answer, 42) + + # Make sure we don't overwrite anything + CONF.set_override('answer', 256) + self.assertEqual(CONF.answer, 256) + CONF.import_opt('answer', 'cinder.tests.declare_conf') + self.assertEqual(CONF.answer, 256) + + def test_runtime_and_unknown_conf(self): + self.assert_('runtime_answer' not in CONF) + import cinder.tests.runtime_conf + self.assert_('runtime_answer' in CONF) + self.assertEqual(CONF.runtime_answer, 54) + + def test_long_vs_short_conf(self): + CONF.clear() + CONF.register_cli_opt(cfg.StrOpt('duplicate_answer_long', + default='val', + help='desc')) + CONF.register_cli_opt(cfg.IntOpt('duplicate_answer', + default=50, + help='desc')) + + argv = ['--duplicate_answer=60'] + CONF(argv, default_config_files=[]) + self.assertEqual(CONF.duplicate_answer, 60) + self.assertEqual(CONF.duplicate_answer_long, 'val') + + def test_conf_leak_left(self): + self.assertEqual(CONF.conf_unittest, 'foo') + self.flags(conf_unittest='bar') + self.assertEqual(CONF.conf_unittest, 'bar') + + def test_conf_leak_right(self): + self.assertEqual(CONF.conf_unittest, 'foo') + self.flags(conf_unittest='bar') + self.assertEqual(CONF.conf_unittest, 'bar') + + def test_conf_overrides(self): + self.assertEqual(CONF.conf_unittest, 'foo') + self.flags(conf_unittest='bar') + self.assertEqual(CONF.conf_unittest, 'bar') + CONF.reset() + self.assertEqual(CONF.conf_unittest, 'foo') diff --git a/cinder/tests/test_flags.py b/cinder/tests/test_flags.py deleted file mode 100644 index 16190484897..00000000000 --- a/cinder/tests/test_flags.py +++ /dev/null @@ -1,83 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# All Rights Reserved. -# Copyright 2011 Red Hat, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from oslo.config import cfg - -from cinder import flags -from cinder import test - -FLAGS = flags.FLAGS -FLAGS.register_opt(cfg.StrOpt('flags_unittest', - default='foo', - help='for testing purposes only')) - - -class FlagsTestCase(test.TestCase): - - def setUp(self): - super(FlagsTestCase, self).setUp() - - def test_declare(self): - self.assert_('answer' not in FLAGS) - flags.DECLARE('answer', 'cinder.tests.declare_flags') - self.assert_('answer' in FLAGS) - self.assertEqual(FLAGS.answer, 42) - - # Make sure we don't overwrite anything - FLAGS.set_override('answer', 256) - self.assertEqual(FLAGS.answer, 256) - flags.DECLARE('answer', 'cinder.tests.declare_flags') - self.assertEqual(FLAGS.answer, 256) - - def test_runtime_and_unknown_flags(self): - self.assert_('runtime_answer' not in FLAGS) - import cinder.tests.runtime_flags - self.assert_('runtime_answer' in FLAGS) - self.assertEqual(FLAGS.runtime_answer, 54) - - def test_long_vs_short_flags(self): - FLAGS.clear() - FLAGS.register_cli_opt(cfg.StrOpt('duplicate_answer_long', - default='val', - help='desc')) - FLAGS.register_cli_opt(cfg.IntOpt('duplicate_answer', - default=50, - help='desc')) - - argv = ['flags_test', '--duplicate_answer=60'] - flags.parse_args(argv, default_config_files=[]) - self.assertEqual(FLAGS.duplicate_answer, 60) - self.assertEqual(FLAGS.duplicate_answer_long, 'val') - - def test_flag_leak_left(self): - self.assertEqual(FLAGS.flags_unittest, 'foo') - self.flags(flags_unittest='bar') - self.assertEqual(FLAGS.flags_unittest, 'bar') - - def test_flag_leak_right(self): - self.assertEqual(FLAGS.flags_unittest, 'foo') - self.flags(flags_unittest='bar') - self.assertEqual(FLAGS.flags_unittest, 'bar') - - def test_flag_overrides(self): - self.assertEqual(FLAGS.flags_unittest, 'foo') - self.flags(flags_unittest='bar') - self.assertEqual(FLAGS.flags_unittest, 'bar') - FLAGS.reset() - self.assertEqual(FLAGS.flags_unittest, 'foo') diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index 6125a9086a0..1348a53562d 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -41,7 +41,7 @@ from cinder.openstack.common import rpc import cinder.policy from cinder import quota from cinder import test -from cinder.tests import fake_flags +from cinder.tests import conf_fixture from cinder.tests.image import fake as fake_image from cinder.volume import configuration as conf from cinder.volume import driver @@ -266,7 +266,7 @@ class VolumeTestCase(test.TestCase): self.assertEquals(volume['volume_type_id'], None) # Create default volume type - vol_type = fake_flags.def_vol_type + vol_type = conf_fixture.def_vol_type db.volume_type_create(context.get_admin_context(), dict(name=vol_type, extra_specs={})) diff --git a/cinder/tests/test_volume_types.py b/cinder/tests/test_volume_types.py index 37210cb6083..c4f99d0d963 100644 --- a/cinder/tests/test_volume_types.py +++ b/cinder/tests/test_volume_types.py @@ -26,7 +26,7 @@ from cinder.db.sqlalchemy import models from cinder import exception from cinder.openstack.common import log as logging from cinder import test -from cinder.tests import fake_flags +from cinder.tests import conf_fixture from cinder.volume import volume_types @@ -84,11 +84,11 @@ class VolumeTypeTestCase(test.TestCase): def test_get_default_volume_type(self): """Ensures default volume type can be retrieved.""" type_ref = volume_types.create(self.ctxt, - fake_flags.def_vol_type, + conf_fixture.def_vol_type, {}) default_vol_type = volume_types.get_default_volume_type() self.assertEqual(default_vol_type.get('name'), - fake_flags.def_vol_type) + conf_fixture.def_vol_type) def test_default_volume_type_missing_in_db(self): """Ensures proper exception raised if default volume type diff --git a/cinder/transfer/__init__.py b/cinder/transfer/__init__.py index 6adbf430707..7a4f44547b2 100644 --- a/cinder/transfer/__init__.py +++ b/cinder/transfer/__init__.py @@ -16,8 +16,12 @@ # Importing full names to not pollute the namespace and cause possible # collisions with use of 'from cinder.transfer import ' elsewhere. -import cinder.flags + +from oslo.config import cfg + import cinder.openstack.common.importutils -API = cinder.openstack.common.importutils.import_class( - cinder.flags.FLAGS.transfer_api_class) + +CONF = cfg.CONF + +API = cinder.openstack.common.importutils.import_class(CONF.transfer_api_class) diff --git a/cinder/volume/__init__.py b/cinder/volume/__init__.py index b7a93efa8c1..eaeba432a69 100644 --- a/cinder/volume/__init__.py +++ b/cinder/volume/__init__.py @@ -18,7 +18,13 @@ # Importing full names to not pollute the namespace and cause possible # collisions with use of 'from cinder.volume import ' elsewhere. -import cinder.flags as flags + + +from oslo.config import cfg + import cinder.openstack.common.importutils as import_utils -API = import_utils.import_class(flags.FLAGS.volume_api_class) + +CONF = cfg.CONF + +API = import_utils.import_class(CONF.volume_api_class) diff --git a/doc/source/devref/cinder.rst b/doc/source/devref/cinder.rst index b629a83ea22..a8b338666b2 100644 --- a/doc/source/devref/cinder.rst +++ b/doc/source/devref/cinder.rst @@ -52,10 +52,10 @@ The :mod:`cinder.exception` Module :show-inheritance: -The :mod:`cinder.flags` Module +The :mod:`cinder.common.config` Module ------------------------------ -.. automodule:: cinder.flags +.. automodule:: cinder.common.config :noindex: :members: :undoc-members: @@ -135,30 +135,20 @@ The :mod:`cinder.wsgi` Module Tests ----- -The :mod:`declare_flags` Module +The :mod:`declare_conf` Module ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: cinder.tests.declare_flags +.. automodule:: cinder.tests.declare_conf :noindex: :members: :undoc-members: :show-inheritance: -The :mod:`fake_flags` Module +The :mod:`conf_fixture` Module ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: cinder.tests.fake_flags - :noindex: - :members: - :undoc-members: - :show-inheritance: - - -The :mod:`flags_unittest` Module -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. automodule:: cinder.tests.flags_unittest +.. automodule:: cinder.tests.conf_fixture :noindex: :members: :undoc-members: @@ -175,16 +165,6 @@ The :mod:`process_unittest` Module :show-inheritance: -The :mod:`real_flags` Module -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. automodule:: cinder.tests.real_flags - :noindex: - :members: - :undoc-members: - :show-inheritance: - - The :mod:`rpc_unittest` Module ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -195,10 +175,10 @@ The :mod:`rpc_unittest` Module :show-inheritance: -The :mod:`runtime_flags` Module +The :mod:`runtime_conf` Module ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: cinder.tests.runtime_flags +.. automodule:: cinder.tests.runtime_conf :noindex: :members: :undoc-members: diff --git a/doc/source/devref/unit_tests.rst b/doc/source/devref/unit_tests.rst index b040122a0ad..8f41c65596b 100644 --- a/doc/source/devref/unit_tests.rst +++ b/doc/source/devref/unit_tests.rst @@ -139,9 +139,9 @@ Gotchas If you are running the unit tests from a shared folder, you may see tests start to fail or stop completely as a result of Python lockfile issues [#f4]_. You can get around this by manually setting or updating the following line in -``cinder/tests/fake_flags.py``:: +``cinder/tests/conf_fixture.py``:: - FLAGS['lock_path'].SetDefault('/tmp') + CONF['lock_path'].SetDefault('/tmp') Note that you may use any location (not just ``/tmp``!) as long as it is not a shared folder.