forked from project-codeflare/codeflare-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_widgets.py
479 lines (417 loc) · 18.1 KB
/
test_widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# Copyright 2024 IBM, Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import codeflare_sdk.common.widgets.widgets as cf_widgets
import pandas as pd
from unittest.mock import MagicMock, patch
from ..utils.unit_test_support import get_local_queue, create_cluster_config
from codeflare_sdk.ray.cluster.cluster import Cluster
from codeflare_sdk.ray.cluster.status import (
RayCluster,
RayClusterStatus,
)
import pytest
from kubernetes import client
@patch.dict(
"os.environ", {"JPY_SESSION_NAME": "example-test"}
) # Mock Jupyter environment variable
def test_cluster_up_down_buttons(mocker):
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
mocker.patch(
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
return_value={"spec": {"domain": "apps.cluster.awsroute.org"}},
)
mocker.patch(
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
)
cluster = Cluster(create_cluster_config())
with patch("ipywidgets.Button") as MockButton, patch(
"ipywidgets.Checkbox"
) as MockCheckbox, patch("ipywidgets.Output"), patch("ipywidgets.HBox"), patch(
"ipywidgets.VBox"
), patch.object(
cluster, "up"
) as mock_up, patch.object(
cluster, "down"
) as mock_down, patch.object(
cluster, "wait_ready"
) as mock_wait_ready:
# Create mock button & CheckBox instances
mock_up_button = MagicMock()
mock_down_button = MagicMock()
mock_wait_ready_check_box = MagicMock()
# Ensure the mock Button class returns the mock button instances in sequence
MockCheckbox.side_effect = [mock_wait_ready_check_box]
MockButton.side_effect = [mock_up_button, mock_down_button]
# Call the method under test
cf_widgets.cluster_up_down_buttons(cluster)
# Simulate checkbox being checked or unchecked
mock_wait_ready_check_box.value = True # Simulate checkbox being checked
# Simulate the button clicks by calling the mock on_click handlers
mock_up_button.on_click.call_args[0][0](None) # Simulate clicking "Cluster Up"
mock_down_button.on_click.call_args[0][0](
None
) # Simulate clicking "Cluster Down"
# Check if the `up` and `down` methods were called
mock_wait_ready.assert_called_once()
mock_up.assert_called_once()
mock_down.assert_called_once()
@patch.dict("os.environ", {}, clear=True) # Mock environment with no variables
def test_is_notebook_false():
assert cf_widgets.is_notebook() is False
@patch.dict(
"os.environ", {"JPY_SESSION_NAME": "example-test"}
) # Mock Jupyter environment variable
def test_is_notebook_true():
assert cf_widgets.is_notebook() is True
def test_view_clusters(mocker, capsys):
# If is not a notebook environment, a warning should be raised
with pytest.warns(
UserWarning,
match="view_clusters can only be used in a Jupyter Notebook environment.",
):
result = cf_widgets.view_clusters("default")
# Assert the function returns None when not in a notebook environment
assert result is None
# Prepare to run view_clusters when notebook environment is detected
mocker.patch("codeflare_sdk.common.widgets.widgets.is_notebook", return_value=True)
mock_get_current_namespace = mocker.patch(
"codeflare_sdk.ray.cluster.cluster.get_current_namespace",
return_value="default",
)
namespace = mock_get_current_namespace.return_value
# Assert the function returns None when no clusters are found
mock_fetch_cluster_data = mocker.patch(
"codeflare_sdk.common.widgets.widgets._fetch_cluster_data",
return_value=pd.DataFrame(),
)
result = cf_widgets.view_clusters()
captured = capsys.readouterr()
assert mock_fetch_cluster_data.return_value.empty
assert "No clusters found in the default namespace." in captured.out
assert result is None
# Prepare to run view_clusters with a test DataFrame
mock_fetch_cluster_data = mocker.patch(
"codeflare_sdk.common.widgets.widgets._fetch_cluster_data",
return_value=pd.DataFrame(
{
"Name": ["test-cluster"],
"Namespace": ["default"],
"Num Workers": ["1"],
"Head GPUs": ["0"],
"Worker GPUs": ["0"],
"Head CPU Req~Lim": ["1~1"],
"Head Memory Req~Lim": ["1Gi~1Gi"],
"Worker CPU Req~Lim": ["1~1"],
"Worker Memory Req~Lim": ["1Gi~1Gi"],
"status": ['<span style="color: green;">Ready ✓</span>'],
}
),
)
# Create a RayClusterManagerWidgets instance
ray_cluster_manager_instance = cf_widgets.RayClusterManagerWidgets(
ray_clusters_df=mock_fetch_cluster_data.return_value, namespace=namespace
)
# Patch the constructor of RayClusterManagerWidgets to return our initialized instance
mock_constructor = mocker.patch(
"codeflare_sdk.common.widgets.widgets.RayClusterManagerWidgets",
return_value=ray_cluster_manager_instance,
)
# Use a spy to track calls to display_widgets without replacing it
spy_display_widgets = mocker.spy(ray_cluster_manager_instance, "display_widgets")
cf_widgets.view_clusters()
mock_constructor.assert_called_once_with(
ray_clusters_df=mock_fetch_cluster_data.return_value, namespace=namespace
)
spy_display_widgets.assert_called_once()
def test_delete_cluster(mocker, capsys):
name = "test-cluster"
namespace = "default"
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
mock_ray_cluster = MagicMock()
mocker.patch(
"kubernetes.client.CustomObjectsApi.get_namespaced_custom_object",
side_effect=[
mock_ray_cluster,
client.ApiException(status=404),
client.ApiException(status=404),
mock_ray_cluster,
],
)
# In this scenario, the RayCluster exists and the AppWrapper does not.
mocker.patch(
"codeflare_sdk.ray.cluster.cluster._check_aw_exists", return_value=False
)
mock_delete_rc = mocker.patch(
"kubernetes.client.CustomObjectsApi.delete_namespaced_custom_object"
)
cf_widgets._delete_cluster(name, namespace)
mock_delete_rc.assert_called_once_with(
group="ray.io",
version="v1",
namespace=namespace,
plural="rayclusters",
name=name,
)
# In this scenario, the AppWrapper exists and the RayCluster does not
mocker.patch(
"codeflare_sdk.ray.cluster.cluster._check_aw_exists", return_value=True
)
mock_delete_aw = mocker.patch(
"kubernetes.client.CustomObjectsApi.delete_namespaced_custom_object"
)
cf_widgets._delete_cluster(name, namespace)
mock_delete_aw.assert_called_once_with(
group="workload.codeflare.dev",
version="v1beta2",
namespace=namespace,
plural="appwrappers",
name=name,
)
# In this scenario, the deletion of the resource times out.
with pytest.raises(
TimeoutError, match=f"Timeout waiting for {name} to be deleted."
):
cf_widgets._delete_cluster(name, namespace, 1)
def test_ray_cluster_manager_widgets_init(mocker, capsys):
namespace = "default"
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
mocker.patch(
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
)
test_ray_clusters_df = pd.DataFrame(
{
"Name": ["test-cluster-1", "test-cluster-2"],
"Namespace": [namespace, namespace],
"Num Workers": ["1", "2"],
"Head GPUs": ["0", "0"],
"Worker GPUs": ["0", "0"],
"Head CPU Req~Lim": ["1~1", "1~1"],
"Head Memory Req~Lim": ["1Gi~1Gi", "1Gi~1Gi"],
"Worker CPU Req~Lim": ["1~1", "1~1"],
"Worker Memory Req~Lim": ["1Gi~1Gi", "1Gi~1Gi"],
"status": [
'<span style="color: green;">Ready ✓</span>',
'<span style="color: green;">Ready ✓</span>',
],
}
)
mock_fetch_cluster_data = mocker.patch(
"codeflare_sdk.common.widgets.widgets._fetch_cluster_data",
return_value=test_ray_clusters_df,
)
mocker.patch(
"codeflare_sdk.ray.cluster.cluster.get_current_namespace",
return_value=namespace,
)
mock_delete_cluster = mocker.patch(
"codeflare_sdk.common.widgets.widgets._delete_cluster"
)
# # Mock ToggleButtons
mock_toggle_buttons = mocker.patch("ipywidgets.ToggleButtons")
mock_button = mocker.patch("ipywidgets.Button")
mock_output = mocker.patch("ipywidgets.Output")
# Initialize the RayClusterManagerWidgets instance
ray_cluster_manager_instance = cf_widgets.RayClusterManagerWidgets(
ray_clusters_df=test_ray_clusters_df, namespace=namespace
)
# Assertions for DataFrame and attributes
assert ray_cluster_manager_instance.ray_clusters_df.equals(
test_ray_clusters_df
), "ray_clusters_df attribute does not match the input DataFrame"
assert (
ray_cluster_manager_instance.namespace == namespace
), f"Expected namespace to be '{namespace}', but got '{ray_cluster_manager_instance.namespace}'"
assert (
ray_cluster_manager_instance.classification_widget.options
== test_ray_clusters_df["Name"].tolist()
), "classification_widget options do not match the input DataFrame"
# Assertions for widgets
mock_toggle_buttons.assert_called_once_with(
options=test_ray_clusters_df["Name"].tolist(),
value=test_ray_clusters_df["Name"].tolist()[0],
description="Select an existing cluster:",
)
assert (
ray_cluster_manager_instance.classification_widget
== mock_toggle_buttons.return_value
), "classification_widget is not set correctly"
assert (
ray_cluster_manager_instance.delete_button == mock_button.return_value
), "delete_button is not set correctly"
assert (
ray_cluster_manager_instance.list_jobs_button == mock_button.return_value
), "list_jobs_button is not set correctly"
assert (
ray_cluster_manager_instance.ray_dashboard_button == mock_button.return_value
), "ray_dashboard_button is not set correctly"
assert (
ray_cluster_manager_instance.refresh_data_button == mock_button.return_value
), "refresh_data_button is not set correctly"
assert (
ray_cluster_manager_instance.raycluster_data_output == mock_output.return_value
), "raycluster_data_output is not set correctly"
assert (
ray_cluster_manager_instance.user_output == mock_output.return_value
), "user_output is not set correctly"
assert (
ray_cluster_manager_instance.url_output == mock_output.return_value
), "url_output is not set correctly"
### Test button click events
mock_delete_button = MagicMock()
mock_list_jobs_button = MagicMock()
mock_ray_dashboard_button = MagicMock()
mock_refresh_dataframe_button = MagicMock()
mock_javascript = mocker.patch("codeflare_sdk.common.widgets.widgets.Javascript")
ray_cluster_manager_instance.url_output = MagicMock()
mock_dashboard_uri = mocker.patch(
"codeflare_sdk.ray.cluster.cluster.Cluster.cluster_dashboard_uri",
return_value="https://ray-dashboard-test-cluster-1-ns.apps.cluster.awsroute.org",
)
# Simulate clicking the list jobs button
ray_cluster_manager_instance.classification_widget.value = "test-cluster-1"
ray_cluster_manager_instance._on_list_jobs_button_click(mock_list_jobs_button)
captured = capsys.readouterr()
assert (
f"Opening Ray Jobs Dashboard for test-cluster-1 cluster:\n{mock_dashboard_uri.return_value}/#/jobs"
in captured.out
)
mock_javascript.assert_called_with(
f'window.open("{mock_dashboard_uri.return_value}/#/jobs", "_blank");'
)
# Simulate clicking the refresh data button
ray_cluster_manager_instance._on_refresh_data_button_click(
mock_refresh_dataframe_button
)
mock_fetch_cluster_data.assert_called_with(namespace)
# Simulate clicking the Ray dashboard button
ray_cluster_manager_instance.classification_widget.value = "test-cluster-1"
ray_cluster_manager_instance._on_ray_dashboard_button_click(
mock_ray_dashboard_button
)
captured = capsys.readouterr()
assert (
f"Opening Ray Dashboard for test-cluster-1 cluster:\n{mock_dashboard_uri.return_value}"
in captured.out
)
mock_javascript.assert_called_with(
f'window.open("{mock_dashboard_uri.return_value}", "_blank");'
)
# Simulate clicking the delete button
ray_cluster_manager_instance.classification_widget.value = "test-cluster-1"
ray_cluster_manager_instance._on_delete_button_click(mock_delete_button)
mock_delete_cluster.assert_called_with("test-cluster-1", namespace)
mock_fetch_cluster_data.return_value = pd.DataFrame()
ray_cluster_manager_instance.classification_widget.value = "test-cluster-2"
ray_cluster_manager_instance._on_delete_button_click(mock_delete_button)
mock_delete_cluster.assert_called_with("test-cluster-2", namespace)
# Assert on deletion that the dataframe is empty
assert (
ray_cluster_manager_instance.ray_clusters_df.empty
), "Expected DataFrame to be empty after deletion"
captured = capsys.readouterr()
assert (
f"Cluster test-cluster-1 in the {namespace} namespace was deleted successfully."
in captured.out
)
def test_fetch_cluster_data(mocker):
# Return empty dataframe when no clusters are found
mocker.patch("codeflare_sdk.ray.cluster.cluster.list_all_clusters", return_value=[])
df = cf_widgets._fetch_cluster_data(namespace="default")
assert df.empty
# Create mock RayCluster objects
mock_raycluster1 = MagicMock(spec=RayCluster)
mock_raycluster1.name = "test-cluster-1"
mock_raycluster1.namespace = "default"
mock_raycluster1.num_workers = 1
mock_raycluster1.head_extended_resources = {"nvidia.com/gpu": "1"}
mock_raycluster1.worker_extended_resources = {"nvidia.com/gpu": "2"}
mock_raycluster1.head_cpu_requests = "500m"
mock_raycluster1.head_cpu_limits = "1000m"
mock_raycluster1.head_mem_requests = "1Gi"
mock_raycluster1.head_mem_limits = "2Gi"
mock_raycluster1.worker_cpu_requests = "1000m"
mock_raycluster1.worker_cpu_limits = "2000m"
mock_raycluster1.worker_mem_requests = "2Gi"
mock_raycluster1.worker_mem_limits = "4Gi"
mock_raycluster1.status = MagicMock()
mock_raycluster1.status.name = "READY"
mock_raycluster1.status = RayClusterStatus.READY
mock_raycluster2 = MagicMock(spec=RayCluster)
mock_raycluster2.name = "test-cluster-2"
mock_raycluster2.namespace = "default"
mock_raycluster2.num_workers = 2
mock_raycluster2.head_extended_resources = {}
mock_raycluster2.worker_extended_resources = {}
mock_raycluster2.head_cpu_requests = None
mock_raycluster2.head_cpu_limits = None
mock_raycluster2.head_mem_requests = None
mock_raycluster2.head_mem_limits = None
mock_raycluster2.worker_cpu_requests = None
mock_raycluster2.worker_cpu_limits = None
mock_raycluster2.worker_mem_requests = None
mock_raycluster2.worker_mem_limits = None
mock_raycluster2.status = MagicMock()
mock_raycluster2.status.name = "SUSPENDED"
mock_raycluster2.status = RayClusterStatus.SUSPENDED
with patch(
"codeflare_sdk.ray.cluster.cluster.list_all_clusters",
return_value=[mock_raycluster1, mock_raycluster2],
):
# Call the function under test
df = cf_widgets._fetch_cluster_data(namespace="default")
# Expected DataFrame
expected_data = {
"Name": ["test-cluster-1", "test-cluster-2"],
"Namespace": ["default", "default"],
"Num Workers": [1, 2],
"Head GPUs": ["nvidia.com/gpu: 1", "0"],
"Worker GPUs": ["nvidia.com/gpu: 2", "0"],
"Head CPU Req~Lim": ["500m~1000m", "0~0"],
"Head Memory Req~Lim": ["1Gi~2Gi", "0~0"],
"Worker CPU Req~Lim": ["1000m~2000m", "0~0"],
"Worker Memory Req~Lim": ["2Gi~4Gi", "0~0"],
"status": [
'<span style="color: green;">Ready ✓</span>',
'<span style="color: #007BFF;">Suspended ❄️</span>',
],
}
expected_df = pd.DataFrame(expected_data)
# Assert that the DataFrame matches expected
pd.testing.assert_frame_equal(
df.reset_index(drop=True), expected_df.reset_index(drop=True)
)
def test_format_status():
# Test each possible status
test_cases = [
(RayClusterStatus.READY, '<span style="color: green;">Ready ✓</span>'),
(
RayClusterStatus.SUSPENDED,
'<span style="color: #007BFF;">Suspended ❄️</span>',
),
(RayClusterStatus.FAILED, '<span style="color: red;">Failed ✗</span>'),
(RayClusterStatus.UNHEALTHY, '<span style="color: purple;">Unhealthy</span>'),
(RayClusterStatus.UNKNOWN, '<span style="color: purple;">Unknown</span>'),
]
for status, expected_output in test_cases:
assert (
cf_widgets._format_status(status) == expected_output
), f"Failed for status: {status}"
# Test an unrecognized status
unrecognized_status = "NotAStatus"
assert (
cf_widgets._format_status(unrecognized_status) == "NotAStatus"
), "Failed for unrecognized status"