]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add unit tests for cinder/api/contrib/quotas
authorElena Ezhova <eezhova@mirantis.com>
Mon, 12 Aug 2013 12:59:06 +0000 (16:59 +0400)
committerElena Ezhova <eezhova@mirantis.com>
Tue, 13 Aug 2013 09:52:46 +0000 (13:52 +0400)
Add tests for serializator in QuotaTemplate class

Add tests for the following functions in QuotaSetsController class:
    -show
    -update
    -defaults

Change-Id: I615440277a54140eb245651d87a225231151083a

cinder/tests/api/contrib/test_quotas.py [new file with mode: 0644]
cinder/tests/policy.json

diff --git a/cinder/tests/api/contrib/test_quotas.py b/cinder/tests/api/contrib/test_quotas.py
new file mode 100644 (file)
index 0000000..922be83
--- /dev/null
@@ -0,0 +1,109 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+#
+# Copyright 2013 OpenStack Foundation
+# All Rights Reserved.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+"""
+Tests for cinder.api.contrib.quotas.py
+"""
+
+from lxml import etree
+import webob.exc
+
+
+from cinder.api.contrib import quotas
+from cinder import context
+from cinder import test
+
+
+def make_body(root=True, gigabytes=1000, snapshots=10,
+              volumes=10, tenant_id='foo'):
+    resources = {'gigabytes': gigabytes,
+                 'snapshots': snapshots,
+                 'volumes': volumes}
+    if tenant_id:
+        resources['id'] = tenant_id
+    if root:
+        result = {'quota_set': resources}
+    else:
+        result = resources
+    return result
+
+
+class QuotaSetsControllerTest(test.TestCase):
+
+    def setUp(self):
+        super(QuotaSetsControllerTest, self).setUp()
+        self.controller = quotas.QuotaSetsController()
+
+        self.req = self.mox.CreateMockAnything()
+        self.req.environ = {'cinder.context': context.get_admin_context()}
+        self.req.environ['cinder.context'].is_admin = True
+
+    def test_defaults(self):
+        result = self.controller.defaults(self.req, 'foo')
+        self.assertDictMatch(result, make_body())
+
+    def test_show(self):
+        result = self.controller.show(self.req, 'foo')
+        self.assertDictMatch(result, make_body())
+
+    def test_show_not_authorized(self):
+        self.req.environ['cinder.context'].is_admin = False
+        self.req.environ['cinder.context'].user_id = 'bad_user'
+        self.req.environ['cinder.context'].project_id = 'bad_project'
+        self.assertRaises(webob.exc.HTTPForbidden, self.controller.show,
+                          self.req, 'foo')
+
+    def test_update(self):
+        body = make_body(gigabytes=2000, snapshots=15,
+                         volumes=5, tenant_id=None)
+        result = self.controller.update(self.req, 'foo', body)
+        self.assertDictMatch(result, body)
+
+    def test_update_wrong_key(self):
+        body = {'quota_set': {'bad': 'bad'}}
+        result = self.controller.update(self.req, 'foo', body)
+        self.assertDictMatch(result, make_body(tenant_id=None))
+
+    def test_update_bad_quota_limit(self):
+        body = {'quota_set': {'gigabytes': -1000}}
+        self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
+                          self.req, 'foo', body)
+
+    def test_update_no_admin(self):
+        self.req.environ['cinder.context'].is_admin = False
+        self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
+                          self.req, 'foo', make_body(tenant_id=None))
+
+
+class QuotaSerializerTest(test.TestCase):
+
+    def setUp(self):
+        super(QuotaSerializerTest, self).setUp()
+        self.req = self.mox.CreateMockAnything()
+        self.req.environ = {'cinder.context': context.get_admin_context()}
+
+    def test_update_serializer(self):
+        serializer = quotas.QuotaTemplate()
+        quota_set = make_body(root=False)
+        text = serializer.serialize({'quota_set': quota_set})
+        tree = etree.fromstring(text)
+        self.assertEqual(tree.tag, 'quota_set')
+        self.assertEqual(tree.get('id'), quota_set['id'])
+        body = make_body(root=False, tenant_id=None)
+        for node in tree:
+            self.assertTrue(node.tag in body)
+            self.assertEquals(str(body[node.tag]), node.text)
index 55f4776db5d2a0bbd4d261fe9aba09dcac743300..5002931e53fb833f379d1e2d82ede9b2ba240aa0 100644 (file)
@@ -41,6 +41,8 @@
     "volume_extension:volume_host_attribute": [["rule:admin_api"]],
     "volume_extension:volume_tenant_attribute": [["rule:admin_api"]],
     "volume_extension:hosts": [["rule:admin_api"]],
+    "volume_extension:quotas:show": [],
+    "volume_extension:quotas:update": [],
 
     "volume:create_transfer": [],
     "volume:accept_transfer": [],