Fix the unit test error about k8s client

Change-Id: I50684b6025a9e634e1c9e106296d4b860eff228b
This commit is contained in:
chenying 2017-12-26 00:22:45 +08:00
parent d565afe9d0
commit 36bc97d810
5 changed files with 23 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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