]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add more asserts to the limiter unit tests to test the RateLimit
authorSeif Lotfy <s.lotfy@telekom.de>
Thu, 15 Aug 2013 03:06:20 +0000 (03:06 +0000)
committerSeif Lotfy <s.lotfy@telekom.de>
Thu, 15 Aug 2013 03:11:55 +0000 (03:11 +0000)
Added new asserts to test the per user set limits instead of using
the defaults only

Change-Id: Ib6651a82c9d7bb4b79d5c68be6372dc1100b816a

cinder/tests/api/v1/test_limits.py
cinder/tests/api/v2/test_limits.py

index 816ba08da1ac1c698814cd803625c1e0946d3014..4440234b73ee22d5360fd3b9a45ddd1fb67ac28e 100644 (file)
@@ -406,7 +406,9 @@ class LimiterTest(BaseLimitTestSuite):
     def setUp(self):
         """Run before each test."""
         super(LimiterTest, self).setUp()
-        userlimits = {'limits.user3': ''}
+        userlimits = {'limits.user3': '',
+                      'limits.user0': '(get, *, .*, 4, minute);'
+                                      '(put, *, .*, 2, minute)'}
         self.limiter = limits.Limiter(TEST_LIMITS, **userlimits)
 
     def _check(self, num, verb, url, username=None):
@@ -463,7 +465,10 @@ class LimiterTest(BaseLimitTestSuite):
         """
         expected = [None] * 11
         results = list(self._check(11, "GET", "/anything"))
+        self.assertEqual(expected, results)
 
+        expected = [None] * 4 + [15.0]
+        results = list(self._check(5, "GET", "/foo", "user0"))
         self.assertEqual(expected, results)
 
     def test_delay_PUT_volumes(self):
@@ -512,16 +517,27 @@ class LimiterTest(BaseLimitTestSuite):
         results = list(self._check(10, "PUT", "/anything"))
         self.assertEqual(expected, results)
 
+        expected = [None] * 2 + [30.0] * 8
+        results = list(self._check(10, "PUT", "/anything", "user0"))
+        self.assertEqual(expected, results)
+
     def test_user_limit(self):
         """
         Test user-specific limits.
         """
         self.assertEqual(self.limiter.levels['user3'], [])
+        self.assertEqual(len(self.limiter.levels['user0']), 2)
 
     def test_multiple_users(self):
         """
         Tests involving multiple users.
         """
+
+        # User0
+        expected = [None] * 2 + [30.0] * 8
+        results = list(self._check(10, "PUT", "/anything", "user0"))
+        self.assertEqual(expected, results)
+
         # User1
         expected = [None] * 10 + [6.0] * 10
         results = list(self._check(20, "PUT", "/anything", "user1"))
@@ -551,6 +567,17 @@ class LimiterTest(BaseLimitTestSuite):
         results = list(self._check(5, "PUT", "/anything", "user2"))
         self.assertEqual(expected, results)
 
+        # User0 again
+        expected = [28.0]
+        results = list(self._check(1, "PUT", "/anything", "user0"))
+        self.assertEqual(expected, results)
+
+        self.time += 28.0
+
+        expected = [None, 30.0]
+        results = list(self._check(2, "PUT", "/anything", "user0"))
+        self.assertEqual(expected, results)
+
 
 class WsgiLimiterTest(BaseLimitTestSuite):
     """
index 9b58d51de346dc4e431ac66bc7ff192c740b470f..50a281259baaae41c692c9d1e2a5d06d06a40c70 100644 (file)
@@ -406,7 +406,9 @@ class LimiterTest(BaseLimitTestSuite):
     def setUp(self):
         """Run before each test."""
         super(LimiterTest, self).setUp()
-        userlimits = {'limits.user3': ''}
+        userlimits = {'limits.user3': '',
+                      'limits.user0': '(get, *, .*, 4, minute);'
+                                      '(put, *, .*, 2, minute)'}
         self.limiter = limits.Limiter(TEST_LIMITS, **userlimits)
 
     def _check(self, num, verb, url, username=None):
@@ -463,7 +465,10 @@ class LimiterTest(BaseLimitTestSuite):
         """
         expected = [None] * 11
         results = list(self._check(11, "GET", "/anything"))
+        self.assertEqual(expected, results)
 
+        expected = [None] * 4 + [15.0]
+        results = list(self._check(5, "GET", "/foo", "user0"))
         self.assertEqual(expected, results)
 
     def test_delay_PUT_volumes(self):
@@ -512,16 +517,27 @@ class LimiterTest(BaseLimitTestSuite):
         results = list(self._check(10, "PUT", "/anything"))
         self.assertEqual(expected, results)
 
+        expected = [None] * 2 + [30.0] * 8
+        results = list(self._check(10, "PUT", "/anything", "user0"))
+        self.assertEqual(expected, results)
+
     def test_user_limit(self):
         """
         Test user-specific limits.
         """
         self.assertEqual(self.limiter.levels['user3'], [])
+        self.assertEqual(len(self.limiter.levels['user0']), 2)
 
     def test_multiple_users(self):
         """
         Tests involving multiple users.
         """
+
+        # User0
+        expected = [None] * 2 + [30.0] * 8
+        results = list(self._check(10, "PUT", "/anything", "user0"))
+        self.assertEqual(expected, results)
+
         # User1
         expected = [None] * 10 + [6.0] * 10
         results = list(self._check(20, "PUT", "/anything", "user1"))
@@ -551,6 +567,17 @@ class LimiterTest(BaseLimitTestSuite):
         results = list(self._check(5, "PUT", "/anything", "user2"))
         self.assertEqual(expected, results)
 
+        # User0 again
+        expected = [28.0]
+        results = list(self._check(1, "PUT", "/anything", "user0"))
+        self.assertEqual(expected, results)
+
+        self.time += 28.0
+
+        expected = [None, 30.0]
+        results = list(self._check(2, "PUT", "/anything", "user0"))
+        self.assertEqual(expected, results)
+
 
 class WsgiLimiterTest(BaseLimitTestSuite):
     """