Skip to content

Commit 84f9684

Browse files
committed
test: test ui up and down buttons
Signed-off-by: Bobbins228 <[email protected]>
1 parent c3666f7 commit 84f9684

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

tests/unit_test.py

+48-1
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@
8282
import ray
8383
import pytest
8484
import yaml
85-
from unittest.mock import MagicMock
85+
from unittest.mock import MagicMock, patch
8686
from pytest_mock import MockerFixture
8787
from ray.job_submission import JobSubmissionClient
8888
from codeflare_sdk.job.ray_jobs import RayJobClient
8989

90+
import ipywidgets as widgets
91+
from IPython.display import display
92+
9093
# For mocking openshift client results
9194
fake_res = openshift.Result("fake")
9295

@@ -2873,6 +2876,50 @@ def test_cluster_config_deprecation_conversion(mocker):
28732876
assert config.worker_cpu_limits == 2
28742877

28752878

2879+
"""
2880+
Ipywidgets tests
2881+
"""
2882+
2883+
2884+
def test_cluster_up_down_buttons(mocker):
2885+
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
2886+
mocker.patch(
2887+
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
2888+
return_value={"spec": {"domain": "apps.cluster.awsroute.org"}},
2889+
)
2890+
mocker.patch(
2891+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
2892+
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
2893+
)
2894+
os.environ["JPY_SESSION_NAME"] = "example-test"
2895+
cluster = Cluster(createClusterConfig())
2896+
2897+
with patch("ipywidgets.Button") as MockButton, patch("ipywidgets.Output"), patch(
2898+
"ipywidgets.HBox"
2899+
), patch.object(cluster, "up") as mock_up, patch.object(
2900+
cluster, "down"
2901+
) as mock_down:
2902+
# Create mock button instances
2903+
mock_up_button = MagicMock()
2904+
mock_down_button = MagicMock()
2905+
2906+
# Ensure the mock Button class returns the mock button instances in sequence
2907+
MockButton.side_effect = [mock_down_button, mock_up_button]
2908+
2909+
# Call the method under test
2910+
cluster._cluster_up_down_buttons()
2911+
2912+
# Simulate the button clicks by calling the mock on_click handlers
2913+
mock_up_button.on_click.call_args[0][0](None) # Simulate clicking "Cluster Up"
2914+
mock_down_button.on_click.call_args[0][0](
2915+
None
2916+
) # Simulate clicking "Cluster Down"
2917+
2918+
# Check if the `up` and `down` methods were called
2919+
mock_up.assert_called_once()
2920+
mock_down.assert_called_once()
2921+
2922+
28762923
# Make sure to always keep this function last
28772924
def test_cleanup():
28782925
os.remove(f"{aw_dir}unit-test-no-kueue.yaml")

0 commit comments

Comments
 (0)