Fix crash in swift-ring-builder's list_parts command.

If you run list_parts against a builder that has never been
rebalanced, you'd get a crash. Now you don't.

To reproduce:

$ swift-ring-builder foo.builder create 8 3 1
$ swift-ring-builder foo.builder add r1z1-1.2.3.4:6000/sda 100
$ swift-ring-builder foo.builder list_parts z1

Change-Id: Ic3edffab0c5c2e9551a2f89ddb881153f0b07db7
This commit is contained in:
Samuel Merritt 2013-03-14 18:49:30 -07:00
parent b115356af6
commit 229ba53a19
2 changed files with 4 additions and 0 deletions

View File

@ -937,6 +937,8 @@ class RingBuilder(object):
Deliberately includes duplicates.
"""
if self._replica2part2dev is None:
return []
return [self.devs[part2dev[part]]
for part2dev in self._replica2part2dev
if part < len(part2dev)]

View File

@ -797,6 +797,8 @@ class TestRingBuilder(unittest.TestCase):
def test_get_part_devices(self):
rb = ring.RingBuilder(8, 3, 1)
self.assertEqual(rb.get_part_devices(0), [])
rb.add_dev({'id': 0, 'region': 0, 'zone': 0, 'weight': 1,
'ip': '127.0.0.1', 'port': 10000, 'device': 'sda1'})
rb.add_dev({'id': 1, 'region': 0, 'zone': 1, 'weight': 1,