-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
987a5ef
commit 0d69561
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |