From b7033277dc19493b4a53705b5ecae7c3c77da999 Mon Sep 17 00:00:00 2001 From: Jens Rosenboom Date: Fri, 1 Jul 2016 10:58:12 +0200 Subject: [PATCH] Fix api_validation for Python 3 * Convert argument to base64.decodestring() to bytes for PY3. * Fix an issue with python3.5 where the format of an internal error message changed. Change-Id: If8184c190e76d8cefb5b097f8fa8cb7564207103 Closes-Bug: 1598062 --- nova/api/validation/validators.py | 2 ++ nova/tests/unit/test_api_validation.py | 9 ++++++++- tests-py3.txt | 1 - 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/nova/api/validation/validators.py b/nova/api/validation/validators.py index 72a3095d1848..96f1377ee6ba 100644 --- a/nova/api/validation/validators.py +++ b/nova/api/validation/validators.py @@ -45,6 +45,8 @@ def _validate_datetime_format(instance): @jsonschema.FormatChecker.cls_checks('base64') def _validate_base64_format(instance): try: + if isinstance(instance, six.text_type): + instance = instance.encode('utf-8') base64.decodestring(instance) except base64.binascii.Error: return False diff --git a/nova/tests/unit/test_api_validation.py b/nova/tests/unit/test_api_validation.py index 68cf7e8f6e6a..4b8b53fece5a 100644 --- a/nova/tests/unit/test_api_validation.py +++ b/nova/tests/unit/test_api_validation.py @@ -18,6 +18,7 @@ import re import fixtures from jsonschema import exceptions as jsonschema_exc import six +import sys from nova.api.openstack import api_version_request as api_version from nova.api import validation @@ -354,7 +355,13 @@ class PatternPropertiesTestCase(APIValidationTestCase): self.check_validation_error(self.post, body={'0123456789a': 'bar'}, expected_detail=detail) - detail = "expected string or buffer" + # Note(jrosenboom): This is referencing an internal python error + # string, which is no stable interface. We need a patch in the + # jsonschema library in order to fix this properly. + if sys.version[:3] == '3.5': + detail = "expected string or bytes-like object" + else: + detail = "expected string or buffer" self.check_validation_error(self.post, body={None: 'bar'}, expected_detail=detail) diff --git a/tests-py3.txt b/tests-py3.txt index cfb234e8264f..c513f6e2f43b 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -56,7 +56,6 @@ nova.tests.unit.network.test_manager.LdapDNSTestCase nova.tests.unit.pci.test_manager.PciDevTrackerTestCase nova.tests.unit.pci.test_stats.PciDeviceStatsTestCase nova.tests.unit.pci.test_stats.PciDeviceStatsWithTagsTestCase -nova.tests.unit.test_api_validation.Base64TestCase nova.tests.unit.test_bdm.BlockDeviceMappingEc2CloudTestCase nova.tests.unit.test_configdrive2.ConfigDriveTestCase nova.tests.unit.test_hacking.HackingTestCase