Skip to content

Commit

Permalink
Python 3: fix a lot of tests
Browse files Browse the repository at this point in the history
As of this commit, the following tests should now be working with Python 3:

- cinder.tests.unit.api.contrib.test_cgsnapshots
- cinder.tests.unit.api.contrib.test_scheduler_hints
- cinder.tests.unit.api.contrib.test_snapshot_actions
- cinder.tests.unit.api.contrib.test_snapshot_manage
- cinder.tests.unit.api.contrib.test_snapshot_unmanage
- cinder.tests.unit.api.contrib.test_volume_encryption_metadata
- cinder.tests.unit.api.contrib.test_volume_host_attribute
- cinder.tests.unit.api.contrib.test_volume_manage
- cinder.tests.unit.api.contrib.test_volume_migration_status_attribute
- cinder.tests.unit.api.contrib.test_volume_tenant_attribute
- cinder.tests.unit.api.contrib.test_volume_unmanage
- cinder.tests.unit.api.v2.test_volumes

Most changes in this patch:
- make sure that Request.body is set to bytes;
- replace jsonutils.dumps with jsonutils.dump_as_bytes;
- replace json.loads with oslo_serialization.jsonutils.loads;
- replace dict.iteritems with dict.items.

Partial-Implements: blueprint cinder-python3
Change-Id: Icbb96ff84b7012b58f7296eea4fbcd620e081614
  • Loading branch information
