Make kubernetes API client generic

Make kubernetes API client generic. Right now each k8s object has
a way to create k8s API client. This was needed to make sure the
tests passed. Now since all the objects-from-bay patches are merged
in its better to make the client creation logic generic.

Change-Id: Ic2a9b16bbd4c13794c7ef95309bd260366ec872e
Closes-Bug: #1502367
This commit is contained in:
Vilobh Meshram 2015-12-21 15:20:49 -08:00
parent 2862a60203
commit fee4b609c0
4 changed files with 47 additions and 285 deletions

View File

@ -40,7 +40,7 @@ class Handler(object):
def service_create(self, context, service):
LOG.debug("service_create")
self.k8s_api = k8s.create_k8s_api_service(context, service.bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, service.bay_uuid)
manifest = k8s_manifest.parse(service.manifest)
try:
resp = self.k8s_api.create_namespaced_service(body=manifest,
@ -73,7 +73,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_service(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(service_ident):
service = objects.Service.get_by_uuid(context,
service_ident,
@ -124,7 +124,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_service(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(service_ident):
service = objects.Service.get_by_uuid(context, service_ident,
bay_uuid, self.k8s_api)
@ -148,7 +148,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_service(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(service_ident):
service = objects.Service.get_by_uuid(context, service_ident,
bay_uuid, self.k8s_api)
@ -163,7 +163,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_service(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
try:
resp = self.k8s_api.list_namespaced_service(namespace='default')
except rest.ApiException as err:
@ -205,7 +205,7 @@ class Handler(object):
# Pod Operations
def pod_create(self, context, pod):
LOG.debug("pod_create")
self.k8s_api = k8s.create_k8s_api_pod(context, pod.bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, pod.bay_uuid)
manifest = k8s_manifest.parse(pod.manifest)
try:
resp = self.k8s_api.create_namespaced_pod(body=manifest,
@ -232,7 +232,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_pod(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(pod_ident):
pod = objects.Pod.get_by_uuid(context, pod_ident,
bay_uuid, self.k8s_api)
@ -271,7 +271,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_pod(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(pod_ident):
pod = objects.Pod.get_by_uuid(context, pod_ident,
bay_uuid, self.k8s_api)
@ -294,7 +294,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_pod(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(pod_ident):
pod = objects.Pod.get_by_uuid(context, pod_ident,
bay_uuid, self.k8s_api)
@ -309,7 +309,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_pod(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
try:
resp = self.k8s_api.list_namespaced_pod(namespace='default')
except rest.ApiException as err:
@ -342,7 +342,7 @@ class Handler(object):
# Replication Controller Operations
def rc_create(self, context, rc):
LOG.debug("rc_create")
self.k8s_api = k8s.create_k8s_api_rc(context, rc.bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, rc.bay_uuid)
manifest = k8s_manifest.parse(rc.manifest)
try:
resp = self.k8s_api.create_namespaced_replication_controller(
@ -368,7 +368,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_rc(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(rc_ident):
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
bay_uuid,
@ -405,7 +405,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_rc(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(rc_ident):
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
bay_uuid,
@ -431,7 +431,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_rc(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
if utils.is_uuid_like(rc_ident):
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
bay_uuid,
@ -448,7 +448,7 @@ class Handler(object):
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_rc(context, bay_uuid)
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
try:
resp = self.k8s_api.list_namespaced_replication_controller(
namespace='default')

View File

@ -20,7 +20,6 @@ from magnum.common.pythonk8sclient.swagger_client import api_client
from magnum.common.pythonk8sclient.swagger_client.apis import apiv_api
from magnum.conductor.handlers.common import cert_manager
from magnum.conductor import utils
from magnum.objects.bay import Bay
LOG = logging.getLogger(__name__)
@ -43,17 +42,12 @@ class K8sAPI(apiv_api.ApivApi):
raise err
return tmp
def __init__(self, context, obj):
def __init__(self, context, bay_uuid):
self.ca_file = None
self.cert_file = None
self.key_file = None
# If the obj is already a Bay we need not retrieve a Bay.
if isinstance(obj, Bay):
bay = obj
else:
bay = utils.retrieve_bay(context, obj.bay_uuid)
bay = utils.retrieve_bay(context, bay_uuid)
if bay.magnum_cert_ref:
self._create_certificate_files(bay)
@ -89,81 +83,7 @@ class K8sAPI(apiv_api.ApivApi):
self.key_file.close()
def create_k8s_api(context, obj):
"""Create a kubernetes API client
Creates connection with Kubernetes master and creates ApivApi instance
to call Kubernetes APIs.
:param context: The security context
:param obj: A bay or a k8s object (Pod, Service, ReplicationController)
"""
return K8sAPI(context, obj)
# NB : This is a place holder class. This class will be
# removed once the objects from bay code for k8s
# objects is merged.
class K8sAPI_Service(apiv_api.ApivApi):
def _create_temp_file_with_content(self, content):
"""Creates temp file and write content to the file.
:param content: file content
:returns: temp file
"""
try:
tmp = NamedTemporaryFile(delete=True)
tmp.write(content)
tmp.flush()
except Exception as err:
LOG.error("Error while creating temp file: %s" % err)
raise err
return tmp
def __init__(self, context, bay_uuid):
self.ca_file = None
self.cert_file = None
self.key_file = None
bay = utils.retrieve_bay(context, bay_uuid)
if bay.magnum_cert_ref:
self._create_certificate_files(bay)
# build a connection with Kubernetes master
client = api_client.ApiClient(bay.api_address,
key_file=self.key_file.name,
cert_file=self.cert_file.name,
ca_certs=self.ca_file.name)
super(K8sAPI_Service, self).__init__(client)
def _create_certificate_files(self, bay):
"""Read certificate and key for a bay and stores in files.
:param bay: Bay object
"""
magnum_cert_obj = cert_manager.get_bay_magnum_cert(bay)
self.cert_file = self._create_temp_file_with_content(
magnum_cert_obj.get_certificate())
private_key = magnum_cert_obj.get_decrypted_private_key()
self.key_file = self._create_temp_file_with_content(
private_key)
ca_cert_obj = cert_manager.get_bay_ca_certificate(bay)
self.ca_file = self._create_temp_file_with_content(
ca_cert_obj.get_certificate())
def __del__(self):
if self.ca_file:
self.ca_file.close()
if self.cert_file:
self.cert_file.close()
if self.key_file:
self.key_file.close()
def create_k8s_api_service(context, bay_uuid):
def create_k8s_api(context, bay_uuid):
"""Create a kubernetes API client
Creates connection with Kubernetes master and creates ApivApi instance
@ -172,152 +92,4 @@ def create_k8s_api_service(context, bay_uuid):
:param context: The security context
:param bay_uuid: Unique identifier for the Bay
"""
return K8sAPI_Service(context, bay_uuid)
# NB : This is a place holder class. This class and create_k8s_api_pod
# method will be removed once the objects from bay code for k8s
# objects is merged. These changes are temporary to get the Unit
# test working.
class K8sAPI_Pod(apiv_api.ApivApi):
def _create_temp_file_with_content(self, content):
"""Creates temp file and write content to the file.
:param content: file content
:returns: temp file
"""
try:
tmp = NamedTemporaryFile(delete=True)
tmp.write(content)
tmp.flush()
except Exception as err:
LOG.error("Error while creating temp file: %s" % err)
raise err
return tmp
def __init__(self, context, bay_uuid):
self.ca_file = None
self.cert_file = None
self.key_file = None
bay = utils.retrieve_bay(context, bay_uuid)
if bay.magnum_cert_ref:
self._create_certificate_files(bay)
# build a connection with Kubernetes master
client = api_client.ApiClient(bay.api_address,
key_file=self.key_file.name,
cert_file=self.cert_file.name,
ca_certs=self.ca_file.name)
super(K8sAPI_Pod, self).__init__(client)
def _create_certificate_files(self, bay):
"""Read certificate and key for a bay and stores in files.
:param bay: Bay object
"""
magnum_cert_obj = cert_manager.get_bay_magnum_cert(bay)
self.cert_file = self._create_temp_file_with_content(
magnum_cert_obj.get_certificate())
private_key = magnum_cert_obj.get_decrypted_private_key()
self.key_file = self._create_temp_file_with_content(
private_key)
ca_cert_obj = cert_manager.get_bay_ca_certificate(bay)
self.ca_file = self._create_temp_file_with_content(
ca_cert_obj.get_certificate())
def __del__(self):
if self.ca_file:
self.ca_file.close()
if self.cert_file:
self.cert_file.close()
if self.key_file:
self.key_file.close()
def create_k8s_api_pod(context, bay_uuid):
"""Create a kubernetes API client
Creates connection with Kubernetes master and creates ApivApi instance
to call Kubernetes APIs.
:param context: The security context
:param bay_uuid: Unique identifier for the Bay
"""
return K8sAPI_Pod(context, bay_uuid)
# NB : This is a place holder class. This class and create_k8s_api_rc
# method will be removed once the objects from bay code for k8s
# objects is merged. These changes are temporary to get the Unit
# test working.
class K8sAPI_RC(apiv_api.ApivApi):
def _create_temp_file_with_content(self, content):
"""Creates temp file and write content to the file.
:param content: file content
:returns: temp file
"""
try:
tmp = NamedTemporaryFile(delete=True)
tmp.write(content)
tmp.flush()
except Exception as err:
LOG.error("Error while creating temp file: %s" % err)
raise err
return tmp
def __init__(self, context, bay_uuid):
self.ca_file = None
self.cert_file = None
self.key_file = None
bay = utils.retrieve_bay(context, bay_uuid)
if bay.magnum_cert_ref:
self._create_certificate_files(bay)
# build a connection with Kubernetes master
client = api_client.ApiClient(bay.api_address,
key_file=self.key_file.name,
cert_file=self.cert_file.name,
ca_certs=self.ca_file.name)
super(K8sAPI_RC, self).__init__(client)
def _create_certificate_files(self, bay):
"""Read certificate and key for a bay and stores in files.
:param bay: Bay object
"""
magnum_cert_obj = cert_manager.get_bay_magnum_cert(bay)
self.cert_file = self._create_temp_file_with_content(
magnum_cert_obj.get_certificate())
private_key = magnum_cert_obj.get_decrypted_private_key()
self.key_file = self._create_temp_file_with_content(
private_key)
ca_cert_obj = cert_manager.get_bay_ca_certificate(bay)
self.ca_file = self._create_temp_file_with_content(
ca_cert_obj.get_certificate())
def __del__(self):
if self.ca_file:
self.ca_file.close()
if self.cert_file:
self.cert_file.close()
if self.key_file:
self.key_file.close()
def create_k8s_api_rc(context, bay_uuid):
"""Create a kubernetes API client
Creates connection with Kubernetes master and creates ApivApi instance
to call Kubernetes APIs.
:param context: The security context
:param bay_uuid: Unique identifier for the Bay
"""
return K8sAPI_RC(context, bay_uuid)
return K8sAPI(context, bay_uuid)

View File

@ -52,7 +52,7 @@ class TestK8sConductor(base.TestCase):
expected_pod.manifest = '{"key": "value"}'
mock_ast.return_value = {}
with patch('magnum.conductor.k8s_api.create_k8s_api_pod') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.pod_create(self.context, expected_pod)
(mock_kube_api.return_value.create_namespaced_pod
@ -63,7 +63,7 @@ class TestK8sConductor(base.TestCase):
manifest = {"key": "value"}
expected_pod.manifest = '{"key": "value"}'
with patch('magnum.conductor.k8s_api.create_k8s_api_pod') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=500)
mock_kube_api.return_value.create_namespaced_pod.side_effect = err
@ -80,7 +80,7 @@ class TestK8sConductor(base.TestCase):
expected_pod = mock.MagicMock()
expected_pod.manifest = '{"key": "value"}'
with patch('magnum.conductor.k8s_api.create_k8s_api_pod') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=409)
mock_kube_api.return_value.create_namespaced_pod.side_effect = err
@ -107,7 +107,7 @@ class TestK8sConductor(base.TestCase):
mock_pod_get_by_uuid.return_value = mock_pod
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_pod') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.pod_delete(self.context,
mock_pod.name,
@ -132,7 +132,7 @@ class TestK8sConductor(base.TestCase):
mock_pod_get_by_uuid.return_value = mock_pod
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_pod') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=500)
mock_kube_api.return_value.delete_namespaced_pod.side_effect = err
@ -161,7 +161,7 @@ class TestK8sConductor(base.TestCase):
mock_pod_get_by_uuid.return_value = mock_pod
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_pod') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=404)
mock_kube_api.return_value.delete_namespaced_pod.side_effect = err
@ -187,7 +187,7 @@ class TestK8sConductor(base.TestCase):
expected_service.manifest = '{"key": "value"}'
mock_ast.return_value = {}
with patch('magnum.conductor.k8s_api.create_k8s_api_service') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.service_create(self.context, expected_service)
(mock_kube_api.return_value.create_namespaced_service
@ -199,7 +199,7 @@ class TestK8sConductor(base.TestCase):
manifest = {"key": "value"}
expected_service.manifest = '{"key": "value"}'
with patch('magnum.conductor.k8s_api.create_k8s_api_service') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=404)
(mock_kube_api.return_value.create_namespaced_service
@ -228,7 +228,7 @@ class TestK8sConductor(base.TestCase):
mock_service_get_by_name.return_value = mock_service
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_service') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.service_delete(self.context,
mock_service.name,
@ -254,7 +254,7 @@ class TestK8sConductor(base.TestCase):
mock_service_get_by_uuid.return_value = mock_service
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_service') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=500)
(mock_kube_api.return_value.delete_namespaced_service
@ -286,7 +286,7 @@ class TestK8sConductor(base.TestCase):
mock_service_get_by_uuid.return_value = mock_service
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_service') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=404)
(mock_kube_api.return_value.delete_namespaced_service
@ -309,7 +309,7 @@ class TestK8sConductor(base.TestCase):
expected_rc.manifest = '{"key": "value"}'
mock_ast.return_value = {}
with patch('magnum.conductor.k8s_api.create_k8s_api_rc') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.rc_create({}, expected_rc)
(mock_kube_api.return_value
@ -321,7 +321,7 @@ class TestK8sConductor(base.TestCase):
manifest = {"key": "value"}
expected_rc.manifest = '{"key": "value"}'
with patch('magnum.conductor.k8s_api.create_k8s_api_rc') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=500)
(mock_kube_api.return_value
@ -350,7 +350,7 @@ class TestK8sConductor(base.TestCase):
bay_uuid = 'test-bay-uuid'
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_rc') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.rc_delete(self.context, mock_rc.name, bay_uuid)
(mock_kube_api.return_value
@ -374,7 +374,7 @@ class TestK8sConductor(base.TestCase):
mock_rc_get_by_uuid.return_value = mock_rc
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_rc') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=500)
(mock_kube_api.return_value
@ -408,7 +408,7 @@ class TestK8sConductor(base.TestCase):
mock_rc_get_by_uuid.return_value = mock_rc
mock_object_has_stack.return_value = True
with patch('magnum.conductor.k8s_api.create_k8s_api_rc') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=404)
(mock_kube_api.return_value
@ -444,7 +444,7 @@ class TestK8sConductor(base.TestCase):
mock_rc_get_by_name.return_value = expected_rc
name_rc = expected_rc.name
with patch('magnum.conductor.k8s_api.create_k8s_api_rc') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.rc_update(self.context, expected_rc.name,
expected_rc.bay_uuid,
@ -473,7 +473,7 @@ class TestK8sConductor(base.TestCase):
expected_rc.manifest = '{"key": "value"}'
name_rc = expected_rc.name
with patch('magnum.conductor.k8s_api.create_k8s_api_rc') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=404)
(mock_kube_api.return_value
@ -513,7 +513,7 @@ class TestK8sConductor(base.TestCase):
mock_service_get_by_uuid.return_value = expected_service
service_name = expected_service.name
with patch('magnum.conductor.k8s_api.create_k8s_api_service') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.service_update(self.context,
expected_service.name,
@ -542,7 +542,7 @@ class TestK8sConductor(base.TestCase):
mock_service_get_by_name.return_value = expected_service
service_name = expected_service.name
with patch('magnum.conductor.k8s_api.create_k8s_api_service') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=404)
(mock_kube_api.return_value.replace_namespaced_service
@ -580,7 +580,7 @@ class TestK8sConductor(base.TestCase):
mock_pod_get_by_name.return_value = expected_pod
name_pod = expected_pod.name
with patch('magnum.conductor.k8s_api.create_k8s_api_pod') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
self.kube_handler.pod_update(self.context, expected_pod.name,
expected_pod.bay_uuid,
@ -608,7 +608,7 @@ class TestK8sConductor(base.TestCase):
expected_pod.manifest = '{"key": "value"}'
name_pod = expected_pod.name
with patch('magnum.conductor.k8s_api.create_k8s_api_pod') as \
with patch('magnum.conductor.k8s_api.create_k8s_api') as \
mock_kube_api:
err = rest.ApiException(status=404)
mock_kube_api.return_value.replace_namespaced_pod.side_effect = err

View File

@ -16,7 +16,6 @@ import mock
from mock import patch
from magnum.conductor import k8s_api
from magnum import objects
from magnum.tests import base
@ -63,7 +62,7 @@ class TestK8sAPI(base.TestCase):
@patch('magnum.conductor.handlers.common.cert_manager.get_bay_magnum_cert')
@patch('magnum.conductor.utils.retrieve_bay')
@patch('magnum.common.pythonk8sclient.swagger_client.api_client.ApiClient')
def _test_create_k8s_api(self, cls,
def _test_create_k8s_api(self, bay_uuid,
mock_api_client,
mock_bay_retrieval,
mock_get_bay_magnum_cert,
@ -86,21 +85,12 @@ class TestK8sAPI(base.TestCase):
file_hdl.name = TestK8sAPI.file_name[content]
context = 'context'
obj = getattr(objects, cls)({})
if cls is not 'Bay':
self.assertFalse(hasattr(obj, 'bay_uuid'))
obj.bay_uuid = 'bay-uuid'
else:
obj = bay_obj
with patch(
'magnum.conductor.k8s_api.K8sAPI._create_temp_file_with_content',
side_effect=self._mock_named_file_creation):
k8s_api.create_k8s_api(context, obj)
k8s_api.create_k8s_api(context, bay_uuid)
if cls is not 'Bay':
mock_bay_retrieval.assert_called_once_with(context, obj.bay_uuid)
mock_bay_retrieval.assert_called_once_with(context, bay_uuid)
mock_api_client.assert_called_once_with(
bay_obj.api_address,
@ -109,13 +99,13 @@ class TestK8sAPI(base.TestCase):
ca_certs='ca-cert-temp-file-name')
def test_create_k8s_api_with_service(self):
self._test_create_k8s_api('Service')
self._test_create_k8s_api('bay-uuid')
def test_create_k8s_api_with_bay(self):
self._test_create_k8s_api('Bay')
def test_create_k8s_api_with_pod(self):
self._test_create_k8s_api('Pod')
self._test_create_k8s_api('bay-uuid')
def test_create_k8s_api_with_rc(self):
self._test_create_k8s_api('ReplicationController')
self._test_create_k8s_api('bay-uuid')