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
This commit is contained in:
Jake Yip 2019-02-20 15:37:58 +11:00
parent 37e602d160
commit 3f7b994acd
3 changed files with 24 additions and 7 deletions

View File

@ -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)

View File

@ -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"""

View File

@ -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)