CyrilRoelandteNovance committed Dec 7, 2015
1 parent ef08af9 commit 75c0483
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 56 deletions.
30 changes: 15 additions & 15 deletions cinder/tests/unit/api/contrib/test_cgsnapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
Tests for cgsnapshot code.
"""

import json
from xml.dom import minidom

import mock
from oslo_serialization import jsonutils
import webob

from cinder.consistencygroup import api as consistencygroupAPI
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_show_cgsnapshot(self):
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('this is a test cgsnapshot',
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_show_cgsnapshot_with_cgsnapshot_NotFound(self):
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'])
Expand All @@ -123,7 +123,7 @@ def test_list_cgsnapshots_json(self):
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(cgsnapshot1.id,
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_list_cgsnapshots_detail_json(self):
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('this is a test cgsnapshot',
Expand Down Expand Up @@ -315,10 +315,10 @@ def test_create_cgsnapshot_json(self, mock_validate):
req = webob.Request.blank('/v2/fake/cgsnapshots')
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['cgsnapshot'])
Expand All @@ -332,12 +332,12 @@ def test_create_cgsnapshot_json(self, mock_validate):
def test_create_cgsnapshot_with_no_body(self):
# omit body from the request
req = webob.Request.blank('/v2/fake/cgsnapshots')
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'])
Expand All @@ -358,11 +358,11 @@ def test_create_with_invalid_cgsnapshot(self, mock_create_cgsnapshot):
"CG Snapshot 1",
"consistencygroup_id": consistencygroup.id}}
req = webob.Request.blank('/v2/fake/cgsnapshots')
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())
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'])
Expand All @@ -386,9 +386,9 @@ def test_create_with_cgsnapshot_not_found(self, mock_create_cgsnapshot):
req = webob.Request.blank('/v2/fake/cgsnapshots')
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'])
Expand Down Expand Up @@ -425,7 +425,7 @@ def test_delete_cgsnapshot_with_cgsnapshot_NotFound(self):
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'])
Expand All @@ -446,7 +446,7 @@ def test_delete_cgsnapshot_with_Invalidcgsnapshot(self):
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'])
Expand Down
6 changes: 3 additions & 3 deletions cinder/tests/unit/api/contrib/test_scheduler_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def fake_create(*args, **kwargs):
body = {'id': id,
'volume_type_id': 'cedef40a-ed67-4d10-800e-17455edce175',
'volume_id': '1', }
req.body = jsonutils.dumps(body)
req.body = jsonutils.dump_as_bytes(body)
res = req.get_response(self.app)
self.assertEqual(202, res.status_int)

Expand All @@ -80,7 +80,7 @@ def fake_create(*args, **kwargs):
'volume_id': '1',
'scheduler_hints': {'a': 'b'}, }

req.body = jsonutils.dumps(body)
req.body = jsonutils.dump_as_bytes(body)
res = req.get_response(self.app)
self.assertEqual(202, res.status_int)

Expand All @@ -94,6 +94,6 @@ def test_create_server_bad_hints(self):
'volume_id': '1',
'scheduler_hints': 'a', }}

req.body = jsonutils.dumps(body)
req.body = jsonutils.dump_as_bytes(body)
res = req.get_response(self.app)
self.assertEqual(400, res.status_int)
6 changes: 3 additions & 3 deletions cinder/tests/unit/api/contrib/test_snapshot_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_update_snapshot_status(self, metadata_get, *args):
body = {'os-update_snapshot_status': {'status': 'available'}}
req = webob.Request.blank('/v2/fake/snapshots/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())
Expand All @@ -62,7 +62,7 @@ def test_update_snapshot_status_invalid_status(self, metadata_get, *args):
body = {'os-update_snapshot_status': {'status': 'in-use'}}
req = webob.Request.blank('/v2/fake/snapshots/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())
Expand All @@ -73,7 +73,7 @@ def test_update_snapshot_status_without_status(self):
body = {'os-update_snapshot_status': {}}
req = webob.Request.blank('/v2/fake/snapshots/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())
Expand Down
2 changes: 1 addition & 1 deletion cinder/tests/unit/api/contrib/test_snapshot_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _get_resp(self, body):
req.environ['cinder.context'] = context.RequestContext('admin',
'fake',
True)
req.body = jsonutils.dumps(body)
req.body = jsonutils.dump_as_bytes(body)
res = req.get_response(app())
return res

Expand Down
2 changes: 1 addition & 1 deletion cinder/tests/unit/api/contrib/test_snapshot_unmanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _get_resp(self, snapshot_id):
'fake',
True)
body = {'os-unmanage': ''}
req.body = jsonutils.dumps(body)
req.body = jsonutils.dump_as_bytes(body)
res = req.get_response(app())
return res

Expand Down
23 changes: 11 additions & 12 deletions cinder/tests/unit/api/contrib/test_volume_encryption_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import json

from oslo_serialization import jsonutils
import webob

from cinder.api.contrib import volume_encryption_metadata
Expand Down Expand Up @@ -81,7 +80,7 @@ def test_index(self):
% self.volume_id)
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))
self.assertEqual(200, res.status_code)
res_dict = json.loads(res.body)
res_dict = jsonutils.loads(res.body)

expected = {
"encryption_key_id": "fake_key",
Expand All @@ -98,7 +97,7 @@ def test_index_bad_tenant_id(self):
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))
self.assertEqual(400, res.status_code)

res_dict = json.loads(res.body)
res_dict = jsonutils.loads(res.body)
expected = {'badRequest': {'code': 400,
'message': 'Malformed request url'}}
self.assertEqual(expected, res_dict)
Expand All @@ -110,7 +109,7 @@ def test_index_bad_volume_id(self):
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))
self.assertEqual(404, res.status_code)

res_dict = json.loads(res.body)
res_dict = jsonutils.loads(res.body)
expected = {'itemNotFound': {'code': 404,
'message': 'VolumeNotFound: Volume '
'%s could not be found.'
Expand All @@ -123,23 +122,23 @@ def test_show_key(self):
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))
self.assertEqual(200, res.status_code)

self.assertEqual('fake_key', res.body)
self.assertEqual(b'fake_key', res.body)

def test_show_control(self):
req = webob.Request.blank('/v2/fake/volumes/%s/encryption/'
'control_location' % self.volume_id)
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))
self.assertEqual(200, res.status_code)

self.assertEqual('front-end', res.body)
self.assertEqual(b'front-end', res.body)

def test_show_provider(self):
req = webob.Request.blank('/v2/fake/volumes/%s/encryption/'
'provider' % self.volume_id)
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))
self.assertEqual(200, res.status_code)

self.assertEqual('nova.volume.encryptors.base.VolumeEncryptor',
self.assertEqual(b'nova.volume.encryptors.base.VolumeEncryptor',
res.body)

def test_show_bad_tenant_id(self):
Expand All @@ -149,7 +148,7 @@ def test_show_bad_tenant_id(self):
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))
self.assertEqual(400, res.status_code)

res_dict = json.loads(res.body)
res_dict = jsonutils.loads(res.body)
expected = {'badRequest': {'code': 400,
'message': 'Malformed request url'}}
self.assertEqual(expected, res_dict)
Expand All @@ -161,7 +160,7 @@ def test_show_bad_volume_id(self):
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))
self.assertEqual(404, res.status_code)

res_dict = json.loads(res.body)
res_dict = jsonutils.loads(res.body)
expected = {'itemNotFound': {'code': 404,
'message': 'VolumeNotFound: Volume '
'%s could not be found.'
Expand All @@ -176,7 +175,7 @@ def test_retrieve_key_admin(self):
res = req.get_response(fakes.wsgi_app(fake_auth_context=ctxt))
self.assertEqual(200, res.status_code)

self.assertEqual('fake_key', res.body)
self.assertEqual(b'fake_key', res.body)

def test_show_volume_not_encrypted_type(self):
self.stubs.Set(db.sqlalchemy.api, 'volume_type_encryption_get',
Expand All @@ -203,7 +202,7 @@ def test_index_volume_not_encrypted_type(self):
res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt))

self.assertEqual(200, res.status_code)
res_dict = json.loads(res.body)
res_dict = jsonutils.loads(res.body)

expected = {
'encryption_key_id': None
Expand Down
12 changes: 6 additions & 6 deletions cinder/tests/unit/api/contrib/test_volume_host_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.

import json
import uuid

from lxml import etree
from oslo_serialization import jsonutils
from oslo_utils import timeutils
import webob

Expand Down Expand Up @@ -82,7 +82,7 @@ def test_get_volume_allowed(self):
req.method = 'GET'
req.environ['cinder.context'] = ctx
res = req.get_response(app())
vol = json.loads(res.body)['volume']
vol = jsonutils.loads(res.body)['volume']
self.assertEqual('host001', vol['os-vol-host-attr:host'])

def test_get_volume_unallowed(self):
Expand All @@ -91,7 +91,7 @@ def test_get_volume_unallowed(self):
req.method = 'GET'
req.environ['cinder.context'] = ctx
res = req.get_response(app())
vol = json.loads(res.body)['volume']
vol = jsonutils.loads(res.body)['volume']
self.assertNotIn('os-vol-host-attr:host', vol)

def test_list_detail_volumes_allowed(self):
Expand All @@ -100,7 +100,7 @@ def test_list_detail_volumes_allowed(self):
req.method = 'GET'
req.environ['cinder.context'] = ctx
res = req.get_response(app())
vol = json.loads(res.body)['volumes']
vol = jsonutils.loads(res.body)['volumes']
self.assertEqual('host001', vol[0]['os-vol-host-attr:host'])

def test_list_detail_volumes_unallowed(self):
Expand All @@ -109,7 +109,7 @@ def test_list_detail_volumes_unallowed(self):
req.method = 'GET'
req.environ['cinder.context'] = ctx
res = req.get_response(app())
vol = json.loads(res.body)['volumes']
vol = jsonutils.loads(res.body)['volumes']
self.assertNotIn('os-vol-host-attr:host', vol[0])

def test_list_simple_volumes_no_host(self):
Expand All @@ -118,7 +118,7 @@ def test_list_simple_volumes_no_host(self):
req.method = 'GET'
req.environ['cinder.context'] = ctx
res = req.get_response(app())
vol = json.loads(res.body)['volumes']
vol = jsonutils.loads(res.body)['volumes']
self.assertNotIn('os-vol-host-attr:host', vol[0])

def test_get_volume_xml(self):
Expand Down
2 changes: 1 addition & 1 deletion cinder/tests/unit/api/contrib/test_volume_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _get_resp(self, body):
req.environ['cinder.context'] = context.RequestContext('admin',
'fake',
True)
req.body = jsonutils.dumps(body)
req.body = jsonutils.dump_as_bytes(body)
res = req.get_response(app())
return res

Expand Down
Loading

0 comments on commit 75c0483

Please sign in to comment.