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 authored and boekhorstb1 committed Sep 9, 2024
1 parent b07f7d9 commit b758793
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
4 changes: 3 additions & 1 deletion src/rookify/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def machine(self) -> Machine:
@property
def k8s(self) -> K8s:
if self.__k8s is None:
self.__k8s = K8s(self._config)
self.__k8s = K8s(self._config["kubernetes"])
return self.__k8s

@property
Expand Down Expand Up @@ -115,6 +115,8 @@ def register_states(cls, machine: Machine, config: Dict[str, Any]) -> None:
)
)
else:
get_logger().debug("Registering states for {0}".format(state_name))

if preflight_state_name is not None:
cls.register_preflight_state(machine, preflight_state_name, handler)

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 b758793

Please sign in to comment.