From 7a9269bca350321596ee6a4ed7597ad423ff11c3 Mon Sep 17 00:00:00 2001 From: Alistair Coles Date: Fri, 28 Apr 2017 14:54:10 +0100 Subject: [PATCH] Fix unit test failing when swift.conf has default policy index >10 In unit/container/test_backend.py test_policy_stat_tracking in classes TestContainerBroker[BeforeMetadata|BeforeSPI|BeforeXSync] fails if the default policy in /etc/swift/swift.conf has an index >10. Those classes monkey patch the container broker to pre-storage-policy index behaviour, so that it's policy index will always be 0. The test fails with a KeyError when asserting that the broker should have stats for the POLICIES.default index even when no object have been PUT with that policy index. When the default policy in swift.conf has index >10, that is neither the broker's policy index (0) nor the policy index of any other object that has been PUT during the test. The test need a patch_policies decorator to remove the coupling with swift.conf policies. However, the assertion that the broker has stats for it's policy index even when no objects were PUT to that index is then extremely unlikely to ever be tested, because the broker's default policy index of 0 is very likely to have used for a PUT object. So this patch also repeats that assertion before any object have been PUT Closes-Bug: #1687029 Change-Id: I8b3678dac83f7329d835059c9973b994bc975a33 --- test/unit/container/test_backend.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/test/unit/container/test_backend.py b/test/unit/container/test_backend.py index abdb373650..5847093afd 100644 --- a/test/unit/container/test_backend.py +++ b/test/unit/container/test_backend.py @@ -874,14 +874,31 @@ class TestContainerBroker(unittest.TestCase): } self.assertEqual(policy_stats, expected) + @patch_policies def test_policy_stat_tracking(self): ts = (Timestamp(t).internal for t in itertools.count(int(time()))) broker = ContainerBroker(':memory:', account='a', container='c') + # Note: in subclasses of this TestCase that inherit the + # ContainerBrokerMigrationMixin, passing POLICIES.default.idx here has + # no effect and broker.get_policy_stats() returns a dict with a single + # entry mapping policy index 0 to the container stats broker.initialize(next(ts), POLICIES.default.idx) stats = defaultdict(dict) + def assert_empty_default_policy_stats(policy_stats): + # if no objects were added for the default policy we still + # expect an entry for the default policy in the returned info + # because the database was initialized with that storage policy + # - but it must be empty. + default_stats = policy_stats[POLICIES.default.idx] + expected = {'object_count': 0, 'bytes_used': 0} + self.assertEqual(default_stats, expected) + + policy_stats = broker.get_policy_stats() + assert_empty_default_policy_stats(policy_stats) + iters = 100 for i in range(iters): policy_index = random.randint(0, iters * 0.1) @@ -894,14 +911,10 @@ class TestContainerBroker(unittest.TestCase): # in each storage policy stats[policy_index][name] = size policy_stats = broker.get_policy_stats() - # if no objects were added for the default policy we still - # expect an entry for the default policy in the returned info - # because the database was initialized with that storage policy - # - but it must be empty. if POLICIES.default.idx not in stats: - default_stats = policy_stats.pop(POLICIES.default.idx) - expected = {'object_count': 0, 'bytes_used': 0} - self.assertEqual(default_stats, expected) + # unlikely, but check empty default index still in policy stats + assert_empty_default_policy_stats(policy_stats) + policy_stats.pop(POLICIES.default.idx) self.assertEqual(len(policy_stats), len(stats)) for policy_index, stat in policy_stats.items(): self.assertEqual(stat['object_count'], len(stats[policy_index]))