@@ -176,7 +176,16 @@ def test_create_secret_from_spec(auto_mock_setup):
176176 "data" : {"test.py" : "print('test')" },
177177 }
178178
179- result = create_secret_from_spec (rayjob , secret_spec )
179+ # Provide valid RayJob result with UID as KubeRay client would
180+ rayjob_result = {
181+ "metadata" : {
182+ "name" : "test-job" ,
183+ "namespace" : "test-namespace" ,
184+ "uid" : "test-uid-12345" ,
185+ }
186+ }
187+
188+ result = create_secret_from_spec (rayjob , secret_spec , rayjob_result )
180189
181190 assert result == "test-files"
182191 mock_api_instance .create_namespaced_secret .assert_called_once ()
@@ -205,7 +214,16 @@ def test_create_secret_already_exists(auto_mock_setup):
205214 "data" : {"test.py" : "print('test')" },
206215 }
207216
208- result = create_secret_from_spec (rayjob , secret_spec )
217+ # Provide valid RayJob result with UID as KubeRay client would
218+ rayjob_result = {
219+ "metadata" : {
220+ "name" : "test-job" ,
221+ "namespace" : "test-namespace" ,
222+ "uid" : "test-uid-67890" ,
223+ }
224+ }
225+
226+ result = create_secret_from_spec (rayjob , secret_spec , rayjob_result )
209227
210228 assert result == "test-files"
211229 mock_api_instance .create_namespaced_secret .assert_called_once ()
@@ -271,92 +289,6 @@ def test_create_secret_with_owner_reference_basic(mocker, auto_mock_setup, caplo
271289 mock_api_instance .create_namespaced_secret .assert_called_once ()
272290
273291
274- def test_create_secret_without_owner_reference_no_uid (mocker , auto_mock_setup , caplog ):
275- """
276- Test creating Secret without owner reference when RayJob has no UID.
277- """
278- mock_api_instance = auto_mock_setup ["k8s_api" ]
279-
280- mock_v1_metadata = mocker .patch ("kubernetes.client.V1ObjectMeta" )
281- mock_metadata_instance = MagicMock ()
282- mock_v1_metadata .return_value = mock_metadata_instance
283-
284- rayjob = RayJob (
285- job_name = "test-job" ,
286- cluster_name = "existing-cluster" ,
287- entrypoint = "python test.py" ,
288- namespace = "test-namespace" ,
289- )
290-
291- secret_spec = {
292- "apiVersion" : "v1" ,
293- "kind" : "Secret" ,
294- "type" : "Opaque" ,
295- "metadata" : {"name" : "test-files" , "namespace" : "test-namespace" },
296- "data" : {"test.py" : "print('test')" },
297- }
298-
299- # RayJob result without UID
300- rayjob_result = {
301- "metadata" : {
302- "name" : "test-job" ,
303- "namespace" : "test-namespace" ,
304- # No UID field
305- }
306- }
307-
308- with caplog .at_level ("WARNING" ):
309- result = create_secret_from_spec (rayjob , secret_spec , rayjob_result )
310-
311- assert result == "test-files"
312-
313- # Verify warning was logged and no owner reference was set
314- assert (
315- "No valid RayJob result with UID found, Secret 'test-files' will not have owner reference"
316- in caplog .text
317- )
318-
319- # The important part is that the warning was logged, indicating no owner reference was set
320- mock_api_instance .create_namespaced_secret .assert_called_once ()
321-
322-
323- def test_create_secret_with_invalid_rayjob_result (auto_mock_setup , caplog ):
324- """
325- Test creating Secret with None or invalid rayjob_result.
326- """
327- mock_api_instance = auto_mock_setup ["k8s_api" ]
328-
329- rayjob = RayJob (
330- job_name = "test-job" ,
331- cluster_name = "existing-cluster" ,
332- entrypoint = "python test.py" ,
333- namespace = "test-namespace" ,
334- )
335-
336- secret_spec = {
337- "apiVersion" : "v1" ,
338- "kind" : "Secret" ,
339- "type" : "Opaque" ,
340- "metadata" : {"name" : "test-files" , "namespace" : "test-namespace" },
341- "data" : {"test.py" : "print('test')" },
342- }
343-
344- # Test with None
345- with caplog .at_level ("WARNING" ):
346- result = create_secret_from_spec (rayjob , secret_spec , None )
347-
348- assert result == "test-files"
349- assert "No valid RayJob result with UID found" in caplog .text
350-
351- # Test with string instead of dict
352- caplog .clear ()
353- with caplog .at_level ("WARNING" ):
354- result = create_secret_from_spec (rayjob , secret_spec , "not-a-dict" )
355-
356- assert result == "test-files"
357- assert "No valid RayJob result with UID found" in caplog .text
358-
359-
360292def test_file_handling_kubernetes_best_practice_flow (mocker , tmp_path ):
361293 """
362294 Test the Kubernetes best practice flow: pre-declare volume, submit, create Secret.
@@ -446,7 +378,13 @@ def test_rayjob_submit_with_files_new_cluster(auto_mock_setup, tmp_path):
446378 Test RayJob submission with file detection for new cluster.
447379 """
448380 mock_api_instance = auto_mock_setup ["rayjob_api" ]
449- mock_api_instance .submit_job .return_value = True
381+ mock_api_instance .submit_job .return_value = {
382+ "metadata" : {
383+ "name" : "test-job" ,
384+ "namespace" : "test-namespace" ,
385+ "uid" : "test-uid-files-12345" ,
386+ }
387+ }
450388
451389 mock_k8s_instance = auto_mock_setup ["k8s_api" ]
452390
@@ -507,8 +445,17 @@ def test_create_secret_api_error_non_409(auto_mock_setup):
507445 "data" : {"test.py" : "print('test')" },
508446 }
509447
448+ # Provide valid RayJob result with UID as KubeRay client would
449+ rayjob_result = {
450+ "metadata" : {
451+ "name" : "test-job" ,
452+ "namespace" : "test-namespace" ,
453+ "uid" : "test-uid-api-error" ,
454+ }
455+ }
456+
510457 with pytest .raises (RuntimeError , match = "Failed to create Secret" ):
511- create_secret_from_spec (rayjob , secret_spec )
458+ create_secret_from_spec (rayjob , secret_spec , rayjob_result )
512459
513460
514461def test_add_file_volumes_existing_volume_skip ():
0 commit comments