From: Seif Lotfy Date: Thu, 15 Aug 2013 03:06:20 +0000 (+0000) Subject: Add more asserts to the limiter unit tests to test the RateLimit X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=6577ec0b00c9fbae5931a1ed036763588e010019;p=openstack-build%2Fcinder-build.git Add more asserts to the limiter unit tests to test the RateLimit Added new asserts to test the per user set limits instead of using the defaults only Change-Id: Ib6651a82c9d7bb4b79d5c68be6372dc1100b816a --- diff --git a/cinder/tests/api/v1/test_limits.py b/cinder/tests/api/v1/test_limits.py index 816ba08da..4440234b7 100644 --- a/cinder/tests/api/v1/test_limits.py +++ b/cinder/tests/api/v1/test_limits.py @@ -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): """ diff --git a/cinder/tests/api/v2/test_limits.py b/cinder/tests/api/v2/test_limits.py index 9b58d51de..50a281259 100644 --- a/cinder/tests/api/v2/test_limits.py +++ b/cinder/tests/api/v2/test_limits.py @@ -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): """