Skip to content

Commit 0c0f562

Browse files
committed
test: test ui up and down buttons
Signed-off-by: Bobbins228 <[email protected]>
1 parent 4d78ffc commit 0c0f562

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

tests/unit_test.py

+78-1
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,14 @@
8383
import ray
8484
import pytest
8585
import yaml
86-
from unittest.mock import MagicMock
86+
from unittest.mock import MagicMock, patch
8787
from pytest_mock import MockerFixture
8888
from ray.job_submission import JobSubmissionClient
8989
from codeflare_sdk.job.ray_jobs import RayJobClient
9090

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

@@ -2874,6 +2877,80 @@ def test_cluster_config_deprecation_conversion(mocker):
28742877
assert config.worker_cpu_limits == 2
28752878

28762879

2880+
"""
2881+
Ipywidgets tests
2882+
"""
2883+
2884+
2885+
@patch.dict(
2886+
"os.environ", {"JPY_SESSION_NAME": "example-test"}
2887+
) # Mock Jupyter environment variable
2888+
def test_cluster_up_down_buttons(mocker):
2889+
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
2890+
mocker.patch(
2891+
"kubernetes.client.CustomObjectsApi.get_cluster_custom_object",
2892+
return_value={"spec": {"domain": "apps.cluster.awsroute.org"}},
2893+
)
2894+
mocker.patch(
2895+
"kubernetes.client.CustomObjectsApi.list_namespaced_custom_object",
2896+
return_value=get_local_queue("kueue.x-k8s.io", "v1beta1", "ns", "localqueues"),
2897+
)
2898+
cluster = Cluster(createClusterConfig())
2899+
2900+
with patch("ipywidgets.Button") as MockButton, patch(
2901+
"ipywidgets.Checkbox"
2902+
) as MockCheckbox, patch("ipywidgets.Output"), patch("ipywidgets.HBox"), patch(
2903+
"ipywidgets.VBox"
2904+
), patch.object(
2905+
cluster, "up"
2906+
) as mock_up, patch.object(
2907+
cluster, "down"
2908+
) as mock_down, patch.object(
2909+
cluster, "wait_ready"
2910+
) as mock_wait_ready:
2911+
# Create mock button & CheckBox instances
2912+
mock_up_button = MagicMock()
2913+
mock_down_button = MagicMock()
2914+
mock_wait_ready_check_box = MagicMock()
2915+
2916+
# Ensure the mock Button class returns the mock button instances in sequence
2917+
MockCheckbox.side_effect = [mock_wait_ready_check_box]
2918+
MockButton.side_effect = [mock_up_button, mock_down_button]
2919+
2920+
# Call the method under test
2921+
cluster_up_down_buttons(cluster)
2922+
2923+
# Simulate checkbox being checked or unchecked
2924+
mock_wait_ready_check_box.value = True # Simulate checkbox being checked
2925+
2926+
# Simulate the button clicks by calling the mock on_click handlers
2927+
mock_up_button.on_click.call_args[0][0](None) # Simulate clicking "Cluster Up"
2928+
mock_down_button.on_click.call_args[0][0](
2929+
None
2930+
) # Simulate clicking "Cluster Down"
2931+
2932+
# Check if the `up` and `down` methods were called
2933+
mock_wait_ready.assert_called_once()
2934+
mock_up.assert_called_once()
2935+
mock_down.assert_called_once()
2936+
2937+
2938+
@patch.dict("os.environ", {}, clear=True) # Mock environment with no variables
2939+
def test_is_notebook_false():
2940+
from codeflare_sdk.cluster.widgets import is_notebook
2941+
2942+
assert is_notebook() is False
2943+
2944+
2945+
@patch.dict(
2946+
"os.environ", {"JPY_SESSION_NAME": "example-test"}
2947+
) # Mock Jupyter environment variable
2948+
def test_is_notebook_true():
2949+
from codeflare_sdk.cluster.widgets import is_notebook
2950+
2951+
assert is_notebook() is True
2952+
2953+
28772954
# Make sure to always keep this function last
28782955
def test_cleanup():
28792956
os.remove(f"{aw_dir}unit-test-no-kueue.yaml")

0 commit comments

Comments
 (0)