|
| 1 | +import pytest |
| 2 | +import sys |
| 3 | +import os |
| 4 | +from time import sleep |
| 5 | + |
| 6 | +# Add the parent directory to the path to import support |
| 7 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 8 | +from support import * |
| 9 | + |
| 10 | +from codeflare_sdk import ( |
| 11 | + TokenAuthentication, |
| 12 | + RayJob, |
| 13 | + ManagedClusterConfig, |
| 14 | +) |
| 15 | +from codeflare_sdk.ray.rayjobs.status import CodeflareRayJobStatus |
| 16 | + |
| 17 | +# This test creates a RayJob that will create and lifecycle its own cluster on OpenShift |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.openshift |
| 21 | +class TestRayJobLifecycledClusterOauth: |
| 22 | + def setup_method(self): |
| 23 | + initialize_kubernetes_client(self) |
| 24 | + |
| 25 | + def teardown_method(self): |
| 26 | + delete_namespace(self) |
| 27 | + delete_kueue_resources(self) |
| 28 | + |
| 29 | + def test_rayjob_with_lifecycled_cluster_oauth(self): |
| 30 | + self.setup_method() |
| 31 | + create_namespace(self) |
| 32 | + create_kueue_resources(self) |
| 33 | + self.run_rayjob_with_lifecycled_cluster_oauth() |
| 34 | + |
| 35 | + def run_rayjob_with_lifecycled_cluster_oauth(self): |
| 36 | + ray_image = get_ray_image() |
| 37 | + |
| 38 | + auth = TokenAuthentication( |
| 39 | + token=run_oc_command(["whoami", "--show-token=true"]), |
| 40 | + server=run_oc_command(["whoami", "--show-server=true"]), |
| 41 | + skip_tls=True, |
| 42 | + ) |
| 43 | + auth.login() |
| 44 | + |
| 45 | + job_name = "lifecycled-cluster-rayjob" |
| 46 | + |
| 47 | + # Create cluster configuration for auto-creation |
| 48 | + cluster_config = ManagedClusterConfig( |
| 49 | + head_cpu_requests="500m", |
| 50 | + head_cpu_limits="500m", |
| 51 | + head_memory_requests=1, |
| 52 | + head_memory_limits=4, |
| 53 | + num_workers=1, |
| 54 | + worker_cpu_requests="500m", |
| 55 | + worker_cpu_limits="500m", |
| 56 | + worker_memory_requests=1, |
| 57 | + worker_memory_limits=4, |
| 58 | + image=ray_image, |
| 59 | + ) |
| 60 | + |
| 61 | + # Create RayJob with embedded cluster - will auto-create and manage cluster lifecycle |
| 62 | + rayjob = RayJob( |
| 63 | + job_name=job_name, |
| 64 | + cluster_config=cluster_config, # This triggers auto-cluster creation |
| 65 | + namespace=self.namespace, |
| 66 | + entrypoint="python -c \"import ray; ray.init(); print('Hello from auto-created cluster!'); print(f'Ray version: {ray.__version__}'); import time; time.sleep(30); print('RayJob completed successfully!')\"", |
| 67 | + runtime_env={ |
| 68 | + "pip": ["torch", "pytorch-lightning", "torchmetrics", "torchvision"], |
| 69 | + "env_vars": get_setup_env_variables(ACCELERATOR="cpu"), |
| 70 | + }, |
| 71 | + shutdown_after_job_finishes=True, # Auto-cleanup cluster after job finishes |
| 72 | + ttl_seconds_after_finished=30, # Wait 30s after job completion before cleanup |
| 73 | + ) |
| 74 | + |
| 75 | + # Submit the job |
| 76 | + print( |
| 77 | + f"Submitting RayJob '{job_name}' with auto-cluster creation and lifecycle management" |
| 78 | + ) |
| 79 | + submission_result = rayjob.submit() |
| 80 | + assert ( |
| 81 | + submission_result == job_name |
| 82 | + ), f"Job submission failed, expected {job_name}, got {submission_result}" |
| 83 | + print( |
| 84 | + f"Successfully submitted RayJob '{job_name}' with cluster '{rayjob.cluster_name}'!" |
| 85 | + ) |
| 86 | + |
| 87 | + # Monitor the job status until completion |
| 88 | + self.monitor_rayjob_completion(rayjob) |
| 89 | + |
| 90 | + # Verify cluster auto-cleanup |
| 91 | + print("🔍 Verifying cluster auto-cleanup after job completion...") |
| 92 | + self.verify_cluster_cleanup(rayjob.cluster_name, timeout=60) |
| 93 | + |
| 94 | + def monitor_rayjob_completion(self, rayjob: RayJob, timeout: int = 900): |
| 95 | + """ |
| 96 | + Monitor a RayJob until it completes or fails. |
| 97 | + Args: |
| 98 | + rayjob: The RayJob instance to monitor |
| 99 | + timeout: Maximum time to wait in seconds (default: 15 minutes) |
| 100 | + """ |
| 101 | + print(f"Monitoring RayJob '{rayjob.name}' status...") |
| 102 | + |
| 103 | + elapsed_time = 0 |
| 104 | + check_interval = 10 # Check every 10 seconds |
| 105 | + |
| 106 | + while elapsed_time < timeout: |
| 107 | + status, ready = rayjob.status(print_to_console=True) |
| 108 | + |
| 109 | + # Check if job has completed (either successfully or failed) |
| 110 | + if status == CodeflareRayJobStatus.COMPLETE: |
| 111 | + print(f"RayJob '{rayjob.name}' completed successfully!") |
| 112 | + return |
| 113 | + elif status == CodeflareRayJobStatus.FAILED: |
| 114 | + raise AssertionError(f"RayJob '{rayjob.name}' failed!") |
| 115 | + elif status == CodeflareRayJobStatus.RUNNING: |
| 116 | + print(f"RayJob '{rayjob.name}' is still running...") |
| 117 | + elif status == CodeflareRayJobStatus.UNKNOWN: |
| 118 | + print(f"RayJob '{rayjob.name}' status is unknown") |
| 119 | + |
| 120 | + # Wait before next check |
| 121 | + sleep(check_interval) |
| 122 | + elapsed_time += check_interval |
| 123 | + |
| 124 | + # If we reach here, the job has timed out |
| 125 | + final_status, _ = rayjob.status(print_to_console=True) |
| 126 | + raise TimeoutError( |
| 127 | + f"RayJob '{rayjob.name}' did not complete within {timeout} seconds. " |
| 128 | + f"Final status: {final_status}" |
| 129 | + ) |
| 130 | + |
| 131 | + def verify_cluster_cleanup(self, cluster_name: str, timeout: int = 60): |
| 132 | + """ |
| 133 | + Verify that the cluster created by the RayJob has been cleaned up. |
| 134 | + Args: |
| 135 | + cluster_name: The name of the cluster to check for cleanup |
| 136 | + timeout: Maximum time to wait for cleanup in seconds (default: 1 minute) |
| 137 | + """ |
| 138 | + from kubernetes import client |
| 139 | + import kubernetes.client.rest |
| 140 | + |
| 141 | + elapsed_time = 0 |
| 142 | + check_interval = 5 # Check every 5 seconds |
| 143 | + |
| 144 | + while elapsed_time < timeout: |
| 145 | + try: |
| 146 | + # Try to get the RayCluster resource |
| 147 | + custom_api = client.CustomObjectsApi() |
| 148 | + custom_api.get_namespaced_custom_object( |
| 149 | + group="ray.io", |
| 150 | + version="v1", |
| 151 | + namespace=self.namespace, |
| 152 | + plural="rayclusters", |
| 153 | + name=cluster_name, |
| 154 | + ) |
| 155 | + print(f"Cluster '{cluster_name}' still exists, waiting for cleanup...") |
| 156 | + sleep(check_interval) |
| 157 | + elapsed_time += check_interval |
| 158 | + except kubernetes.client.rest.ApiException as e: |
| 159 | + if e.status == 404: |
| 160 | + print( |
| 161 | + f"✅ Cluster '{cluster_name}' has been successfully cleaned up!" |
| 162 | + ) |
| 163 | + return |
| 164 | + else: |
| 165 | + raise e |
| 166 | + |
| 167 | + # If we reach here, the cluster was not cleaned up in time |
| 168 | + raise TimeoutError( |
| 169 | + f"Cluster '{cluster_name}' was not cleaned up within {timeout} seconds" |
| 170 | + ) |
0 commit comments