From e31af46bca482531259a13cb9031e53c820c3f27 Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Thu, 24 Nov 2016 20:15:34 +0800 Subject: [PATCH] Port compute unit tests to Python 3 Use base64 module from oslo.serialization to handle base64 encoding and decoding. Partially-Implements: blueprint goal-python35 Change-Id: I973c4932f5f71e09d4b6c4b3077bdc5119af41dc --- nova/compute/api.py | 9 ++++++++- nova/tests/unit/compute/test_compute.py | 12 ++++++------ tests-py3.txt | 4 ---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/nova/compute/api.py b/nova/compute/api.py index 6cd25fecedf1..552d4c367b59 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -28,6 +28,7 @@ import string from oslo_log import log as logging from oslo_messaging import exceptions as oslo_exceptions +from oslo_serialization import base64 as base64utils from oslo_serialization import jsonutils from oslo_utils import excutils from oslo_utils import strutils @@ -832,7 +833,13 @@ class API(base.Base): length=l, maxsize=MAX_USERDATA_SIZE) try: - base64.decodestring(user_data) + # TODO(gcb): Just use base64utils.decode_as_bytes(user_data) + # when https://review.openstack.org/#/c/410797/ is merged and + # ensure oslo.serialization >=2.15.0 in Nova requirements.txt. + if six.PY3: + base64utils.decode_as_bytes(user_data) + else: + base64.decodestring(user_data) except base64.binascii.Error: raise exception.InstanceUserDataMalformed() diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index daebf6de9305..754604af0832 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -17,7 +17,6 @@ # under the License. """Tests for compute service.""" -import base64 import datetime import operator import sys @@ -30,6 +29,7 @@ import mock from neutronclient.common import exceptions as neutron_exceptions from oslo_log import log as logging import oslo_messaging as messaging +from oslo_serialization import base64 from oslo_serialization import jsonutils from oslo_utils import fixture as utils_fixture from oslo_utils import timeutils @@ -2740,7 +2740,7 @@ class ComputeTestCase(BaseTestCase): def test_rebuild_with_injected_files(self): # Ensure instance can be rebuilt with injected files. injected_files = [ - (b'/a/b/c', base64.b64encode(b'foobarbaz')), + (b'/a/b/c', base64.encode_as_bytes(b'foobarbaz')), ] self.decoded_files = [ @@ -7940,7 +7940,7 @@ class ComputeAPITestCase(BaseTestCase): # base64 (refs, resv_id) = self.compute_api.create( self.context, inst_type, self.fake_image['id'], - user_data=base64.encodestring(b'1' * 48510)) + user_data=base64.encode_as_text(b'1' * 48510)) def test_populate_instance_for_create(self, num_instances=1): base_options = {'image_ref': self.fake_image['id'], @@ -11705,8 +11705,8 @@ class ComputeInjectedFilesTestCase(BaseTestCase): def test_injected_success(self): # test with valid b64 encoded content. injected_files = [ - ('/a/b/c', base64.b64encode(b'foobarbaz')), - ('/d/e/f', base64.b64encode(b'seespotrun')), + ('/a/b/c', base64.encode_as_bytes(b'foobarbaz')), + ('/d/e/f', base64.encode_as_bytes(b'seespotrun')), ] decoded_files = [ @@ -11718,7 +11718,7 @@ class ComputeInjectedFilesTestCase(BaseTestCase): def test_injected_invalid(self): # test with invalid b64 encoded content injected_files = [ - ('/a/b/c', base64.b64encode(b'foobarbaz')), + ('/a/b/c', base64.encode_as_bytes(b'foobarbaz')), ('/d/e/f', 'seespotrun'), ] diff --git a/tests-py3.txt b/tests-py3.txt index 9a8b8e7be7ed..90d8caaeb743 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -6,10 +6,6 @@ nova.tests.unit.api.openstack.compute.test_security_group_default_rules.TestSecu nova.tests.unit.api.openstack.compute.test_security_groups.SecurityGroupsOutputTestV21 nova.tests.unit.api.openstack.compute.test_security_groups.TestSecurityGroupRulesV21 nova.tests.unit.api.openstack.compute.test_user_data.ServersControllerCreateTest -nova.tests.unit.compute.test_compute.ComputeAPITestCase.test_create_with_base64_user_data -nova.tests.unit.compute.test_compute_cells.CellsComputeAPITestCase.test_create_with_base64_user_data -nova.tests.unit.compute.test_compute_mgr.ComputeManagerUnitTestCase.test_run_pending_deletes -nova.tests.unit.compute.test_host_api.ComputeHostAPICellsTestCase nova.tests.unit.test_wsgi.TestWSGIServerWithSSL nova.tests.unit.virt.libvirt.test_firewall.IptablesFirewallTestCase nova.tests.unit.virt.libvirt.test_imagebackend.EncryptedLvmTestCase