Skip to content

Commit c18c7bc

Browse files
committed
test: update unit tests after refactor
1 parent b0a25f1 commit c18c7bc

14 files changed

+351
-259
lines changed

src/codeflare_sdk/common/kueue/test_kueue.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def test_cluster_creation_no_aw_local_queue(mocker):
5151
config.write_to_file = True
5252
config.local_queue = "local-queue-default"
5353
cluster = Cluster(config)
54-
assert cluster.app_wrapper_yaml == f"{aw_dir}unit-test-cluster-kueue.yaml"
55-
assert cluster.app_wrapper_name == "unit-test-cluster-kueue"
54+
assert cluster.resource_yaml == f"{aw_dir}unit-test-cluster-kueue.yaml"
5655
assert filecmp.cmp(
5756
f"{aw_dir}unit-test-cluster-kueue.yaml",
5857
f"{parent}/tests/test_cluster_yamls/kueue/ray_cluster_kueue.yaml",
@@ -65,10 +64,9 @@ def test_cluster_creation_no_aw_local_queue(mocker):
6564
config.write_to_file = False
6665
cluster = Cluster(config)
6766

68-
test_rc = yaml.load(cluster.app_wrapper_yaml, Loader=yaml.FullLoader)
6967
with open(f"{parent}/tests/test_cluster_yamls/kueue/ray_cluster_kueue.yaml") as f:
7068
expected_rc = yaml.load(f, Loader=yaml.FullLoader)
71-
assert test_rc == expected_rc
69+
assert cluster.resource_yaml == expected_rc
7270

7371

7472
def test_aw_creation_local_queue(mocker):
@@ -87,8 +85,7 @@ def test_aw_creation_local_queue(mocker):
8785
config.write_to_file = True
8886
config.local_queue = "local-queue-default"
8987
cluster = Cluster(config)
90-
assert cluster.app_wrapper_yaml == f"{aw_dir}unit-test-aw-kueue.yaml"
91-
assert cluster.app_wrapper_name == "unit-test-aw-kueue"
88+
assert cluster.resource_yaml == f"{aw_dir}unit-test-aw-kueue.yaml"
9289
assert filecmp.cmp(
9390
f"{aw_dir}unit-test-aw-kueue.yaml",
9491
f"{parent}/tests/test_cluster_yamls/kueue/aw_kueue.yaml",
@@ -102,10 +99,9 @@ def test_aw_creation_local_queue(mocker):
10299
config.write_to_file = False
103100
cluster = Cluster(config)
104101

105-
test_rc = yaml.load(cluster.app_wrapper_yaml, Loader=yaml.FullLoader)
106102
with open(f"{parent}/tests/test_cluster_yamls/kueue/aw_kueue.yaml") as f:
107103
expected_rc = yaml.load(f, Loader=yaml.FullLoader)
108-
assert test_rc == expected_rc
104+
assert cluster.resource_yaml == expected_rc
109105

110106

111107
def test_get_local_queue_exists_fail(mocker):

src/codeflare_sdk/common/utils/unit_test_support.py

+36
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import yaml
2121
from pathlib import Path
2222
from kubernetes import client
23+
from unittest.mock import patch
2324

2425
parent = Path(__file__).resolve().parents[4] # project directory
2526
aw_dir = os.path.expanduser("~/.codeflare/resources/")
@@ -381,3 +382,38 @@ def mocked_ingress(port, cluster_name="unit-test-cluster", annotations: dict = N
381382
),
382383
)
383384
return mock_ingress
385+
386+
387+
@patch.dict("os.environ", {"NB_PREFIX": "test-prefix"})
388+
def create_cluster_all_config_params(mocker, cluster_name, is_appwrapper) -> Cluster:
389+
mocker.patch(
390+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
391+
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
392+
)
393+
394+
config = ClusterConfiguration(
395+
name=cluster_name,
396+
namespace="ns",
397+
head_cpu_requests=4,
398+
head_cpu_limits=8,
399+
head_memory_requests=12,
400+
head_memory_limits=16,
401+
head_extended_resource_requests={"nvidia.com/gpu": 1, "intel.com/gpu": 2},
402+
worker_cpu_requests=4,
403+
worker_cpu_limits=8,
404+
num_workers=10,
405+
worker_memory_requests=12,
406+
worker_memory_limits=16,
407+
appwrapper=is_appwrapper,
408+
envs={"key1": "value1", "key2": "value2"},
409+
image="example/ray:tag",
410+
image_pull_secrets=["secret1", "secret2"],
411+
write_to_file=True,
412+
verify_tls=True,
413+
labels={"key1": "value1", "key2": "value2"},
414+
worker_extended_resource_requests={"nvidia.com/gpu": 1},
415+
extended_resource_mapping={"example.com/gpu": "GPU", "intel.com/gpu": "TPU"},
416+
overwrite_default_resource_mapping=True,
417+
local_queue="local-queue-default",
418+
)
419+
return Cluster(config)

src/codeflare_sdk/ray/appwrapper/test_status.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020
from codeflare_sdk.ray.appwrapper import AppWrapper, AppWrapperStatus
2121
from codeflare_sdk.ray.cluster.status import CodeFlareClusterStatus
22+
from codeflare_sdk.common.utils.unit_test_support import get_local_queue
2223
import os
2324

2425
aw_dir = os.path.expanduser("~/.codeflare/resources/")
@@ -28,8 +29,8 @@ def test_cluster_status(mocker):
2829
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
2930
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
3031
mocker.patch(
31-
"codeflare_sdk.common.kueue.kueue.local_queue_exists",
32-
return_value="true",
32+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
33+
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
3334
)
3435
fake_aw = AppWrapper("test", AppWrapperStatus.FAILED)
3536

@@ -39,7 +40,7 @@ def test_cluster_status(mocker):
3940
namespace="ns",
4041
write_to_file=True,
4142
appwrapper=True,
42-
local_queue="local_default_queue",
43+
local_queue="local-queue-default",
4344
)
4445
)
4546
mocker.patch(

src/codeflare_sdk/ray/cluster/test_generate_yaml.py src/codeflare_sdk/ray/cluster/test_build_ray_cluster.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
from collections import namedtuple
1515
import sys
16-
from .generate_yaml import gen_names, update_image
16+
from .build_ray_cluster import gen_names, update_image
1717
import uuid
1818

1919

@@ -39,7 +39,7 @@ def test_gen_names_without_name(mocker):
3939
def test_update_image_without_supported_python_version(mocker):
4040
# Mock SUPPORTED_PYTHON_VERSIONS
4141
mocker.patch.dict(
42-
"codeflare_sdk.ray.cluster.generate_yaml.SUPPORTED_PYTHON_VERSIONS",
42+
"codeflare_sdk.ray.cluster.build_ray_cluster.SUPPORTED_PYTHON_VERSIONS",
4343
{
4444
"3.9": "ray-py3.9",
4545
"3.11": "ray-py3.11",
@@ -55,16 +55,13 @@ def test_update_image_without_supported_python_version(mocker):
5555
# Mock warnings.warn to check if it gets called
5656
warn_mock = mocker.patch("warnings.warn")
5757

58-
# Create a sample spec
59-
spec = {"containers": [{"image": None}]}
60-
6158
# Call the update_image function with no image provided
62-
update_image(spec, None)
59+
image = update_image(None)
6360

6461
# Assert that the warning was called with the expected message
6562
warn_mock.assert_called_once_with(
6663
"No default Ray image defined for 3.8. Please provide your own image or use one of the following python versions: 3.9, 3.11."
6764
)
6865

69-
# Assert that no image was set in the containers since the Python version is not supported
70-
assert spec["containers"][0]["image"] is None
66+
# Assert that no image was set since the Python version is not supported
67+
assert image is None

src/codeflare_sdk/ray/cluster/test_cluster.py

+46-118
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,17 @@
2525
arg_check_apply_effect,
2626
get_local_queue,
2727
createClusterConfig,
28-
route_list_retrieval,
2928
get_ray_obj,
30-
get_aw_obj,
31-
get_named_aw,
3229
get_obj_none,
3330
get_ray_obj_with_status,
3431
get_aw_obj_with_status,
3532
)
36-
from codeflare_sdk.ray.cluster.generate_yaml import (
37-
is_openshift_cluster,
38-
is_kind_cluster,
39-
)
33+
from codeflare_sdk.ray.cluster.cluster import _is_openshift_cluster
4034
from pathlib import Path
4135
from unittest.mock import MagicMock
4236
from kubernetes import client
37+
import yaml
38+
import filecmp
4339
import os
4440

4541
parent = Path(__file__).resolve().parents[4] # project directory
@@ -205,7 +201,7 @@ def test_local_client_url(mocker):
205201
return_value="rayclient-unit-test-cluster-localinter-ns.apps.cluster.awsroute.org",
206202
)
207203
mocker.patch(
208-
"codeflare_sdk.ray.cluster.cluster.Cluster.create_app_wrapper",
204+
"codeflare_sdk.ray.cluster.cluster.Cluster.create_resource",
209205
return_value="unit-test-cluster-localinter.yaml",
210206
)
211207

@@ -225,120 +221,51 @@ def test_local_client_url(mocker):
225221
"""
226222

227223

228-
def test_get_cluster_openshift(mocker):
224+
def test_get_cluster_no_appwrapper(mocker):
225+
"""
226+
This test uses the "test all params" unit test file as a comparison
227+
"""
228+
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
229229
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
230-
# Mock the client.ApisApi function to return a mock object
231-
mock_api = MagicMock()
232-
mock_api.get_api_versions.return_value.groups = [
233-
MagicMock(versions=[MagicMock(group_version="route.openshift.io/v1")])
234-
]
235-
mocker.patch("kubernetes.client.ApisApi", return_value=mock_api)
236230
mocker.patch(
237-
"codeflare_sdk.common.kueue.kueue.local_queue_exists",
238-
return_value="true",
231+
"codeflare_sdk.ray.cluster.cluster._check_aw_exists",
232+
return_value=False,
239233
)
240234

241-
assert is_openshift_cluster()
242-
243-
def custom_side_effect(group, version, namespace, plural, **kwargs):
244-
if plural == "routes":
245-
return route_list_retrieval("route.openshift.io", "v1", "ns", "routes")
246-
elif plural == "rayclusters":
247-
return get_ray_obj("ray.io", "v1", "ns", "rayclusters")
248-
elif plural == "appwrappers":
249-
return get_aw_obj("workload.codeflare.dev", "v1beta2", "ns", "appwrappers")
250-
elif plural == "localqueues":
251-
return get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues")
252-
253-
mocker.patch(
254-
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object", get_aw_obj
255-
)
256-
257-
mocker.patch(
258-
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
259-
side_effect=custom_side_effect,
260-
)
261-
mocker.patch(
262-
"kubernetes.client.CustomObjectsApi.get_namespaced_custom_object",
263-
return_value=get_named_aw,
264-
)
265-
mocker.patch(
266-
"kubernetes.client.CustomObjectsApi.get_namespaced_custom_object",
267-
side_effect=route_list_retrieval("route.openshift.io", "v1", "ns", "routes")[
268-
"items"
269-
],
270-
)
271-
mocker.patch(
272-
"codeflare_sdk.common.kueue.kueue.local_queue_exists",
273-
return_value="true",
274-
)
275-
276-
cluster = get_cluster(
277-
"test-cluster-a", "ns"
278-
) # see tests/test_cluster_yamls/support_clusters
279-
cluster_config = cluster.config
280-
281-
assert cluster_config.name == "test-cluster-a" and cluster_config.namespace == "ns"
282-
assert cluster_config.head_cpu_requests == 2 and cluster_config.head_cpu_limits == 2
283-
assert (
284-
cluster_config.head_memory_requests == "8G"
285-
and cluster_config.head_memory_limits == "8G"
286-
)
287-
assert (
288-
cluster_config.worker_cpu_requests == 1
289-
and cluster_config.worker_cpu_limits == 1
290-
)
291-
assert (
292-
cluster_config.worker_memory_requests == "2G"
293-
and cluster_config.worker_memory_limits == "2G"
294-
)
295-
assert cluster_config.num_workers == 1
296-
assert cluster_config.write_to_file == False
297-
assert cluster_config.local_queue == "local_default_queue"
235+
with open(f"{expected_clusters_dir}/ray/unit-test-all-params.yaml") as f:
236+
expected_rc = yaml.load(f, Loader=yaml.FullLoader)
237+
mocker.patch(
238+
"kubernetes.client.CustomObjectsApi.get_namespaced_custom_object",
239+
return_value=expected_rc,
240+
)
241+
get_cluster("test-all-params", "ns", write_to_file=True)
242+
assert filecmp.cmp(
243+
f"{aw_dir}test-all-params.yaml",
244+
f"{expected_clusters_dir}/ray/unit-test-all-params.yaml",
245+
shallow=True,
246+
)
298247

299248

300-
def test_get_cluster(mocker):
301-
# test get_cluster for Kind Clusters
249+
def test_get_cluster_with_appwrapper(mocker):
302250
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
303251
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
304252
mocker.patch(
305-
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
306-
side_effect=get_ray_obj,
307-
)
308-
mocker.patch(
309-
"kubernetes.client.CustomObjectsApi.get_namespaced_custom_object",
310-
side_effect=get_named_aw,
311-
)
312-
mocker.patch(
313-
"kubernetes.client.NetworkingV1Api.list_namespaced_ingress",
314-
return_value=ingress_retrieval(cluster_name="quicktest", client_ing=True),
315-
)
316-
mocker.patch(
317-
"codeflare_sdk.common.kueue.kueue.local_queue_exists",
318-
return_value="true",
253+
"codeflare_sdk.ray.cluster.cluster._check_aw_exists",
254+
return_value=True,
319255
)
320-
cluster = get_cluster(
321-
"test-cluster-a"
322-
) # see tests/test_cluster_yamls/support_clusters
323-
cluster_config = cluster.config
324256

325-
assert cluster_config.name == "test-cluster-a" and cluster_config.namespace == "ns"
326-
assert cluster_config.head_cpu_requests == 2 and cluster_config.head_cpu_limits == 2
327-
assert (
328-
cluster_config.head_memory_requests == "8G"
329-
and cluster_config.head_memory_limits == "8G"
330-
)
331-
assert (
332-
cluster_config.worker_cpu_requests == 1
333-
and cluster_config.worker_cpu_limits == 1
334-
)
335-
assert (
336-
cluster_config.worker_memory_requests == "2G"
337-
and cluster_config.worker_memory_limits == "2G"
338-
)
339-
assert cluster_config.num_workers == 1
340-
assert cluster_config.write_to_file == False
341-
assert cluster_config.local_queue == "local_default_queue"
257+
with open(f"{expected_clusters_dir}/appwrapper/unit-test-all-params.yaml") as f:
258+
expected_aw = yaml.load(f, Loader=yaml.FullLoader)
259+
mocker.patch(
260+
"kubernetes.client.CustomObjectsApi.get_namespaced_custom_object",
261+
return_value=expected_aw,
262+
)
263+
get_cluster("aw-all-params", "ns", write_to_file=True)
264+
assert filecmp.cmp(
265+
f"{aw_dir}aw-all-params.yaml",
266+
f"{expected_clusters_dir}/appwrapper/unit-test-all-params.yaml",
267+
shallow=True,
268+
)
342269

343270

344271
def test_wait_ready(mocker, capsys):
@@ -356,10 +283,6 @@ def test_wait_ready(mocker, capsys):
356283
mocker.patch(
357284
"codeflare_sdk.ray.cluster.cluster._ray_cluster_status", return_value=None
358285
)
359-
mocker.patch(
360-
"codeflare_sdk.common.kueue.kueue.local_queue_exists",
361-
return_value="true",
362-
)
363286
mocker.patch.object(
364287
client.CustomObjectsApi,
365288
"list_namespaced_custom_object",
@@ -381,7 +304,6 @@ def test_wait_ready(mocker, capsys):
381304
namespace="ns",
382305
write_to_file=False,
383306
appwrapper=True,
384-
local_queue="local-queue-default",
385307
)
386308
)
387309
try:
@@ -460,7 +382,7 @@ def test_list_queue_rayclusters(mocker, capsys):
460382
]
461383
mocker.patch("kubernetes.client.ApisApi", return_value=mock_api)
462384

463-
assert is_openshift_cluster() == True
385+
assert _is_openshift_cluster() == True
464386
mocker.patch(
465387
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
466388
return_value=get_obj_none("ray.io", "v1", "ns", "rayclusters"),
@@ -566,7 +488,7 @@ def test_map_to_ray_cluster(mocker):
566488
mocker.patch("kubernetes.config.load_kube_config")
567489

568490
mocker.patch(
569-
"codeflare_sdk.ray.cluster.cluster.is_openshift_cluster", return_value=True
491+
"codeflare_sdk.ray.cluster.cluster._is_openshift_cluster", return_value=True
570492
)
571493

572494
mock_api_client = mocker.MagicMock(spec=client.ApiClient)
@@ -608,3 +530,9 @@ def custom_side_effect(group, version, namespace, plural, **kwargs):
608530

609531
assert result is not None
610532
assert result.dashboard == rc_dashboard
533+
534+
535+
# Make sure to always keep this function last
536+
def test_cleanup():
537+
os.remove(f"{aw_dir}test-all-params.yaml")
538+
os.remove(f"{aw_dir}aw-all-params.yaml")

0 commit comments

Comments
 (0)