Validate numericness of ports in builder files.

You can't really goof this up using bin/swift-ring-builder, but if you
have code that uses swift.common.ring.RingBuilder directly, you can
stuff e.g. "6002" in where you mean 6002, resulting in some fairly
baffling failures. (Yes, I have done this.)

Change-Id: I87b7b7066b9ea2ce6f82255605da99cf0d283689
This commit is contained in:
Samuel Merritt 2013-01-22 18:56:48 -08:00
parent d54a5a93dc
commit f2941b0846
2 changed files with 12 additions and 1 deletions
swift/common/ring
test/unit/common/ring

@ -335,7 +335,7 @@ class RingBuilder(object):
:raises RingValidationError: problem was found with the ring. :raises RingValidationError: problem was found with the ring.
""" """
# "len" showed up in profling, so it's just computed once. # "len" showed up in profiling, so it's just computed once.
dev_len = len(self.devs) dev_len = len(self.devs)
if sum(d['parts'] for d in self._iter_devs()) != \ if sum(d['parts'] for d in self._iter_devs()) != \
self.parts * self.replicas: self.parts * self.replicas:
@ -360,6 +360,12 @@ class RingBuilder(object):
"to a device." % "to a device." %
(part, replica)) (part, replica))
for dev in self._iter_devs():
if not isinstance(dev['port'], int):
raise exceptions.RingValidationError(
"Device %d has port %r, which is not an integer." %
(dev['id'], dev['port']))
if stats: if stats:
weight_of_one_part = self.weight_of_one_part() weight_of_one_part = self.weight_of_one_part()
worst = 0 worst = 0

@ -615,6 +615,11 @@ class TestRingBuilder(unittest.TestCase):
self.assertRaises(exceptions.RingValidationError, rb.validate) self.assertRaises(exceptions.RingValidationError, rb.validate)
rb.devs[1]['parts'] += 1 rb.devs[1]['parts'] += 1
# Test non-numeric port
rb.devs[1]['port'] = '10001'
self.assertRaises(exceptions.RingValidationError, rb.validate)
rb.devs[1]['port'] = 10001
# Test partition on nonexistent device # Test partition on nonexistent device
rb.pretend_min_part_hours_passed() rb.pretend_min_part_hours_passed()
orig_dev_id = rb._replica2part2dev[0][0] orig_dev_id = rb._replica2part2dev[0][0]