@@ -390,7 +390,7 @@ def test_submit_validation_no_entrypoint(mocker):
390390 )
391391
392392 with pytest .raises (
393- ValueError , match = "entrypoint must be provided to submit a RayJob"
393+ ValueError , match = "Entrypoint must be provided to submit a RayJob"
394394 ):
395395 rayjob .submit ()
396396
@@ -1903,3 +1903,123 @@ def test_add_script_volumes_existing_mount_skip():
19031903 # Should still have only one mount and no volume added
19041904 assert len (config .volumes ) == 0 # Volume not added due to mount skip
19051905 assert len (config .volume_mounts ) == 1
1906+
1907+
1908+ def test_rayjob_stop_success (mocker , caplog ):
1909+ """Test successful RayJob stop operation."""
1910+ mocker .patch ("kubernetes.config.load_kube_config" )
1911+
1912+ mock_api_class = mocker .patch ("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi" )
1913+ mock_api_instance = MagicMock ()
1914+ mock_api_class .return_value = mock_api_instance
1915+
1916+ mocker .patch ("codeflare_sdk.ray.rayjobs.rayjob.RayClusterApi" )
1917+
1918+ mock_api_instance .suspend_job .return_value = {
1919+ "metadata" : {"name" : "test-rayjob" },
1920+ "spec" : {"suspend" : True },
1921+ }
1922+
1923+ rayjob = RayJob (
1924+ job_name = "test-rayjob" ,
1925+ cluster_name = "test-cluster" ,
1926+ namespace = "test-namespace" ,
1927+ entrypoint = "python script.py" ,
1928+ )
1929+
1930+ with caplog .at_level ("INFO" ):
1931+ result = rayjob .stop ()
1932+
1933+ assert result is True
1934+
1935+ mock_api_instance .suspend_job .assert_called_once_with (
1936+ name = "test-rayjob" , k8s_namespace = "test-namespace"
1937+ )
1938+
1939+ # Verify success message was logged
1940+ assert "Successfully stopped the RayJob test-rayjob" in caplog .text
1941+
1942+
1943+ def test_rayjob_stop_failure (mocker ):
1944+ """Test RayJob stop operation when API call fails."""
1945+ mocker .patch ("kubernetes.config.load_kube_config" )
1946+
1947+ mock_api_class = mocker .patch ("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi" )
1948+ mock_api_instance = MagicMock ()
1949+ mock_api_class .return_value = mock_api_instance
1950+
1951+ mocker .patch ("codeflare_sdk.ray.rayjobs.rayjob.RayClusterApi" )
1952+
1953+ mock_api_instance .suspend_job .return_value = None
1954+
1955+ rayjob = RayJob (
1956+ job_name = "test-rayjob" ,
1957+ cluster_name = "test-cluster" ,
1958+ namespace = "test-namespace" ,
1959+ entrypoint = "python script.py" ,
1960+ )
1961+
1962+ with pytest .raises (RuntimeError , match = "Failed to stop the RayJob test-rayjob" ):
1963+ rayjob .stop ()
1964+
1965+ mock_api_instance .suspend_job .assert_called_once_with (
1966+ name = "test-rayjob" , k8s_namespace = "test-namespace"
1967+ )
1968+
1969+
1970+ def test_rayjob_resubmit_success (mocker ):
1971+ """Test successful RayJob resubmit operation."""
1972+ mocker .patch ("kubernetes.config.load_kube_config" )
1973+
1974+ mock_api_class = mocker .patch ("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi" )
1975+ mock_api_instance = MagicMock ()
1976+ mock_api_class .return_value = mock_api_instance
1977+
1978+ mocker .patch ("codeflare_sdk.ray.rayjobs.rayjob.RayClusterApi" )
1979+
1980+ mock_api_instance .resubmit_job .return_value = {
1981+ "metadata" : {"name" : "test-rayjob" },
1982+ "spec" : {"suspend" : False },
1983+ }
1984+
1985+ rayjob = RayJob (
1986+ job_name = "test-rayjob" ,
1987+ cluster_name = "test-cluster" ,
1988+ namespace = "test-namespace" ,
1989+ entrypoint = "python script.py" ,
1990+ )
1991+
1992+ result = rayjob .resubmit ()
1993+
1994+ assert result is True
1995+
1996+ mock_api_instance .resubmit_job .assert_called_once_with (
1997+ name = "test-rayjob" , k8s_namespace = "test-namespace"
1998+ )
1999+
2000+
2001+ def test_rayjob_resubmit_failure (mocker ):
2002+ """Test RayJob resubmit operation when API call fails."""
2003+ mocker .patch ("kubernetes.config.load_kube_config" )
2004+
2005+ mock_api_class = mocker .patch ("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi" )
2006+ mock_api_instance = MagicMock ()
2007+ mock_api_class .return_value = mock_api_instance
2008+
2009+ mocker .patch ("codeflare_sdk.ray.rayjobs.rayjob.RayClusterApi" )
2010+
2011+ mock_api_instance .resubmit_job .return_value = None
2012+
2013+ rayjob = RayJob (
2014+ job_name = "test-rayjob" ,
2015+ cluster_name = "test-cluster" ,
2016+ namespace = "test-namespace" ,
2017+ entrypoint = "python script.py" ,
2018+ )
2019+
2020+ with pytest .raises (RuntimeError , match = "Failed to resubmit the RayJob test-rayjob" ):
2021+ rayjob .resubmit ()
2022+
2023+ mock_api_instance .resubmit_job .assert_called_once_with (
2024+ name = "test-rayjob" , k8s_namespace = "test-namespace"
2025+ )
0 commit comments