Search in sources :

Example 66 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactoryTest method shouldNotCreateCredentialsSecretIfExists.

@Test
public void shouldNotCreateCredentialsSecretIfExists() throws Exception {
    // given
    namespaceFactory = spy(new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool));
    KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
    prepareNamespace(toReturnNamespace);
    doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
    MixedOperation mixedOperation = mock(MixedOperation.class);
    lenient().when(k8sClient.secrets()).thenReturn(mixedOperation);
    lenient().when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
    // when
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
    namespaceFactory.getOrCreate(identity);
    // then
    verify(namespaceOperation, never()).create(any());
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 67 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactoryTest method shouldCreatePreferencesConfigmapIfNotExists.

@Test
public void shouldCreatePreferencesConfigmapIfNotExists() throws Exception {
    // given
    namespaceFactory = spy(new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, Set.of(new PreferencesConfigMapConfigurator(clientFactory)), clientFactory, cheClientFactory, userManager, preferenceManager, pool));
    KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
    when(toReturnNamespace.getName()).thenReturn("namespaceName");
    doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
    MixedOperation mixedOperation = mock(MixedOperation.class);
    when(k8sClient.configMaps()).thenReturn(mixedOperation);
    when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
    when(namespaceResource.get()).thenReturn(null);
    // when
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
    namespaceFactory.getOrCreate(identity);
    // then
    ArgumentCaptor<ConfigMap> configMapCaptor = ArgumentCaptor.forClass(ConfigMap.class);
    verify(namespaceOperation).create(configMapCaptor.capture());
    ConfigMap configmap = configMapCaptor.getValue();
    Assert.assertEquals(configmap.getMetadata().getName(), PREFERENCES_CONFIGMAP_NAME);
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) PreferencesConfigMapConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.PreferencesConfigMapConfigurator) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 68 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactoryTest method shouldCreateAndBindCredentialsSecretRole.

@Test
public void shouldCreateAndBindCredentialsSecretRole() throws Exception {
    // given
    var serviceAccountConfigurator = new WorkspaceServiceAccountConfigurator("serviceAccount", "cr2, cr3", clientFactory);
    namespaceFactory = spy(new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, Set.of(serviceAccountConfigurator), clientFactory, cheClientFactory, userManager, preferenceManager, pool));
    KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
    prepareNamespace(toReturnNamespace);
    when(toReturnNamespace.getName()).thenReturn("workspace123");
    doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
    when(clientFactory.create(any())).thenReturn(k8sClient);
    when(cheClientFactory.create()).thenReturn(k8sClient);
    // when
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
    namespaceFactory.getOrCreate(identity);
    // then
    Optional<Role> roleOptional = k8sClient.rbac().roles().inNamespace("workspace123").list().getItems().stream().filter(r -> r.getMetadata().getName().equals(SECRETS_ROLE_NAME)).findAny();
    assertTrue(roleOptional.isPresent());
    PolicyRule rule = roleOptional.get().getRules().get(0);
    assertEquals(rule.getResources(), singletonList("secrets"));
    assertEquals(rule.getResourceNames(), singletonList(CREDENTIALS_SECRET_NAME));
    assertEquals(rule.getApiGroups(), singletonList(""));
    assertEquals(rule.getVerbs(), Arrays.asList("get", "patch"));
    assertTrue(k8sClient.rbac().roleBindings().inNamespace("workspace123").list().getItems().stream().anyMatch(rb -> rb.getMetadata().getName().equals("serviceAccount-secrets")));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Role(io.fabric8.kubernetes.api.model.rbac.Role) Arrays(java.util.Arrays) KubernetesClientFactory(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesClientFactory) Listeners(org.testng.annotations.Listeners) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) Collections.singletonList(java.util.Collections.singletonList) Mockito.doThrow(org.mockito.Mockito.doThrow) Resource(io.fabric8.kubernetes.client.dsl.Resource) CheServerKubernetesClientFactory(org.eclipse.che.workspace.infrastructure.kubernetes.CheServerKubernetesClientFactory) PreferenceManager(org.eclipse.che.api.user.server.PreferenceManager) Map(java.util.Map) Status(io.fabric8.kubernetes.api.model.Status) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) Set(java.util.Set) PolicyRule(io.fabric8.kubernetes.api.model.rbac.PolicyRule) Stream(java.util.stream.Stream) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) Mockito.mock(org.mockito.Mockito.mock) Optional.empty(java.util.Optional.empty) Mock(org.mockito.Mock) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Mockito.spy(org.mockito.Mockito.spy) Mockito.lenient(org.mockito.Mockito.lenient) ValidationException(org.eclipse.che.api.core.ValidationException) PHASE_ATTRIBUTE(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.PHASE_ATTRIBUTE) SECRETS_ROLE_NAME(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.SECRETS_ROLE_NAME) Assert(org.testng.Assert) NamespaceConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator) KubernetesServer(io.fabric8.kubernetes.client.server.mock.KubernetesServer) WorkspaceImplBuilder(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl.WorkspaceImplBuilder) Constants(org.eclipse.che.api.workspace.shared.Constants) ConfigurationException(org.eclipse.che.inject.ConfigurationException) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Mockito.never(org.mockito.Mockito.never) KubernetesNamespaceMetaImpl(org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl) CredentialsSecretConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.CredentialsSecretConfigurator) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) RoleBindingList(io.fabric8.kubernetes.api.model.rbac.RoleBindingList) Role(io.fabric8.kubernetes.api.model.rbac.Role) LoggerFactory(org.slf4j.LoggerFactory) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) ServiceAccountList(io.fabric8.kubernetes.api.model.ServiceAccountList) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) WorkspaceServiceAccountConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.WorkspaceServiceAccountConfigurator) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) BeforeMethod(org.testng.annotations.BeforeMethod) NamespaceList(io.fabric8.kubernetes.api.model.NamespaceList) FilterWatchListDeletable(io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable) Collectors(java.util.stream.Collectors) RoleList(io.fabric8.kubernetes.api.model.rbac.RoleList) KubernetesSharedPool(org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSharedPool) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Namespace(io.fabric8.kubernetes.api.model.Namespace) WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE) Secret(io.fabric8.kubernetes.api.model.Secret) Optional(java.util.Optional) PREFERENCES_CONFIGMAP_NAME(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.PREFERENCES_CONFIGMAP_NAME) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) Assert.assertNull(org.testng.Assert.assertNull) NamespaceProvisioner(org.eclipse.che.workspace.infrastructure.kubernetes.provision.NamespaceProvisioner) DataProvider(org.testng.annotations.DataProvider) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ArgumentCaptor(org.mockito.ArgumentCaptor) PreferencesConfigMapConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.PreferencesConfigMapConfigurator) Appender(ch.qos.logback.core.Appender) ClusterRoleBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBuilder) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) Assert.fail(org.testng.Assert.fail) DEFAULT_ATTRIBUTE(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.DEFAULT_ATTRIBUTE) CREDENTIALS_SECRET_NAME(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.CREDENTIALS_SECRET_NAME) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) NAMESPACE_TEMPLATE_ATTRIBUTE(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory.NAMESPACE_TEMPLATE_ATTRIBUTE) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Assert.assertTrue(org.testng.Assert.assertTrue) Sets(org.testng.collections.Sets) UserManager(org.eclipse.che.api.user.server.UserManager) Collections(java.util.Collections) WorkspaceServiceAccountConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.WorkspaceServiceAccountConfigurator) PolicyRule(io.fabric8.kubernetes.api.model.rbac.PolicyRule) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 69 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class KubernetesMachinesCacheTest method shouldGetMachines.

