|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from lightning_cloud.openapi import ( |
| 7 | + Externalv1LightningappInstance, |
| 8 | + V1LightningappInstanceArtifact, |
| 9 | + V1ListCloudSpacesResponse, |
| 10 | + V1ListLightningappInstanceArtifactsResponse, |
| 11 | + V1ListLightningappInstancesResponse, |
| 12 | + V1ListMembershipsResponse, |
| 13 | + V1Membership, |
| 14 | +) |
| 15 | + |
| 16 | +from lightning.app.cli.commands import cd, ls, rm |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.skipif(sys.platform == "win32", reason="not supported on windows yet") |
| 20 | +def test_rm(monkeypatch): |
| 21 | + """This test validates rm behaves as expected.""" |
| 22 | + |
| 23 | + if os.path.exists(cd._CD_FILE): |
| 24 | + os.remove(cd._CD_FILE) |
| 25 | + |
| 26 | + client = MagicMock() |
| 27 | + client.projects_service_list_memberships.return_value = V1ListMembershipsResponse( |
| 28 | + memberships=[ |
| 29 | + V1Membership(name="project-0", project_id="project-id-0"), |
| 30 | + V1Membership(name="project-1", project_id="project-id-1"), |
| 31 | + V1Membership(name="project 2", project_id="project-id-2"), |
| 32 | + ] |
| 33 | + ) |
| 34 | + |
| 35 | + client.lightningapp_instance_service_list_lightningapp_instances().get.return_value = ( |
| 36 | + V1ListLightningappInstancesResponse( |
| 37 | + lightningapps=[ |
| 38 | + Externalv1LightningappInstance( |
| 39 | + name="app-name-0", |
| 40 | + id="app-id-0", |
| 41 | + ), |
| 42 | + Externalv1LightningappInstance( |
| 43 | + name="app-name-1", |
| 44 | + id="app-id-1", |
| 45 | + ), |
| 46 | + Externalv1LightningappInstance( |
| 47 | + name="app name 2", |
| 48 | + id="app-id-1", |
| 49 | + ), |
| 50 | + ] |
| 51 | + ) |
| 52 | + ) |
| 53 | + |
| 54 | + client.cloud_space_service_list_cloud_spaces().get.return_value = V1ListCloudSpacesResponse(cloudspaces=[]) |
| 55 | + |
| 56 | + clusters = MagicMock() |
| 57 | + clusters.clusters = [MagicMock()] |
| 58 | + client.projects_service_list_project_cluster_bindings.return_value = clusters |
| 59 | + |
| 60 | + def fn(*args, prefix, **kwargs): |
| 61 | + splits = [split for split in prefix.split("/") if split != ""] |
| 62 | + if len(splits) == 2: |
| 63 | + return V1ListLightningappInstanceArtifactsResponse( |
| 64 | + artifacts=[ |
| 65 | + V1LightningappInstanceArtifact(filename="file_1.txt"), |
| 66 | + V1LightningappInstanceArtifact(filename="folder_1/file_2.txt"), |
| 67 | + V1LightningappInstanceArtifact(filename="folder_2/folder_3/file_3.txt"), |
| 68 | + V1LightningappInstanceArtifact(filename="folder_2/file_4.txt"), |
| 69 | + ] |
| 70 | + ) |
| 71 | + elif splits[-1] == "folder_1": |
| 72 | + return V1ListLightningappInstanceArtifactsResponse( |
| 73 | + artifacts=[V1LightningappInstanceArtifact(filename="file_2.txt")] |
| 74 | + ) |
| 75 | + elif splits[-1] == "folder_2": |
| 76 | + return V1ListLightningappInstanceArtifactsResponse( |
| 77 | + artifacts=[ |
| 78 | + V1LightningappInstanceArtifact(filename="folder_3/file_3.txt"), |
| 79 | + V1LightningappInstanceArtifact(filename="file_4.txt"), |
| 80 | + ] |
| 81 | + ) |
| 82 | + elif splits[-1] == "folder_3": |
| 83 | + return V1ListLightningappInstanceArtifactsResponse( |
| 84 | + artifacts=[ |
| 85 | + V1LightningappInstanceArtifact(filename="file_3.txt"), |
| 86 | + ] |
| 87 | + ) |
| 88 | + |
| 89 | + client.lightningapp_instance_service_list_project_artifacts = fn |
| 90 | + |
| 91 | + client.lightningapp_instance_service_delete_project_artifact = MagicMock() |
| 92 | + |
| 93 | + monkeypatch.setattr(rm, "LightningClient", MagicMock(return_value=client)) |
| 94 | + monkeypatch.setattr(ls, "LightningClient", MagicMock(return_value=client)) |
| 95 | + |
| 96 | + assert ls.ls() == ["project-0", "project-1", "project 2"] |
| 97 | + assert "/project-0" == cd.cd("project-0", verify=False) |
| 98 | + |
| 99 | + assert f"/project-0{os.sep}app-name-1" == cd.cd("app-name-1", verify=False) |
| 100 | + |
| 101 | + assert f"/project-0{os.sep}app-name-1{os.sep}folder_1" == cd.cd("folder_1", verify=False) |
| 102 | + |
| 103 | + rm.rm("file_2.txt") |
| 104 | + |
| 105 | + kwargs = client.lightningapp_instance_service_delete_project_artifact._mock_call_args.kwargs |
| 106 | + assert kwargs["project_id"] == "project-id-0" |
| 107 | + assert kwargs["filename"] == "/lightningapps/app-id-1/folder_1/file_2.txt" |
| 108 | + |
| 109 | + os.remove(cd._CD_FILE) |
0 commit comments