1# Copyright 2013 Huawei Technologies Co., Ltd
2# All Rights Reserved.
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15
16"""
17Tests for cinder.api.contrib.quota_classes.py
18"""
19
20
21import mock
22
23import webob.exc
24
25
26from cinder.api.contrib import quota_classes
27from cinder import context
28from cinder import exception
29from cinder import quota
30from cinder import test
31from cinder.tests.unit import fake_constants as fake
32from cinder.volume import volume_types
33
34
35QUOTAS = quota.QUOTAS
36GROUP_QUOTAS = quota.GROUP_QUOTAS
37
38
39def make_body(root=True, gigabytes=1000, snapshots=10,
40              volumes=10, backups=10,
41              backup_gigabytes=1000, per_volume_gigabytes=-1,
42              volume_types_faked=None,
43              tenant_id=fake.PROJECT_ID, groups=10):
44    resources = {'gigabytes': gigabytes,
45                 'snapshots': snapshots,
46                 'volumes': volumes,
47                 'backups': backups,
48                 'per_volume_gigabytes': per_volume_gigabytes,
49                 'backup_gigabytes': backup_gigabytes,
50                 'groups': groups}
51    if not volume_types_faked:
52        volume_types_faked = {'fake_type': None}
53    for volume_type in volume_types_faked:
54        resources['gigabytes_' + volume_type] = -1
55        resources['snapshots_' + volume_type] = -1
56        resources['volumes_' + volume_type] = -1
57
58    if tenant_id:
59        resources['id'] = tenant_id
60    if root:
61        result = {'quota_class_set': resources}
62    else:
63        result = resources
64    return result
65
66
67def make_response_body(root=True, ctxt=None, quota_class='foo',
68                       request_body=None, tenant_id=fake.PROJECT_ID):
69    resources = {}
70    if not ctxt:
71        ctxt = context.get_admin_context()
72    resources.update(QUOTAS.get_class_quotas(ctxt, quota_class))
73    resources.update(GROUP_QUOTAS.get_class_quotas(ctxt, quota_class))
74    if not request_body and not request_body['quota_class_set']:
75        resources.update(request_body['quota_class_set'])
76
77    if tenant_id:
78        resources['id'] = tenant_id
79    if root:
80        result = {'quota_class_set': resources}
81    else:
82        result = resources
83    return result
84
85
86class QuotaClassSetsControllerTest(test.TestCase):
87
88    def setUp(self):
89        super(QuotaClassSetsControllerTest, self).setUp()
90        self.controller = quota_classes.QuotaClassSetsController()
91
92        self.ctxt = context.get_admin_context()
93        self.req = mock.Mock()
94        self.req.environ = {'cinder.context': self.ctxt}
95        self.req.environ['cinder.context'].is_admin = True
96
97    def test_show(self):
98        volume_types.create(self.ctxt, 'fake_type')
99        result = self.controller.show(self.req, fake.PROJECT_ID)
100        self.assertDictEqual(make_body(), result)
101
102    def test_show_not_authorized(self):
103        self.req.environ['cinder.context'].is_admin = False
104        self.req.environ['cinder.context'].user_id = fake.USER_ID
105        self.req.environ['cinder.context'].project_id = fake.PROJECT_ID
106        self.assertRaises(exception.PolicyNotAuthorized, self.controller.show,
107                          self.req, fake.PROJECT_ID)
108
109    def test_update(self):
110        volume_types.create(self.ctxt, 'fake_type')
111        body = make_body(gigabytes=2000, snapshots=15,
112                         volumes=5, tenant_id=None)
113        result = self.controller.update(self.req, fake.PROJECT_ID, body)
114        self.assertDictEqual(body, result)
115
116    @mock.patch('cinder.api.openstack.wsgi.Controller.validate_string_length')
117    @mock.patch('cinder.utils.validate_integer')
118    def test_update_limit(self, mock_validate_integer, mock_validate):
119        mock_validate_integer.return_value = 5
120        volume_types.create(self.ctxt, 'fake_type')
121        body = make_body(volumes=5)
122        result = self.controller.update(self.req, fake.PROJECT_ID, body)
123        self.assertEqual(5, result['quota_class_set']['volumes'])
124        self.assertTrue(mock_validate.called)
125        self.assertTrue(mock_validate_integer.called)
126
127    def test_update_wrong_key(self):
128        volume_types.create(self.ctxt, 'fake_type')
129        body = {'quota_class_set': {'bad': 'bad'}}
130        result = self.controller.update(self.req, fake.PROJECT_ID, body)
131        self.assertDictEqual(make_body(tenant_id=None), result)
132
133    def test_update_invalid_key_value(self):
134        body = {'quota_class_set': {'gigabytes': "should_be_int"}}
135        self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
136                          self.req, fake.PROJECT_ID, body)
137
138    def test_update_bad_quota_limit(self):
139        body = {'quota_class_set': {'gigabytes': -1000}}
140        self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
141                          self.req, fake.PROJECT_ID, body)
142
143    def test_update_no_admin(self):
144        self.req.environ['cinder.context'].is_admin = False
145        self.assertRaises(exception.PolicyNotAuthorized,
146                          self.controller.update, self.req, fake.PROJECT_ID,
147                          make_body(tenant_id=None))
148
149    def test_update_with_more_volume_types(self):
150        volume_types.create(self.ctxt, 'fake_type_1')
151        volume_types.create(self.ctxt, 'fake_type_2')
152        body = {'quota_class_set': {'gigabytes_fake_type_1': 1111,
153                                    'volumes_fake_type_2': 2222}}
154        result = self.controller.update(self.req, fake.PROJECT_ID, body)
155        self.assertDictEqual(make_response_body(ctxt=self.ctxt,
156                                                quota_class=fake.PROJECT_ID,
157                                                request_body=body,
158                                                tenant_id=None),
159                             result)
160