55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
from unittest import mock
|
|
|
|
import kubernetes.client.api.core_v1_api
|
|
import pytest
|
|
|
|
import urllib3.response
|
|
from kubernetes.client import ApiException
|
|
from kubernetes.client.rest import RESTResponse
|
|
from kube import KubernetesService
|
|
|
|
# fmt: off
|
|
_result_deleted = {'api_version': 'v1', 'code': None, 'details': None, 'kind': 'Namespace', 'message': None, 'metadata': {'_continue': None, 'remaining_item_count': None, 'resource_version': '4424', 'self_link': '/api/v1/namespaces/mydep3',}, 'reason': None, 'status': "{'phase': 'Terminating'}",}
|
|
_result_not_found = ApiException(status=404, http_resp=RESTResponse(urllib3.response.HTTPResponse(body=b'{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"namespaces \\"mydep3\\" not found","reason":"NotFound","details":{"name":"mydep3","kind":"namespaces"},"code":404}')))
|
|
# fmt: on
|
|
|
|
|
|
@pytest.fixture()
|
|
def mock_k8s():
|
|
yield mock.MagicMock(autospec=kubernetes.client.api.core_v1_api.CoreV1Api)
|
|
|
|
|
|
def test_delete_forbidden_namespace(mock_k8s):
|
|
service = KubernetesService(mock_k8s)
|
|
|
|
with pytest.raises(AssertionError):
|
|
service.delete_namespace('default')
|
|
|
|
|
|
def test_delete_namespace(mock_k8s):
|
|
mock_k8s.delete_namespace.return_value = _result_deleted
|
|
service = KubernetesService(mock_k8s)
|
|
|
|
service.delete_namespace('app-uuid')
|
|
|
|
mock_k8s.delete_namespace.assert_called_once_with('app-uuid')
|
|
|
|
|
|
def test_deleting_nonexistent_namespace_is_ok(mock_k8s):
|
|
mock_k8s.delete_namespace.side_effect = _result_not_found
|
|
service = KubernetesService(mock_k8s)
|
|
|
|
with mock.patch('logging.Logger.error') as m_log:
|
|
service.delete_namespace('app-uuid')
|
|
|
|
m_log.assert_called()
|
|
|
|
|
|
def test_any_other_error_bubbles_up(mock_k8s):
|
|
mock_k8s.delete_namespace.side_effect = ApiException(status=500, http_resp=urllib3.response.HTTPResponse(body=b''))
|
|
|
|
service = KubernetesService(mock_k8s)
|
|
|
|
with pytest.raises(ApiException):
|
|
service.delete_namespace('app-uuid')
|