1
+ from calendar import c
2
+ from time import sleep
1
3
from codeflare_sdk import Cluster , ClusterConfiguration
2
4
import pytest
3
- import time
4
5
from kubernetes import client
5
- from codeflare_sdk .common .utils import constants
6
6
7
7
from support import (
8
8
initialize_kubernetes_client ,
@@ -40,7 +40,6 @@ def test_cluster_apply(self):
40
40
worker_cpu_limits = "1" ,
41
41
worker_memory_requests = "1Gi" ,
42
42
worker_memory_limits = "2Gi" ,
43
- image = f"rayproject/ray:{ constants .RAY_VERSION } " ,
44
43
write_to_file = True ,
45
44
verify_tls = False ,
46
45
)
@@ -50,9 +49,9 @@ def test_cluster_apply(self):
50
49
cluster .apply ()
51
50
52
51
# Wait for the cluster to be ready
53
- cluster .wait_ready (dashboard_check = False )
52
+ cluster .wait_ready ()
54
53
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"
56
55
57
56
# Verify the cluster is created
58
57
ray_cluster = get_ray_cluster (cluster_name , namespace )
@@ -61,7 +60,7 @@ def test_cluster_apply(self):
61
60
ray_cluster ["spec" ]["workerGroupSpecs" ][0 ]["replicas" ] == 1
62
61
), "Initial worker count does not match"
63
62
64
- # Update configuration with 2 workers
63
+ # Update configuration with 3 workers
65
64
updated_config = ClusterConfiguration (
66
65
name = cluster_name ,
67
66
namespace = namespace ,
@@ -74,7 +73,6 @@ def test_cluster_apply(self):
74
73
worker_cpu_limits = "1" ,
75
74
worker_memory_requests = "1Gi" ,
76
75
worker_memory_limits = "2Gi" ,
77
- image = f"rayproject/ray:{ constants .RAY_VERSION } " ,
78
76
write_to_file = True ,
79
77
verify_tls = False ,
80
78
)
@@ -83,15 +81,10 @@ def test_cluster_apply(self):
83
81
cluster .config = updated_config
84
82
cluster .apply ()
85
83
86
- # Give Kubernetes a moment to process the update
87
- time .sleep (5 )
88
-
89
84
# Wait for the updated cluster to be ready
90
- cluster .wait_ready (dashboard_check = False )
85
+ cluster .wait_ready ()
91
86
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"
95
88
96
89
# Verify the cluster is updated
97
90
updated_ray_cluster = get_ray_cluster (cluster_name , namespace )
@@ -101,19 +94,67 @@ def test_cluster_apply(self):
101
94
102
95
# Clean up
103
96
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"
104
100
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 )
109
104
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
116
107
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