From 92a5414f2558dc8b768c1a2d350fc5bb9ebc029a Mon Sep 17 00:00:00 2001 From: Clay Gerrard Date: Thu, 24 Feb 2011 12:28:17 -0600 Subject: [PATCH] went ahead and spoofed a [func_test] section --- swift/common/utils.py | 11 +++-- test/__init__.py | 12 +++--- test/functionalnosetests/swift_testing.py | 12 +++--- test/unit/common/test_utils.py | 51 +++++++++++++---------- 4 files changed, 48 insertions(+), 38 deletions(-) diff --git a/swift/common/utils.py b/swift/common/utils.py index 744f65c997..d6a000fe90 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -737,7 +737,7 @@ def readconf(conf, section_name=None, log_name=None, defaults=None): """ Read config file and return config items as a dict - :param conf: path to config file + :param conf: path to config file, or a file-like object (hasattr readline) :param section_name: config section to read (will return all sections if not defined) :param log_name: name to be used with logging (will use section_name if @@ -748,9 +748,12 @@ def readconf(conf, section_name=None, log_name=None, defaults=None): if defaults is None: defaults = {} c = ConfigParser(defaults) - if not c.read(conf): - print _("Unable to read config file %s") % conf - sys.exit(1) + if hasattr(conf, 'read'): + c.readfp(conf) + else: + if not c.read(conf): + print _("Unable to read config file %s") % conf + sys.exit(1) if section_name: if c.has_section(section_name): conf = dict(c.items(section_name)) diff --git a/test/__init__.py b/test/__init__.py index 6c370b2e52..0aaba02f8a 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -5,6 +5,7 @@ import __builtin__ import sys import os from ConfigParser import MissingSectionHeaderError +from StringIO import StringIO from swift.common.utils import readconf @@ -15,13 +16,12 @@ def get_config(): '/etc/swift/func_test.conf') config = {} try: - config = readconf(config_file, 'func_test') + try: + config = readconf(config_file, 'func_test') + except MissingSectionHeaderError: + config_fp = StringIO('[func_test]\n' + open(config_file).read()) + config = readconf(config_fp, 'func_test') except SystemExit: print >>sys.stderr, 'UNABLE TO READ FUNCTIONAL TESTS CONFIG FILE' - except MissingSectionHeaderError: - # rather than mock the stream to spoof a section header, display an - # error to the user and let them fix it. - print >>sys.stderr, 'UNABLE TO READ FUNCTIONAL TESTS CONFIG FILE ' \ - 'DUE TO NO [func_test] SECTION' return config diff --git a/test/functionalnosetests/swift_testing.py b/test/functionalnosetests/swift_testing.py index 3c3c915f92..fbc60626c2 100644 --- a/test/functionalnosetests/swift_testing.py +++ b/test/functionalnosetests/swift_testing.py @@ -13,12 +13,12 @@ from swift.common.client import get_auth, http_connection conf = get_config() -if not conf: - # If no conf was read, fall back to old school env - swift_test_auth = os.environ.get('SWIFT_TEST_AUTH') - swift_test_user = [os.environ.get('SWIFT_TEST_USER'), None, None] - swift_test_key = [os.environ.get('SWIFT_TEST_KEY'), None, None] -else: +# If no conf was read, we will fall back to old school env vars +swift_test_auth = os.environ.get('SWIFT_TEST_AUTH') +swift_test_user = [os.environ.get('SWIFT_TEST_USER'), None, None] +swift_test_key = [os.environ.get('SWIFT_TEST_KEY'), None, None] + +if conf: swift_test_auth = 'http' if conf.get('auth_ssl', 'no').lower() in ('yes', 'true', 'on', '1'): swift_test_auth = 'https' diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index 9dd375590e..a5c8749f64 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -487,29 +487,36 @@ foo = bar [section2] log_name = yarr''' - f = open('/tmp/test', 'wb') - f.write(conf) - f.close() - result = utils.readconf('/tmp/test') - expected = {'log_name': None, - 'section1': {'foo': 'bar'}, - 'section2': {'log_name': 'yarr'}} - self.assertEquals(result, expected) - result = utils.readconf('/tmp/test', 'section1') - expected = {'log_name': 'section1', 'foo': 'bar'} - self.assertEquals(result, expected) - result = utils.readconf('/tmp/test', 'section2').get('log_name') - expected = 'yarr' - self.assertEquals(result, expected) - result = utils.readconf('/tmp/test', 'section1', - log_name='foo').get('log_name') - expected = 'foo' - self.assertEquals(result, expected) - result = utils.readconf('/tmp/test', 'section1', - defaults={'bar': 'baz'}) - expected = {'log_name': 'section1', 'foo': 'bar', 'bar': 'baz'} - self.assertEquals(result, expected) + # setup a real file + with open('/tmp/test', 'wb') as f: + f.write(conf) + make_filename = lambda: '/tmp/test' + # setup a file stream + make_fp = lambda: StringIO(conf) + for conf_object_maker in (make_filename, make_fp): + result = utils.readconf(conf_object_maker()) + expected = {'log_name': None, + 'section1': {'foo': 'bar'}, + 'section2': {'log_name': 'yarr'}} + self.assertEquals(result, expected) + result = utils.readconf(conf_object_maker(), 'section1') + expected = {'log_name': 'section1', 'foo': 'bar'} + self.assertEquals(result, expected) + result = utils.readconf(conf_object_maker(), + 'section2').get('log_name') + expected = 'yarr' + self.assertEquals(result, expected) + result = utils.readconf(conf_object_maker(), 'section1', + log_name='foo').get('log_name') + expected = 'foo' + self.assertEquals(result, expected) + result = utils.readconf(conf_object_maker(), 'section1', + defaults={'bar': 'baz'}) + expected = {'log_name': 'section1', 'foo': 'bar', 'bar': 'baz'} + self.assertEquals(result, expected) + self.assertRaises(SystemExit, utils.readconf, '/tmp/test', 'section3') os.unlink('/tmp/test') + self.assertRaises(SystemExit, utils.readconf, '/tmp/test') def test_drop_privileges(self): user = getuser()