Fix misleading error msg if swift.conf unreadable
Several tools are returning a misleading error message if swift.conf is missing or not readable by the user, stating that the hash pre-/suffixes are missing. Let's fix this by catching the real issue down below. Change-Id: I7a47e6260ed51a3b7d9665b3a4510520429ae158
This commit is contained in:
parent
1c0661e920
commit
78a4070c90
@ -233,25 +233,26 @@ def validate_hash_conf():
|
||||
|
||||
if six.PY3:
|
||||
# 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:
|
||||
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:
|
||||
HASH_PATH_SUFFIX = hash_conf.get('swift-hash',
|
||||
'swift_hash_path_suffix')
|
||||
if six.PY3:
|
||||
HASH_PATH_SUFFIX = HASH_PATH_SUFFIX.encode('latin1')
|
||||
except (NoSectionError, NoOptionError):
|
||||
pass
|
||||
try:
|
||||
HASH_PATH_PREFIX = hash_conf.get('swift-hash',
|
||||
'swift_hash_path_prefix')
|
||||
if six.PY3:
|
||||
HASH_PATH_PREFIX = HASH_PATH_PREFIX.encode('latin1')
|
||||
except (NoSectionError, NoOptionError):
|
||||
pass
|
||||
try:
|
||||
HASH_PATH_SUFFIX = hash_conf.get('swift-hash',
|
||||
'swift_hash_path_suffix')
|
||||
if six.PY3:
|
||||
HASH_PATH_SUFFIX = HASH_PATH_SUFFIX.encode('latin1')
|
||||
except (NoSectionError, NoOptionError):
|
||||
pass
|
||||
try:
|
||||
HASH_PATH_PREFIX = hash_conf.get('swift-hash',
|
||||
'swift_hash_path_prefix')
|
||||
if six.PY3:
|
||||
HASH_PATH_PREFIX = HASH_PATH_PREFIX.encode('latin1')
|
||||
except (NoSectionError, NoOptionError):
|
||||
pass
|
||||
|
||||
if not HASH_PATH_SUFFIX and not HASH_PATH_PREFIX:
|
||||
raise InvalidHashPathConfigError()
|
||||
@ -259,7 +260,7 @@ def validate_hash_conf():
|
||||
|
||||
try:
|
||||
validate_hash_conf()
|
||||
except InvalidHashPathConfigError:
|
||||
except (InvalidHashPathConfigError, IOError):
|
||||
# could get monkey patched or lazy loaded
|
||||
pass
|
||||
|
||||
|
@ -248,7 +248,7 @@ class TestRing(TestRingBase):
|
||||
with mock.patch.object(utils, 'HASH_PATH_SUFFIX', b''), \
|
||||
mock.patch.object(utils, 'HASH_PATH_PREFIX', b''), \
|
||||
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):
|
||||
self.assertEqual(self.ring.replica_count, 3)
|
||||
|
@ -2085,11 +2085,21 @@ class TestUtils(unittest.TestCase):
|
||||
['swift-hash-xxx'],
|
||||
['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):
|
||||
|
||||
class FakeConfigParser(object):
|
||||
def read(self, conf_path, encoding=None):
|
||||
return [conf_path]
|
||||
def readfp(self, fp):
|
||||
pass
|
||||
|
||||
def get(self, section, option):
|
||||
if section not in sections:
|
||||
@ -2101,6 +2111,8 @@ class TestUtils(unittest.TestCase):
|
||||
|
||||
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',
|
||||
'/dev/null'), \
|
||||
mock.patch('swift.common.utils.ConfigParser',
|
||||
FakeConfigParser):
|
||||
try:
|
||||
|
Loading…
Reference in New Issue
Block a user