Skip to content

Commit 486a6bd

Browse files
committed
upped coverage
Signed-off-by: Pat O'Connor <[email protected]>
1 parent 4b60884 commit 486a6bd

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

src/codeflare_sdk/ray/rayjobs/test_rayjob.py

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ def test_rayjob_init_validation_both_provided(mocker):
9393
"""Test that providing both cluster_name and cluster_config raises error."""
9494
# Mock kubernetes config loading
9595
mocker.patch("kubernetes.config.load_kube_config")
96-
96+
9797
# Mock the RayjobApi class entirely
9898
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
99-
99+
100100
cluster_config = ClusterConfiguration(name="test-cluster", namespace="test")
101101

102102
with pytest.raises(
@@ -114,10 +114,10 @@ def test_rayjob_init_validation_neither_provided(mocker):
114114
"""Test that providing neither cluster_name nor cluster_config raises error."""
115115
# Mock kubernetes config loading (though this should fail before reaching it)
116116
mocker.patch("kubernetes.config.load_kube_config")
117-
117+
118118
# Mock the RayjobApi class entirely (though this should fail before reaching it)
119119
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
120-
120+
121121
with pytest.raises(
122122
ValueError, match="Either cluster_name or cluster_config must be provided"
123123
):
@@ -128,10 +128,10 @@ def test_rayjob_init_with_cluster_config(mocker):
128128
"""Test RayJob initialization with cluster configuration for auto-creation."""
129129
# Mock kubernetes config loading
130130
mocker.patch("kubernetes.config.load_kube_config")
131-
131+
132132
# Mock the RayjobApi class entirely
133133
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
134-
134+
135135
cluster_config = ClusterConfiguration(
136136
name="auto-cluster", namespace="test-namespace", num_workers=2
137137
)
@@ -152,10 +152,10 @@ def test_rayjob_cluster_name_generation(mocker):
152152
"""Test that cluster names are generated when config has empty name."""
153153
# Mock kubernetes config loading
154154
mocker.patch("kubernetes.config.load_kube_config")
155-
155+
156156
# Mock the RayjobApi class entirely
157157
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
158-
158+
159159
cluster_config = ClusterConfiguration(
160160
name="", # Empty name should trigger generation
161161
namespace="test-namespace",
@@ -170,11 +170,77 @@ def test_rayjob_cluster_name_generation(mocker):
170170
assert cluster_config.name == "my-job-cluster" # Should be updated
171171

172172

173+
def test_rayjob_cluster_config_namespace_none(mocker):
174+
"""Test that cluster config namespace is set when None."""
175+
# Mock kubernetes config loading
176+
mocker.patch("kubernetes.config.load_kube_config")
177+
178+
# Mock the RayjobApi class entirely
179+
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
180+
181+
cluster_config = ClusterConfiguration(
182+
name="test-cluster",
183+
namespace=None, # This should be set to job namespace
184+
num_workers=1,
185+
)
186+
187+
rayjob = RayJob(
188+
job_name="test-job",
189+
cluster_config=cluster_config,
190+
namespace="job-namespace",
191+
entrypoint="python script.py"
192+
)
193+
194+
assert cluster_config.namespace == "job-namespace"
195+
assert rayjob.namespace == "job-namespace"
196+
197+
198+
def test_rayjob_with_active_deadline_seconds(mocker):
199+
"""Test RayJob CR generation with active deadline seconds."""
200+
# Mock kubernetes config loading
201+
mocker.patch("kubernetes.config.load_kube_config")
202+
203+
# Mock the RayjobApi class entirely
204+
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
205+
206+
rayjob = RayJob(
207+
job_name="test-job",
208+
cluster_name="test-cluster",
209+
namespace="test-namespace",
210+
entrypoint="python main.py",
211+
active_deadline_seconds=30,
212+
)
213+
214+
rayjob_cr = rayjob._build_rayjob_cr()
215+
216+
assert rayjob_cr["spec"]["activeDeadlineSeconds"] == 30
217+
218+
219+
def test_build_ray_cluster_spec_no_config_error(mocker):
220+
"""Test _build_ray_cluster_spec raises error when no cluster config."""
221+
# Mock kubernetes config loading
222+
mocker.patch("kubernetes.config.load_kube_config")
223+
224+
# Mock the RayjobApi class entirely
225+
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
226+
227+
# Create RayJob with cluster_name (no cluster_config)
228+
rayjob = RayJob(
229+
job_name="test-job",
230+
cluster_name="existing-cluster",
231+
entrypoint="python script.py",
232+
)
233+
234+
# Line 198: Should raise RuntimeError when trying to build spec without config
235+
with pytest.raises(RuntimeError, match="No cluster configuration provided"):
236+
rayjob._build_ray_cluster_spec()
237+
238+
173239
@patch("codeflare_sdk.ray.rayjobs.rayjob.build_ray_cluster")
174240
def test_build_ray_cluster_spec(mock_build_ray_cluster, mocker):
175241
"""Test _build_ray_cluster_spec method."""
176242
mocker.patch("kubernetes.config.load_kube_config")
177-
243+
178244
# Mock the RayjobApi class entirely
179245
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
180246

@@ -216,7 +282,7 @@ def test_build_ray_cluster_spec(mock_build_ray_cluster, mocker):
216282
def test_build_rayjob_cr_with_existing_cluster(mocker):
217283
"""Test _build_rayjob_cr method with existing cluster."""
218284
mocker.patch("kubernetes.config.load_kube_config")
219-
285+
220286
# Mock the RayjobApi class entirely
221287
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
222288

@@ -251,7 +317,7 @@ def test_build_rayjob_cr_with_existing_cluster(mocker):
251317
def test_build_rayjob_cr_with_auto_cluster(mock_build_ray_cluster, mocker):
252318
"""Test _build_rayjob_cr method with auto-created cluster."""
253319
mocker.patch("kubernetes.config.load_kube_config")
254-
320+
255321
# Mock the RayjobApi class entirely
256322
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
257323

@@ -304,9 +370,6 @@ def test_submit_with_auto_cluster(mock_build_ray_cluster, mocker):
304370
"""Test successful submission with auto-created cluster."""
305371
mocker.patch("kubernetes.config.load_kube_config")
306372

307-
# Mock the RayjobApi class entirely
308-
mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi")
309-
310373
mock_ray_cluster = {
311374
"apiVersion": "ray.io/v1",
312375
"kind": "RayCluster",

0 commit comments

Comments
 (0)