From bcd838a38d822a670dba8f490ae6a55cbc455426 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 20 Jun 2016 11:48:19 +0200 Subject: [PATCH] Port API v1 unit tests to Python 3 * Replace len(MultiDict.keys()) with len(MultiDict). On Python 3, MultiDict.keys() now returns a generator which has no length. * Replace json with jsonutils to fix Unicode vs bytes issues. * Use a bytes strings for the HTTP body, not Unicode strings. * Replace LOG.exception() with LOG.error() when the argument is a string, not an exception object. * Only declare PackageException.__str__() on Python 2. The default implementation of __str__() is fine on Python 3. Moreover, the 'message' attribute has been removed from Exception on Python 3. * tox.ini: run the following API v1 unit tests on Python 3.4 - test_actions - test_catalog - test_env_templates - test_environments - test_sessions Blueprint: murano-python-3-support Change-Id: I4c8bedef019495477ae668769595ac4abc1ac0f4 --- murano/api/v1/catalog.py | 2 +- murano/api/v1/environments.py | 2 +- murano/api/v1/templates.py | 4 +- murano/common/wsgi.py | 4 +- murano/packages/exceptions.py | 5 +- murano/tests/unit/api/v1/test_actions.py | 2 +- murano/tests/unit/api/v1/test_catalog.py | 42 +++--- .../tests/unit/api/v1/test_env_templates.py | 142 +++++++++--------- murano/tests/unit/api/v1/test_environments.py | 55 +++---- murano/tests/unit/api/v1/test_sessions.py | 14 +- tox.ini | 5 + 11 files changed, 148 insertions(+), 129 deletions(-) diff --git a/murano/api/v1/catalog.py b/murano/api/v1/catalog.py index ff0d80ad..63f1ccfe 100644 --- a/murano/api/v1/catalog.py +++ b/murano/api/v1/catalog.py @@ -100,7 +100,7 @@ def _validate_body(body): 'Uploading file is too large. ' 'The limit is {0} Mb').format(mb_limit)) - if len(body.keys()) > 2: + if len(body) > 2: msg = _("'multipart/form-data' request body should contain 1 or 2 " "parts: json string and zip archive. Current body consists " "of {amount} part(s)").format(amount=len(body.keys())) diff --git a/murano/api/v1/environments.py b/murano/api/v1/environments.py index 84c985ac..85c2dbd6 100644 --- a/murano/api/v1/environments.py +++ b/murano/api/v1/environments.py @@ -66,7 +66,7 @@ class Controller(object): if not('name' in body and body['name'].strip()): msg = _('Please, specify a name of the environment to create') - LOG.exception(msg) + LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) name = six.text_type(body['name']) diff --git a/murano/api/v1/templates.py b/murano/api/v1/templates.py index 997cf960..f765010e 100644 --- a/murano/api/v1/templates.py +++ b/murano/api/v1/templates.py @@ -280,14 +280,14 @@ class Controller(object): if not('name' in body and body['name'].strip()): msg = _('Please, specify a name of the environment template.') - LOG.exception(msg) + LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) name = six.text_type(body['name']) if len(name) > 255: msg = _('Environment template name should be 255 characters ' 'maximum') - LOG.exception(msg) + LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) diff --git a/murano/common/wsgi.py b/murano/common/wsgi.py index 72d5dc1b..d65609de 100644 --- a/murano/common/wsgi.py +++ b/murano/common/wsgi.py @@ -538,8 +538,8 @@ class JSONDictSerializer(DictSerializer): return _dtime.isoformat() return six.text_type(obj) if result: - data.body = jsonutils.dumps(result) - return jsonutils.dumps(data, default=sanitizer) + data.body = jsonutils.dump_as_bytes(result) + return jsonutils.dump_as_bytes(data, default=sanitizer) class XMLDictSerializer(DictSerializer): diff --git a/murano/packages/exceptions.py b/murano/packages/exceptions.py index 61edfd14..fc41b0b1 100644 --- a/murano/packages/exceptions.py +++ b/murano/packages/exceptions.py @@ -19,8 +19,9 @@ import murano.common.exceptions as e class PackageException(e.Error): - def __str__(self): - return six.text_type(self.message).encode('UTF-8') + if six.PY2: + def __str__(self): + return six.text_type(self.message).encode('UTF-8') class PackageClassLoadError(PackageException): diff --git a/murano/tests/unit/api/v1/test_actions.py b/murano/tests/unit/api/v1/test_actions.py index 25af1e1c..ec37dfcb 100644 --- a/murano/tests/unit/api/v1/test_actions.py +++ b/murano/tests/unit/api/v1/test_actions.py @@ -87,7 +87,7 @@ class TestActionsApi(tb.ControllerTest, tb.MuranoApiTestCase): 'id': '12345' } - req = self._post('/environments/12345/actions/actionID_action', '{}') + req = self._post('/environments/12345/actions/actionID_action', b'{}') result = self.controller.execute(req, '12345', 'actionsID_action', '{}') diff --git a/murano/tests/unit/api/v1/test_catalog.py b/murano/tests/unit/api/v1/test_catalog.py index e6028ed4..2ea970a7 100644 --- a/murano/tests/unit/api/v1/test_catalog.py +++ b/murano/tests/unit/api/v1/test_catalog.py @@ -15,12 +15,13 @@ import cgi import imghdr -import json import os import uuid import mock +from oslo_serialization import jsonutils from oslo_utils import timeutils +import six from six.moves import cStringIO from six.moves import range @@ -427,7 +428,7 @@ class TestCatalogApi(test_base.ControllerTest, test_base.MuranoApiTestCase): result = req.get_response(self.api) self.assertEqual(406, result.status_code) - self.assertTrue('Acceptable response can not be provided' + self.assertTrue(b'Acceptable response can not be provided' in result.body) def test_get_ui_definition(self): @@ -461,7 +462,7 @@ class TestCatalogApi(test_base.ControllerTest, test_base.MuranoApiTestCase): result = req.get_response(self.api) self.assertEqual(406, result.status_code) - self.assertTrue('Acceptable response can not be provided' + self.assertTrue(b'Acceptable response can not be provided' in result.body) def test_get_logo(self): @@ -496,7 +497,7 @@ class TestCatalogApi(test_base.ControllerTest, test_base.MuranoApiTestCase): result = req.get_response(self.api) self.assertEqual(406, result.status_code) - self.assertTrue('Acceptable response can not be provided' + self.assertTrue(b'Acceptable response can not be provided' in result.body) def test_add_public_unauthorized(self): @@ -518,7 +519,7 @@ class TestCatalogApi(test_base.ControllerTest, test_base.MuranoApiTestCase): file_obj.file = file_obj_str package_from_dir, _ = self._test_package() - body = '''\ + body_fmt = '''\ --BOUNDARY Content-Disposition: form-data; name="__metadata__" @@ -530,6 +531,13 @@ Content-Disposition: form-data; name="ziparchive"; filename="file.zip" This is a fake zip archive --BOUNDARY--''' + def format_body(content): + content = jsonutils.dumps(content) + body = body_fmt.format(content) + if six.PY3: + body = body.encode('utf-8') + return body + with mock.patch('murano.packages.load_utils.load_from_file') as lff: ctxmgr = mock.Mock() ctxmgr.__enter__ = mock.Mock(return_value=package_from_dir) @@ -539,14 +547,14 @@ This is a fake zip archive # Uploading a non-public package req = self._post( '/catalog/packages', - body.format(json.dumps({'is_public': False})), + format_body({'is_public': False}), content_type='multipart/form-data; ; boundary=BOUNDARY', ) res = req.get_response(self.api) self.assertEqual(200, res.status_code) self.is_admin = True - app_id = json.loads(res.body)['id'] + app_id = jsonutils.loads(res.body)['id'] req = self._delete('/catalog/packages/{0}'.format(app_id)) res = req.get_response(self.api) @@ -554,7 +562,7 @@ This is a fake zip archive # Uploading a public package fails req = self._post( '/catalog/packages', - body.format(json.dumps({'is_public': True})), + format_body({'is_public': True}), content_type='multipart/form-data; ; boundary=BOUNDARY', ) res = req.get_response(self.api) @@ -564,7 +572,7 @@ This is a fake zip archive self.is_admin = True req = self._post( '/catalog/packages', - body.format(json.dumps({'is_public': True})), + format_body({'is_public': True}), content_type='multipart/form-data; ; boundary=BOUNDARY', ) res = req.get_response(self.api) @@ -587,9 +595,9 @@ This is a fake zip archive } body = {'name': 'new_category'} - req = self._post('/catalog/categories', json.dumps(body)) + req = self._post('/catalog/categories', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - processed_result = json.loads(result.body) + processed_result = jsonutils.loads(result.body) self.assertIn('id', processed_result.keys()) expected['id'] = processed_result['id'] self.assertDictEqual(expected, processed_result) @@ -612,7 +620,7 @@ This is a fake zip archive req = self._delete('/catalog/categories/12345') processed_result = req.get_response(self.api) - self.assertEqual('', processed_result.body) + self.assertEqual(b'', processed_result.body) self.assertEqual(200, processed_result.status_code) def test_add_category_failed_for_non_admin(self): @@ -626,7 +634,7 @@ This is a fake zip archive timeutils.utcnow.override_time = fake_now body = {'name': 'new_category'} - req = self._post('/catalog/categories', json.dumps(body)) + req = self._post('/catalog/categories', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(403, result.status_code) @@ -644,7 +652,7 @@ This is a fake zip archive timeutils.utcnow.override_time = fake_now body = {'name': 'cat' * 80} - req = self._post('/catalog/categories', json.dumps(body)) + req = self._post('/catalog/categories', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) result_message = result.text.replace('\n', '') @@ -662,7 +670,7 @@ This is a fake zip archive req = self._get('/catalog/categories') result = req.get_response(self.api) self.assertEqual(200, result.status_code) - result_categories = json.loads(result.body)['categories'] + result_categories = jsonutils.loads(result.body)['categories'] self.assertEqual(2, len(result_categories)) self.assertEqual(names, [c['name'] for c in result_categories]) @@ -671,7 +679,7 @@ This is a fake zip archive self.expect_policy_check('get_category') result = req.get_response(self.api) self.assertEqual(200, result.status_code) - result_categories = json.loads(result.body)['categories'] + result_categories = jsonutils.loads(result.body)['categories'] self.assertEqual(names, [c['name'] for c in result_categories]) names.reverse() @@ -681,7 +689,7 @@ This is a fake zip archive self.expect_policy_check('get_category') result = req.get_response(self.api) self.assertEqual(200, result.status_code) - result_categories = json.loads(result.body)['categories'] + result_categories = jsonutils.loads(result.body)['categories'] self.assertEqual(names, [c['name'] for c in result_categories]) def test_list_category_negative(self): diff --git a/murano/tests/unit/api/v1/test_env_templates.py b/murano/tests/unit/api/v1/test_env_templates.py index fb349916..f7eed495 100644 --- a/murano/tests/unit/api/v1/test_env_templates.py +++ b/murano/tests/unit/api/v1/test_env_templates.py @@ -13,9 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from oslo_config import fixture as config_fixture +from oslo_serialization import jsonutils from oslo_utils import timeutils from murano.api.v1 import templates @@ -41,7 +40,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): req = self._get('/templates') result = req.get_response(self.api) - self.assertEqual({'templates': []}, json.loads(result.body)) + self.assertEqual({'templates': []}, jsonutils.loads(result.body)) def test_create_env_templates(self): """Create an template, test template.show().""" @@ -64,10 +63,10 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): 'updated': timeutils.isotime(fake_now)[:-1]} body = {'name': 'mytemp'} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) # Reset the policy expectation self.expect_policy_check('list_env_templates') @@ -75,7 +74,8 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): req = self._get('/templates') result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual({'templates': [expected]}, json.loads(result.body)) + self.assertEqual({'templates': [expected]}, + jsonutils.loads(result.body)) expected['services'] = [] @@ -83,7 +83,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): {'env_template_id': self.uuids[0]}) req = self._get('/templates/%s' % self.uuids[0]) result = req.get_response(self.api) - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) def test_list_public_env_templates(self): """Create an template, test templates.public().""" @@ -95,16 +95,17 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': 'mytemp2', 'is_public': True} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertTrue(json.loads(result.body)['is_public']) + self.assertTrue(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('list_env_templates') req = self._get('/templates', {'is_public': True}) result = req.get_response(self.api) - self.assertEqual(1, len(json.loads(result.body))) - self.assertTrue(json.loads(result.body)['templates'][0]['is_public']) + data = jsonutils.loads(result.body) + self.assertEqual(1, len(data)) + self.assertTrue(data['templates'][0]['is_public']) def test_clone_env_templates(self): """Create an template, test templates.public().""" @@ -115,18 +116,18 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': 'mytemp2', 'is_public': True} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - env_template_id = json.loads(result.body)['id'] - self.assertTrue(json.loads(result.body)['is_public']) + env_template_id = jsonutils.loads(result.body)['id'] + self.assertTrue(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('clone_env_template') body = {'name': 'clone', 'is_public': False} req = self._post('/templates/%s/clone' % env_template_id, - json.dumps(body)) + jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertFalse(json.loads(result.body)['is_public']) - self.assertEqual('clone', json.loads(result.body)['name']) + self.assertFalse(jsonutils.loads(result.body)['is_public']) + self.assertEqual('clone', jsonutils.loads(result.body)['name']) def test_clone_env_templates_private(self): """Create an template, test templates.public().""" @@ -137,15 +138,15 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': 'mytemp2', 'is_public': False} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - env_template_id = json.loads(result.body)['id'] - self.assertFalse(json.loads(result.body)['is_public']) + env_template_id = jsonutils.loads(result.body)['id'] + self.assertFalse(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('clone_env_template') body = {'name': 'clone', 'is_public': False} req = self._post('/templates/%s/clone' % env_template_id, - json.dumps(body)) + jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(result.status_code, 403) @@ -162,15 +163,15 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': 'mytemp'} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertFalse(json.loads(result.body)['is_public']) + self.assertFalse(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('list_env_templates') req = self._get('/templates', {'is_public': True}) result = req.get_response(self.api) - self.assertFalse(0, len(json.loads(result.body))) + self.assertFalse(0, len(jsonutils.loads(result.body))) def test_list_private_env_templates(self): """Test listing private templates @@ -185,20 +186,20 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': 'mytemp', 'is_public': False} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertFalse(json.loads(result.body)['is_public']) + self.assertFalse(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('create_env_template') body = {'name': 'mytemp1', 'is_public': True} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertTrue(json.loads(result.body)['is_public']) + self.assertTrue(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('list_env_templates') req = self._get('/templates', {'is_public': False}) result = req.get_response(self.api) - self.assertEqual(1, len(json.loads(result.body)['templates'])) + self.assertEqual(1, len(jsonutils.loads(result.body)['templates'])) def test_list_env_templates(self): """Test listing public templates when there aren't any @@ -213,21 +214,21 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': 'mytemp', 'is_public': False} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertFalse(json.loads(result.body)['is_public']) + self.assertFalse(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('create_env_template') body = {'name': 'mytemp1', 'is_public': True} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertTrue(json.loads(result.body)['is_public']) + self.assertTrue(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('list_env_templates') req = self._get('/templates') result = req.get_response(self.api) - self.assertEqual(2, len(json.loads(result.body)['templates'])) + self.assertEqual(2, len(jsonutils.loads(result.body)['templates'])) def test_list_env_templates_with_different_tenant(self): """Test listing public template from another tenant @@ -242,22 +243,23 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': 'mytemp', 'is_public': False} - req = self._post('/templates', json.dumps(body), tenant='first_tenant') + req = self._post('/templates', jsonutils.dump_as_bytes(body), + tenant='first_tenant') result = req.get_response(self.api) - self.assertFalse(json.loads(result.body)['is_public']) + self.assertFalse(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('create_env_template') body = {'name': 'mytemp1', 'is_public': True} - req = self._post('/templates', json.dumps(body), + req = self._post('/templates', jsonutils.dump_as_bytes(body), tenant='second_tenant') result = req.get_response(self.api) - self.assertTrue(json.loads(result.body)['is_public']) + self.assertTrue(jsonutils.loads(result.body)['is_public']) self.expect_policy_check('list_env_templates') req = self._get('/templates', tenant='first_tenant') result = req.get_response(self.api) - self.assertEqual(2, len(json.loads(result.body)['templates'])) + self.assertEqual(2, len(jsonutils.loads(result.body)['templates'])) def test_illegal_template_name_create(self): """Check that an illegal temp name results in an HTTPClientError.""" @@ -269,7 +271,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': ' '} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) @@ -283,7 +285,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'name': 'a' * 256} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) result_msg = result.text.replace('\n', '') @@ -299,7 +301,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_env_template') body = {'invalid': 'test'} - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) @@ -354,7 +356,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): body = { 'name': 'renamed_temp' } - req = self._put('/templates/12345', json.dumps(body)) + req = self._put('/templates/12345', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) @@ -367,7 +369,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): expected['created'] = timeutils.isotime(expected['created'])[:-1] expected['updated'] = timeutils.isotime(expected['updated'])[:-1] - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) def test_delete_env_templates(self): """Test that environment deletion results in the correct rpc call.""" @@ -398,7 +400,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): result = req.get_response(self.api) # Should this be expected behavior? - self.assertEqual('', result.body) + self.assertEqual(b'', result.body) self.assertEqual(200, result.status_code) def test_create_env_templates_with_applications(self): @@ -466,9 +468,9 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): ] } - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) # Reset the policy expectation self.expect_policy_check('list_env_templates') @@ -477,7 +479,8 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): result = req.get_response(self.api) del expected['services'] self.assertEqual(200, result.status_code) - self.assertEqual({'templates': [expected]}, json.loads(result.body)) + self.assertEqual({'templates': [expected]}, + jsonutils.loads(result.body)) # Reset the policy expectation self.expect_policy_check('show_env_template', @@ -485,7 +488,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): expected['services'] = services req = self._get('/templates/%s' % self.uuids[0]) result = req.get_response(self.api) - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) def test_add_application_to_template(self): """Create an template, test template.show().""" @@ -522,21 +525,21 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): "name": "template_name", } - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) body = services req = self._post('/templates/%s/services' % self.uuids[0], - json.dumps(body)) + jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual(services, json.loads(result.body)) + self.assertEqual(services, jsonutils.loads(result.body)) req = self._get('/templates/%s/services' % self.uuids[0]) result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual(1, len(json.loads(result.body))) + self.assertEqual(1, len(jsonutils.loads(result.body))) service_no_instance = [ { @@ -551,14 +554,14 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): ] req = self._post('/templates/%s/services' % self.uuids[0], - json.dumps(service_no_instance)) + jsonutils.dump_as_bytes(service_no_instance)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) req = self._get('/templates/%s/services' % self.uuids[0]) result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual(2, len(json.loads(result.body))) + self.assertEqual(2, len(jsonutils.loads(result.body))) def test_delete_application_in_template(self): """Create an template, test template.show().""" @@ -585,15 +588,15 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): ] } - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual(1, len(json.loads(result.body)['services'])) + self.assertEqual(1, len(jsonutils.loads(result.body)['services'])) req = self._get('/templates/%s/services' % self.uuids[0]) result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual(1, len(json.loads(result.body))) + self.assertEqual(1, len(jsonutils.loads(result.body))) service_id = '54cea43d-5970-4c73-b9ac-fea656f3c722' req = self._get('/templates/' + self.uuids[0] + @@ -606,7 +609,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual(0, len(json.loads(result.body)['services'])) + self.assertEqual(0, len(jsonutils.loads(result.body)['services'])) req = self._get('/templates/' + self.uuids[0] + '/services/' + service_id) @@ -630,11 +633,11 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_environment', {'env_template_id': self.uuids[0]}) req = self._post('/templates/%s/create-environment' % - self.uuids[0], json.dumps(body_env)) + self.uuids[0], jsonutils.dump_as_bytes(body_env)) session_result = req.get_response(self.api) self.assertEqual(200, session_result.status_code) self.assertIsNotNone(session_result) - body_returned = json.loads(session_result.body) + body_returned = jsonutils.loads(session_result.body) self.assertEqual(self.uuids[4], body_returned['session_id']) self.assertEqual(self.uuids[3], body_returned['environment_id']) @@ -657,11 +660,11 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): body = {'name': 'my_template'} req = self._post('/templates/%s/create-environment' % - self.uuids[0], json.dumps(body)) + self.uuids[0], jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertIsNotNone(result) self.assertEqual(200, result.status_code) - body_returned = json.loads(result.body) + body_returned = jsonutils.loads(result.body) self.assertEqual(self.uuids[4], body_returned['session_id']) self.assertEqual(self.uuids[3], body_returned['environment_id']) @@ -677,7 +680,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): {'env_template_id': self.uuids[0]}) body = {'invalid': 'test'} req = self._post('/templates/%s/create-environment' % - self.uuids[0], json.dumps(body)) + self.uuids[0], jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) @@ -706,10 +709,10 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): ] } - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual(1, len(json.loads(result.body)['services'])) + self.assertEqual(1, len(jsonutils.loads(result.body)['services'])) req = self._delete('/templates/{0}/services/{1}'.format(self.uuids[0], "NO_EXIST")) @@ -727,7 +730,7 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): body = {'name': 'test'} req = self._post('/templates/%s/create-environment' - % env_template_id, json.dumps(body)) + % env_template_id, jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(404, result.status_code) @@ -736,7 +739,8 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now - req = self._post('/templates', json.dumps({'name': 'name'})) + req = self._post('/templates', + jsonutils.dump_as_bytes({'name': 'name'})) result = req.get_response(self.api) self.assertEqual(200, result.status_code) @@ -772,5 +776,5 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase): ] } - req = self._post('/templates', json.dumps(body)) + req = self._post('/templates', jsonutils.dump_as_bytes(body)) req.get_response(self.api) diff --git a/murano/tests/unit/api/v1/test_environments.py b/murano/tests/unit/api/v1/test_environments.py index ed620cd9..3d22d2d7 100644 --- a/murano/tests/unit/api/v1/test_environments.py +++ b/murano/tests/unit/api/v1/test_environments.py @@ -14,9 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from oslo_config import fixture as config_fixture +from oslo_serialization import jsonutils from oslo_utils import timeutils from murano.api.v1 import environments @@ -42,7 +41,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): req = self._get('/environments') result = req.get_response(self.api) - self.assertEqual({'environments': []}, json.loads(result.body)) + self.assertEqual({'environments': []}, jsonutils.loads(result.body)) def test_list_all_tenants(self): """Check whether all_tenants param is taken into account.""" @@ -55,7 +54,8 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_environment') body = {'name': 'my_env'} - req = self._post('/environments', json.dumps(body), tenant="other") + req = self._post('/environments', jsonutils.dump_as_bytes(body), + tenant="other") req.get_response(self.api) self._check_listing(False, 'list_environments', 0) @@ -65,7 +65,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check(expected_check) req = self._get('/environments', {'all_tenants': all_tenants}) response = req.get_response(self.api) - body = json.loads(response.body) + body = jsonutils.loads(response.body) self.assertEqual(200, response.status_code) self.assertEqual(expected_count, len(body['environments'])) @@ -93,9 +93,9 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): } body = {'name': 'my_env'} - req = self._post('/environments', json.dumps(body)) + req = self._post('/environments', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) expected['status'] = 'ready' @@ -105,7 +105,8 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): req = self._get('/environments') result = req.get_response(self.api) self.assertEqual(200, result.status_code) - self.assertEqual({'environments': [expected]}, json.loads(result.body)) + self.assertEqual({'environments': [expected]}, + jsonutils.loads(result.body)) expected['services'] = [] expected['acquired_by'] = None @@ -117,7 +118,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): req = self._get('/environments/%s' % uuids[-1]) result = req.get_response(self.api) - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) self.assertEqual(3, mock_uuid.call_count) def test_illegal_environment_name_create(self): @@ -130,7 +131,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_environment') body = {'name': ' '} - req = self._post('/environments', json.dumps(body)) + req = self._post('/environments', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) @@ -144,7 +145,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_environment') body = {'name': u'$yaql \u2665 unicode'} - req = self._post('/environments', json.dumps(body)) + req = self._post('/environments', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) @@ -158,7 +159,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_environment') body = {'no_name': 'fake'} - req = self._post('/environments', json.dumps(body)) + req = self._post('/environments', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) result_msg = result.text.replace('\n', '') @@ -175,7 +176,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): self.expect_policy_check('create_environment') body = {'name': 'a' * 256} - req = self._post('/environments', json.dumps(body)) + req = self._post('/environments', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) result_msg = result.text.replace('\n', '') @@ -184,7 +185,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): def test_create_environment_with_empty_body(self): """Check that empty request body results in an HTTPBadResquest.""" - body = '' + body = b'' req = self._post('/environments', body) result = req.get_response(self.api) self.assertEqual(400, result.status_code) @@ -244,7 +245,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): body = { 'name': 'renamed_env' } - req = self._put('/environments/12345', json.dumps(body)) + req = self._put('/environments/12345', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) @@ -258,7 +259,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): expected['updated'] = timeutils.isotime(expected['updated'])[:-1] expected['acquired_by'] = None - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) def test_update_environment_with_invalid_name(self): """Test that invalid env name returns HTTPBadRequest @@ -278,7 +279,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): body = { 'name': ' ' } - req = self._put('/environments/111', json.dumps(body)) + req = self._put('/environments/111', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) result_msg = result.text.replace('\n', '') @@ -300,7 +301,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): body = { 'name': 'env2' } - req = self._put('/environments/111', json.dumps(body)) + req = self._put('/environments/111', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(409, result.status_code) @@ -323,7 +324,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): body = { 'name': new_name } - req = self._put('/environments/111', json.dumps(body)) + req = self._put('/environments/111', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) result_msg = result.text.replace('\n', '') @@ -333,21 +334,21 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): def test_delete_environment(self): """Test that environment deletion results in the correct rpc call.""" result = self._test_delete_or_abandon(abandon=False) - self.assertEqual('', result.body) + self.assertEqual(b'', result.body) self.assertEqual(200, result.status_code) def test_abandon_environment(self): """Check that abandon feature works""" result = self._test_delete_or_abandon(abandon=True) - self.assertEqual('', result.body) + self.assertEqual(b'', result.body) self.assertEqual(200, result.status_code) def test_abandon_environment_of_different_tenant(self): """Test abandon environment belongs to another tenant.""" result = self._test_delete_or_abandon(abandon=True, tenant='not_match') self.assertEqual(403, result.status_code) - self.assertTrue(('User is not authorized to access these' - ' tenant resources') in result.body) + self.assertTrue((b'User is not authorized to access these' + b' tenant resources') in result.body) def test_get_last_status_of_different_tenant(self): """Test get last services status of env belongs to another tenant.""" @@ -355,8 +356,8 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): req = self._get('/environments/111/lastStatus', tenant='not_match') result = req.get_response(self.api) self.assertEqual(403, result.status_code) - self.assertTrue(('User is not authorized to access these' - ' tenant resources') in result.body) + self.assertTrue((b'User is not authorized to access these' + b' tenant resources') in result.body) def test_get_environment(self): """Test GET request of an environment in ready status""" @@ -384,7 +385,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): 'services': [], 'status': 'ready', } - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) def test_get_environment_acquired(self): """Test GET request of an environment in deploying status""" @@ -430,7 +431,7 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase): 'services': [], 'status': states.EnvironmentStatus.DEPLOYING, } - self.assertEqual(expected, json.loads(result.body)) + self.assertEqual(expected, jsonutils.loads(result.body)) def _create_fake_environment(self, env_name='my-env', env_id='123'): fake_now = timeutils.utcnow() diff --git a/murano/tests/unit/api/v1/test_sessions.py b/murano/tests/unit/api/v1/test_sessions.py index 4122545c..842f4855 100644 --- a/murano/tests/unit/api/v1/test_sessions.py +++ b/murano/tests/unit/api/v1/test_sessions.py @@ -13,9 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from oslo_config import fixture as config_fixture +from oslo_serialization import jsonutils from murano.api.v1 import environments from murano.api.v1 import sessions @@ -49,10 +48,11 @@ class TestSessionsApi(tb.ControllerTest, tb.MuranoApiTestCase): # Create environment for user #1 request = self._post( - '/environments', json.dumps({'name': 'test_environment_1'}), + '/environments', + jsonutils.dump_as_bytes({'name': 'test_environment_1'}), **CREDENTIALS_1 ) - response_body = json.loads(request.get_response(self.api).body) + response_body = jsonutils.loads(request.get_response(self.api).body) self.assertEqual(CREDENTIALS_1['tenant'], response_body['tenant_id']) @@ -62,10 +62,10 @@ class TestSessionsApi(tb.ControllerTest, tb.MuranoApiTestCase): request = self._post( '/environments/{environment_id}/configure' .format(environment_id=ENVIRONMENT_ID), - '', + b'', **CREDENTIALS_1 ) - response_body = json.loads(request.get_response(self.api).body) + response_body = jsonutils.loads(request.get_response(self.api).body) SESSION_ID = response_body['id'] @@ -75,7 +75,7 @@ class TestSessionsApi(tb.ControllerTest, tb.MuranoApiTestCase): '/environments/{environment_id}/sessions/' '{session_id}/deploy' .format(environment_id=ENVIRONMENT_ID, session_id=SESSION_ID), - '', + b'', **CREDENTIALS_2 ) response = request.get_response(self.api) diff --git a/tox.ini b/tox.ini index b3f23f25..59a86f0f 100644 --- a/tox.ini +++ b/tox.ini @@ -19,6 +19,11 @@ whitelist_externals = bash commands = python -m testtools.run \ murano/tests/unit/api/middleware/test_ssl.py \ murano/tests/unit/api/v1/cloudfoundry/test_cfapi.py \ + murano/tests/unit/api/v1/test_actions.py \ + murano/tests/unit/api/v1/test_catalog.py \ + murano/tests/unit/api/v1/test_env_templates.py \ + murano/tests/unit/api/v1/test_environments.py \ + murano/tests/unit/api/v1/test_sessions.py \ murano/tests/unit/cmd/test_engine_workers.py \ murano/tests/unit/common/helpers/test_token_sanitizer.py \ murano/tests/unit/common/test_is_different.py \