|
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,68 @@ 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 | +@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 | + |
2876 | 2941 | # Make sure to always keep this function last
|
2877 | 2942 | def test_cleanup():
|
2878 | 2943 | os.remove(f"{aw_dir}unit-test-no-kueue.yaml")
|
|
0 commit comments