]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Do not assume order of quotas dictionary elements
authorHenry Gessau <gessau@cisco.com>
Sun, 3 Aug 2014 17:40:01 +0000 (13:40 -0400)
committerHenry Gessau <gessau@cisco.com>
Sun, 3 Aug 2014 17:42:41 +0000 (13:42 -0400)
This fixes the quotas db unit test that breaks with a randomized PYTHONHASHSEED
(see the bug report).

The test assumed that the quotas dictionary from plugin.get_all_quotas() had
elements in a particular order. Found with PYTHONHASHSEED=1235130571.

The fix refactors the test case to handle an unsorted quotas dictionary. Also
choose some different quota limits to make it clearer what is being verified.

Partial-bug: #1348818

Note: There are several other unrelated unit tests that also break with a
randomized PYTHONHASHSEED, but they are not addressed here. They will be
addressed in separate patches.

Change-Id: I3892b51082629e6ebce175f64f501717741baa37

neutron/tests/unit/db/test_quota_db.py

index 813f0166a7045442fab1a302884d3bffbb9e70d2..9c6bca355bffdd4cbe5f4e5243d9373e231370f8 100644 (file)
@@ -78,24 +78,31 @@ class TestDbQuotaDriver(base.BaseTestCase):
         resource_1 = 'res_test_1'
         resource_2 = 'res_test_2'
 
-        resources = {resource_1: TestResource(resource_1, 1),
-                     resource_2: TestResource(resource_2, 1)}
+        resources = {resource_1: TestResource(resource_1, 3),
+                     resource_2: TestResource(resource_2, 5)}
 
-        self.plugin.update_quota_limit(self.context, project_1, resource_1, 2)
-        self.plugin.update_quota_limit(self.context, project_2, resource_2, 2)
+        self.plugin.update_quota_limit(self.context, project_1, resource_1, 7)
+        self.plugin.update_quota_limit(self.context, project_2, resource_2, 9)
         quotas = self.plugin.get_all_quotas(self.context, resources)
 
+        # Expect two tenants' quotas
         self.assertEqual(2, len(quotas))
-
-        self.assertEqual(3, len(quotas[0]))
-        self.assertEqual(project_1, quotas[0]['tenant_id'])
-        self.assertEqual(2, quotas[0][resource_1])
-        self.assertEqual(1, quotas[0][resource_2])
-
-        self.assertEqual(3, len(quotas[1]))
-        self.assertEqual(project_2, quotas[1]['tenant_id'])
-        self.assertEqual(1, quotas[1][resource_1])
-        self.assertEqual(2, quotas[1][resource_2])
+        # But not quotas for the same tenant twice
+        self.assertNotEqual(quotas[0]['tenant_id'], quotas[1]['tenant_id'])
+
+        # Check the expected limits. The quotas can be in any order.
+        for quota in quotas:
+            self.assertEqual(3, len(quota))
+            project = quota['tenant_id']
+            self.assertIn(project, (project_1, project_2))
+            if project == project_1:
+                expected_limit_r1 = 7
+                expected_limit_r2 = 5
+            if project == project_2:
+                expected_limit_r1 = 3
+                expected_limit_r2 = 9
+            self.assertEqual(expected_limit_r1, quota[resource_1])
+            self.assertEqual(expected_limit_r2, quota[resource_2])
 
     def test_limit_check(self):
         resources = {RESOURCE: TestResource(RESOURCE, 2)}