From ce018c91b160ac7967ca40a3d055ce9a8bccf7ec Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 9 Feb 2016 18:38:11 +0100 Subject: [PATCH] py3: Fix usage of JSON in API contrib Replace json.dumps() and jsonutils.dumps() with jsonutils.dump_as_bytes() to get bytes on Python 3. The result is used as HTTP body, and the body type must be bytes. Replace also json.loads() with jsonutils.loads(). Partial-Implements: blueprint cinder-python3 Change-Id: I3d3a18f2013b42dc2d8d7b90f32f50756513d668 --- cinder/tests/unit/api/contrib/test_backups.py | 202 +++++++++--------- .../api/contrib/test_consistencygroups.py | 132 ++++++------ .../unit/api/contrib/test_volume_actions.py | 51 +++-- .../api/contrib/test_volume_image_metadata.py | 31 ++- .../api/contrib/test_volume_replication.py | 5 +- .../unit/api/contrib/test_volume_transfer.py | 64 +++--- .../contrib/test_volume_type_encryption.py | 72 +++---- 7 files changed, 277 insertions(+), 280 deletions(-) diff --git a/cinder/tests/unit/api/contrib/test_backups.py b/cinder/tests/unit/api/contrib/test_backups.py index d12017e76..3016e4b5a 100644 --- a/cinder/tests/unit/api/contrib/test_backups.py +++ b/cinder/tests/unit/api/contrib/test_backups.py @@ -17,11 +17,11 @@ Tests for Backup code. """ -import json from xml.dom import minidom import ddt import mock +from oslo_serialization import jsonutils from oslo_utils import timeutils import six import webob @@ -115,7 +115,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual('az1', res_dict['backup']['availability_zone']) @@ -163,7 +163,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -179,7 +179,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(3, len(res_dict['backups'][0])) @@ -234,7 +234,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(2, len(res_dict['backups'])) @@ -258,7 +258,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(2, len(res_dict['backups'])) @@ -283,7 +283,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(1, len(res_dict['backups'])) @@ -305,7 +305,7 @@ class BackupsAPITestCase(test.TestCase): req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(NUM_ELEMENTS_IN_BACKUP, len(res_dict['backups'][0])) @@ -369,7 +369,7 @@ class BackupsAPITestCase(test.TestCase): req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(1, len(res_dict['backups'])) self.assertEqual(200, res.status_int) @@ -380,7 +380,7 @@ class BackupsAPITestCase(test.TestCase): req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(1, len(res_dict['backups'])) self.assertEqual(200, res.status_int) @@ -391,7 +391,7 @@ class BackupsAPITestCase(test.TestCase): req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(1, len(res_dict['backups'])) self.assertEqual(200, res.status_int) @@ -495,7 +495,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(2, len(res_dict['backups'])) @@ -518,7 +518,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(2, len(res_dict['backups'])) @@ -541,7 +541,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(1, len(res_dict['backups'])) @@ -573,10 +573,10 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['backup']) @@ -605,10 +605,10 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -637,10 +637,10 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['backup']) @@ -671,10 +671,10 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['backup']) self.assertTrue(_mock_service_get_all_by_topic.called) @@ -744,9 +744,9 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['backup']) @@ -779,9 +779,9 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Invalid backup: The parent backup must be ' @@ -794,12 +794,12 @@ class BackupsAPITestCase(test.TestCase): def test_create_backup_with_no_body(self): # omit body from the request req = webob.Request.blank('/v2/fake/backups') - req.body = json.dumps(None) + req.body = jsonutils.dump_as_bytes(None) req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -817,9 +817,9 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -837,9 +837,9 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -860,9 +860,9 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -886,10 +886,10 @@ class BackupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(500, res.status_int) self.assertEqual(500, res_dict['computeFault']['code']) self.assertEqual('Service cinder-backup could not be found.', @@ -919,9 +919,9 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Invalid backup: No backups available to do ' @@ -1067,7 +1067,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'DELETE' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -1081,7 +1081,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'DELETE' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1109,7 +1109,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'DELETE' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Invalid backup: Incremental backups ' @@ -1149,9 +1149,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertEqual(backup_id, res_dict['restore']['backup_id']) @@ -1188,12 +1188,12 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/%s/restore' % backup_id) - req.body = json.dumps(None) + req.body = jsonutils.dump_as_bytes(None) req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1211,10 +1211,10 @@ class BackupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1241,9 +1241,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertEqual(backup_id, res_dict['restore']['backup_id']) @@ -1269,9 +1269,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) description = 'auto-created_from_restore_from_backup' # Assert that we have indeed passed on the name parameter @@ -1296,9 +1296,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertEqual(backup_id, res_dict['restore']['backup_id']) @@ -1325,9 +1325,9 @@ class BackupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1345,9 +1345,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1368,9 +1368,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1388,9 +1388,9 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/9999/restore') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -1407,9 +1407,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -1438,9 +1438,9 @@ class BackupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(413, res.status_int) self.assertEqual(413, res_dict['overLimit']['code']) @@ -1466,9 +1466,9 @@ class BackupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(413, res.status_int) self.assertEqual(413, res_dict['overLimit']['code']) @@ -1488,9 +1488,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1516,9 +1516,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertEqual(backup_id, res_dict['restore']['backup_id']) @@ -1542,9 +1542,9 @@ class BackupsAPITestCase(test.TestCase): backup_id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertEqual(backup_id, res_dict['restore']['backup_id']) @@ -1587,7 +1587,7 @@ class BackupsAPITestCase(test.TestCase): req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) # verify that request is successful self.assertEqual(200, res.status_int) self.assertEqual(backup_service, @@ -1633,7 +1633,7 @@ class BackupsAPITestCase(test.TestCase): req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) self.assertEqual('Backup %s could not be found.' % backup_id, @@ -1649,7 +1649,7 @@ class BackupsAPITestCase(test.TestCase): req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Invalid backup: Backup status must be available ' @@ -1671,7 +1671,7 @@ class BackupsAPITestCase(test.TestCase): req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1685,7 +1685,7 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_service': backup_service, 'backup_url': backup_url}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' @@ -1712,12 +1712,12 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_service': backup_service, 'backup_url': backup_url}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) # verify that request is successful self.assertEqual(201, res.status_int) @@ -1755,12 +1755,12 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_service': backup_service, 'backup_url': backup_url}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) # verify that request is successful self.assertEqual(201, res.status_int) @@ -1831,12 +1831,12 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_service': backup_service, 'backup_url': backup_url}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(500, res.status_int) self.assertEqual(500, res_dict['computeFault']['code']) self.assertEqual('Service %s could not be found.' @@ -1852,12 +1852,12 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_service': backup_service, 'backup_url': backup_url}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual("Invalid input received: Can't parse backup record.", @@ -1875,12 +1875,12 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_service': backup_service, 'backup_url': backup_url}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Invalid backup: Backup already exists in database.', @@ -1905,12 +1905,12 @@ class BackupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_service': backup_service, 'backup_url': backup_url}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(500, res.status_int) self.assertEqual(500, res_dict['computeFault']['code']) self.assertEqual('Service %s could not be found.' % backup_service, @@ -1926,11 +1926,11 @@ class BackupsAPITestCase(test.TestCase): # test with no backup_service req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_url': backup_url}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Incorrect request body format.', @@ -1939,12 +1939,12 @@ class BackupsAPITestCase(test.TestCase): # test with no backup_url req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {'backup_service': backup_service}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Incorrect request body format.', @@ -1953,12 +1953,12 @@ class BackupsAPITestCase(test.TestCase): # test with no backup_url and backup_url req = webob.Request.blank('/v2/fake/backups/import_record') body = {'backup-record': {}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Incorrect request body format.', @@ -1968,12 +1968,12 @@ class BackupsAPITestCase(test.TestCase): ctx = context.RequestContext('admin', 'fake', is_admin=True) req = webob.Request.blank('/v2/fake/backups/import_record') - req.body = json.dumps(None) + req.body = jsonutils.dump_as_bytes(None) req.method = 'POST' req.headers['content-type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=ctx)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) # verify that request is successful self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -2016,7 +2016,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertTrue(res_dict['backup']['is_incremental']) @@ -2028,7 +2028,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertFalse(res_dict['backup']['is_incremental']) @@ -2040,7 +2040,7 @@ class BackupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertTrue(res_dict['backup']['is_incremental']) diff --git a/cinder/tests/unit/api/contrib/test_consistencygroups.py b/cinder/tests/unit/api/contrib/test_consistencygroups.py index d63d758bc..658d851ab 100644 --- a/cinder/tests/unit/api/contrib/test_consistencygroups.py +++ b/cinder/tests/unit/api/contrib/test_consistencygroups.py @@ -17,11 +17,11 @@ Tests for consistency group code. """ -import ddt -import json from xml.dom import minidom +import ddt import mock +from oslo_serialization import jsonutils import webob import cinder.consistencygroup @@ -78,7 +78,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual('az1', @@ -114,7 +114,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -128,7 +128,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual('az1', @@ -152,7 +152,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(consistencygroup3.id, @@ -210,7 +210,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(1, len(res_dict['consistencygroups'])) @@ -236,7 +236,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(2, len(res_dict['consistencygroups'])) @@ -260,7 +260,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(2, len(res_dict['consistencygroups'])) @@ -288,7 +288,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt)) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(1, len(res_dict['consistencygroups'])) @@ -310,7 +310,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expect_result = [consistencygroup1.id, consistencygroup2.id, consistencygroup3.id] expect_result.sort() @@ -338,7 +338,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual('az1', @@ -468,9 +468,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['consistencygroup']) @@ -484,12 +484,12 @@ class ConsistencyGroupsAPITestCase(test.TestCase): def test_create_consistencygroup_with_no_body(self): # omit body from the request req = webob.Request.blank('/v2/fake/consistencygroups') - req.body = json.dumps(None) + req.body = jsonutils.dump_as_bytes(None) req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -505,7 +505,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": True}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) consistencygroup = objects.ConsistencyGroup.get_by_id( @@ -520,9 +520,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/9999/delete') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(None) + req.body = jsonutils.dump_as_bytes(None) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -537,9 +537,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": False}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -558,7 +558,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": True}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(202, res.status_int) @@ -604,7 +604,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"invalid_request_element": {"force": False}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) @@ -617,7 +617,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": "abcd"}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) @@ -630,7 +630,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": ""}} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) @@ -643,9 +643,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -697,7 +697,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "description": description, "add_volumes": add_volumes, "remove_volumes": remove_volumes, }} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) consistencygroup = objects.ConsistencyGroup.get_by_id( @@ -721,9 +721,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "description": None, "add_volumes": "fake-volume-uuid", "remove_volumes": None, }} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -747,9 +747,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "description": "new description", "add_volumes": None, "remove_volumes": "fake-volume-uuid", }} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -773,9 +773,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "description": "", "add_volumes": None, "remove_volumes": None, }} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -800,9 +800,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "description": "", "add_volumes": add_volumes, "remove_volumes": None, }} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -834,9 +834,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "description": "", "add_volumes": add_volumes, "remove_volumes": None, }} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -866,9 +866,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "description": "", "add_volumes": add_volumes, "remove_volumes": None, }} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -888,9 +888,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "description": None, "add_volumes": None, "remove_volumes": None, }} - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -926,9 +926,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['consistencygroup']) @@ -960,9 +960,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['consistencygroup']) @@ -1000,9 +1000,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1021,9 +1021,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1038,9 +1038,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1067,9 +1067,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1099,9 +1099,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1122,9 +1122,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1146,9 +1146,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -1166,9 +1166,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -1199,9 +1199,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -1231,9 +1231,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) diff --git a/cinder/tests/unit/api/contrib/test_volume_actions.py b/cinder/tests/unit/api/contrib/test_volume_actions.py index 99abddf86..702351a7a 100644 --- a/cinder/tests/unit/api/contrib/test_volume_actions.py +++ b/cinder/tests/unit/api/contrib/test_volume_actions.py @@ -14,7 +14,6 @@ import datetime import iso8601 -import json import uuid import mock @@ -82,7 +81,7 @@ class VolumeActionsTest(test.TestCase): req = webob.Request.blank('/v2/fake/volumes/%s/action' % self.UUID) req.method = 'POST' - req.body = jsonutils.dumps({_action: None}) + req.body = jsonutils.dump_as_bytes({_action: None}) req.content_type = 'application/json' res = req.get_response(app) self.assertEqual(202, res.status_int) @@ -94,7 +93,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-initialize_connection': {'connector': 'fake'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -107,7 +106,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-initialize_connection': {}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -121,7 +120,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-initialize_connection': {'connector': 'fake'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -134,7 +133,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-terminate_connection': {'connector': 'fake'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -147,7 +146,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-terminate_connection': {}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -161,7 +160,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-terminate_connection': {'connector': 'fake'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -173,7 +172,7 @@ class VolumeActionsTest(test.TestCase): 'mode': 'rw'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -185,7 +184,7 @@ class VolumeActionsTest(test.TestCase): req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" req.headers["content-type"] = "application/json" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(202, res.status_int) @@ -195,7 +194,7 @@ class VolumeActionsTest(test.TestCase): 'mountpoint': '/dev/vdc'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -240,7 +239,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-detach': {'attachment_id': 'fakeuuid'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -283,7 +282,7 @@ class VolumeActionsTest(test.TestCase): req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" req.headers["content-type"] = "application/json" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) @@ -294,7 +293,7 @@ class VolumeActionsTest(test.TestCase): req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" req.headers["content-type"] = "application/json" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) body = {'os-attach': {'host_name': 'fake_host', @@ -303,7 +302,7 @@ class VolumeActionsTest(test.TestCase): req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" req.headers["content-type"] = "application/json" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) @@ -316,7 +315,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-begin_detaching': {'fake': 'fake'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -331,7 +330,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-roll_detaching': {'fake': 'fake'}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -346,7 +345,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-extend': {'new_size': 5}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -362,7 +361,7 @@ class VolumeActionsTest(test.TestCase): body = {'os-extend': {'new_size': 5}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -380,7 +379,7 @@ class VolumeActionsTest(test.TestCase): body = {"os-update_readonly_flag": {}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(return_code, res.status_int) @@ -403,7 +402,7 @@ class VolumeActionsTest(test.TestCase): body = {"os-set_bootable": {}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(return_code, res.status_int) @@ -451,7 +450,7 @@ class VolumeRetypeActionsTest(VolumeActionsTest): req.method = 'POST' req.headers['content-type'] = 'application/json' retype_body = {'new_type': new_type, 'migration_policy': 'never'} - req.body = jsonutils.dumps({'os-retype': retype_body}) + req.body = jsonutils.dump_as_bytes({'os-retype': retype_body}) res = req.get_response(fakes.wsgi_app()) self.assertEqual(expected_status, res.status_int) @@ -470,7 +469,7 @@ class VolumeRetypeActionsTest(VolumeActionsTest): req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = 'POST' req.headers['content-type'] = 'application/json' - req.body = jsonutils.dumps({'os-retype': None}) + req.body = jsonutils.dump_as_bytes({'os-retype': None}) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) @@ -480,7 +479,7 @@ class VolumeRetypeActionsTest(VolumeActionsTest): req.method = 'POST' req.headers['content-type'] = 'application/json' retype_body = {'new_type': 'foo', 'migration_policy': 'invalid'} - req.body = jsonutils.dumps({'os-retype': retype_body}) + req.body = jsonutils.dump_as_bytes({'os-retype': retype_body}) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) @@ -716,7 +715,7 @@ class VolumeImageActionsTest(test.TestCase): req = webob.Request.blank('/v2/tenant1/volumes/%s/action' % id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) @@ -730,7 +729,7 @@ class VolumeImageActionsTest(test.TestCase): req = webob.Request.blank('/v2/tenant1/volumes/%s/action' % id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(400, res.status_int) diff --git a/cinder/tests/unit/api/contrib/test_volume_image_metadata.py b/cinder/tests/unit/api/contrib/test_volume_image_metadata.py index 505150b04..207f2ac2d 100644 --- a/cinder/tests/unit/api/contrib/test_volume_image_metadata.py +++ b/cinder/tests/unit/api/contrib/test_volume_image_metadata.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import json import uuid from xml.dom import minidom @@ -117,12 +116,12 @@ class VolumeImageMetadataTest(test.TestCase): return res def _get_image_metadata(self, body): - return json.loads(body)['volume']['volume_image_metadata'] + return jsonutils.loads(body)['volume']['volume_image_metadata'] def _get_image_metadata_list(self, body): return [ volume['volume_image_metadata'] - for volume in json.loads(body)['volumes'] + for volume in jsonutils.loads(body)['volumes'] ] def _create_volume_and_glance_metadata(self): @@ -172,13 +171,13 @@ class VolumeImageMetadataTest(test.TestCase): body = {"os-set_image_metadata": {"metadata": fake_image_metadata}} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = "POST" - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(200, res.status_int) self.assertEqual(fake_image_metadata, - json.loads(res.body)["metadata"]) + jsonutils.loads(res.body)["metadata"]) def test_create_with_keys_case_insensitive(self): # If the keys in uppercase_and_lowercase, should return the one @@ -201,13 +200,13 @@ class VolumeImageMetadataTest(test.TestCase): req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = 'POST' - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(200, res.status_int) self.assertEqual(fake_image_metadata, - json.loads(res.body)["metadata"]) + jsonutils.loads(res.body)["metadata"]) def test_create_empty_body(self): req = fakes.HTTPRequest.blank('/v2/fake/volumes/1/action') @@ -226,7 +225,7 @@ class VolumeImageMetadataTest(test.TestCase): body = {"os-set_image_metadata": { "metadata": {"image_name": "fake"}} } - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) self.assertRaises(webob.exc.HTTPNotFound, self.controller.create, req, 1, body) @@ -242,7 +241,7 @@ class VolumeImageMetadataTest(test.TestCase): } # Test for long key - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, self.controller.create, req, 1, data) @@ -250,7 +249,7 @@ class VolumeImageMetadataTest(test.TestCase): data = {"os-set_image_metadata": { "metadata": {"key": "v" * 260}} } - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, self.controller.create, req, 1, data) @@ -258,7 +257,7 @@ class VolumeImageMetadataTest(test.TestCase): data = {"os-set_image_metadata": { "metadata": {"": "value1"}} } - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, req, 1, data) @@ -271,7 +270,7 @@ class VolumeImageMetadataTest(test.TestCase): } req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = 'POST' - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) @@ -283,7 +282,7 @@ class VolumeImageMetadataTest(test.TestCase): } req = fakes.HTTPRequest.blank('/v2/fake/volumes/1/action') req.method = 'POST' - req.body = jsonutils.dumps(data) + req.body = jsonutils.dump_as_bytes(data) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPNotFound, @@ -298,7 +297,7 @@ class VolumeImageMetadataTest(test.TestCase): } req = fakes.HTTPRequest.blank('/v2/fake/volumes/1/action') req.method = 'POST' - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPNotFound, @@ -308,13 +307,13 @@ class VolumeImageMetadataTest(test.TestCase): body = {"os-show_image_metadata": None} req = webob.Request.blank('/v2/fake/volumes/1/action') req.method = 'POST' - req.body = jsonutils.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.headers["content-type"] = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(200, res.status_int) self.assertEqual(fake_image_metadata, - json.loads(res.body)["metadata"]) + jsonutils.loads(res.body)["metadata"]) class ImageMetadataXMLDeserializer(common.MetadataXMLDeserializer): diff --git a/cinder/tests/unit/api/contrib/test_volume_replication.py b/cinder/tests/unit/api/contrib/test_volume_replication.py index f71874e9b..20e688461 100644 --- a/cinder/tests/unit/api/contrib/test_volume_replication.py +++ b/cinder/tests/unit/api/contrib/test_volume_replication.py @@ -16,10 +16,9 @@ Tests for volume replication API code. """ -import json - import mock from oslo_config import cfg +from oslo_serialization import jsonutils import webob from cinder import context @@ -60,7 +59,7 @@ class VolumeReplicationAPITestCase(test.TestCase): else: body = {'os-%s-replica' % operation: ''} req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) req.environ['cinder.context'] = context.RequestContext('admin', 'fake', True) diff --git a/cinder/tests/unit/api/contrib/test_volume_transfer.py b/cinder/tests/unit/api/contrib/test_volume_transfer.py index 999c157be..3407c28f8 100644 --- a/cinder/tests/unit/api/contrib/test_volume_transfer.py +++ b/cinder/tests/unit/api/contrib/test_volume_transfer.py @@ -17,10 +17,10 @@ Tests for volume transfer code. """ -import json -import mock from xml.dom import minidom +import mock +from oslo_serialization import jsonutils import webob from cinder.api.contrib import volume_transfer @@ -74,7 +74,7 @@ class VolumeTransferAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual('test_transfer', res_dict['transfer']['name']) self.assertEqual(transfer['id'], res_dict['transfer']['id']) @@ -106,7 +106,7 @@ class VolumeTransferAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -123,7 +123,7 @@ class VolumeTransferAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(4, len(res_dict['transfers'][0])) @@ -175,7 +175,7 @@ class VolumeTransferAPITestCase(test.TestCase): req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_int) self.assertEqual(5, len(res_dict['transfers'][0])) @@ -264,10 +264,10 @@ class VolumeTransferAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/os-volume-transfer') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['transfer']) @@ -307,12 +307,12 @@ class VolumeTransferAPITestCase(test.TestCase): def test_create_transfer_with_no_body(self): req = webob.Request.blank('/v2/fake/os-volume-transfer') - req.body = json.dumps(None) + req.body = jsonutils.dump_as_bytes(None) req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -325,9 +325,9 @@ class VolumeTransferAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/os-volume-transfer') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -341,9 +341,9 @@ class VolumeTransferAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/os-volume-transfer') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -357,9 +357,9 @@ class VolumeTransferAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/os-volume-transfer') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -385,7 +385,7 @@ class VolumeTransferAPITestCase(test.TestCase): req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -401,7 +401,7 @@ class VolumeTransferAPITestCase(test.TestCase): req.method = 'DELETE' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -419,9 +419,9 @@ class VolumeTransferAPITestCase(test.TestCase): transfer['id']) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(202, res.status_int) self.assertEqual(transfer['id'], res_dict['transfer']['id']) @@ -459,12 +459,12 @@ class VolumeTransferAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/os-volume-transfer/%s/accept' % transfer['id']) - req.body = json.dumps(None) + req.body = jsonutils.dump_as_bytes(None) req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -483,10 +483,10 @@ class VolumeTransferAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -503,9 +503,9 @@ class VolumeTransferAPITestCase(test.TestCase): transfer['id']) req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) @@ -525,9 +525,9 @@ class VolumeTransferAPITestCase(test.TestCase): req = webob.Request.blank('/v2/fake/os-volume-transfer/1/accept') req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) @@ -560,9 +560,9 @@ class VolumeTransferAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(413, res.status_int) self.assertEqual(413, res_dict['overLimit']['code']) @@ -592,9 +592,9 @@ class VolumeTransferAPITestCase(test.TestCase): req.method = 'POST' req.headers['Content-Type'] = 'application/json' - req.body = json.dumps(body) + req.body = jsonutils.dump_as_bytes(body) res = req.get_response(fakes.wsgi_app()) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(413, res.status_int) self.assertEqual(413, res_dict['overLimit']['code']) diff --git a/cinder/tests/unit/api/contrib/test_volume_type_encryption.py b/cinder/tests/unit/api/contrib/test_volume_type_encryption.py index 3aeaec9a7..369589fe5 100644 --- a/cinder/tests/unit/api/contrib/test_volume_type_encryption.py +++ b/cinder/tests/unit/api/contrib/test_volume_type_encryption.py @@ -13,9 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. -import json import mock +from oslo_serialization import jsonutils import webob from cinder.api.openstack import wsgi @@ -74,7 +74,7 @@ class VolumeTypeEncryptionTest(test.TestCase): db.volume_type_create(context.get_admin_context(), volume_type) return self._get_response(volume_type, req_method='POST', - req_body=json.dumps(body), + req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') def test_index(self): @@ -86,7 +86,7 @@ class VolumeTypeEncryptionTest(test.TestCase): res = self._get_response(volume_type) self.assertEqual(200, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expected = stub_volume_type_encryption() self.assertEqual(expected, res_dict) @@ -97,7 +97,7 @@ class VolumeTypeEncryptionTest(test.TestCase): volume_type = self._default_volume_type res = self._get_response(volume_type) self.assertEqual(404, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expected = { 'itemNotFound': { @@ -113,7 +113,7 @@ class VolumeTypeEncryptionTest(test.TestCase): self._create_type_and_encryption(volume_type) res = self._get_response(volume_type, url='/v2/fake/types/%s/encryption/key_size') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_code) self.assertEqual(256, res_dict['key_size']) @@ -126,7 +126,7 @@ class VolumeTypeEncryptionTest(test.TestCase): res = self._get_response(volume_type, url='/v2/fake/types/%s/encryption/provider') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_code) self.assertEqual('fake_provider', res_dict['provider']) @@ -138,7 +138,7 @@ class VolumeTypeEncryptionTest(test.TestCase): res = self._get_response(volume_type, url='/v2/fake/types/%s/encryption/fake') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(404, res.status_code) expected = { @@ -162,7 +162,7 @@ class VolumeTypeEncryptionTest(test.TestCase): self.assertEqual(0, len(self.notifier.notifications)) res = self._get_response(volume_type) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(200, res.status_code) # Confirm that volume type has no encryption information # before create. @@ -171,9 +171,9 @@ class VolumeTypeEncryptionTest(test.TestCase): # Create encryption specs for the volume type # with the defined body. res = self._get_response(volume_type, req_method='POST', - req_body=json.dumps(body), + req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(1, len(self.notifier.notifications)) @@ -230,9 +230,9 @@ class VolumeTypeEncryptionTest(test.TestCase): # Attempt to create encryption without first creating type res = self._get_response(volume_type, req_method='POST', - req_body=json.dumps(body), + req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(0, len(self.notifier.notifications)) self.assertEqual(404, res.status_code) @@ -254,9 +254,9 @@ class VolumeTypeEncryptionTest(test.TestCase): # Try to create encryption specs for a volume type # that already has them. res = self._get_response(volume_type, req_method='POST', - req_body=json.dumps(body), + req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expected = { 'badRequest': { @@ -290,9 +290,9 @@ class VolumeTypeEncryptionTest(test.TestCase): # Try to create encryption specs for a volume type # with a volume. res = self._get_response(volume_type, req_method='POST', - req_body=json.dumps(body), + req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expected = { 'badRequest': { @@ -311,10 +311,10 @@ class VolumeTypeEncryptionTest(test.TestCase): volume_type = self._default_volume_type db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type, req_method='POST', - req_body=json.dumps(body), + req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expected = { 'badRequest': { @@ -367,7 +367,7 @@ class VolumeTypeEncryptionTest(test.TestCase): # Test that before create, there's nothing with a get res = self._get_response(volume_type) self.assertEqual(200, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual({}, res_dict) body = {"encryption": {'cipher': 'cipher', @@ -378,15 +378,15 @@ class VolumeTypeEncryptionTest(test.TestCase): # Create, and test that get returns something res = self._get_response(volume_type, req_method='POST', - req_body=json.dumps(body), + req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) res = self._get_response(volume_type, req_method='GET', req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(volume_type['id'], res_dict['volume_type_id']) # Delete, and test that get returns nothing @@ -399,7 +399,7 @@ class VolumeTypeEncryptionTest(test.TestCase): req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual({}, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type['id']) @@ -417,13 +417,13 @@ class VolumeTypeEncryptionTest(test.TestCase): # Create encryption with volume type, and test with GET res = self._get_response(volume_type, req_method='POST', - req_body=json.dumps(body), + req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') res = self._get_response(volume_type, req_method='GET', req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(volume_type['id'], res_dict['volume_type_id']) # Create volumes with the volume type @@ -450,7 +450,7 @@ class VolumeTypeEncryptionTest(test.TestCase): req_headers='application/json', url='/v2/fake/types/%s/encryption/provider') self.assertEqual(400, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expected = { 'badRequest': { 'code': 400, @@ -474,7 +474,7 @@ class VolumeTypeEncryptionTest(test.TestCase): req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual({}, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type['id']) @@ -497,7 +497,7 @@ class VolumeTypeEncryptionTest(test.TestCase): "code": 404 } } - self.assertEqual(expected, json.loads(res.body)) + self.assertEqual(expected, jsonutils.loads(res.body)) db.volume_type_destroy(context.get_admin_context(), volume_type['id']) @mock.patch('cinder.api.openstack.wsgi.Controller.validate_integer') @@ -519,17 +519,17 @@ class VolumeTypeEncryptionTest(test.TestCase): res = self.\ _get_response(volume_type, req_method='PUT', - req_body=json.dumps(update_body), + req_body=jsonutils.dump_as_bytes(update_body), req_headers='application/json', url='/v2/fake/types/%s/encryption/fake_type_id') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(512, res_dict['encryption']['key_size']) self.assertEqual('fake_provider2', res_dict['encryption']['provider']) # Get Encryption Specs res = self._get_response(volume_type) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) # Confirm Encryption Specs self.assertEqual(512, res_dict['key_size']) @@ -547,11 +547,11 @@ class VolumeTypeEncryptionTest(test.TestCase): # Update Encryption res = self.\ _get_response(volume_type, req_method='PUT', - req_body=json.dumps(update_body), + req_body=jsonutils.dump_as_bytes(update_body), req_headers='application/json', url='/v2/fake/types/%s/encryption/fake_type_id') - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expected = { 'badRequest': { @@ -608,7 +608,7 @@ class VolumeTypeEncryptionTest(test.TestCase): # Get the Encryption res = self._get_response(volume_type) self.assertEqual(200, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) self.assertEqual(volume_type['id'], res_dict['volume_type_id']) # Update, and test that there is an error since volumes exist @@ -616,11 +616,11 @@ class VolumeTypeEncryptionTest(test.TestCase): res = self.\ _get_response(volume_type, req_method='PUT', - req_body=json.dumps(update_body), + req_body=jsonutils.dump_as_bytes(update_body), req_headers='application/json', url='/v2/fake/types/%s/encryption/fake_type_id') self.assertEqual(400, res.status_code) - res_dict = json.loads(res.body) + res_dict = jsonutils.loads(res.body) expected = { 'badRequest': { 'code': 400, -- 2.45.2