diff options
author | Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com> | 2018-07-23 07:54:22 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-07-23 07:54:22 +0000 |
commit | 4203a505e0d90ea198577bd477670697c6a7df14 (patch) | |
tree | d741cb2eb5417a509b276110bd5cdfc5ff8314f0 | |
parent | 3a67d335d32ba460033ba042c1dd66ea2ab852a1 (diff) | |
parent | 48d6322b8e64a04437b1b816cfb3d956932a5298 (diff) |
Merge "kubernetes_utils: avoid 404 error code in delete_service()"
-rw-r--r-- | yardstick/common/kubernetes_utils.py | 13 | ||||
-rw-r--r-- | yardstick/orchestrator/kubernetes.py | 2 | ||||
-rw-r--r-- | yardstick/tests/unit/common/test_kubernetes_utils.py | 31 | ||||
-rw-r--r-- | yardstick/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 8d9fc41c9..cfbeb3fa3 100644 --- a/yardstick/orchestrator/kubernetes.py +++ b/yardstick/orchestrator/kubernetes.py @@ -308,7 +308,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 a73a4a132..5a6f8c6a0 100644 --- a/yardstick/tests/unit/orchestrator/test_kubernetes.py +++ b/yardstick/tests/unit/orchestrator/test_kubernetes.py @@ -611,7 +611,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): |