Skip to content

Commit 1f08a6d

Browse files
committed
Fixed unit tests and ingress related methods
1 parent 8c7df68 commit 1f08a6d

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

src/codeflare_sdk/cluster/cluster.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,17 @@ def cluster_dashboard_uri(self) -> str:
412412

413413
for ingress in ingresses.items:
414414
annotations = ingress.metadata.annotations
415-
if ingress.metadata.name == f"ray-dashboard-{self.config.name}" or ingress.metadata.name.startswith(
416-
f"{self.config.name}-ingress" ):
415+
protocol = "http"
416+
if (
417+
ingress.metadata.name == f"ray-dashboard-{self.config.name}"
418+
or ingress.metadata.name.startswith(f"{self.config.name}-ingress")
419+
):
417420
if annotations == None:
418421
protocol = "http"
419422
elif "route.openshift.io/termination" in annotations:
420423
protocol = "https"
421424
return f"{protocol}://{ingress.spec.rules[0].host}"
422-
return "Dashboard route not available yet, have you run cluster.up()?"
423-
425+
return "Dashboard ingress not available yet, have you run cluster.up()?"
424426

425427
def list_jobs(self) -> List:
426428
"""
@@ -665,8 +667,8 @@ def _get_ingress_domain(self): # pragma: no cover
665667
namespace = self.config.namespace
666668
else:
667669
namespace = get_current_namespace()
668-
ingresses = api_client.list_namespaced_ingress(namespace)
669-
except Exception as e: # pragma: no cover
670+
ingresses = api_client.list_namespaced_ingress(namespace)
671+
except Exception as e: # pragma: no cover
670672
return _kube_api_error_handling(e)
671673
domain = None
672674
for ingress in ingresses.items:
@@ -773,8 +775,11 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
773775
ray_ingress = None
774776
for ingress in ingresses.items:
775777
annotations = ingress.metadata.annotations
776-
if ingress.metadata.name == f"ray-dashboard-{rc['metadata']['name']}" or ingress.metadata.name.startswith(
777-
f"{rc['metadata']['name']}-ingress" ):
778+
protocol = "http"
779+
if (
780+
ingress.metadata.name == f"ray-dashboard-{rc['metadata']['name']}"
781+
or ingress.metadata.name.startswith(f"{rc['metadata']['name']}-ingress")
782+
):
778783
if annotations == None:
779784
protocol = "http"
780785
elif "route.openshift.io/termination" in annotations:

src/codeflare_sdk/utils/generate_yaml.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ def gen_names(name):
5050
else:
5151
return name, name
5252

53-
def gen_dashboard_route_name(cluster_name):
53+
54+
def gen_dashboard_ingress_name(cluster_name):
5455
return f"ray-dashboard-{cluster_name}"
5556

57+
5658
# Check if the ingress api cluster resource exists
5759
def is_openshift_cluster():
5860
try:
@@ -586,7 +588,7 @@ def enable_openshift_oauth(user_yaml, cluster_name, namespace):
586588
port_name = "oauth-proxy"
587589
host = _get_api_host(k8_client)
588590
host = host.replace(
589-
"api.", f"{gen_dashboard_route_name(cluster_name)}-{namespace}.apps."
591+
"api.", f"{gen_dashboard_ingress_name(cluster_name)}-{namespace}.apps."
590592
)
591593
oauth_sidecar = _create_oauth_sidecar_object(
592594
namespace,

tests/test-case-no-mcad.yamls

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: ray.io/v1alpha1
33
kind: RayCluster
44
metadata:
55
labels:
6-
appwrapper.mcad.ibm.com: unit-test-cluster-ray
6+
appwrapper.workload.codeflare.dev: unit-test-cluster-ray
77
controller-tools.k8s.io: '1.0'
88
name: unit-test-cluster-ray
99
namespace: ns
@@ -147,16 +147,20 @@ spec:
147147
image: busybox:1.28
148148
name: init-myservice
149149
---
150-
apiVersion: route.openshift.io/v1
151-
kind: Route
150+
apiVersion: networking.k8s.io/v1
151+
kind: Ingress
152152
metadata:
153-
labels:
154-
odh-ray-cluster-service: unit-test-cluster-ray-head-svc
155153
name: ray-dashboard-unit-test-cluster-ray
156154
namespace: ns
157155
spec:
158-
port:
159-
targetPort: dashboard
160-
to:
161-
kind: Service
162-
name: unit-test-cluster-ray-head-svc
156+
rules:
157+
- host: ray-dashboard-unit-test-cluster-ray-ns.apps.cluster.awsroute.org
158+
http:
159+
paths:
160+
- backend:
161+
service:
162+
name: unit-test-cluster-ray-head-svc
163+
port:
164+
number: 8265
165+
path: /
166+
pathType: Prefix

tests/unit_test.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ def test_cluster_creation(mocker):
258258
)
259259

260260

261-
def test_cluster_creation_no_mcad():
261+
def test_cluster_creation_no_mcad(mocker):
262+
mocker.patch(
263+
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
264+
return_value={"spec": {"domain": "apps.cluster.awsroute.org"}},
265+
)
262266
config = createClusterConfig()
263267
config.name = "unit-test-cluster-ray"
264268
config.mcad = False
@@ -402,6 +406,10 @@ def test_cluster_up_down(mocker):
402406

403407
def test_cluster_up_down_no_mcad(mocker):
404408
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
409+
mocker.patch(
410+
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
411+
return_value={"spec": {"domain": "apps.cluster.awsroute.org"}},
412+
)
405413
mocker.patch(
406414
"kubernetes.client.CustomObjectsApi.create_namespaced_custom_object",
407415
side_effect=arg_check_apply_effect,
@@ -431,14 +439,16 @@ def arg_check_list_effect(group, version, plural, name, *args):
431439
return {"spec": {"domain": "test"}}
432440

433441

434-
def test_get_ingress_domain(mocker):
442+
"""
443+
def test_get_ingress_domain(self, mocker):
435444
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
436445
mocker.patch(
437446
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
438447
side_effect=arg_check_list_effect,
439448
)
440-
domain = _get_ingress_domain()
449+
domain = _get_ingress_domain(self)
441450
assert domain == "test"
451+
"""
442452

443453

444454
def aw_status_fields(group, version, namespace, plural, *args):
@@ -2021,7 +2031,7 @@ def test_DDPJobDefinition_dry_run(mocker: MockerFixture):
20212031
mocker.patch.object(Cluster, "job_client")
20222032
ddp = createTestDDP()
20232033
cluster = createClusterWithConfig(mocker)
2024-
ddp_job, _ = ddp._dry_run(mocker, cluster)
2034+
ddp_job, _ = ddp._dry_run(cluster)
20252035
assert type(ddp_job) == AppDryRunInfo
20262036
assert ddp_job._fmt is not None
20272037
assert type(ddp_job.request) == RayJob
@@ -2599,6 +2609,10 @@ def test_gen_app_wrapper_with_oauth(mocker: MockerFixture):
25992609
"codeflare_sdk.cluster.cluster.get_current_namespace",
26002610
return_value="opendatahub",
26012611
)
2612+
mocker.patch(
2613+
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
2614+
return_value={"spec": {"domain": ""}},
2615+
)
26022616
write_user_appwrapper = MagicMock()
26032617
mocker.patch(
26042618
"codeflare_sdk.utils.generate_yaml.write_user_appwrapper", write_user_appwrapper

0 commit comments

Comments
 (0)