@Test
public void shouldGetMachines() throws Exception {
    // given
    RuntimeIdentity runtimeId = runtimeStates[0].getRuntimeId();
    // when
    machineCache.getMachines(runtimeId);
    // then
    Map<String, KubernetesMachineImpl> fetched = machineCache.getMachines(runtimeId);
    assertEquals(fetched.size(), 2);
    assertTrue(fetched.keySet().containsAll(asList("machine1", "machine2")));
    assertTrue(fetched.values().containsAll(asList(machines[0], machines[1])));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) KubernetesMachineImpl(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl) Test(org.testng.annotations.Test)

Example 70 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project devspaces-images by redhat-developer.

the class KubernetesMachinesCacheTest method shouldUpdateServerStatus.

@Test
public void shouldUpdateServerStatus() throws Exception {
    // given
    RuntimeIdentity runtimeId = runtimeStates[0].getRuntimeId();
    // when
    machineCache.updateServerStatus(runtimeId, machines[0].getName(), "server1", ServerStatus.RUNNING);
    // then
    KubernetesServerImpl fetchedServer = machineCache.getServer(runtimeId, "machine1", "server1");
    assertEquals(fetchedServer.getStatus(), ServerStatus.RUNNING);
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) KubernetesServerImpl(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesServerImpl) Test(org.testng.annotations.Test)

Aggregations

RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)128 Test (org.testng.annotations.Test)88 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)70 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)40 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)28 InternalEnvironment (org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment)24 RuntimeContext (org.eclipse.che.api.workspace.server.spi.RuntimeContext)22 Map (java.util.Map)20 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)18 List (java.util.List)16 Set (java.util.Set)14 ServerException (org.eclipse.che.api.core.ServerException)14 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)14 Traced (org.eclipse.che.commons.annotation.Traced)14 KubernetesMachineImpl (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl)14 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)12 Secret (io.fabric8.kubernetes.api.model.Secret)12 String.format (java.lang.String.format)12 Collectors (java.util.stream.Collectors)12 Named (javax.inject.Named)12