1# Copyright 2014 OpenStack Foundation 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12# implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16from unittest import mock 17 18import ddt 19 20from cinderclient.tests.unit import utils 21from cinderclient.v2 import limits 22 23 24REQUEST_ID = 'req-test-request-id' 25 26 27def _get_default_RateLimit(verb="verb1", uri="uri1", regex="regex1", 28 value="value1", 29 remain="remain1", unit="unit1", 30 next_available="next1"): 31 return limits.RateLimit(verb, uri, regex, value, remain, unit, 32 next_available) 33 34 35class TestLimits(utils.TestCase): 36 def test_repr(self): 37 limit = limits.Limits(None, {"foo": "bar"}, resp=REQUEST_ID) 38 self.assertEqual("<Limits>", repr(limit)) 39 self._assert_request_id(limit) 40 41 def test_absolute(self): 42 limit = limits.Limits( 43 None, 44 {"absolute": {"name1": "value1", "name2": "value2"}}, 45 resp=REQUEST_ID) 46 l1 = limits.AbsoluteLimit("name1", "value1") 47 l2 = limits.AbsoluteLimit("name2", "value2") 48 for item in limit.absolute: 49 self.assertIn(item, [l1, l2]) 50 self._assert_request_id(limit) 51 52 def test_rate(self): 53 limit = limits.Limits( 54 None, 55 { 56 "rate": [ 57 { 58 "uri": "uri1", 59 "regex": "regex1", 60 "limit": [ 61 { 62 "verb": "verb1", 63 "value": "value1", 64 "remaining": "remain1", 65 "unit": "unit1", 66 "next-available": "next1", 67 }, 68 ], 69 }, 70 { 71 "uri": "uri2", 72 "regex": "regex2", 73 "limit": [ 74 { 75 "verb": "verb2", 76 "value": "value2", 77 "remaining": "remain2", 78 "unit": "unit2", 79 "next-available": "next2", 80 }, 81 ], 82 }, ], 83 }, 84 resp=REQUEST_ID) 85 l1 = limits.RateLimit("verb1", "uri1", "regex1", "value1", "remain1", 86 "unit1", "next1") 87 l2 = limits.RateLimit("verb2", "uri2", "regex2", "value2", "remain2", 88 "unit2", "next2") 89 for item in limit.rate: 90 self.assertIn(item, [l1, l2]) 91 self._assert_request_id(limit) 92 93 94class TestRateLimit(utils.TestCase): 95 def test_equal(self): 96 l1 = _get_default_RateLimit() 97 l2 = _get_default_RateLimit() 98 self.assertEqual(l1, l2) 99 100 def test_not_equal_verbs(self): 101 l1 = _get_default_RateLimit() 102 l2 = _get_default_RateLimit(verb="verb2") 103 self.assertNotEqual(l1, l2) 104 105 def test_not_equal_uris(self): 106 l1 = _get_default_RateLimit() 107 l2 = _get_default_RateLimit(uri="uri2") 108 self.assertNotEqual(l1, l2) 109 110 def test_not_equal_regexps(self): 111 l1 = _get_default_RateLimit() 112 l2 = _get_default_RateLimit(regex="regex2") 113 self.assertNotEqual(l1, l2) 114 115 def test_not_equal_values(self): 116 l1 = _get_default_RateLimit() 117 l2 = _get_default_RateLimit(value="value2") 118 self.assertNotEqual(l1, l2) 119 120 def test_not_equal_remains(self): 121 l1 = _get_default_RateLimit() 122 l2 = _get_default_RateLimit(remain="remain2") 123 self.assertNotEqual(l1, l2) 124 125 def test_not_equal_units(self): 126 l1 = _get_default_RateLimit() 127 l2 = _get_default_RateLimit(unit="unit2") 128 self.assertNotEqual(l1, l2) 129 130 def test_not_equal_next_available(self): 131 l1 = _get_default_RateLimit() 132 l2 = _get_default_RateLimit(next_available="next2") 133 self.assertNotEqual(l1, l2) 134 135 def test_repr(self): 136 l1 = _get_default_RateLimit() 137 self.assertEqual("<RateLimit: method=verb1 uri=uri1>", repr(l1)) 138 139 140class TestAbsoluteLimit(utils.TestCase): 141 def test_equal(self): 142 l1 = limits.AbsoluteLimit("name1", "value1") 143 l2 = limits.AbsoluteLimit("name1", "value1") 144 self.assertEqual(l1, l2) 145 146 def test_not_equal_values(self): 147 l1 = limits.AbsoluteLimit("name1", "value1") 148 l2 = limits.AbsoluteLimit("name1", "value2") 149 self.assertNotEqual(l1, l2) 150 151 def test_not_equal_names(self): 152 l1 = limits.AbsoluteLimit("name1", "value1") 153 l2 = limits.AbsoluteLimit("name2", "value1") 154 self.assertNotEqual(l1, l2) 155 156 def test_repr(self): 157 l1 = limits.AbsoluteLimit("name1", "value1") 158 self.assertEqual("<AbsoluteLimit: name=name1>", repr(l1)) 159 160 161@ddt.ddt 162class TestLimitsManager(utils.TestCase): 163 @ddt.data(None, 'test') 164 def test_get(self, tenant_id): 165 api = mock.Mock() 166 api.client.get.return_value = ( 167 None, 168 {"limits": {"absolute": {"name1": "value1", }}, 169 "no-limits": {"absolute": {"name2": "value2", }}}) 170 l1 = limits.AbsoluteLimit("name1", "value1") 171 limitsManager = limits.LimitsManager(api) 172 173 lim = limitsManager.get(tenant_id) 174 query_str = '' 175 if tenant_id: 176 query_str = '?tenant_id=%s' % tenant_id 177 api.client.get.assert_called_once_with('/limits%s' % query_str) 178 179 self.assertIsInstance(lim, limits.Limits) 180 for limit in lim.absolute: 181 self.assertEqual(l1, limit) 182