]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Port API admin action tests to Python 3
authorVictor Stinner <vstinner@redhat.com>
Wed, 7 Oct 2015 21:44:46 +0000 (23:44 +0200)
committerVictor Stinner <vstinner@redhat.com>
Wed, 14 Oct 2015 20:17:26 +0000 (22:17 +0200)
* Replace ast.literal_eval(response.body) with response.json
* Use oslo_serialization.jsonutils.dump_as_bytes() to encode the
  response to JSON as bytes for the HTTP body.
* tests-py3.txt: add cinder.tests.unit.api.contrib.test_admin_actions

Partial-Implements: blueprint cinder-python3
Change-Id: Ib30c701b9dbbcc774dfc5f19a7174ab318006206

cinder/tests/unit/api/contrib/test_admin_actions.py
tests-py3.txt

index c6f0c802424ab237344062a7026bbbf31c66333d..5128cacea76ed25411860d4d4397afbaf2b81c90 100644 (file)
@@ -10,8 +10,6 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
-import ast
-
 import fixtures
 import mock
 from oslo_concurrency import lockutils
@@ -77,8 +75,7 @@ class AdminActionsTest(test.TestCase):
         req = webob.Request.blank('/v2/fake/volumes/%s/action' % volume['id'])
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
-        req.body = \
-            jsonutils.dumps({'os-reset_status': updated_status})
+        req.body = jsonutils.dump_as_bytes({'os-reset_status': updated_status})
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
         return resp
@@ -88,8 +85,7 @@ class AdminActionsTest(test.TestCase):
                                   snapshot.id)
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
-        req.body = \
-            jsonutils.dumps({'os-reset_status': updated_status})
+        req.body = jsonutils.dump_as_bytes({'os-reset_status': updated_status})
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
         return resp
@@ -98,8 +94,7 @@ class AdminActionsTest(test.TestCase):
         req = webob.Request.blank('/v2/fake/backups/%s/action' % backup['id'])
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
-        req.body = \
-            jsonutils.dumps({'os-reset_status': updated_status})
+        req.body = jsonutils.dump_as_bytes({'os-reset_status': updated_status})
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
         return resp
@@ -297,8 +292,8 @@ class AdminActionsTest(test.TestCase):
                                   'missing-volume-id')
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
-        req.body = jsonutils.dumps({'os-reset_status': {'status':
-                                                        'available'}})
+        body = {'os-reset_status': {'status': 'available'}}
+        req.body = jsonutils.dump_as_bytes(body)
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
         self.assertEqual(404, resp.status_int)
@@ -384,7 +379,7 @@ class AdminActionsTest(test.TestCase):
         req = webob.Request.blank('/v2/fake/volumes/%s/action' % volume['id'])
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
-        req.body = jsonutils.dumps({'os-force_delete': {}})
+        req.body = jsonutils.dump_as_bytes({'os-force_delete': {}})
         # attach admin context to request
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
@@ -411,7 +406,7 @@ class AdminActionsTest(test.TestCase):
         req = webob.Request.blank(path)
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
-        req.body = jsonutils.dumps({'os-force_delete': {}})
+        req.body = jsonutils.dump_as_bytes({'os-force_delete': {}})
         # attach admin context to request
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
@@ -451,9 +446,9 @@ class AdminActionsTest(test.TestCase):
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
         # request status of 'error'
-        req.body = jsonutils.dumps({'os-force_detach':
-                                   {'attachment_id': attachment['id'],
-                                    'connector': connector}})
+        body = {'os-force_detach': {'attachment_id': attachment['id'],
+                                    'connector': connector}}
+        req.body = jsonutils.dump_as_bytes(body)
         # attach admin context to request
         req.environ['cinder.context'] = ctx
         # make request
@@ -509,9 +504,9 @@ class AdminActionsTest(test.TestCase):
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
         # request status of 'error'
-        req.body = jsonutils.dumps({'os-force_detach':
-                                    {'attachment_id': attachment['id'],
-                                     'connector': connector}})
+        body = {'os-force_detach': {'attachment_id': attachment['id'],
+                                    'connector': connector}}
+        req.body = jsonutils.dump_as_bytes(body)
         # attach admin context to request
         req.environ['cinder.context'] = ctx
         # make request
@@ -569,8 +564,8 @@ class AdminActionsTest(test.TestCase):
                                       volume['id'])
             req.method = 'POST'
             req.headers['content-type'] = 'application/json'
-            req.body = jsonutils.dumps({'os-force_detach':
-                                        {'attachment_id': 'fake'}})
+            body = {'os-force_detach': {'attachment_id': 'fake'}}
+            req.body = jsonutils.dump_as_bytes(body)
             # attach admin context to request
             req.environ['cinder.context'] = ctx
             # make request
