From 939f72a2357ff594fa7a7a108faccc10ee0f34ab Mon Sep 17 00:00:00 2001 From: Jake Yip Date: Wed, 20 Feb 2019 15:37:58 +1100 Subject: [PATCH] python3 fixes Update uses of b64encode() as describe in OpenStack docs [1]. Also add tests. [1]: https://wiki.openstack.org/wiki/Python3#Serialization:_base64.2C_JSON.2C_etc. Change-Id: I5aa6ba509979e0532d2837153aa5363d1e13631e (cherry picked from commit 3f7b994acd3eaac2321006b34cebfcb07d924934) --- magnumclient/common/utils.py | 10 +++++----- magnumclient/tests/osc/unit/v1/fakes.py | 12 +++++++++++- magnumclient/tests/osc/unit/v1/test_clusters.py | 9 ++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/magnumclient/common/utils.py b/magnumclient/common/utils.py index ee7a2bd2..c0d17a75 100644 --- a/magnumclient/common/utils.py +++ b/magnumclient/common/utils.py @@ -15,7 +15,6 @@ # License for the specific language governing permissions and limitations # under the License. -import base64 import os from cryptography.hazmat.backends import default_backend @@ -24,6 +23,7 @@ from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives import serialization from cryptography import x509 from cryptography.x509.oid import NameOID +from oslo_serialization import base64 from oslo_serialization import jsonutils from magnumclient import exceptions as exc @@ -215,9 +215,9 @@ def _config_cluster_kubernetes(cluster, cluster_template, cfg_dir, " client-key-data: %(key)s\n" % {'name': cluster.name, 'api_address': cluster.api_address, - 'key': base64.b64encode(certs['key']), - 'cert': base64.b64encode(certs['cert']), - 'ca': base64.b64encode(certs['ca'])}) + 'key': base64.encode_as_text(certs['key']), + 'cert': base64.encode_as_text(certs['cert']), + 'ca': base64.encode_as_text(certs['ca'])}) else: cfg = ("apiVersion: v1\n" "clusters:\n" @@ -250,7 +250,7 @@ def _config_cluster_kubernetes(cluster, cluster_template, cfg_dir, " fi\n" % {'name': cluster.name, 'api_address': cluster.api_address, - 'ca': base64.b64encode(certs['ca'])}) + 'ca': base64.encode_as_text(certs['ca'])}) if os.path.exists(cfg_file) and not force: raise exc.CommandError("File %s exists, aborting." % cfg_file) diff --git a/magnumclient/tests/osc/unit/v1/fakes.py b/magnumclient/tests/osc/unit/v1/fakes.py index df11d411..3005547f 100644 --- a/magnumclient/tests/osc/unit/v1/fakes.py +++ b/magnumclient/tests/osc/unit/v1/fakes.py @@ -62,12 +62,17 @@ class FakeQuotasModelManager(object): pass +class FakeCertificatesModelManager(FakeBaseModelManager): + def get(self, cluster_uuid): + pass + + class MagnumFakeContainerInfra(object): def __init__(self): self.cluster_templates = FakeBaseModelManager() self.clusters = FakeBaseModelManager() self.mservices = FakeBaseModelManager() - self.certificates = FakeBaseModelManager() + self.certificates = FakeCertificatesModelManager() self.stats = FakeStatsModelManager() self.quotas = FakeQuotasModelManager() @@ -237,6 +242,11 @@ class FakeCluster(object): return cluster +class FakeCert(object): + def __init__(self, pem): + self.pem = pem + + class FakeQuota(object): """Fake one or more Quota""" diff --git a/magnumclient/tests/osc/unit/v1/test_clusters.py b/magnumclient/tests/osc/unit/v1/test_clusters.py index 4783c138..d4cfd955 100644 --- a/magnumclient/tests/osc/unit/v1/test_clusters.py +++ b/magnumclient/tests/osc/unit/v1/test_clusters.py @@ -37,6 +37,8 @@ class TestCluster(magnum_fakes.TestMagnumClientOSCV1): super(TestCluster, self).setUp() self.clusters_mock = self.app.client_manager.container_infra.clusters + self.certificates_mock = \ + self.app.client_manager.container_infra.certificates class TestClusterCreate(TestCluster): @@ -360,10 +362,15 @@ class TestClusterConfig(TestCluster): self.clusters_mock.get = mock.Mock() self.clusters_mock.get.return_value = self._cluster + cert = magnum_fakes.FakeCert(pem='foo bar') + self.certificates_mock.create = mock.Mock() + self.certificates_mock.create.return_value = cert + self.certificates_mock.get = mock.Mock() + self.certificates_mock.get.return_value = cert + # Fake the cluster_template attr = dict() attr['name'] = 'fake-ct' - attr['tls_disabled'] = True self._cluster_template = \ magnum_fakes.FakeClusterTemplate.create_one_cluster_template(attr)