From 0d69561125df5a9d969459d6730efb8cb3f14463 Mon Sep 17 00:00:00 2001 From: Rafael te Boekhorst Date: Tue, 11 Jun 2024 16:47:09 +0200 Subject: [PATCH] feat: adding test for k8s prerequisites fix: adding 'object' to module path in order to use correct class definition, allows to remove ignore comments for mypy Signed-off-by: Rafael te Boekhorst --- src/rookify/modules/module.py | 2 +- tests/mock_k8s_prerequisite_check.py | 15 +++++ tests/modules/test_k8s_prerequisites_check.py | 60 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/mock_k8s_prerequisite_check.py create mode 100644 tests/modules/test_k8s_prerequisites_check.py diff --git a/src/rookify/modules/module.py b/src/rookify/modules/module.py index ec6ae8b..2098a54 100644 --- a/src/rookify/modules/module.py +++ b/src/rookify/modules/module.py @@ -12,7 +12,7 @@ from .template import Template -class ModuleHandler: +class ModuleHandler(object): """ ModuleHandler is an abstract class that modules have to extend. """ diff --git a/tests/mock_k8s_prerequisite_check.py b/tests/mock_k8s_prerequisite_check.py new file mode 100644 index 0000000..a18152a --- /dev/null +++ b/tests/mock_k8s_prerequisite_check.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- + +from rookify.modules.k8s_prerequisites_check.main import K8sPrerequisitesCheckHandler +from typing import Any +from .mock_k8s import MockK8s + + +# Note: currently this test works with pytest but not with unittest, which is not able to import needed classes +class MockK8sPrerequisitesCheckHandler(K8sPrerequisitesCheckHandler): + def __init__(self, request_callback: Any, *args: Any, **kwargs: Any) -> None: + K8sPrerequisitesCheckHandler.__init__(self, *args, **kwargs) + self._k8s = MockK8s(request_callback) + + def execute(self) -> None: + K8sPrerequisitesCheckHandler.execute(self) diff --git a/tests/modules/test_k8s_prerequisites_check.py b/tests/modules/test_k8s_prerequisites_check.py new file mode 100644 index 0000000..e91daa8 --- /dev/null +++ b/tests/modules/test_k8s_prerequisites_check.py @@ -0,0 +1,60 @@ +import unittest +from unittest.mock import Mock + +from rookify.modules.exception import ModuleException +from typing import Any, Dict, List +from ..mock_k8s_prerequisite_check import MockK8sPrerequisitesCheckHandler + + +# Note: currently this test works with pytest but not with unittest, which is not able to import needed classes +class TestK8sPrerequisitesCheckHandler(unittest.TestCase): + def setUp(self) -> None: + # Mock configuration + self.config = {"rook": {"cluster": {"namespace": "test-namespace"}}} + + def _request_callback( + self, method: str, *args: List[Any], **kwargs: Dict[Any, Any] + ) -> Any: + if method == "core_v1_api.list_namespace": + return self._mock_list_namespace() + + def _mock_list_namespace(self) -> Any: + class Metadata: + def __init__(self, name: str): + self.name = name + + class Namespace: + def __init__(self, name: str): + self.metadata = Metadata(name) + + class NamespaceList: + def __init__(self, items: List[Namespace]): + self.items = items + + return NamespaceList( + [ + Namespace("default"), + Namespace("kube-system"), + Namespace("test-namespace"), + ] + ) + + def test_namespaces(self) -> None: + # Instantiate K8sPrerequisitesCheckHandler with the mock ModuleHandler + handler_instance = MockK8sPrerequisitesCheckHandler( + self._request_callback, Mock(), self.config + ) + # Set the k8s attribute to the mock_k8s instance + handler_instance.preflight() + + def test_namespaces_fails(self) -> None: + # Instantiate K8sPrerequisitesCheckHandler with the mock ModuleHandler + handler_instance = MockK8sPrerequisitesCheckHandler( + self._request_callback, Mock(), self.config + ) + + # Modify the config to have a different namespace than what is expected + handler_instance._config["rook"]["cluster"]["namespace"] = "wrong-namespace" + # Call the preflight method to run the test + with self.assertRaises(ModuleException): + handler_instance.preflight()