|
| 1 | +import requests |
| 2 | + |
| 3 | +from time import sleep |
| 4 | + |
| 5 | +from torchx.specs.api import AppState, is_terminal |
| 6 | + |
| 7 | +from codeflare_sdk.cluster.cluster import Cluster, ClusterConfiguration |
| 8 | +from codeflare_sdk.job.jobs import DDPJobDefinition |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from support import * |
| 13 | + |
| 14 | +# This test Creates a Ray cluster with openshift_oauth enable and covers the Ray Job submission with authentication and without authentication functionality |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.openshift |
| 18 | +class TestRayClusterSDKOauth: |
| 19 | + def setup_method(self): |
| 20 | + initialize_kubernetes_client(self) |
| 21 | + |
| 22 | + def teardown_method(self): |
| 23 | + delete_namespace(self) |
| 24 | + |
| 25 | + def test_mnist_ray_cluster_sdk_auth(self): |
| 26 | + self.setup_method() |
| 27 | + create_namespace(self) |
| 28 | + self.run_mnist_raycluster_sdk_oauth() |
| 29 | + |
| 30 | + def run_mnist_raycluster_sdk_oauth(self): |
| 31 | + ray_image = get_ray_image() |
| 32 | + |
| 33 | + cluster = Cluster( |
| 34 | + ClusterConfiguration( |
| 35 | + name="mnist", |
| 36 | + namespace=self.namespace, |
| 37 | + num_workers=1, |
| 38 | + head_cpus="500m", |
| 39 | + head_memory=2, |
| 40 | + min_cpus="500m", |
| 41 | + max_cpus=1, |
| 42 | + min_memory=1, |
| 43 | + max_memory=2, |
| 44 | + num_gpus=0, |
| 45 | + instascale=False, |
| 46 | + image=ray_image, |
| 47 | + openshift_oauth=True, |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + cluster.up() |
| 52 | + self.assert_appwrapper_exists() |
| 53 | + |
| 54 | + cluster.status() |
| 55 | + |
| 56 | + cluster.wait_ready() |
| 57 | + self.assert_raycluster_exists() |
| 58 | + |
| 59 | + cluster.status() |
| 60 | + |
| 61 | + cluster.details() |
| 62 | + |
| 63 | + self.assert_jobsubmit_withoutLogin(cluster) |
| 64 | + |
| 65 | + self.assert_jobsubmit_withlogin(cluster) |
| 66 | + |
| 67 | + # Assertions |
| 68 | + |
| 69 | + def assert_jobsubmit_withoutLogin(self, cluster): |
| 70 | + dashboard_url = cluster.cluster_dashboard_uri() |
| 71 | + jobdata = { |
| 72 | + "entrypoint": "python mnist.py", |
| 73 | + "runtime_env": { |
| 74 | + "working_dir": "./tests/e2e/", |
| 75 | + "pip": "mnist_pip_requirements.txt", |
| 76 | + }, |
| 77 | + } |
| 78 | + try: |
| 79 | + response = requests.post( |
| 80 | + dashboard_url + "/api/jobs/", verify=False, json=jobdata |
| 81 | + ) |
| 82 | + if response.status_code == 403: |
| 83 | + assert True |
| 84 | + else: |
| 85 | + response.raise_for_status() |
| 86 | + assert False |
| 87 | + |
| 88 | + except Exception as e: |
| 89 | + print(f"An unexpected error occurred. Error: {e}") |
| 90 | + assert False |
| 91 | + |
| 92 | + def assert_jobsubmit_withlogin(self, cluster): |
| 93 | + self.assert_appwrapper_exists() |
| 94 | + jobdef = DDPJobDefinition( |
| 95 | + name="mnist", |
| 96 | + script="./tests/e2e/mnist.py", |
| 97 | + scheduler_args={"requirements": "./tests/e2e/mnist_pip_requirements.txt"}, |
| 98 | + ) |
| 99 | + job = jobdef.submit(cluster) |
| 100 | + |
| 101 | + done = False |
| 102 | + time = 0 |
| 103 | + timeout = 900 |
| 104 | + while not done: |
| 105 | + status = job.status() |
| 106 | + if is_terminal(status.state): |
| 107 | + break |
| 108 | + if not done: |
| 109 | + print(status) |
| 110 | + if timeout and time >= timeout: |
| 111 | + raise TimeoutError(f"job has timed out after waiting {timeout}s") |
| 112 | + sleep(5) |
| 113 | + time += 5 |
| 114 | + |
| 115 | + print(job.status()) |
| 116 | + self.assert_job_completion(status) |
| 117 | + |
| 118 | + print(job.logs()) |
| 119 | + |
| 120 | + cluster.down() |
| 121 | + |
| 122 | + def assert_appwrapper_exists(self): |
| 123 | + try: |
| 124 | + self.custom_api.get_namespaced_custom_object( |
| 125 | + "workload.codeflare.dev", |
| 126 | + "v1beta1", |
| 127 | + self.namespace, |
| 128 | + "appwrappers", |
| 129 | + "mnist", |
| 130 | + ) |
| 131 | + print( |
| 132 | + f"AppWrapper 'mnist' has been created in the namespace: '{self.namespace}'" |
| 133 | + ) |
| 134 | + assert True |
| 135 | + except Exception as e: |
| 136 | + print(f"AppWrapper 'mnist' has not been created. Error: {e}") |
| 137 | + assert False |
| 138 | + |
| 139 | + def assert_raycluster_exists(self): |
| 140 | + try: |
| 141 | + self.custom_api.get_namespaced_custom_object( |
| 142 | + "ray.io", "v1", self.namespace, "rayclusters", "mnist" |
| 143 | + ) |
| 144 | + print( |
| 145 | + f"RayCluster 'mnist' created successfully in the namespace: '{self.namespace}'" |
| 146 | + ) |
| 147 | + assert True |
| 148 | + except Exception as e: |
| 149 | + print(f"RayCluster 'mnist' has not been created. Error: {e}") |
| 150 | + assert False |
| 151 | + |
| 152 | + def assert_job_completion(self, status): |
| 153 | + if status.state == AppState.SUCCEEDED: |
| 154 | + print(f"Job has completed: '{status.state}'") |
| 155 | + assert True |
| 156 | + else: |
| 157 | + print(f"Job has completed: '{status.state}'") |
| 158 | + assert False |
0 commit comments