went ahead and spoofed a [func_test] section

This commit is contained in:
Clay Gerrard 2011-02-24 12:28:17 -06:00
parent c1b3c8d799
commit 92a5414f25
4 changed files with 48 additions and 38 deletions

View File

@ -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 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 :param section_name: config section to read (will return all sections if
not defined) not defined)
:param log_name: name to be used with logging (will use section_name if :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: if defaults is None:
defaults = {} defaults = {}
c = ConfigParser(defaults) c = ConfigParser(defaults)
if not c.read(conf): if hasattr(conf, 'read'):
print _("Unable to read config file %s") % conf c.readfp(conf)
sys.exit(1) else:
if not c.read(conf):
print _("Unable to read config file %s") % conf
sys.exit(1)
if section_name: if section_name:
if c.has_section(section_name): if c.has_section(section_name):
conf = dict(c.items(section_name)) conf = dict(c.items(section_name))

View File

@ -5,6 +5,7 @@ import __builtin__
import sys import sys
import os import os
from ConfigParser import MissingSectionHeaderError from ConfigParser import MissingSectionHeaderError
from StringIO import StringIO
from swift.common.utils import readconf from swift.common.utils import readconf
@ -15,13 +16,12 @@ def get_config():
'/etc/swift/func_test.conf') '/etc/swift/func_test.conf')
config = {} config = {}
try: 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: except SystemExit:
print >>sys.stderr, 'UNABLE TO READ FUNCTIONAL TESTS CONFIG FILE' 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 return config

View File

@ -13,12 +13,12 @@ from swift.common.client import get_auth, http_connection
conf = get_config() conf = get_config()
if not conf: # If no conf was read, we will fall back to old school env vars
# If no conf was read, fall back to old school env swift_test_auth = os.environ.get('SWIFT_TEST_AUTH')
swift_test_auth = os.environ.get('SWIFT_TEST_AUTH') swift_test_user = [os.environ.get('SWIFT_TEST_USER'), None, None]
swift_test_user = [os.environ.get('SWIFT_TEST_USER'), None, None] swift_test_key = [os.environ.get('SWIFT_TEST_KEY'), None, None]
swift_test_key = [os.environ.get('SWIFT_TEST_KEY'), None, None]
else: if conf:
swift_test_auth = 'http' swift_test_auth = 'http'
if conf.get('auth_ssl', 'no').lower() in ('yes', 'true', 'on', '1'): if conf.get('auth_ssl', 'no').lower() in ('yes', 'true', 'on', '1'):
swift_test_auth = 'https' swift_test_auth = 'https'

View File

@ -487,29 +487,36 @@ foo = bar
[section2] [section2]
log_name = yarr''' log_name = yarr'''
f = open('/tmp/test', 'wb') # setup a real file
f.write(conf) with open('/tmp/test', 'wb') as f:
f.close() f.write(conf)
result = utils.readconf('/tmp/test') make_filename = lambda: '/tmp/test'
expected = {'log_name': None, # setup a file stream
'section1': {'foo': 'bar'}, make_fp = lambda: StringIO(conf)
'section2': {'log_name': 'yarr'}} for conf_object_maker in (make_filename, make_fp):
self.assertEquals(result, expected) result = utils.readconf(conf_object_maker())
result = utils.readconf('/tmp/test', 'section1') expected = {'log_name': None,
expected = {'log_name': 'section1', 'foo': 'bar'} 'section1': {'foo': 'bar'},
self.assertEquals(result, expected) 'section2': {'log_name': 'yarr'}}
result = utils.readconf('/tmp/test', 'section2').get('log_name') self.assertEquals(result, expected)
expected = 'yarr' result = utils.readconf(conf_object_maker(), 'section1')
self.assertEquals(result, expected) expected = {'log_name': 'section1', 'foo': 'bar'}
result = utils.readconf('/tmp/test', 'section1', self.assertEquals(result, expected)
log_name='foo').get('log_name') result = utils.readconf(conf_object_maker(),
expected = 'foo' 'section2').get('log_name')
self.assertEquals(result, expected) expected = 'yarr'
result = utils.readconf('/tmp/test', 'section1', self.assertEquals(result, expected)
defaults={'bar': 'baz'}) result = utils.readconf(conf_object_maker(), 'section1',
expected = {'log_name': 'section1', 'foo': 'bar', 'bar': 'baz'} log_name='foo').get('log_name')
self.assertEquals(result, expected) 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') os.unlink('/tmp/test')
self.assertRaises(SystemExit, utils.readconf, '/tmp/test')
def test_drop_privileges(self): def test_drop_privileges(self):
user = getuser() user = getuser()