diff --git a/src/rookify/modules/module.py b/src/rookify/modules/module.py index 2499916..91f3131 100644 --- a/src/rookify/modules/module.py +++ b/src/rookify/modules/module.py @@ -25,19 +25,18 @@ def __init__(self, machine: Machine, config: Dict[str, Any]): :param config: The global config file :return: returns nothing """ + self._ceph: Optional[Ceph] = None self._config = config + self._k8s: Optional[K8s] = None self._machine = machine - - self.__ceph: Optional[Ceph] = None - self.__k8s: Optional[K8s] = None - self.__ssh: Optional[SSH] = None - self.__logger = get_logger() + self._ssh: Optional[SSH] = None + self._logger = get_logger() @property def ceph(self) -> Ceph: - if self.__ceph is None: - self.__ceph = Ceph(self._config["ceph"]) - return self.__ceph + if self._ceph is None: + self._ceph = Ceph(self._config["ceph"]) + return self._ceph @property def machine(self) -> Machine: @@ -45,19 +44,19 @@ def machine(self) -> Machine: @property def k8s(self) -> K8s: - if self.__k8s is None: - self.__k8s = K8s(self._config["kubernetes"]) - return self.__k8s + if self._k8s is None: + self._k8s = K8s(self._config["kubernetes"]) + return self._k8s @property def logger(self) -> structlog.getLogger: - return self.__logger + return self._logger @property def ssh(self) -> SSH: - if self.__ssh is None: - self.__ssh = SSH(self._config["ssh"]) - return self.__ssh + if self._ssh is None: + self._ssh = SSH(self._config["ssh"]) + return self._ssh @abc.abstractmethod def preflight(self) -> None: diff --git a/tests/mock_k8s_prerequisite_check.py b/tests/mock_k8s_prerequisite_check.py index fc329de..6be782d 100644 --- a/tests/mock_k8s_prerequisite_check.py +++ b/tests/mock_k8s_prerequisite_check.py @@ -1,26 +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, *args: Any, **kwargs: Any) -> None: - super().__init__(*args, **kwargs) - self._k8s = None - - @property - def k8s(self) -> Any: - return self._k8s - - @k8s.setter - def k8s(self, value: Any) -> None: - self._k8s = value - - def preflight(self) -> None: - super().preflight() - pass + def __init__(self, request_callback: Any, *args: Any, **kwargs: Any) -> None: + K8sPrerequisitesCheckHandler.__init__(self, *args, **kwargs) + self._k8s = MockK8s(request_callback) # type: ignore - # Note: This solves the error 'cannot instantiate abstract class "MockK8sPrerequisitesCheckHandler" with abstract attribute "execute"' def execute(self) -> None: - super().execute() - pass + K8sPrerequisitesCheckHandler.execute(self) diff --git a/tests/modules/test_k8s_prerequisites_check.py b/tests/modules/test_k8s_prerequisites_check.py index 8a1ae28..e91daa8 100644 --- a/tests/modules/test_k8s_prerequisites_check.py +++ b/tests/modules/test_k8s_prerequisites_check.py @@ -2,9 +2,8 @@ from unittest.mock import Mock from rookify.modules.exception import ModuleException -from ..mock_k8s import MockK8s -from ..mock_k8s_prerequisite_check import MockK8sPrerequisitesCheckHandler 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 @@ -41,22 +40,19 @@ def __init__(self, items: List[Namespace]): ) def test_namespaces(self) -> None: - # Instantiate MockK8s - mock_k8s = MockK8s(self._request_callback) # Instantiate K8sPrerequisitesCheckHandler with the mock ModuleHandler - handler_instance = MockK8sPrerequisitesCheckHandler(Mock(), self.config) + handler_instance = MockK8sPrerequisitesCheckHandler( + self._request_callback, Mock(), self.config + ) # Set the k8s attribute to the mock_k8s instance - handler_instance.k8s = mock_k8s - # Call the preflight method to run the test handler_instance.preflight() def test_namespaces_fails(self) -> None: - # Instantiate MockK8s - mock_k8s = MockK8s(self._request_callback) # Instantiate K8sPrerequisitesCheckHandler with the mock ModuleHandler - handler_instance = MockK8sPrerequisitesCheckHandler(Mock(), self.config) - # Set the k8s attribute to the mock_k8s instance - handler_instance.k8s = mock_k8s + 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