Skip to content

Commit d115a7c

Browse files
committed
fixed tests
1 parent bc8b538 commit d115a7c

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

tests/test_grouped_jobs.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest.mock import AsyncMock, MagicMock, patch
33
from collections import defaultdict
44

5-
from robusta_krr.core.integrations.kubernetes import KubernetesLoader
5+
from robusta_krr.core.integrations.kubernetes import ClusterLoader
66
from robusta_krr.core.models.config import Config
77
from robusta_krr.api.models import K8sObjectData
88

@@ -13,17 +13,21 @@ def mock_config():
1313
config = MagicMock(spec=Config)
1414
config.job_grouping_labels = ["app", "team"]
1515
config.job_grouping_limit = 3 # Small limit for testing
16+
config.max_workers = 4
17+
config.get_kube_client = MagicMock()
18+
config.resources = "*"
1619
return config
1720

1821

1922
@pytest.fixture
2023
def mock_kubernetes_loader(mock_config):
21-
"""Create a KubernetesLoader instance with mocked dependencies"""
24+
"""Create a ClusterLoader instance with mocked dependencies"""
2225
with patch("robusta_krr.core.integrations.kubernetes.settings", mock_config):
23-
loader = KubernetesLoader()
26+
loader = ClusterLoader()
2427
loader.batch = MagicMock()
2528
loader.core = MagicMock()
2629
loader.executor = MagicMock()
30+
loader._ClusterLoader__hpa_list = {} # type: ignore # needed for mock
2731
return loader
2832

2933

@@ -34,7 +38,11 @@ def create_mock_job(name: str, namespace: str, labels: dict):
3438
job.metadata.namespace = namespace
3539
job.metadata.labels = labels
3640
job.metadata.owner_references = []
37-
job.spec.template.spec.containers = [MagicMock()]
41+
42+
# Create a mock container with a proper name
43+
container = MagicMock()
44+
container.name = "main-container"
45+
job.spec.template.spec.containers = [container]
3846
return job
3947

4048

@@ -66,8 +74,10 @@ def mock_build_scannable_object(item, container, kind):
6674

6775
mock_kubernetes_loader._KubernetesLoader__build_scannable_object = mock_build_scannable_object
6876

69-
# Call the method
70-
result = await mock_kubernetes_loader._list_all_groupedjobs()
77+
# Patch the settings to use our mock config
78+
with patch("robusta_krr.core.integrations.kubernetes.settings", mock_config):
79+
# Call the method
80+
result = await mock_kubernetes_loader._list_all_groupedjobs()
7181

7282
# Verify we got 2 groups (frontend and backend)
7383
assert len(result) == 2
@@ -116,8 +126,10 @@ def mock_build_scannable_object(item, container, kind):
116126

117127
mock_kubernetes_loader._KubernetesLoader__build_scannable_object = mock_build_scannable_object
118128

119-
# Call the method
120-
result = await mock_kubernetes_loader._list_all_groupedjobs()
129+
# Patch the settings to use our mock config
130+
with patch("robusta_krr.core.integrations.kubernetes.settings", mock_config):
131+
# Call the method
132+
result = await mock_kubernetes_loader._list_all_groupedjobs()
121133

122134
# Verify we got 2 groups (one per namespace)
123135
assert len(result) == 2
@@ -157,8 +169,10 @@ def mock_build_scannable_object(item, container, kind):
157169

158170
mock_kubernetes_loader._KubernetesLoader__build_scannable_object = mock_build_scannable_object
159171

160-
# Call the method
161-
result = await mock_kubernetes_loader._list_all_groupedjobs()
172+
# Patch the settings to use our mock config
173+
with patch("robusta_krr.core.integrations.kubernetes.settings", mock_config):
174+
# Call the method
175+
result = await mock_kubernetes_loader._list_all_groupedjobs()
162176

163177
# Verify we got 1 group with only 1 job (the one without CronJob owner)
164178
assert len(result) == 1
@@ -201,8 +215,10 @@ def mock_build_scannable_object(item, container, kind):
201215

202216
mock_kubernetes_loader._KubernetesLoader__build_scannable_object = mock_build_scannable_object
203217

204-
# Call the method
205-
result = await mock_kubernetes_loader._list_all_groupedjobs()
218+
# Patch the settings to use our mock config
219+
with patch("robusta_krr.core.integrations.kubernetes.settings", mock_config):
220+
# Call the method
221+
result = await mock_kubernetes_loader._list_all_groupedjobs()
206222

207223
# Verify we got 3 groups (one for each label+value combination)
208224
assert len(result) == 3

0 commit comments

Comments
 (0)