Skip to content

Commit

Permalink
feat: adding test for k8s prerequisites
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
boekhorstb1 committed Sep 9, 2024
1 parent 987a5ef commit 0d69561
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/rookify/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .template import Template


class ModuleHandler:
class ModuleHandler(object):
"""
ModuleHandler is an abstract class that modules have to extend.
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/mock_k8s_prerequisite_check.py
Original file line number Diff line number Diff line change
@@ -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)
60 changes: 60 additions & 0 deletions tests/modules/test_k8s_prerequisites_check.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0d69561

Please sign in to comment.