~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/compute/contrib/test_quotas.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-01-20 11:54:15 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: package-import@ubuntu.com-20120120115415-h2ujma9o536o1ut6
Tags: upstream-2012.1~e3~20120120.12170
ImportĀ upstreamĀ versionĀ 2012.1~e3~20120120.12170

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2011 OpenStack LLC.
 
4
# All Rights Reserved.
 
5
#
 
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
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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
 
16
#    under the License.
 
17
 
 
18
import webob
 
19
from lxml import etree
 
20
 
 
21
from nova.api.openstack import wsgi
 
22
from nova.api.openstack.compute.contrib import quotas
 
23
from nova import context
 
24
from nova import test
 
25
from nova.tests.api.openstack import fakes
 
26
 
 
27
 
 
28
def quota_set(id):
 
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}}
 
33
 
 
34
 
 
35
def quota_set_list():
 
36
    return {'quota_set_list': [quota_set('1234'), quota_set('5678'),
 
37
                               quota_set('update_me')]}
 
38
 
 
39
 
 
40
class QuotaSetsTest(test.TestCase):
 
41
 
 
42
    def setUp(self):
 
43
        super(QuotaSetsTest, self).setUp()
 
44
        self.controller = quotas.QuotaSetsController()
 
45
        self.user_id = 'fake'
 
46
        self.project_id = 'fake'
 
47
        self.user_context = context.RequestContext(self.user_id,
 
48
                                                   self.project_id)
 
49
        self.admin_context = context.RequestContext(self.user_id,
 
50
                                                    self.project_id,
 
51
                                                    is_admin=True)
 
52
 
 
53
    def test_format_quota_set(self):
 
54
        raw_quota_set = {
 
55
            'instances': 10,
 
56
            'cores': 20,
 
57
            'ram': 51200,
 
58
            'volumes': 10,
 
59
            'floating_ips': 10,
 
60
            'metadata_items': 128,
 
61
            'gigabytes': 1000,
 
62
            'injected_files': 5,
 
63
            'injected_file_content_bytes': 10240}
 
64
 
 
65
        quota_set = quotas.QuotaSetsController()._format_quota_set('1234',
 
66
                                                            raw_quota_set)
 
67
        qs = quota_set['quota_set']
 
68
 
 
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)
 
79
 
 
80
    def test_quotas_defaults(self):
 
81
        uri = '/v2/fake_tenant/os-quota-sets/fake_tenant/defaults'
 
82
 
 
83
        req = fakes.HTTPRequest.blank(uri)
 
84
        res_dict = self.controller.defaults(req, 'fake_tenant')
 
85
 
 
86
        expected = {'quota_set': {
 
87
                    'id': 'fake_tenant',
 
88
                    'instances': 10,
 
89
                    'cores': 20,
 
90
                    'ram': 51200,
 
91
                    'volumes': 10,
 
92
                    'gigabytes': 1000,
 
93
                    'floating_ips': 10,
 
94
                    'metadata_items': 128,
 
95
                    'injected_files': 5,
 
96
                    'injected_file_content_bytes': 10240}}
 
97
 
 
98
        self.assertEqual(res_dict, expected)
 
99
 
 
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)
 
104
 
 
105
        self.assertEqual(res_dict, quota_set('1234'))
 
106
 
 
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,
 
110
                          req, 1234)
 
111
 
 
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}}
 
118
 
 
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)
 
122
 
 
123
        self.assertEqual(res_dict, body)
 
124
 
 
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}}
 
131
 
 
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)
 
135
 
 
136
 
 
137
class QuotaXMLSerializerTest(test.TestCase):
 
138
    def setUp(self):
 
139
        super(QuotaXMLSerializerTest, self).setUp()
 
140
        self.serializer = quotas.QuotaTemplate()
 
141
        self.deserializer = wsgi.XMLDeserializer()
 
142
 
 
143
    def test_serializer(self):
 
144
        exemplar = dict(quota_set=dict(
 
145
                id='project_id',
 
146
                metadata_items=10,
 
147
                injected_file_content_bytes=20,
 
148
                volumes=30,
 
149
                gigabytes=40,
 
150
                ram=50,
 
151
                floating_ips=60,
 
152
                instances=70,
 
153
                injected_files=80,
 
154
                cores=90))
 
155
        text = self.serializer.serialize(exemplar)
 
156
 
 
157
        print text
 
158
        tree = etree.fromstring(text)
 
159
 
 
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))
 
163
        for child in tree:
 
164
            self.assertTrue(child.tag in exemplar['quota_set'])
 
165
            self.assertEqual(int(child.text), exemplar['quota_set'][child.tag])
 
166
 
 
167
    def test_deserializer(self):
 
168
        exemplar = dict(quota_set=dict(
 
169
                metadata_items='10',
 
170
                injected_file_content_bytes='20',
 
171
                volumes='30',
 
172
                gigabytes='40',
 
173
                ram='50',
 
174
                floating_ips='60',
 
175
                instances='70',
 
176
                injected_files='80',
 
177
                cores='90'))
 
178
        intext = ("<?xml version='1.0' encoding='UTF-8'?>\n"
 
179
                  '<quota_set>'
 
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>'
 
185
                  '<ram>50</ram>'
 
186
                  '<floating_ips>60</floating_ips>'
 
187
                  '<instances>70</instances>'
 
188
                  '<injected_files>80</injected_files>'
 
189
                  '<cores>90</cores>'
 
190
                  '</quota_set>')
 
191
 
 
192
        result = self.deserializer.deserialize(intext)['body']
 
193
        self.assertEqual(result, exemplar)