From 36bc97d8100d2086dad39884bbb4f3d6ea197d5c Mon Sep 17 00:00:00 2001 From: chenying Date: Tue, 26 Dec 2017 00:22:45 +0800 Subject: [PATCH] Fix the unit test error about k8s client Change-Id: I50684b6025a9e634e1c9e106296d4b860eff228b --- karbor/services/protection/clients/k8s.py | 6 +++--- karbor/tests/unit/clients/test_k8s_client.py | 7 +++++-- karbor/tests/unit/plugins/test_pod_protectable_plugin.py | 6 ++++-- .../tests/unit/plugins/test_volume_protectable_plugin.py | 8 ++++++-- .../tests/unit/protection/test_pod_protection_plugin.py | 8 +++++--- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/karbor/services/protection/clients/k8s.py b/karbor/services/protection/clients/k8s.py index 9e21de40..735bef3b 100644 --- a/karbor/services/protection/clients/k8s.py +++ b/karbor/services/protection/clients/k8s.py @@ -12,7 +12,7 @@ from kubernetes import client from kubernetes.client import api_client -from kubernetes.client import ConfigurationObject +from kubernetes.client.configuration import Configuration from oslo_config import cfg from oslo_log import log as logging @@ -44,11 +44,11 @@ def create(context, conf, **kwargs): LOG.info('Creating the kubernetes client with url %s.', client_config.k8s_host) - config = ConfigurationObject() + config = Configuration() config.host = client_config.k8s_host config.ssl_ca_cert = client_config.k8s_ssl_ca_cert config.cert_file = client_config.k8s_cert_file config.key_file = client_config.k8s_key_file - k8s_api_client = api_client.ApiClient(config=config) + k8s_api_client = api_client.ApiClient(config) k8s_core_v1_api = client.CoreV1Api(k8s_api_client) return k8s_core_v1_api diff --git a/karbor/tests/unit/clients/test_k8s_client.py b/karbor/tests/unit/clients/test_k8s_client.py index 075330d4..139867b9 100644 --- a/karbor/tests/unit/clients/test_k8s_client.py +++ b/karbor/tests/unit/clients/test_k8s_client.py @@ -16,6 +16,8 @@ from karbor.context import RequestContext from karbor.services.protection.clients import k8s from karbor.tests import base +import mock + class KubernetesClientTest(base.TestCase): def setUp(self): @@ -41,7 +43,8 @@ class KubernetesClientTest(base.TestCase): '/etc/provider.d/client-admin.key', 'k8s_client') - def test_create_client(self): + @mock.patch('kubernetes.client.api_client.ApiClient.__del__') + def test_create_client(self, mock_k8s_delete): client = k8s.create(self._context, self.conf) - self.assertEqual(client.api_client.host, self.host_url) + self.assertEqual(client.api_client.configuration.host, self.host_url) diff --git a/karbor/tests/unit/plugins/test_pod_protectable_plugin.py b/karbor/tests/unit/plugins/test_pod_protectable_plugin.py index 7eb605c5..34de52fc 100644 --- a/karbor/tests/unit/plugins/test_pod_protectable_plugin.py +++ b/karbor/tests/unit/plugins/test_pod_protectable_plugin.py @@ -46,9 +46,10 @@ class PodProtectablePluginTest(base.TestCase): self.assertEqual(("OS::Keystone::Project"), plugin.get_parent_resource_types()) + @mock.patch('kubernetes.client.api_client.ApiClient.__del__') @mock.patch('kubernetes.client.apis.core_v1_api.' 'CoreV1Api.list_namespaced_pod') - def test_list_resources(self, mock_pod_list): + def test_list_resources(self, mock_pod_list, mock_pod_delete): plugin = K8sPodProtectablePlugin(self._context, cfg.CONF) pod = V1Pod(api_version="v1", kind="Pod", @@ -65,9 +66,10 @@ class PodProtectablePluginTest(base.TestCase): 'default:busybox-test')], plugin.list_resources(self._context)) + @mock.patch('kubernetes.client.api_client.ApiClient.__del__') @mock.patch('kubernetes.client.apis.core_v1_api.' 'CoreV1Api.read_namespaced_pod') - def test_show_resource(self, mock_pod_get): + def test_show_resource(self, mock_pod_get, mock_pod_delete): plugin = K8sPodProtectablePlugin(self._context, cfg.CONF) pod = V1Pod(api_version="v1", kind="Pod", diff --git a/karbor/tests/unit/plugins/test_volume_protectable_plugin.py b/karbor/tests/unit/plugins/test_volume_protectable_plugin.py index ff4752fe..51cc04b5 100644 --- a/karbor/tests/unit/plugins/test_volume_protectable_plugin.py +++ b/karbor/tests/unit/plugins/test_volume_protectable_plugin.py @@ -151,6 +151,7 @@ class VolumeProtectablePluginTest(base.TestCase): {'availability_zone': 'az1'})], plugin.get_dependent_resources(self._context, project)) + @mock.patch('kubernetes.client.api_client.ApiClient.__del__') @mock.patch.object(volumes.VolumeManager, 'list') @mock.patch('kubernetes.client.apis.core_v1_api.' 'CoreV1Api.read_persistent_volume') @@ -161,7 +162,8 @@ class VolumeProtectablePluginTest(base.TestCase): def test_get_pod_dependent_resources(self, mock_pod_read, mock_pvc_read, mock_pv_read, - mock_volume_list): + mock_volume_list, + mock_delete): plugin = VolumeProtectablePlugin(self._context) pod = V1Pod(api_version="v1", kind="Pod", @@ -171,9 +173,11 @@ class VolumeProtectablePluginTest(base.TestCase): uid="dd8236e1-8c6c-11e7-9b7a-fa163e18e097"), spec=V1PodSpec( volumes=[V1Volume( + name="name123", persistent_volume_claim=( V1PersistentVolumeClaimVolumeSource( - claim_name="cinder-claim1'")))]), + claim_name="cinder-claim1'")))], + containers=[]), status=V1PodStatus(phase="Running")) pvc = V1PersistentVolumeClaim( diff --git a/karbor/tests/unit/protection/test_pod_protection_plugin.py b/karbor/tests/unit/protection/test_pod_protection_plugin.py index ef815e9c..a920a24d 100644 --- a/karbor/tests/unit/protection/test_pod_protection_plugin.py +++ b/karbor/tests/unit/protection/test_pod_protection_plugin.py @@ -110,8 +110,7 @@ class PodProtectionPluginTest(base.TestCase): project_id='abcd', auth_token='efgh', service_catalog=None) - self.k8s_client = client_factory.ClientFactory.create_client( - "k8s", self.cntxt) + self.k8s_client = None self.checkpoint = Checkpoint() def test_get_options_schema(self): @@ -132,8 +131,11 @@ class PodProtectionPluginTest(base.TestCase): self.assertEqual(options_schema, pod_plugin_schemas.SAVED_INFO_SCHEMA) + @mock.patch('kubernetes.client.api_client.ApiClient.__del__') @mock.patch('karbor.services.protection.clients.k8s.create') - def test_create_backup(self, mock_k8s_create): + def test_create_backup(self, mock_k8s_create, mock_k8s_delete): + self.k8s_client = client_factory.ClientFactory.create_client( + "k8s", self.cntxt) resource = Resource(id="c88b92a8-e8b4-504c-bad4-343d92061871", type=constants.POD_RESOURCE_TYPE, name='default:busybox-test')