1+ from calendar import c
2+ from time import sleep
13from codeflare_sdk import Cluster , ClusterConfiguration
24import pytest
3- import time
45from kubernetes import client
5- from codeflare_sdk .common .utils import constants
66
77from support import (
88 initialize_kubernetes_client ,
@@ -40,7 +40,6 @@ def test_cluster_apply(self):
4040 worker_cpu_limits = "1" ,
4141 worker_memory_requests = "1Gi" ,
4242 worker_memory_limits = "2Gi" ,
43- image = f"rayproject/ray:{ constants .RAY_VERSION } " ,
4443 write_to_file = True ,
4544 verify_tls = False ,
4645 )
@@ -50,9 +49,9 @@ def test_cluster_apply(self):
5049 cluster .apply ()
5150
5251 # Wait for the cluster to be ready
53- cluster .wait_ready (dashboard_check = False )
52+ cluster .wait_ready ()
5453 status , ready = cluster .status ()
55- assert ready , f"Cluster { cluster_name } is not ready: { status } "
54+ assert ready , f"Cluster { cluster_name } is not ready"
5655
5756 # Verify the cluster is created
5857 ray_cluster = get_ray_cluster (cluster_name , namespace )
@@ -61,7 +60,7 @@ def test_cluster_apply(self):
6160 ray_cluster ["spec" ]["workerGroupSpecs" ][0 ]["replicas" ] == 1
6261 ), "Initial worker count does not match"
6362
64- # Update configuration with 2 workers
63+ # Update configuration with 3 workers
6564 updated_config = ClusterConfiguration (
6665 name = cluster_name ,
6766 namespace = namespace ,
@@ -74,7 +73,6 @@ def test_cluster_apply(self):
7473 worker_cpu_limits = "1" ,
7574 worker_memory_requests = "1Gi" ,
7675 worker_memory_limits = "2Gi" ,
77- image = f"rayproject/ray:{ constants .RAY_VERSION } " ,
7876 write_to_file = True ,
7977 verify_tls = False ,
8078 )
@@ -83,15 +81,10 @@ def test_cluster_apply(self):
8381 cluster .config = updated_config
8482 cluster .apply ()
8583
86- # Give Kubernetes a moment to process the update
87- time .sleep (5 )
88-
8984 # Wait for the updated cluster to be ready
90- cluster .wait_ready (dashboard_check = False )
85+ cluster .wait_ready ()
9186 updated_status , updated_ready = cluster .status ()
92- assert (
93- updated_ready
94- ), f"Cluster { cluster_name } is not ready after update: { updated_status } "
87+ assert updated_ready , f"Cluster { cluster_name } is not ready after update"
9588
9689 # Verify the cluster is updated
9790 updated_ray_cluster = get_ray_cluster (cluster_name , namespace )
@@ -101,19 +94,67 @@ def test_cluster_apply(self):
10194
10295 # Clean up
10396 cluster .down ()
97+ sleep (10 )
98+ ray_cluster = get_ray_cluster (cluster_name , namespace )
99+ assert ray_cluster is None , "Cluster was not deleted successfully"
104100
105- # Wait for deletion to complete (finalizers may delay deletion)
106- max_wait = 30 # seconds
107- wait_interval = 2
108- elapsed = 0
101+ def test_apply_invalid_update (self ):
102+ self .setup_method ()
103+ create_namespace (self )
109104
110- while elapsed < max_wait :
111- ray_cluster = get_ray_cluster (cluster_name , namespace )
112- if ray_cluster is None :
113- break
114- time .sleep (wait_interval )
115- elapsed += wait_interval
105+ cluster_name = "test-cluster-apply-invalid"
106+ namespace = self .namespace
116107
117- assert (
118- ray_cluster is None
119- ), f"Cluster was not deleted successfully after { max_wait } s"
108+ # Initial configuration
109+ initial_config = ClusterConfiguration (
110+ name = cluster_name ,
111+ namespace = namespace ,
112+ num_workers = 1 ,
113+ head_cpu_requests = "500m" ,
114+ head_cpu_limits = "1" ,
115+ head_memory_requests = "1Gi" ,
116+ head_memory_limits = "2Gi" ,
117+ worker_cpu_requests = "500m" ,
118+ worker_cpu_limits = "1" ,
119+ worker_memory_requests = "1Gi" ,
120+ worker_memory_limits = "2Gi" ,
121+ write_to_file = True ,
122+ verify_tls = False ,
123+ )
124+
125+ # Create the cluster
126+ cluster = Cluster (initial_config )
127+ cluster .apply ()
128+
129+ # Wait for the cluster to be ready
130+ cluster .wait_ready ()
131+ status , ready = cluster .status ()
132+ assert ready , f"Cluster { cluster_name } is not ready"
133+
134+ # Update with an invalid configuration (e.g., immutable field change)
135+ invalid_config = ClusterConfiguration (
136+ name = cluster_name ,
137+ namespace = namespace ,
138+ num_workers = 2 ,
139+ head_cpu_requests = "1" ,
140+ head_cpu_limits = "2" , # Changing CPU limits (immutable)
141+ head_memory_requests = "1Gi" ,
142+ head_memory_limits = "2Gi" ,
143+ worker_cpu_requests = "500m" ,
144+ worker_cpu_limits = "1" ,
145+ worker_memory_requests = "1Gi" ,
146+ worker_memory_limits = "2Gi" ,
147+ write_to_file = True ,
148+ verify_tls = False ,
149+ )
150+
151+ # Try to apply the invalid configuration and expect failure
152+ cluster .config = invalid_config
153+ cluster .apply ()
154+
155+ cluster .wait_ready ()
156+ status , ready = cluster .status ()
157+ assert ready , f"Cluster { cluster_name } is not ready"
158+
159+ # Clean up
160+ cluster .down ()
0 commit comments