]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add unit test to cinder cgsnapshot api
authorHaiwei Xu <xu-haiwei@mxw.nes.nec.co.jp>
Wed, 17 Sep 2014 05:58:20 +0000 (14:58 +0900)
committerHaiwei Xu <xu-haiwei@mxw.nes.nec.co.jp>
Mon, 22 Sep 2014 08:40:52 +0000 (17:40 +0900)
This patch adds unit tests to cgsnapshot create api,
in case cgsnapshot is invalid or can not be found.

Change-Id: I6ec73b8db413e5c085f8a48830f5e1446c2d7240

cinder/tests/api/contrib/test_cgsnapshots.py

index 695b54c0fdd22683f49f950980448dbd402a0df8..3ecef85761a09c8445d628b5152efefd748998db 100644 (file)
@@ -20,10 +20,13 @@ Tests for cgsnapshot code.
 import json
 from xml.dom import minidom
 
+import mock
 import webob
 
+from cinder.consistencygroup import api as consistencygroupAPI
 from cinder import context
 from cinder import db
+from cinder import exception
 from cinder.openstack.common import log as logging
 from cinder import test
 from cinder.tests.api import fakes
@@ -384,6 +387,57 @@ class CgsnapshotsAPITestCase(test.TestCase):
                          'The server could not comply with the request since'
                          ' it is either malformed or otherwise incorrect.')
 
+    @mock.patch.object(consistencygroupAPI.API, 'create_cgsnapshot',
+                       side_effect=exception.InvalidCgSnapshot(
+                           reason='invalid cgsnapshot'))
+    def test_create_with_invalid_cgsnapshot(self, mock_create_cgsnapshot):
+        consistencygroup_id = utils.create_consistencygroup(self.context)['id']
+        utils.create_volume(
+            self.context,
+            consistencygroup_id=consistencygroup_id)['id']
+
+        body = {"cgsnapshot": {"name": "cg1",
+                               "description":
+                               "CG Snapshot 1",
+                               "consistencygroup_id": consistencygroup_id}}
+        req = webob.Request.blank('/v2/fake/cgsnapshots')
+        req.body = json.dumps(body)
+        req.method = 'POST'
+        req.headers['Content-Type'] = 'application/json'
+        res = req.get_response(fakes.wsgi_app())
+        res_dict = json.loads(res.body)
+
+        self.assertEqual(400, res.status_int)
+        self.assertEqual(400, res_dict['badRequest']['code'])
+        self.assertEqual('Invalid CgSnapshot: invalid cgsnapshot',
+                         res_dict['badRequest']['message'])
+
+    @mock.patch.object(consistencygroupAPI.API, 'create_cgsnapshot',
+                       side_effect=exception.CgSnapshotNotFound(
+                           cgsnapshot_id='invalid_id'))
+    def test_create_with_cgsnapshot_not_found(self, mock_create_cgsnapshot):
+        consistencygroup_id = utils.create_consistencygroup(self.context)['id']
+        utils.create_volume(
+            self.context,
+            consistencygroup_id=consistencygroup_id)['id']
+
+        body = {"cgsnapshot": {"name": "cg1",
+                               "description":
+                               "CG Snapshot 1",
+                               "consistencygroup_id": consistencygroup_id}}
+
+        req = webob.Request.blank('/v2/fake/cgsnapshots')
+        req.method = 'POST'
+        req.headers['Content-Type'] = 'application/json'
+        req.body = json.dumps(body)
+        res = req.get_response(fakes.wsgi_app())
+        res_dict = json.loads(res.body)
+
+        self.assertEqual(404, res.status_int)
+        self.assertEqual(404, res_dict['itemNotFound']['code'])
+        self.assertEqual('CgSnapshot invalid_id could not be found.',
+                         res_dict['itemNotFound']['message'])
+
     def test_delete_cgsnapshot_available(self):
         consistencygroup_id = utils.create_consistencygroup(self.context)['id']
         volume_id = utils.create_volume(