1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
from lxml import etree
21
from nova.api.openstack import wsgi
22
from nova.api.openstack.compute.contrib import quotas
23
from nova import context
25
from nova.tests.api.openstack import fakes
29
return {'quota_set': {'id': id, 'metadata_items': 128, 'volumes': 10,
30
'gigabytes': 1000, 'ram': 51200, 'floating_ips': 10,
31
'instances': 10, 'injected_files': 5, 'cores': 20,
32
'injected_file_content_bytes': 10240}}
36
return {'quota_set_list': [quota_set('1234'), quota_set('5678'),
37
quota_set('update_me')]}
40
class QuotaSetsTest(test.TestCase):
43
super(QuotaSetsTest, self).setUp()
44
self.controller = quotas.QuotaSetsController()
46
self.project_id = 'fake'
47
self.user_context = context.RequestContext(self.user_id,
49
self.admin_context = context.RequestContext(self.user_id,
53
def test_format_quota_set(self):
60
'metadata_items': 128,
63
'injected_file_content_bytes': 10240}
65
quota_set = quotas.QuotaSetsController()._format_quota_set('1234',
67
qs = quota_set['quota_set']
69
self.assertEqual(qs['id'], '1234')
70
self.assertEqual(qs['instances'], 10)
71
self.assertEqual(qs['cores'], 20)
72
self.assertEqual(qs['ram'], 51200)
73
self.assertEqual(qs['volumes'], 10)
74
self.assertEqual(qs['gigabytes'], 1000)
75
self.assertEqual(qs['floating_ips'], 10)
76
self.assertEqual(qs['metadata_items'], 128)
77
self.assertEqual(qs['injected_files'], 5)
78
self.assertEqual(qs['injected_file_content_bytes'], 10240)
80
def test_quotas_defaults(self):
81
uri = '/v2/fake_tenant/os-quota-sets/fake_tenant/defaults'
83
req = fakes.HTTPRequest.blank(uri)
84
res_dict = self.controller.defaults(req, 'fake_tenant')
86
expected = {'quota_set': {
94
'metadata_items': 128,
96
'injected_file_content_bytes': 10240}}
98
self.assertEqual(res_dict, expected)
100
def test_quotas_show_as_admin(self):
101
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/1234',
102
use_admin_context=True)
103
res_dict = self.controller.show(req, 1234)
105
self.assertEqual(res_dict, quota_set('1234'))
107
def test_quotas_show_as_unauthorized_user(self):
108
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/1234')
109
self.assertRaises(webob.exc.HTTPForbidden, self.controller.show,
112
def test_quotas_update_as_admin(self):
113
body = {'quota_set': {'instances': 50, 'cores': 50,
114
'ram': 51200, 'volumes': 10,
115
'gigabytes': 1000, 'floating_ips': 10,
116
'metadata_items': 128, 'injected_files': 5,
117
'injected_file_content_bytes': 10240}}
119
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/update_me',
120
use_admin_context=True)
121
res_dict = self.controller.update(req, 'update_me', body)
123
self.assertEqual(res_dict, body)
125
def test_quotas_update_as_user(self):
126
body = {'quota_set': {'instances': 50, 'cores': 50,
127
'ram': 51200, 'volumes': 10,
128
'gigabytes': 1000, 'floating_ips': 10,
129
'metadata_items': 128, 'injected_files': 5,
130
'injected_file_content_bytes': 10240}}
132
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/update_me')
133
self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
134
req, 'update_me', body)
137
class QuotaXMLSerializerTest(test.TestCase):
139
super(QuotaXMLSerializerTest, self).setUp()
140
self.serializer = quotas.QuotaTemplate()
141
self.deserializer = wsgi.XMLDeserializer()
143
def test_serializer(self):
144
exemplar = dict(quota_set=dict(
147
injected_file_content_bytes=20,
155
text = self.serializer.serialize(exemplar)
158
tree = etree.fromstring(text)
160
self.assertEqual('quota_set', tree.tag)
161
self.assertEqual('project_id', tree.get('id'))
162
self.assertEqual(len(exemplar['quota_set']) - 1, len(tree))
164
self.assertTrue(child.tag in exemplar['quota_set'])
165
self.assertEqual(int(child.text), exemplar['quota_set'][child.tag])
167
def test_deserializer(self):
168
exemplar = dict(quota_set=dict(
170
injected_file_content_bytes='20',
178
intext = ("<?xml version='1.0' encoding='UTF-8'?>\n"
180
'<metadata_items>10</metadata_items>'
181
'<injected_file_content_bytes>20'
182
'</injected_file_content_bytes>'
183
'<volumes>30</volumes>'
184
'<gigabytes>40</gigabytes>'
186
'<floating_ips>60</floating_ips>'
187
'<instances>70</instances>'
188
'<injected_files>80</injected_files>'
192
result = self.deserializer.deserialize(intext)['body']
193
self.assertEqual(result, exemplar)