UniqueWorkspacePVCStrategy implements WorkspaceVolumesStrategy { @Override public void cleanup(Workspace workspace) throws InfrastructureException { if (EphemeralWorkspaceUtility.isEphemeral(workspace)) { return; } factory .get(workspace) .persistentVolumeClaims() .delete(ImmutableMap.of(CHE_WORKSPACE_ID_LABEL, workspace.getId())); } @Inject  UniqueWorkspacePVCStrategy(
      @Named("che.infra.kubernetes.pvc.wait_bound") boolean waitBound,
      KubernetesNamespaceFactory factory,
      EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter,
      PVCProvisioner pvcProvisioner,
      SubPathPrefixes subpathPrefixes); @Override void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity); @Traced @Override void prepare(
      KubernetesEnvironment k8sEnv,
      RuntimeIdentity identity,
      long timeoutMillis,
      Map<String, String> startOptions); @Override void cleanup(Workspace workspace); static final String UNIQUE_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); strategy.cleanup(workspace); verify(pvcs).delete(ImmutableMap.of(CHE_WORKSPACE_ID_LABEL, 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"); strategy.cleanup(workspace); verify(pvcs).delete(any()); }
@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"); strategy.cleanup(workspace); verify(pvcs, never()).delete(any()); }