1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414from ..utils .unit_test_support import (
15- apply_template ,
1615 get_local_queue ,
17- createClusterConfig ,
16+ create_cluster_config ,
1817 get_template_variables ,
18+ apply_template ,
1919)
2020from unittest .mock import patch
2121from codeflare_sdk .ray .cluster .cluster import Cluster , ClusterConfiguration
2222import yaml
2323import os
2424import filecmp
2525from pathlib import Path
26- from .kueue import list_local_queues
26+ from .kueue import list_local_queues , local_queue_exists , add_queue_label
2727
2828parent = Path (__file__ ).resolve ().parents [4 ] # project directory
2929aw_dir = os .path .expanduser ("~/.codeflare/resources/" )
@@ -51,7 +51,7 @@ def test_cluster_creation_no_aw_local_queue(mocker):
5151 "kubernetes.client.CustomObjectsApi.list_namespaced_custom_object" ,
5252 return_value = get_local_queue ("kueue.x-k8s.io" , "v1beta1" , "ns" , "localqueues" ),
5353 )
54- config = createClusterConfig ()
54+ config = create_cluster_config ()
5555 config .name = "unit-test-cluster-kueue"
5656 config .write_to_file = True
5757 config .local_queue = "local-queue-default"
@@ -67,7 +67,7 @@ def test_cluster_creation_no_aw_local_queue(mocker):
6767 assert cluster_kueue == expected_rc
6868
6969 # With resources loaded in memory, no Local Queue specified.
70- config = createClusterConfig ()
70+ config = create_cluster_config ()
7171 config .name = "unit-test-cluster-kueue"
7272 config .write_to_file = False
7373 cluster = Cluster (config )
@@ -84,7 +84,7 @@ def test_aw_creation_local_queue(mocker):
8484 "kubernetes.client.CustomObjectsApi.list_namespaced_custom_object" ,
8585 return_value = get_local_queue ("kueue.x-k8s.io" , "v1beta1" , "ns" , "localqueues" ),
8686 )
87- config = createClusterConfig ()
87+ config = create_cluster_config ()
8888 config .name = "unit-test-aw-kueue"
8989 config .appwrapper = True
9090 config .write_to_file = True
@@ -101,7 +101,7 @@ def test_aw_creation_local_queue(mocker):
101101 assert aw_kueue == expected_rc
102102
103103 # With resources loaded in memory, no Local Queue specified.
104- config = createClusterConfig ()
104+ config = create_cluster_config ()
105105 config .name = "unit-test-aw-kueue"
106106 config .appwrapper = True
107107 config .write_to_file = False
@@ -120,7 +120,7 @@ def test_get_local_queue_exists_fail(mocker):
120120 "kubernetes.client.CustomObjectsApi.list_namespaced_custom_object" ,
121121 return_value = get_local_queue ("kueue.x-k8s.io" , "v1beta1" , "ns" , "localqueues" ),
122122 )
123- config = createClusterConfig ()
123+ config = create_cluster_config ()
124124 config .name = "unit-test-aw-kueue"
125125 config .appwrapper = True
126126 config .write_to_file = True
@@ -175,6 +175,123 @@ def test_list_local_queues(mocker):
175175 assert lqs == []
176176
177177
178+ def test_local_queue_exists_found (mocker ):
179+ # Mock Kubernetes client and list_namespaced_custom_object method
180+ mocker .patch ("kubernetes.config.load_kube_config" , return_value = "ignore" )
181+ mock_api_instance = mocker .Mock ()
182+ mocker .patch ("kubernetes.client.CustomObjectsApi" , return_value = mock_api_instance )
183+ mocker .patch ("codeflare_sdk.ray.cluster.cluster.config_check" )
184+
185+ # Mock return value for list_namespaced_custom_object
186+ mock_api_instance .list_namespaced_custom_object .return_value = {
187+ "items" : [
188+ {"metadata" : {"name" : "existing-queue" }},
189+ {"metadata" : {"name" : "another-queue" }},
190+ ]
191+ }
192+
193+ # Call the function
194+ namespace = "test-namespace"
195+ local_queue_name = "existing-queue"
196+ result = local_queue_exists (namespace , local_queue_name )
197+
198+ # Assertions
199+ assert result is True
200+ mock_api_instance .list_namespaced_custom_object .assert_called_once_with (
201+ group = "kueue.x-k8s.io" ,
202+ version = "v1beta1" ,
203+ namespace = namespace ,
204+ plural = "localqueues" ,
205+ )
206+
207+
208+ def test_local_queue_exists_not_found (mocker ):
209+ # Mock Kubernetes client and list_namespaced_custom_object method
210+ mocker .patch ("kubernetes.config.load_kube_config" , return_value = "ignore" )
211+ mock_api_instance = mocker .Mock ()
212+ mocker .patch ("kubernetes.client.CustomObjectsApi" , return_value = mock_api_instance )
213+ mocker .patch ("codeflare_sdk.ray.cluster.cluster.config_check" )
214+
215+ # Mock return value for list_namespaced_custom_object
216+ mock_api_instance .list_namespaced_custom_object .return_value = {
217+ "items" : [
218+ {"metadata" : {"name" : "another-queue" }},
219+ {"metadata" : {"name" : "different-queue" }},
220+ ]
221+ }
222+
223+ # Call the function
224+ namespace = "test-namespace"
225+ local_queue_name = "non-existent-queue"
226+ result = local_queue_exists (namespace , local_queue_name )
227+
228+ # Assertions
229+ assert result is False
230+ mock_api_instance .list_namespaced_custom_object .assert_called_once_with (
231+ group = "kueue.x-k8s.io" ,
232+ version = "v1beta1" ,
233+ namespace = namespace ,
234+ plural = "localqueues" ,
235+ )
236+
237+
238+ import pytest
239+ from unittest import mock # If you're also using mocker from pytest-mock
240+
241+
242+ def test_add_queue_label_with_valid_local_queue (mocker ):
243+ # Mock the kubernetes.client.CustomObjectsApi and its response
244+ mock_api_instance = mocker .patch ("kubernetes.client.CustomObjectsApi" )
245+ mock_api_instance .return_value .list_namespaced_custom_object .return_value = {
246+ "items" : [
247+ {"metadata" : {"name" : "valid-queue" }},
248+ ]
249+ }
250+
251+ # Mock other dependencies
252+ mocker .patch ("codeflare_sdk.common.kueue.local_queue_exists" , return_value = True )
253+ mocker .patch (
254+ "codeflare_sdk.common.kueue.get_default_kueue_name" ,
255+ return_value = "default-queue" ,
256+ )
257+
258+ # Define input item and parameters
259+ item = {"metadata" : {}}
260+ namespace = "test-namespace"
261+ local_queue = "valid-queue"
262+
263+ # Call the function
264+ add_queue_label (item , namespace , local_queue )
265+
266+ # Assert that the label is added to the item
267+ assert item ["metadata" ]["labels" ] == {"kueue.x-k8s.io/queue-name" : "valid-queue" }
268+
269+
270+ def test_add_queue_label_with_invalid_local_queue (mocker ):
271+ # Mock the kubernetes.client.CustomObjectsApi and its response
272+ mock_api_instance = mocker .patch ("kubernetes.client.CustomObjectsApi" )
273+ mock_api_instance .return_value .list_namespaced_custom_object .return_value = {
274+ "items" : [
275+ {"metadata" : {"name" : "valid-queue" }},
276+ ]
277+ }
278+
279+ # Mock the local_queue_exists function to return False
280+ mocker .patch ("codeflare_sdk.common.kueue.local_queue_exists" , return_value = False )
281+
282+ # Define input item and parameters
283+ item = {"metadata" : {}}
284+ namespace = "test-namespace"
285+ local_queue = "invalid-queue"
286+
287+ # Call the function and expect a ValueError
288+ with pytest .raises (
289+ ValueError ,
290+ match = "local_queue provided does not exist or is not in this namespace" ,
291+ ):
292+ add_queue_label (item , namespace , local_queue )
293+
294+
178295# Make sure to always keep this function last
179296def test_cleanup ():
180297 os .remove (f"{ aw_dir } unit-test-cluster-kueue.yaml" )
0 commit comments