Skip to content

Commit

Permalink
Fix ModuleHandler mock for module k8s_prerequisites_check
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Wolf <[email protected]>
  • Loading branch information
NotTheEvilOne committed Jun 12, 2024
1 parent 99fcadb commit 79fbc90
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
29 changes: 14 additions & 15 deletions src/rookify/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,38 @@ 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:
return 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:
Expand Down
25 changes: 7 additions & 18 deletions tests/mock_k8s_prerequisite_check.py
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 8 additions & 12 deletions tests/modules/test_k8s_prerequisites_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 79fbc90

Please sign in to comment.