@@ -586,8 +581,8 @@ class AdminActionsTest(test.TestCase):
                                       volume['id'])
             req.method = 'POST'
             req.headers['content-type'] = 'application/json'
-            req.body = jsonutils.dumps({'os-force_detach':
-                                        {'attachment_id': 'fake'}})
+            body = {'os-force_detach': {'attachment_id': 'fake'}}
+            req.body = jsonutils.dump_as_bytes(body)
             # attach admin context to request
             req.environ['cinder.context'] = ctx
             # make request
@@ -604,9 +599,9 @@ class AdminActionsTest(test.TestCase):
                                       volume['id'])
             req.method = 'POST'
             req.headers['content-type'] = 'application/json'
-            req.body = jsonutils.dumps({'os-force_detach':
-                                       {'attachment_id': 'fake',
-                                        'connector': connector}})
+            body = {'os-force_detach': {'attachment_id': 'fake',
+                                        'connector': connector}}
+            req.body = jsonutils.dump_as_bytes(body)
 
             # attach admin context to request
             req.environ['cinder.context'] = ctx
@@ -656,9 +651,9 @@ class AdminActionsTest(test.TestCase):
                                       volume['id'])
             req.method = 'POST'
             req.headers['content-type'] = 'application/json'
-            req.body = jsonutils.dumps({'os-force_detach':
-                                       {'attachment_id': 'fake',
-                                        'connector': connector}})
+            body = {'os-force_detach': {'attachment_id': 'fake',
+                                        'connector': connector}}
+            req.body = jsonutils.dump_as_bytes(body)
             # attach admin context to request
             req.environ['cinder.context'] = ctx
             # make request
@@ -820,7 +815,7 @@ class AdminActionsTest(test.TestCase):
         req.headers['content-type'] = 'application/json'
         body = {'os-migrate_volume': {'host': host,
                                       'force_host_copy': force_host_copy}}
-        req.body = jsonutils.dumps(body)
+        req.body = jsonutils.dump_as_bytes(body)
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
         # verify status
@@ -868,7 +863,7 @@ class AdminActionsTest(test.TestCase):
         req.headers['content-type'] = 'application/json'
         body = {'os-migrate_volume': {'host': host,
                                       'force_host_copy': False}}
-        req.body = jsonutils.dumps(body)
+        req.body = jsonutils.dump_as_bytes(body)
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
         # verify status
@@ -922,12 +917,13 @@ class AdminActionsTest(test.TestCase):
         req.headers['content-type'] = 'application/json'
         body = {'new_volume': new_volume['id'], 'error': error}
         if no_body:
-            req.body = jsonutils.dumps({'': body})
+            body = {'': body}
         else:
-            req.body = jsonutils.dumps({'os-migrate_volume_completion': body})
+            body = {'os-migrate_volume_completion': body}
+        req.body = jsonutils.dump_as_bytes(body)
         req.environ['cinder.context'] = ctx
         resp = req.get_response(app())
-        resp_dict = ast.literal_eval(resp.body)
+        resp_dict = resp.json
         # verify status
         self.assertEqual(expected_status, resp.status_int)
         if expected_id:
@@ -1024,7 +1020,7 @@ class AdminActionsTest(test.TestCase):
         req = webob.Request.blank('/v2/fake/backups/%s/action' % id)
         req.method = 'POST'
         req.headers['Content-Type'] = 'application/json'
-        req.body = jsonutils.dumps({'os-force_delete': {}})
+        req.body = jsonutils.dump_as_bytes({'os-force_delete': {}})
         req.environ['cinder.context'] = ctx
         res = req.get_response(app())
 
@@ -1062,7 +1058,7 @@ class AdminActionsTest(test.TestCase):
         req = webob.Request.blank('/v2/fake/backups/%s/action' % id)
         req.method = 'POST'
         req.headers['Content-Type'] = 'application/json'
-        req.body = jsonutils.dumps({'os-force_delete': {}})
+        req.body = jsonutils.dump_as_bytes({'os-force_delete': {}})
         req.environ['cinder.context'] = ctx
         res = req.get_response(app())
         self.assertEqual(405, res.status_int)
index d3c57beccebc26da159f84c93d7f57ec2c741725..b4de72841016ce00f0070bc85361fb61a5e72ba6 100644 (file)
@@ -1,3 +1,4 @@
+cinder.tests.unit.api.contrib.test_admin_actions
 cinder.tests.unit.api.contrib.test_availability_zones
 cinder.tests.unit.api.contrib.test_capabilities
 cinder.tests.unit.api.contrib.test_extended_snapshot_attributes