From 48d6322b8e64a04437b1b816cfb3d956932a5298 Mon Sep 17 00:00:00 2001 From: "Mytnyk, Volodymyr" Date: Wed, 18 Jul 2018 17:05:24 +0100 Subject: kubernetes_utils: avoid 404 error code in delete_service() JIRA: YARDSTICK-1321 Change-Id: Id0f3a5d93f11674fd3908a7f5cf7214afcfa3600 Signed-off-by: Mytnyk, Volodymyr --- yardstick/common/kubernetes_utils.py | 13 +++++---- yardstick/orchestrator/kubernetes.py | 2 +- .../tests/unit/common/test_kubernetes_utils.py | 31 ++++++++++++++++++++++ .../tests/unit/orchestrator/test_kubernetes.py | 3 ++- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/yardstick/common/kubernetes_utils.py b/yardstick/common/kubernetes_utils.py index 35e590f2b..4bc1ac27f 100644 --- a/yardstick/common/kubernetes_utils.py +++ b/yardstick/common/kubernetes_utils.py @@ -75,15 +75,18 @@ def create_service(template, raise -def delete_service(name, - namespace='default', - **kwargs): # pragma: no cover +def delete_service(name, namespace='default', skip_codes=None, **kwargs): + skip_codes = [] if not skip_codes else skip_codes core_v1_api = get_core_api() try: body = client.V1DeleteOptions() core_v1_api.delete_namespaced_service(name, namespace, body, **kwargs) - except ApiException: - LOG.exception('Delete Service failed') + except ApiException as e: + if e.status in skip_codes: + LOG.info(e.reason) + else: + raise exceptions.KubernetesApiException( + action='delete', resource='Service') def get_service_list(namespace='default', **kwargs): diff --git a/yardstick/orchestrator/kubernetes.py b/yardstick/orchestrator/kubernetes.py index 98832908c..bf7aa9c1b 100644 --- a/yardstick/orchestrator/kubernetes.py +++ b/yardstick/orchestrator/kubernetes.py @@ -274,7 +274,7 @@ class ServiceNodePortObject(object): k8s_utils.create_service(self.template) def delete(self): - k8s_utils.delete_service(self._name) + k8s_utils.delete_service(self._name, skip_codes=[404]) class CustomResourceDefinitionObject(object): diff --git a/yardstick/tests/unit/common/test_kubernetes_utils.py b/yardstick/tests/unit/common/test_kubernetes_utils.py index bdc2c12d5..637424f48 100644 --- a/yardstick/tests/unit/common/test_kubernetes_utils.py +++ b/yardstick/tests/unit/common/test_kubernetes_utils.py @@ -250,3 +250,34 @@ class DeletePodTestCase(base.BaseUnitTestCase): mock_get_api.return_value = mock_api kubernetes_utils.delete_pod(mock.ANY, skip_codes=[404]) + + +class DeleteServiceTestCase(base.BaseUnitTestCase): + @mock.patch.object(client, "V1DeleteOptions") + @mock.patch.object(kubernetes_utils, 'get_core_api') + def test_execute_correct(self, mock_get_api, mock_options): + mock_api = mock.Mock() + mock_get_api.return_value = mock_api + mock_options.return_value = None + kubernetes_utils.delete_service("name", "default", None) + mock_api.delete_namespaced_service.assert_called_once_with( + "name", 'default', None) + + @mock.patch.object(kubernetes_utils, 'get_core_api') + def test_execute_exception(self, mock_get_api): + mock_api = mock.Mock() + mock_api.delete_namespaced_service.side_effect = rest.ApiException(status=200) + + mock_get_api.return_value = mock_api + with self.assertRaises(exceptions.KubernetesApiException): + kubernetes_utils.delete_service(mock.ANY, skip_codes=[404]) + + @mock.patch.object(kubernetes_utils, 'get_core_api') + @mock.patch.object(kubernetes_utils, 'LOG') + def test_execute_skip_exception(self, mock_log, mock_get_api): + mock_api = mock.Mock() + mock_api.delete_namespaced_service.side_effect = rest.ApiException(status=404) + + mock_get_api.return_value = mock_api + kubernetes_utils.delete_service(mock.ANY, skip_codes=[404]) + mock_log.info.assert_called_once() diff --git a/yardstick/tests/unit/orchestrator/test_kubernetes.py b/yardstick/tests/unit/orchestrator/test_kubernetes.py index 8d351e419..0c6fd0adf 100644 --- a/yardstick/tests/unit/orchestrator/test_kubernetes.py +++ b/yardstick/tests/unit/orchestrator/test_kubernetes.py @@ -545,7 +545,8 @@ class ServiceNodePortObjectTestCase(base.BaseUnitTestCase): def test_delete(self, mock_delete_service): nodeport_object = kubernetes.ServiceNodePortObject('fake_name') nodeport_object.delete() - mock_delete_service.assert_called_once_with('fake_name-service') + mock_delete_service.assert_called_once_with('fake_name-service', + skip_codes=[404]) class KubernetesTemplate(base.BaseUnitTestCase): -- cgit 1.2.3-korg