Skip to content

Commit

Permalink
feat: add unit-test to check for empty response in list_deployment_fo…
Browse files Browse the repository at this point in the history
…r_all_namespaces()

Signed-off-by: Boekhorst <[email protected]>
  • Loading branch information
boekhorstb1 committed Sep 9, 2024
1 parent 85503cc commit 28f5dfd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/mock_k8s_prerequisite_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@


# Note: currently this test works with pytest but not with unittest, which is not able to import needed classes
class MockK8sPrerequisitesCheckHandler(K8sPrerequisitesCheckHandler):
class MockK8sPrerequisitesCheckHandler(K8sPrerequisitesCheckHandler): # type: ignore
def __init__(self, request_callback: Any, *args: Any, **kwargs: Any) -> None:
K8sPrerequisitesCheckHandler.__init__(self, *args, **kwargs)
self._k8s = MockK8s(request_callback) # type: ignore
self._k8s = MockK8s(request_callback)

def execute(self) -> None:
K8sPrerequisitesCheckHandler.execute(self)
17 changes: 17 additions & 0 deletions tests/modules/test_k8s_prerequisites_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ class TestK8sPrerequisitesCheckHandler(unittest.TestCase):
def setUp(self) -> None:
# Mock configuration
self.config = {"rook": {"cluster": {"namespace": "test-namespace"}}}
# No response option
self.empty_response = False

def _request_callback(
self, method: str, *args: List[Any], **kwargs: Dict[Any, Any]
) -> Any:
if method == "apps_v1_api.list_deployment_for_all_namespaces":
if self.empty_response is True:
return MockResponse([])
return MockResponse(["apple", "banana", "cherry"])
if method == "core_v1_api.list_namespace":
return self._mock_list_namespace()
Expand Down Expand Up @@ -49,6 +53,19 @@ def test_namespaces(self) -> None:
# Set the k8s attribute to the mock_k8s instance
handler_instance.preflight()

def test_list_deployment_for_all_namespaces_fails(self) -> None:
# Set no response
self.empty_response = True

# Instantiate K8sPrerequisitesCheckHandler with the mock ModuleHandler
handler_instance = MockK8sPrerequisitesCheckHandler(
self._request_callback, Mock(), self.config
)

# Call the preflight method to run the test
with self.assertRaises(ModuleException):
handler_instance.preflight()

def test_namespaces_fails(self) -> None:
# Instantiate K8sPrerequisitesCheckHandler with the mock ModuleHandler
handler_instance = MockK8sPrerequisitesCheckHandler(
Expand Down

0 comments on commit 28f5dfd

Please sign in to comment.