CheNamespace { public List<ConfigMap> createConfigMaps(List<ConfigMap> configMaps, RuntimeIdentity identity) throws InfrastructureException { if (configMaps.isEmpty()) { return Collections.emptyList(); } validate(identity, WorkspaceStatus.STARTING); List<ConfigMap> createdConfigMaps = new ArrayList<>(); for (ConfigMap cm : configMaps) { createdConfigMaps.add(createConfigMap(cm, identity)); } return createdConfigMaps; } @Inject  CheNamespace(
      CheInstallationLocation installationLocation,
      CheServerKubernetesClientFactory clientFactory,
      WorkspaceRuntimes workspaceRuntimes); List<ConfigMap> createConfigMaps(List<ConfigMap> configMaps, RuntimeIdentity identity); void cleanUp(String workspaceId);  }
@Test public void testCreateConfigMaps() throws InfrastructureException { when(internalRuntime.getOwner()).thenReturn(OWNER_ID); when(internalRuntime.getStatus()).thenReturn(WorkspaceStatus.STARTING); Map<String, String> cheNamespaceAnnotations = ImmutableMap.of(CREATE_IN_CHE_INSTALLATION_NAMESPACE, "true"); ConfigMap cm1 = new ConfigMapBuilder() .withNewMetadata() .withName("cm1") .withAnnotations(cheNamespaceAnnotations) .endMetadata() .build(); ConfigMap cm2 = new ConfigMapBuilder() .withNewMetadata() .withName("cm2") .withAnnotations(cheNamespaceAnnotations) .endMetadata() .build(); when(clientFactory.create()).thenReturn(kubeClient); when(kubeClient.configMaps()).thenReturn(kubeConfigMaps); when(kubeConfigMaps.inNamespace(CHE_NAMESPACE)).thenReturn(kubeConfigMapsInNamespace); when(kubeConfigMapsInNamespace.create(any(ConfigMap.class))).thenReturn(cm1).thenReturn(cm2); List<ConfigMap> configMapsToCreate = Arrays.asList(cm1, cm2); List<ConfigMap> createdConfigMaps = cheNamespace.createConfigMaps(configMapsToCreate, identity); assertEquals(createdConfigMaps.size(), 2); createdConfigMaps.forEach( cm -> assertEquals(cm.getMetadata().getLabels().get(CHE_WORKSPACE_ID_LABEL), WORKSPACE_ID)); }
@Test(expectedExceptions = InfrastructureException.class) public void failWhenTryToCreateCmWithoutAnnotation() throws InfrastructureException { when(internalRuntime.getOwner()).thenReturn(OWNER_ID); when(internalRuntime.getStatus()).thenReturn(WorkspaceStatus.STARTING); ConfigMap cm1 = new ConfigMapBuilder().withNewMetadata().withName("cm1").endMetadata().build(); when(clientFactory.create()).thenReturn(kubeClient); when(kubeClient.configMaps()).thenReturn(kubeConfigMaps); when(kubeConfigMaps.inNamespace(CHE_NAMESPACE)).thenReturn(kubeConfigMapsInNamespace); when(kubeConfigMapsInNamespace.create(any(ConfigMap.class))).thenReturn(cm1); List<ConfigMap> configMapsToCreate = Collections.singletonList(cm1); cheNamespace.createConfigMaps(configMapsToCreate, identity); }
@Test(expectedExceptions = InfrastructureException.class) public void failWhenTryToCreateCmWithWronglySetAnnotation() throws InfrastructureException { when(internalRuntime.getOwner()).thenReturn(OWNER_ID); when(internalRuntime.getStatus()).thenReturn(WorkspaceStatus.STARTING); ConfigMap cm1 = new ConfigMapBuilder() .withNewMetadata() .withName("cm1") .withAnnotations( Collections.singletonMap(CREATE_IN_CHE_INSTALLATION_NAMESPACE, "blabol")) .endMetadata() .build(); when(clientFactory.create()).thenReturn(kubeClient); when(kubeClient.configMaps()).thenReturn(kubeConfigMaps); when(kubeConfigMaps.inNamespace(CHE_NAMESPACE)).thenReturn(kubeConfigMapsInNamespace); when(kubeConfigMapsInNamespace.create(any(ConfigMap.class))).thenReturn(cm1); List<ConfigMap> configMapsToCreate = Collections.singletonList(cm1); cheNamespace.createConfigMaps(configMapsToCreate, identity); }
@Test(expectedExceptions = InfrastructureException.class) public void testCreateConfigmapFailsWhenNoWorkspaceIdFoundInRuntimes() throws InfrastructureException, ServerException { when(workspaceRuntimes.getInternalRuntime(WORKSPACE_ID)).thenThrow(ServerException.class); when(internalRuntime.getOwner()).thenReturn(OWNER_ID); when(internalRuntime.getStatus()).thenReturn(WorkspaceStatus.STARTING); ConfigMap cm1 = new ConfigMapBuilder().withNewMetadata().withName("cm1").endMetadata().build(); cheNamespace.createConfigMaps(Collections.singletonList(cm1), identity); }
@Test(expectedExceptions = InfrastructureException.class) public void testCreateConfigmapFailsWhenOwnerDontMatch() throws InfrastructureException { when(internalRuntime.getOwner()).thenReturn("nope"); when(internalRuntime.getStatus()).thenReturn(WorkspaceStatus.STARTING); ConfigMap cm1 = new ConfigMapBuilder().withNewMetadata().withName("cm1").endMetadata().build(); cheNamespace.createConfigMaps(Collections.singletonList(cm1), identity); }
@Test(expectedExceptions = InfrastructureException.class) public void testCreateConfigmapFailsWhenWorkspaceStatusIsNotStarting() throws InfrastructureException { when(internalRuntime.getOwner()).thenReturn(OWNER_ID); when(internalRuntime.getStatus()).thenReturn(WorkspaceStatus.RUNNING); ConfigMap cm1 = new ConfigMapBuilder().withNewMetadata().withName("cm1").endMetadata().build(); cheNamespace.createConfigMaps(Collections.singletonList(cm1), identity); }
@Test public void testCreateEmptyReturnEmpty() throws InfrastructureException { assertTrue(cheNamespace.createConfigMaps(Collections.emptyList(), identity).isEmpty()); }