use of org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method shouldRecoverEachRuntimeOnlyOnce.
@Test
public void shouldRecoverEachRuntimeOnlyOnce() throws Exception {
// Given
Set<RuntimeIdentity> identities = generateRuntimeIdentitySet(200);
doReturn(identities).when(infrastructure).getIdentities();
for (RuntimeIdentity identity : identities) {
mockWorkspaceWithDevfile(identity);
RuntimeContext context = mockContext(identity);
when(context.getRuntime()).thenReturn(new TestInternalRuntime(context, emptyMap(), WorkspaceStatus.STARTING));
doReturn(context).when(infrastructure).prepare(eq(identity), any());
}
when(statuses.get(anyString())).thenReturn(WorkspaceStatus.STARTING);
InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
CountDownLatch finishLatch = new CountDownLatch(1);
WorkspaceRuntimes runtimesSpy = spy(runtimes);
// When
WorkspaceRuntimes.RecoverRuntimesTask recoverRuntimesTask = runtimesSpy.new RecoverRuntimesTask(identities);
new Thread(() -> {
recoverRuntimesTask.run();
finishLatch.countDown();
}).start();
// simulate all WorkspaceManager methods that uses WorkspaceManager.normalizeState
new Thread(() -> {
List<RuntimeIdentity> runtimeIdentities = new ArrayList<>(identities);
Collections.shuffle(runtimeIdentities);
for (RuntimeIdentity runtimeIdentity : runtimeIdentities) {
if (finishLatch.getCount() > 0) {
try {
runtimesSpy.injectRuntime(WorkspaceImpl.builder().setId(runtimeIdentity.getWorkspaceId()).build());
} catch (ServerException e) {
fail(e.getMessage());
}
} else {
break;
}
}
}).start();
finishLatch.await();
// Then
verify(runtimesSpy, Mockito.times(identities.size())).recoverOne(any(RuntimeInfrastructure.class), any(RuntimeIdentity.class));
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method runtimeRecoveryContinuesThroughRuntimeException.
@Test
public void runtimeRecoveryContinuesThroughRuntimeException() throws Exception {
// Given
RuntimeIdentityImpl identity1 = new RuntimeIdentityImpl("workspace1", "env1", "owner1", "infraNamespace");
RuntimeIdentityImpl identity2 = new RuntimeIdentityImpl("workspace2", "env2", "owner2", "infraNamespace");
RuntimeIdentityImpl identity3 = new RuntimeIdentityImpl("workspace3", "env3", "owner3", "infraNamespace");
Set<RuntimeIdentity> identities = ImmutableSet.<RuntimeIdentity>builder().add(identity1).add(identity2).add(identity3).build();
mockWorkspaceWithConfig(identity1);
mockWorkspaceWithConfig(identity2);
mockWorkspaceWithConfig(identity3);
when(statuses.get(anyString())).thenReturn(WorkspaceStatus.STARTING);
RuntimeContext context1 = mockContext(identity1);
when(context1.getRuntime()).thenReturn(new TestInternalRuntime(context1, emptyMap(), WorkspaceStatus.STARTING));
doReturn(context1).when(infrastructure).prepare(eq(identity1), any());
RuntimeContext context2 = mockContext(identity2);
RuntimeContext context3 = mockContext(identity3);
when(context3.getRuntime()).thenReturn(new TestInternalRuntime(context3, emptyMap(), WorkspaceStatus.STARTING));
doReturn(context3).when(infrastructure).prepare(eq(identity3), any());
InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
// Want to fail recovery of identity2
doThrow(new RuntimeException("oops!")).when(infrastructure).prepare(eq(identity2), any(InternalEnvironment.class));
// When
runtimes.new RecoverRuntimesTask(identities).run();
// Then
verify(infrastructure).prepare(identity1, internalEnvironment);
verify(infrastructure).prepare(identity2, internalEnvironment);
verify(infrastructure).prepare(identity3, internalEnvironment);
WorkspaceImpl workspace1 = WorkspaceImpl.builder().setId(identity1.getWorkspaceId()).build();
runtimes.injectRuntime(workspace1);
assertNotNull(workspace1.getRuntime());
assertEquals(workspace1.getStatus(), WorkspaceStatus.STARTING);
WorkspaceImpl workspace3 = WorkspaceImpl.builder().setId(identity3.getWorkspaceId()).build();
runtimes.injectRuntime(workspace3);
assertNotNull(workspace3.getRuntime());
assertEquals(workspace3.getStatus(), WorkspaceStatus.STARTING);
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method internalEnvironmentCreationShouldRespectNoEnvironmentCase.
@Test
public void internalEnvironmentCreationShouldRespectNoEnvironmentCase() throws Exception {
InternalEnvironmentFactory noEnvFactory = mock(InternalEnvironmentFactory.class);
runtimes = new WorkspaceRuntimes(eventService, ImmutableMap.of(TEST_ENVIRONMENT_TYPE, testEnvFactory, NO_ENVIRONMENT_RECIPE_TYPE, noEnvFactory), infrastructure, sharedPool, workspaceDao, dbInitializer, probeScheduler, statuses, lockService, devfileConverter, false);
InternalEnvironment expectedEnvironment = mock(InternalEnvironment.class);
when(noEnvFactory.create(eq(null))).thenReturn(expectedEnvironment);
InternalEnvironment actualEnvironment = runtimes.createInternalEnvironment(null, emptyMap(), emptyList(), null);
assertEquals(actualEnvironment, expectedEnvironment);
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method shouldReturnRuntimesIdsOfActiveWorkspacesForGivenOwner.
@Test
public void shouldReturnRuntimesIdsOfActiveWorkspacesForGivenOwner() throws Exception {
// given
String ws1 = generate("workspace", 6);
String ws2 = generate("workspace", 6);
String ws3 = generate("workspace", 6);
String owner = generate("user", 6);
when(statuses.asMap()).thenReturn(ImmutableMap.of(ws1, WorkspaceStatus.STARTING, ws2, WorkspaceStatus.RUNNING, ws3, WorkspaceStatus.STOPPING));
RuntimeIdentityImpl runtimeIdentity1 = new RuntimeIdentityImpl(ws1, generate("env", 6), owner, generate("infraNamespace", 6));
RuntimeIdentityImpl runtimeIdentity2 = new RuntimeIdentityImpl(ws2, generate("env", 6), generate("user", 6), generate("infraNamespace", 6));
RuntimeIdentityImpl runtimeIdentity3 = new RuntimeIdentityImpl(ws3, generate("env", 6), generate("user", 6), generate("infraNamespace", 6));
mockWorkspaceWithConfig(runtimeIdentity1);
mockWorkspaceWithConfig(runtimeIdentity2);
mockWorkspaceWithConfig(runtimeIdentity3);
RuntimeContext context1 = mockContext(runtimeIdentity1);
RuntimeContext context2 = mockContext(runtimeIdentity2);
RuntimeContext context3 = mockContext(runtimeIdentity3);
when(context1.getRuntime()).thenReturn(new TestInternalRuntime(context1, emptyMap(), WorkspaceStatus.STARTING));
when(context2.getRuntime()).thenReturn(new TestInternalRuntime(context2, emptyMap(), WorkspaceStatus.RUNNING));
when(context3.getRuntime()).thenReturn(new TestInternalRuntime(context3, emptyMap(), WorkspaceStatus.STOPPING));
doReturn(context1).when(infrastructure).prepare(eq(runtimeIdentity1), any());
doReturn(context2).when(infrastructure).prepare(eq(runtimeIdentity2), any());
doReturn(context3).when(infrastructure).prepare(eq(runtimeIdentity3), any());
Set<RuntimeIdentity> identities = ImmutableSet.of(runtimeIdentity1, runtimeIdentity2, runtimeIdentity3);
doReturn(identities).when(infrastructure).getIdentities();
InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
// when
Set<String> active = runtimes.getActive(owner);
// then
assertEquals(active.size(), 1);
assertTrue(active.containsAll(asList(ws1)));
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment in project che-server by eclipse-che.
the class KubernetesPluginsToolingApplier method apply.
@Override
public void apply(RuntimeIdentity runtimeIdentity, InternalEnvironment internalEnvironment, Collection<ChePlugin> chePlugins) throws InfrastructureException {
if (chePlugins.isEmpty()) {
return;
}
KubernetesEnvironment k8sEnv = (KubernetesEnvironment) internalEnvironment;
Map<String, PodData> pods = k8sEnv.getPodsData();
switch(pods.size()) {
case 0:
addToolingPod(k8sEnv);
pods = k8sEnv.getPodsData();
break;
case 1:
break;
default:
throw new InfrastructureException("Che plugins tooling configuration can be applied to a workspace with one pod only");
}
PodData pod = pods.values().iterator().next();
CommandsResolver commandsResolver = new CommandsResolver(k8sEnv);
for (ChePlugin chePlugin : chePlugins) {
Map<String, ComponentImpl> devfilePlugins = k8sEnv.getDevfile().getComponents().stream().filter(c -> c.getType().equals("cheEditor") || c.getType().equals("chePlugin")).collect(Collectors.toMap(ComponentImpl::getId, Function.identity()));
if (!devfilePlugins.containsKey(chePlugin.getId())) {
throw new InfrastructureException(String.format("The downloaded plugin '%s' configuration does not have the " + "corresponding component in devfile. Devfile contains the following cheEditor/chePlugins: %s", chePlugin.getId(), devfilePlugins.keySet()));
}
ComponentImpl pluginRelatedComponent = devfilePlugins.get(chePlugin.getId());
for (CheContainer container : chePlugin.getInitContainers()) {
Container k8sInitContainer = toK8sContainer(container);
envVars.apply(k8sInitContainer, pluginRelatedComponent.getEnv());
chePluginsVolumeApplier.applyVolumes(pod, k8sInitContainer, container.getVolumes(), k8sEnv);
pod.getSpec().getInitContainers().add(k8sInitContainer);
}
Collection<CommandImpl> pluginRelatedCommands = commandsResolver.resolve(chePlugin);
for (CheContainer container : chePlugin.getContainers()) {
addSidecar(pod, container, chePlugin, k8sEnv, pluginRelatedCommands, pluginRelatedComponent, runtimeIdentity);
}
}
chePlugins.forEach(chePlugin -> populateWorkspaceEnvVars(chePlugin, k8sEnv));
}
Aggregations