From 967c9c69621589233069a2fd695f0d1bdb7b5edb Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Fri, 22 Jan 2021 14:06:02 -0800 Subject: [PATCH] ring: Prevent IndexErrors in calc_replica_count Change-Id: Ibdf85fd80ba4c1bc216153a2f5fa4d7b6d9b432e --- swift/common/ring/ring.py | 2 ++ test/unit/common/ring/test_builder.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/swift/common/ring/ring.py b/swift/common/ring/ring.py index 99370fd284..68635acd67 100644 --- a/swift/common/ring/ring.py +++ b/swift/common/ring/ring.py @@ -36,6 +36,8 @@ from swift.common.ring.utils import tiers_for_dev def calc_replica_count(replica2part2dev_id): + if not replica2part2dev_id: + return 0 base = len(replica2part2dev_id) - 1 extra = 1.0 * len(replica2part2dev_id[-1]) / len(replica2part2dev_id[0]) return base + extra diff --git a/test/unit/common/ring/test_builder.py b/test/unit/common/ring/test_builder.py index b9421b3043..6d96a321a1 100644 --- a/test/unit/common/ring/test_builder.py +++ b/test/unit/common/ring/test_builder.py @@ -73,6 +73,11 @@ class TestRingBuilder(unittest.TestCase): self.assertFalse(rb.devs_changed) self.assertEqual(rb.version, 0) self.assertIsNotNone(rb._last_part_moves) + rd = rb.get_ring() + self.assertEqual(rd.devs, []) + self.assertEqual(rd.version, 0) + self.assertIsNone(rd.next_part_power) + self.assertEqual(rd.replica_count, 0) def test_overlarge_part_powers(self): expected_msg = 'part_power must be at most 32 (was 33)'