|
82 | 82 | import ray
|
83 | 83 | import pytest
|
84 | 84 | import yaml
|
85 |
| -from unittest.mock import MagicMock |
| 85 | +from unittest.mock import MagicMock, patch |
86 | 86 | from pytest_mock import MockerFixture
|
87 | 87 | from ray.job_submission import JobSubmissionClient
|
88 | 88 | from codeflare_sdk.job.ray_jobs import RayJobClient
|
89 | 89 |
|
| 90 | +import ipywidgets as widgets |
| 91 | +from IPython.display import display |
| 92 | + |
90 | 93 | # For mocking openshift client results
|
91 | 94 | fake_res = openshift.Result("fake")
|
92 | 95 |
|
@@ -2873,6 +2876,50 @@ def test_cluster_config_deprecation_conversion(mocker):
|
2873 | 2876 | assert config.worker_cpu_limits == 2
|
2874 | 2877 |
|
2875 | 2878 |
|
| 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 | + |
2876 | 2923 | # Make sure to always keep this function last
|
2877 | 2924 | def test_cleanup():
|
2878 | 2925 | os.remove(f"{aw_dir}unit-test-no-kueue.yaml")
|
|
0 commit comments