|
4 | 4 | import click
|
5 | 5 | import pytest
|
6 | 6 | from lightning_cloud.openapi import (
|
| 7 | + Externalv1LightningappInstance, |
7 | 8 | V1AWSClusterDriverSpec,
|
8 | 9 | V1ClusterDriver,
|
9 | 10 | V1ClusterPerformanceProfile,
|
|
14 | 15 | V1CreateClusterRequest,
|
15 | 16 | V1GetClusterResponse,
|
16 | 17 | V1KubernetesClusterDriver,
|
| 18 | + V1LightningappInstanceState, |
| 19 | + V1LightningappInstanceStatus, |
| 20 | + V1ListLightningappInstancesResponse, |
| 21 | + V1ListMembershipsResponse, |
| 22 | + V1Membership, |
17 | 23 | )
|
18 | 24 |
|
19 | 25 | from lightning_app.cli import cmd_clusters
|
@@ -95,17 +101,99 @@ def test_list_clusters(api: mock.MagicMock):
|
95 | 101 | api.assert_called_once_with(phase_not_in=[V1ClusterState.DELETED])
|
96 | 102 |
|
97 | 103 |
|
| 104 | +@pytest.fixture() |
| 105 | +def fixture_list_instances_empty(): |
| 106 | + return V1ListLightningappInstancesResponse([]) |
| 107 | + |
| 108 | + |
98 | 109 | @mock.patch("lightning_cloud.login.Auth.authenticate", MagicMock())
|
| 110 | +@mock.patch("lightning_app.utilities.network.LightningClient.projects_service_list_memberships") |
| 111 | +@mock.patch("lightning_app.utilities.network.LightningClient.lightningapp_instance_service_list_lightningapp_instances") |
99 | 112 | @mock.patch("lightning_app.utilities.network.LightningClient.cluster_service_delete_cluster")
|
100 | 113 | @mock.patch("lightning_app.utilities.network.LightningClient.cluster_service_get_cluster")
|
101 |
| -def test_delete_cluster_api(api_get: mock.MagicMock, api_delete: mock.MagicMock, async_or_interrupt, spec): |
| 114 | +def test_delete_cluster_api( |
| 115 | + api_get: mock.MagicMock, |
| 116 | + api_delete: mock.MagicMock, |
| 117 | + api_list_instances: mock.MagicMock, |
| 118 | + api_list_memberships: mock.MagicMock, |
| 119 | + async_or_interrupt, |
| 120 | + spec, |
| 121 | + fixture_list_instances_empty, |
| 122 | +): |
| 123 | + api_list_memberships.return_value = V1ListMembershipsResponse([V1Membership(project_id="test-project")]) |
| 124 | + api_list_instances.return_value = fixture_list_instances_empty |
102 | 125 | api_get.return_value = V1GetClusterResponse(spec=spec)
|
103 | 126 | cluster_manager = AWSClusterManager()
|
104 | 127 | cluster_manager.delete(cluster_id="test-7", do_async=async_or_interrupt)
|
105 | 128 |
|
106 | 129 | api_delete.assert_called_once_with(id="test-7", force=False)
|
107 | 130 |
|
108 | 131 |
|
| 132 | +@mock.patch("click.confirm") |
| 133 | +@mock.patch("lightning_cloud.login.Auth.authenticate", MagicMock()) |
| 134 | +@mock.patch("lightning_app.utilities.network.LightningClient.projects_service_list_memberships") |
| 135 | +@mock.patch("lightning_app.utilities.network.LightningClient.lightningapp_instance_service_list_lightningapp_instances") |
| 136 | +@mock.patch("lightning_app.utilities.network.LightningClient.cluster_service_delete_cluster") |
| 137 | +@mock.patch("lightning_app.utilities.network.LightningClient.cluster_service_get_cluster") |
| 138 | +def test_delete_cluster_with_stopped_apps( |
| 139 | + api_get: mock.MagicMock, |
| 140 | + api_delete: mock.MagicMock, |
| 141 | + api_list_instances: mock.MagicMock, |
| 142 | + api_list_memberships: mock.MagicMock, |
| 143 | + click_mock: mock.MagicMock, |
| 144 | + spec, |
| 145 | +): |
| 146 | + api_list_memberships.return_value = V1ListMembershipsResponse([V1Membership(project_id="test-project")]) |
| 147 | + api_list_instances.side_effect = [ |
| 148 | + # when querying for running apps |
| 149 | + V1ListLightningappInstancesResponse([]), |
| 150 | + # when querying for stopped apps |
| 151 | + V1ListLightningappInstancesResponse( |
| 152 | + lightningapps=[ |
| 153 | + Externalv1LightningappInstance( |
| 154 | + status=V1LightningappInstanceStatus( |
| 155 | + phase=V1LightningappInstanceState.STOPPED, |
| 156 | + ) |
| 157 | + ) |
| 158 | + ] |
| 159 | + ), |
| 160 | + ] |
| 161 | + api_get.return_value = V1GetClusterResponse(spec=spec) |
| 162 | + cluster_manager = AWSClusterManager() |
| 163 | + |
| 164 | + cluster_manager.delete(cluster_id="test-7", do_async=True) |
| 165 | + api_delete.assert_called_once_with(id="test-7", force=False) |
| 166 | + click_mock.assert_called_once_with("Are you sure you want to continue?", abort=True) |
| 167 | + |
| 168 | + |
| 169 | +@mock.patch("lightning_cloud.login.Auth.authenticate", MagicMock()) |
| 170 | +@mock.patch("lightning_app.utilities.network.LightningClient.projects_service_list_memberships") |
| 171 | +@mock.patch("lightning_app.utilities.network.LightningClient.lightningapp_instance_service_list_lightningapp_instances") |
| 172 | +@mock.patch("lightning_app.utilities.network.LightningClient.cluster_service_get_cluster") |
| 173 | +def test_delete_cluster_with_running_apps( |
| 174 | + api_get: mock.MagicMock, |
| 175 | + api_list_instances: mock.MagicMock, |
| 176 | + api_list_memberships: mock.MagicMock, |
| 177 | + spec, |
| 178 | +): |
| 179 | + api_list_memberships.return_value = V1ListMembershipsResponse([V1Membership(project_id="test-project")]) |
| 180 | + api_list_instances.return_value = V1ListLightningappInstancesResponse( |
| 181 | + lightningapps=[ |
| 182 | + Externalv1LightningappInstance( |
| 183 | + status=V1LightningappInstanceStatus( |
| 184 | + phase=V1LightningappInstanceState.RUNNING, |
| 185 | + ) |
| 186 | + ) |
| 187 | + ] |
| 188 | + ) |
| 189 | + api_get.return_value = V1GetClusterResponse(spec=spec) |
| 190 | + cluster_manager = AWSClusterManager() |
| 191 | + |
| 192 | + with pytest.raises(click.ClickException) as exception: |
| 193 | + cluster_manager.delete(cluster_id="test-7") |
| 194 | + exception.match("apps running") |
| 195 | + |
| 196 | + |
109 | 197 | class Test_check_cluster_id_is_valid:
|
110 | 198 | @pytest.mark.parametrize("name", ["test-7", "0wildgoat"])
|
111 | 199 | def test_valid(self, name):
|
|
0 commit comments