def test_used_limits_negative(self, ver_project, _mock_policy_enforce, _mock_get_project_quotas): version, has_project = ver_project fake_req = FakeRequest(fakes.FakeRequestContext(fake.USER_ID, fake.PROJECT_ID, is_admin=True), api_version=version) if has_project: fake_req = FakeRequest(fakes.FakeRequestContext(fake.USER_ID, fake.PROJECT_ID, is_admin=True), filter={'project_id': fake.UUID1}, api_version=version) obj = { "limits": { "rate": [], "absolute": {}, }, } res = wsgi.ResponseObject(obj) def get_project_quotas(context, project_id, quota_class=None, defaults=True, usages=True): if project_id == fake.UUID1: return {"gigabytes": {'limit': 5, 'in_use': -1}} return {"gigabytes": {'limit': 10, 'in_use': -2}} _mock_get_project_quotas.side_effect = get_project_quotas # allow user to access used limits _mock_policy_enforce.return_value = None self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] # if admin, only 3.39 and req contains project_id filter, cinder # returns the specified project's quota. if version == '3.39' and has_project: self.assertEqual(0, abs_limits['totalGigabytesUsed']) else: self.assertEqual(0, abs_limits['totalGigabytesUsed']) fake_req = FakeRequest(fakes.FakeRequestContext(fake.USER_ID, fake.PROJECT_ID), api_version=version) if has_project: fake_req = FakeRequest(fakes.FakeRequestContext(fake.USER_ID, fake.PROJECT_ID), filter={'project_id': fake.UUID1}, api_version=version) # if non-admin, cinder always returns self quota. self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] self.assertEqual(0, abs_limits['totalGigabytesUsed'])
def test_used_limits(self): fake_req = FakeRequest(fakes.FakeRequestContext('fake', 'fake')) obj = { "limits": { "rate": [], "absolute": {}, }, } res = wsgi.ResponseObject(obj) quota_map = { 'totalVolumesUsed': 'volumes', 'totalGigabytesUsed': 'gigabytes', 'totalSnapshotsUsed': 'snapshots', } limits = {} for display_name, q in quota_map.iteritems(): limits[q] = {'limit': 2, 'in_use': 1} def stub_get_project_quotas(context, project_id, usages=True): return limits self.stubs.Set(quota.QUOTAS, "get_project_quotas", stub_get_project_quotas) self.mox.ReplayAll() self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] for used_limit, value in abs_limits.iteritems(): self.assertEqual(value, limits[quota_map[used_limit]]['in_use'])
def test_used_limits(self, _mock_policy_enforce, _mock_get_project_quotas): fake_req = FakeRequest( fakes.FakeRequestContext(fake.USER_ID, fake.PROJECT_ID)) obj = { "limits": { "rate": [], "absolute": {}, }, } res = wsgi.ResponseObject(obj) quota_map = { 'totalVolumesUsed': 'volumes', 'totalGigabytesUsed': 'gigabytes', 'totalSnapshotsUsed': 'snapshots', } limits = {} for display_name, q in quota_map.items(): limits[q] = {'limit': 2, 'in_use': 1} _mock_get_project_quotas.return_value = limits # allow user to access used limits _mock_policy_enforce.return_value = None self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] for used_limit, value in abs_limits.items(): self.assertEqual(value, limits[quota_map[used_limit]]['in_use']) obj = { "limits": { "rate": [], "absolute": {}, }, } res = wsgi.ResponseObject(obj) # unallow user to access used limits _mock_policy_enforce.side_effect = exception.NotAuthorized self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] self.assertNotIn('totalVolumesUsed', abs_limits) self.assertNotIn('totalGigabytesUsed', abs_limits) self.assertNotIn('totalSnapshotsUsed', abs_limits)
def test_used_limits(self, ver_project, _mock_policy_authorize, _mock_get_project_quotas): version, has_project = ver_project fake_req = FakeRequest(fakes.FakeRequestContext(fake.USER_ID, fake.PROJECT_ID, is_admin=True), api_version=version) if has_project: fake_req = FakeRequest(fakes.FakeRequestContext(fake.USER_ID, fake.PROJECT_ID, is_admin=True), filter={'project_id': fake.UUID1}, api_version=version) obj = { "limits": { "rate": [], "absolute": {}, }, } res = wsgi.ResponseObject(obj) def get_project_quotas(context, project_id, quota_class=None, defaults=True, usages=True): if project_id == fake.UUID1: return {"gigabytes": {'limit': 5, 'in_use': 1}} return {"gigabytes": {'limit': 10, 'in_use': 2}} _mock_get_project_quotas.side_effect = get_project_quotas # allow user to access used limits _mock_policy_authorize.return_value = True self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] # if admin, only LIMITS_ADMIN_FILTER and req contains project_id # filter, cinder returns the specified project's quota. if version == mv.LIMITS_ADMIN_FILTER and has_project: self.assertEqual(1, abs_limits['totalGigabytesUsed']) else: self.assertEqual(2, abs_limits['totalGigabytesUsed']) fake_req = FakeRequest(fakes.FakeRequestContext( fake.USER_ID, fake.PROJECT_ID), api_version=version) if has_project: fake_req = FakeRequest(fakes.FakeRequestContext( fake.USER_ID, fake.PROJECT_ID), filter={'project_id': fake.UUID1}, api_version=version) # if non-admin, cinder always returns self quota. self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] self.assertEqual(2, abs_limits['totalGigabytesUsed']) obj = { "limits": { "rate": [], "absolute": {}, }, } res = wsgi.ResponseObject(obj) # unallow user to access used limits _mock_policy_authorize.return_value = False self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] self.assertNotIn('totalVolumesUsed', abs_limits) self.assertNotIn('totalGigabytesUsed', abs_limits) self.assertNotIn('totalSnapshotsUsed', abs_limits)