diff --git a/swift/common/storage_policy.py b/swift/common/storage_policy.py index 51eca182a7..ab456886d4 100644 --- a/swift/common/storage_policy.py +++ b/swift/common/storage_policy.py @@ -206,6 +206,9 @@ class BaseStoragePolicy(object): def __int__(self): return self.idx + def __hash__(self): + return hash(self.idx) + def __eq__(self, other): return self.idx == int(other) diff --git a/test/unit/common/test_storage_policy.py b/test/unit/common/test_storage_policy.py index 693a956d86..ba30b568a2 100644 --- a/test/unit/common/test_storage_policy.py +++ b/test/unit/common/test_storage_policy.py @@ -1085,6 +1085,20 @@ class TestStoragePolicies(unittest.TestCase): p503 = test_policies[503] self.assertTrue(501 < p503 < 507) + def test_storage_policies_as_dict_keys(self): + # We have tests that expect to be able to map policies + # to expected values in a dict; check that we can use + # policies as keys. + test_policies = [StoragePolicy(0, 'aay', True), + StoragePolicy(1, 'bee', False), + StoragePolicy(2, 'cee', False)] + policy_to_name_map = {p: p.name for p in test_policies} + self.assertEqual(sorted(policy_to_name_map.keys()), test_policies) + self.assertIs(test_policies[0], next( + p for p in policy_to_name_map.keys() if p.is_default)) + for p in test_policies: + self.assertEqual(policy_to_name_map[p], p.name) + def test_get_object_ring(self): test_policies = [StoragePolicy(0, 'aay', True), StoragePolicy(1, 'bee', False),