Merge "Fix misleading error msg if swift.conf unreadable"
This commit is contained in:
commit
29d46ca9f6
@ -233,25 +233,26 @@ def validate_hash_conf():
|
|||||||
|
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
# Use Latin1 to accept arbitrary bytes in the hash prefix/suffix
|
# Use Latin1 to accept arbitrary bytes in the hash prefix/suffix
|
||||||
confs_read = hash_conf.read(SWIFT_CONF_FILE, encoding='latin1')
|
with open(SWIFT_CONF_FILE, encoding='latin1') as swift_conf_file:
|
||||||
|
hash_conf.readfp(swift_conf_file)
|
||||||
else:
|
else:
|
||||||
confs_read = hash_conf.read(SWIFT_CONF_FILE)
|
with open(SWIFT_CONF_FILE) as swift_conf_file:
|
||||||
|
hash_conf.readfp(swift_conf_file)
|
||||||
|
|
||||||
if confs_read:
|
try:
|
||||||
try:
|
HASH_PATH_SUFFIX = hash_conf.get('swift-hash',
|
||||||
HASH_PATH_SUFFIX = hash_conf.get('swift-hash',
|
'swift_hash_path_suffix')
|
||||||
'swift_hash_path_suffix')
|
if six.PY3:
|
||||||
if six.PY3:
|
HASH_PATH_SUFFIX = HASH_PATH_SUFFIX.encode('latin1')
|
||||||
HASH_PATH_SUFFIX = HASH_PATH_SUFFIX.encode('latin1')
|
except (NoSectionError, NoOptionError):
|
||||||
except (NoSectionError, NoOptionError):
|
pass
|
||||||
pass
|
try:
|
||||||
try:
|
HASH_PATH_PREFIX = hash_conf.get('swift-hash',
|
||||||
HASH_PATH_PREFIX = hash_conf.get('swift-hash',
|
'swift_hash_path_prefix')
|
||||||
'swift_hash_path_prefix')
|
if six.PY3:
|
||||||
if six.PY3:
|
HASH_PATH_PREFIX = HASH_PATH_PREFIX.encode('latin1')
|
||||||
HASH_PATH_PREFIX = HASH_PATH_PREFIX.encode('latin1')
|
except (NoSectionError, NoOptionError):
|
||||||
except (NoSectionError, NoOptionError):
|
pass
|
||||||
pass
|
|
||||||
|
|
||||||
if not HASH_PATH_SUFFIX and not HASH_PATH_PREFIX:
|
if not HASH_PATH_SUFFIX and not HASH_PATH_PREFIX:
|
||||||
raise InvalidHashPathConfigError()
|
raise InvalidHashPathConfigError()
|
||||||
@ -259,7 +260,7 @@ def validate_hash_conf():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
validate_hash_conf()
|
validate_hash_conf()
|
||||||
except InvalidHashPathConfigError:
|
except (InvalidHashPathConfigError, IOError):
|
||||||
# could get monkey patched or lazy loaded
|
# could get monkey patched or lazy loaded
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -248,7 +248,7 @@ class TestRing(TestRingBase):
|
|||||||
with mock.patch.object(utils, 'HASH_PATH_SUFFIX', b''), \
|
with mock.patch.object(utils, 'HASH_PATH_SUFFIX', b''), \
|
||||||
mock.patch.object(utils, 'HASH_PATH_PREFIX', b''), \
|
mock.patch.object(utils, 'HASH_PATH_PREFIX', b''), \
|
||||||
mock.patch.object(utils, 'SWIFT_CONF_FILE', ''):
|
mock.patch.object(utils, 'SWIFT_CONF_FILE', ''):
|
||||||
self.assertRaises(SystemExit, ring.Ring, self.testdir, 'whatever')
|
self.assertRaises(IOError, ring.Ring, self.testdir, 'whatever')
|
||||||
|
|
||||||
def test_replica_count(self):
|
def test_replica_count(self):
|
||||||
self.assertEqual(self.ring.replica_count, 3)
|
self.assertEqual(self.ring.replica_count, 3)
|
||||||
|
@ -2085,11 +2085,21 @@ class TestUtils(unittest.TestCase):
|
|||||||
['swift-hash-xxx'],
|
['swift-hash-xxx'],
|
||||||
['swift_hash_path_suffix', 'swift_hash_path_prefix'], True)
|
['swift_hash_path_suffix', 'swift_hash_path_prefix'], True)
|
||||||
|
|
||||||
|
# Unreadable/missing swift.conf causes IOError
|
||||||
|
# We mock in case the unit tests are run on a laptop with SAIO,
|
||||||
|
# which does have a natural /etc/swift/swift.conf.
|
||||||
|
with mock.patch('swift.common.utils.HASH_PATH_PREFIX', b''), \
|
||||||
|
mock.patch('swift.common.utils.HASH_PATH_SUFFIX', b''), \
|
||||||
|
mock.patch('swift.common.utils.SWIFT_CONF_FILE',
|
||||||
|
'/nosuchfile'), \
|
||||||
|
self.assertRaises(IOError):
|
||||||
|
utils.validate_hash_conf()
|
||||||
|
|
||||||
def _test_validate_hash_conf(self, sections, options, should_raise_error):
|
def _test_validate_hash_conf(self, sections, options, should_raise_error):
|
||||||
|
|
||||||
class FakeConfigParser(object):
|
class FakeConfigParser(object):
|
||||||
def read(self, conf_path, encoding=None):
|
def readfp(self, fp):
|
||||||
return [conf_path]
|
pass
|
||||||
|
|
||||||
def get(self, section, option):
|
def get(self, section, option):
|
||||||
if section not in sections:
|
if section not in sections:
|
||||||
@ -2101,6 +2111,8 @@ class TestUtils(unittest.TestCase):
|
|||||||
|
|
||||||
with mock.patch('swift.common.utils.HASH_PATH_PREFIX', b''), \
|
with mock.patch('swift.common.utils.HASH_PATH_PREFIX', b''), \
|
||||||
mock.patch('swift.common.utils.HASH_PATH_SUFFIX', b''), \
|
mock.patch('swift.common.utils.HASH_PATH_SUFFIX', b''), \
|
||||||
|
mock.patch('swift.common.utils.SWIFT_CONF_FILE',
|
||||||
|
'/dev/null'), \
|
||||||
mock.patch('swift.common.utils.ConfigParser',
|
mock.patch('swift.common.utils.ConfigParser',
|
||||||
FakeConfigParser):
|
FakeConfigParser):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user