CommonPVCStrategy implements WorkspaceVolumesStrategy { @Override public void cleanup(Workspace workspace) throws InfrastructureException { if (EphemeralWorkspaceUtility.isEphemeral(workspace)) { return; } String workspaceId = workspace.getId(); PersistentVolumeClaim pvc = createCommonPVC(workspaceId); pvcSubPathHelper.removeDirsAsync( workspaceId, factory.get(workspace).getName(), pvc.getMetadata().getName(), subpathPrefixes.getWorkspaceSubPath(workspaceId)); } @Inject  CommonPVCStrategy(
      @Named("che.infra.kubernetes.pvc.name") String configuredPVCName,
      @Named("che.infra.kubernetes.pvc.quantity") String pvcQuantity,
      @Named("che.infra.kubernetes.pvc.access_mode") String pvcAccessMode,
      @Named("che.infra.kubernetes.pvc.precreate_subpaths") boolean preCreateDirs,
      @Named("che.infra.kubernetes.pvc.storage_class_name") String pvcStorageClassName,
      @Named("che.infra.kubernetes.pvc.wait_bound") boolean waitBound,
      PVCSubPathHelper pvcSubPathHelper,
      KubernetesNamespaceFactory factory,
      EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter,
      PVCProvisioner pvcProvisioner,
      PodsVolumes podsVolumes,
      SubPathPrefixes subpathPrefixes); @Override void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity); @Override @Traced void prepare(
      KubernetesEnvironment k8sEnv,
      RuntimeIdentity identity,
      long timeoutMillis,
      Map<String, String> startOptions); @Override void cleanup(Workspace workspace); static final String COMMON_STRATEGY; }
@Test public void shouldDeletePVCsIfThereIsNoPersistAttributeInWorkspaceConfigWhenCleanupCalled() throws Exception { Workspace workspace = mock(Workspace.class); lenient().when(workspace.getId()).thenReturn(WORKSPACE_ID); WorkspaceConfig workspaceConfig = mock(WorkspaceConfig.class); lenient().when(workspace.getConfig()).thenReturn(workspaceConfig); Map<String, String> workspaceConfigAttributes = new HashMap<>(); lenient().when(workspaceConfig.getAttributes()).thenReturn(workspaceConfigAttributes); KubernetesNamespace ns = mock(KubernetesNamespace.class); when(factory.get(eq(workspace))).thenReturn(ns); when(ns.getName()).thenReturn("ns"); commonPVCStrategy.cleanup(workspace); verify(pvcSubPathHelper).removeDirsAsync(WORKSPACE_ID, "ns", PVC_NAME, WORKSPACE_ID); }
@Test public void shouldDeletePVCsIfPersistAttributeIsSetToTrueInWorkspaceConfigWhenCleanupCalled() throws Exception { Workspace workspace = mock(Workspace.class); lenient().when(workspace.getId()).thenReturn(WORKSPACE_ID); WorkspaceConfig workspaceConfig = mock(WorkspaceConfig.class); lenient().when(workspace.getConfig()).thenReturn(workspaceConfig); Map<String, String> workspaceConfigAttributes = new HashMap<>(); lenient().when(workspaceConfig.getAttributes()).thenReturn(workspaceConfigAttributes); workspaceConfigAttributes.put(PERSIST_VOLUMES_ATTRIBUTE, "true"); KubernetesNamespace ns = mock(KubernetesNamespace.class); when(factory.get(eq(workspace))).thenReturn(ns); when(ns.getName()).thenReturn("ns"); commonPVCStrategy.cleanup(workspace); verify(pvcSubPathHelper).removeDirsAsync(WORKSPACE_ID, "ns", PVC_NAME, WORKSPACE_ID); }
@Test public void shouldDoNothingIfPersistAttributeIsSetToFalseInWorkspaceConfigWhenCleanupCalled() throws Exception { Workspace workspace = mock(Workspace.class); lenient().when(workspace.getId()).thenReturn(WORKSPACE_ID); WorkspaceConfig workspaceConfig = mock(WorkspaceConfig.class); lenient().when(workspace.getConfig()).thenReturn(workspaceConfig); Map<String, String> workspaceConfigAttributes = new HashMap<>(); lenient().when(workspaceConfig.getAttributes()).thenReturn(workspaceConfigAttributes); workspaceConfigAttributes.put(PERSIST_VOLUMES_ATTRIBUTE, "false"); commonPVCStrategy.cleanup(workspace); verify(pvcSubPathHelper, never()).removeDirsAsync(WORKSPACE_ID, null, WORKSPACE_ID); }