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
24
from nova.tests.api.openstack import fakes
28
return {'quota_set': {'id': id, 'metadata_items': 128, 'volumes': 10,
29
'gigabytes': 1000, 'ram': 51200, 'floating_ips': 10,
30
'instances': 10, 'injected_files': 5, 'cores': 20,
31
'injected_file_content_bytes': 10240}}
35
return {'quota_set_list': [quota_set('1234'), quota_set('5678'),
36
quota_set('update_me')]}
39
class QuotaSetsTest(test.TestCase):
42
super(QuotaSetsTest, self).setUp()
43
self.controller = quotas.QuotaSetsController()
45
def test_format_quota_set(self):
52
'metadata_items': 128,
55
'injected_file_content_bytes': 10240}
57
quota_set = quotas.QuotaSetsController()._format_quota_set('1234',
59
qs = quota_set['quota_set']
61
self.assertEqual(qs['id'], '1234')
62
self.assertEqual(qs['instances'], 10)
63
self.assertEqual(qs['cores'], 20)
64
self.assertEqual(qs['ram'], 51200)
65
self.assertEqual(qs['volumes'], 10)
66
self.assertEqual(qs['gigabytes'], 1000)
67
self.assertEqual(qs['floating_ips'], 10)
68
self.assertEqual(qs['metadata_items'], 128)
69
self.assertEqual(qs['injected_files'], 5)
70
self.assertEqual(qs['injected_file_content_bytes'], 10240)
72
def test_quotas_defaults(self):
73
uri = '/v2/fake_tenant/os-quota-sets/fake_tenant/defaults'
75
req = fakes.HTTPRequest.blank(uri)
76
res_dict = self.controller.defaults(req, 'fake_tenant')
78
expected = {'quota_set': {
86
'metadata_items': 128,
88
'injected_file_content_bytes': 10240}}
90
self.assertEqual(res_dict, expected)
92
def test_quotas_show_as_admin(self):
93
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/1234',
94
use_admin_context=True)
95
res_dict = self.controller.show(req, 1234)
97
self.assertEqual(res_dict, quota_set('1234'))
99
def test_quotas_show_as_unauthorized_user(self):
100
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/1234')
101
self.assertRaises(webob.exc.HTTPForbidden, self.controller.show,
104
def test_quotas_update_as_admin(self):
105
body = {'quota_set': {'instances': 50, 'cores': 50,
106
'ram': 51200, 'volumes': 10,
107
'gigabytes': 1000, 'floating_ips': 10,
108
'metadata_items': 128, 'injected_files': 5,
109
'injected_file_content_bytes': 10240}}
111
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/update_me',
112
use_admin_context=True)
113
res_dict = self.controller.update(req, 'update_me', body)
115
self.assertEqual(res_dict, body)
117
def test_quotas_update_as_user(self):
118
body = {'quota_set': {'instances': 50, 'cores': 50,
119
'ram': 51200, 'volumes': 10,
120
'gigabytes': 1000, 'floating_ips': 10,
121
'metadata_items': 128, 'injected_files': 5,
122
'injected_file_content_bytes': 10240}}
124
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/update_me')
125
self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
126
req, 'update_me', body)
129
class QuotaXMLSerializerTest(test.TestCase):
131
super(QuotaXMLSerializerTest, self).setUp()
132
self.serializer = quotas.QuotaTemplate()
133
self.deserializer = wsgi.XMLDeserializer()
135
def test_serializer(self):
136
exemplar = dict(quota_set=dict(
139
injected_file_content_bytes=20,
147
text = self.serializer.serialize(exemplar)
150
tree = etree.fromstring(text)
152
self.assertEqual('quota_set', tree.tag)
153
self.assertEqual('project_id', tree.get('id'))
154
self.assertEqual(len(exemplar['quota_set']) - 1, len(tree))
156
self.assertTrue(child.tag in exemplar['quota_set'])
157
self.assertEqual(int(child.text), exemplar['quota_set'][child.tag])
159
def test_deserializer(self):
160
exemplar = dict(quota_set=dict(
162
injected_file_content_bytes='20',
170
intext = ("<?xml version='1.0' encoding='UTF-8'?>\n"
172
'<metadata_items>10</metadata_items>'
173
'<injected_file_content_bytes>20'
174
'</injected_file_content_bytes>'
175
'<volumes>30</volumes>'
176
'<gigabytes>40</gigabytes>'
178
'<floating_ips>60</floating_ips>'
179
'<instances>70</instances>'
180
'<injected_files>80</injected_files>'
184
result = self.deserializer.deserialize(intext)['body']
185
self.assertEqual(result, exemplar)