Skip to content

Commit 9e43686

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

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

tests/unit_test.py

+66-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,68 @@ def test_cluster_config_deprecation_conversion(mocker):
28732876
assert config.worker_cpu_limits == 2
28742877

28752878

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

0 commit comments

Comments
 (0)