|
| 1 | +# Copyright 2024 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +from unittest.mock import MagicMock |
| 17 | +from codeflare_sdk.ray.rayjobs.rayjob import RayJob |
| 18 | + |
| 19 | + |
| 20 | +def test_rayjob_submit_success(mocker): |
| 21 | + """Test successful RayJob submission.""" |
| 22 | + # Mock kubernetes config loading |
| 23 | + mocker.patch("kubernetes.config.load_kube_config") |
| 24 | + |
| 25 | + # Mock the RayjobApi class entirely |
| 26 | + mock_api_class = mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi") |
| 27 | + mock_api_instance = MagicMock() |
| 28 | + mock_api_class.return_value = mock_api_instance |
| 29 | + |
| 30 | + # Configure the mock to return success when submit is called |
| 31 | + mock_api_instance.submit.return_value = {"metadata": {"name": "test-rayjob"}} |
| 32 | + |
| 33 | + # Create RayJob instance |
| 34 | + rayjob = RayJob( |
| 35 | + job_name="test-rayjob", |
| 36 | + cluster_name="test-ray-cluster", |
| 37 | + namespace="test-namespace", |
| 38 | + entrypoint="python -c 'print(\"hello world\")'", |
| 39 | + runtime_env={"pip": ["requests"]}, |
| 40 | + ) |
| 41 | + |
| 42 | + # Submit the job |
| 43 | + job_id = rayjob.submit() |
| 44 | + |
| 45 | + # Assertions |
| 46 | + assert job_id == "test-rayjob" |
| 47 | + |
| 48 | + # Verify the API was called with correct parameters |
| 49 | + mock_api_instance.submit_job.assert_called_once() |
| 50 | + call_args = mock_api_instance.submit_job.call_args |
| 51 | + |
| 52 | + # Check the namespace parameter |
| 53 | + assert call_args.kwargs["k8s_namespace"] == "test-namespace" |
| 54 | + |
| 55 | + # Check the job custom resource |
| 56 | + job_cr = call_args.kwargs["job"] |
| 57 | + assert job_cr["metadata"]["name"] == "test-rayjob" |
| 58 | + assert job_cr["metadata"]["namespace"] == "test-namespace" |
| 59 | + assert job_cr["spec"]["entrypoint"] == "python -c 'print(\"hello world\")'" |
| 60 | + assert job_cr["spec"]["clusterSelector"]["ray.io/cluster"] == "test-ray-cluster" |
| 61 | + assert job_cr["spec"]["runtimeEnvYAML"] == "{'pip': ['requests']}" |
| 62 | + |
| 63 | + |
| 64 | +def test_rayjob_submit_failure(mocker): |
| 65 | + """Test RayJob submission failure.""" |
| 66 | + # Mock kubernetes config loading |
| 67 | + mocker.patch("kubernetes.config.load_kube_config") |
| 68 | + |
| 69 | + # Mock the RayjobApi class entirely |
| 70 | + mock_api_class = mocker.patch("codeflare_sdk.ray.rayjobs.rayjob.RayjobApi") |
| 71 | + mock_api_instance = MagicMock() |
| 72 | + mock_api_class.return_value = mock_api_instance |
| 73 | + |
| 74 | + # Configure the mock to return failure (False/None) when submit_job is called |
| 75 | + mock_api_instance.submit_job.return_value = None |
| 76 | + |
| 77 | + # Create a RayJob instance |
| 78 | + rayjob = RayJob( |
| 79 | + job_name="test-rayjob", |
| 80 | + cluster_name="test-ray-cluster", |
| 81 | + namespace="default", |
| 82 | + entrypoint="python script.py", |
| 83 | + runtime_env={"pip": ["numpy"]}, |
| 84 | + ) |
| 85 | + |
| 86 | + # Test that RuntimeError is raised on failure |
| 87 | + with pytest.raises(RuntimeError, match="Failed to submit RayJob test-rayjob"): |
| 88 | + rayjob.submit() |
0 commit comments