Skip to content

Commit

Permalink
Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sleshchenko committed Apr 5, 2018
1 parent 6ad7826 commit 27a3dca
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private DockerInternalRuntime(
urlRewriter,
warnings,
// TODO null value is not fine here
running ? WorkspaceStatus.RUNNING : WorkspaceStatus.STOPPED);
running ? WorkspaceStatus.RUNNING : null);
this.networks = networks;
this.containerStarter = machineStarter;
this.eventService = eventService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ private InternalRuntime<?> getInternalRuntime(String workspaceId)
private Runtime getRuntime(String workspaceId) throws ServerException, InfrastructureException {
InternalRuntime<?> internalRuntime = getInternalRuntime(workspaceId);
return new RuntimeImpl(
internalRuntime.getActiveEnv(), internalRuntime.getMachines(), internalRuntime.getOwner());
internalRuntime.getActiveEnv(),
internalRuntime.getMachines(),
internalRuntime.getOwner(),
internalRuntime.getWarnings());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Objects;
import java.util.stream.Collectors;
import org.eclipse.che.api.core.model.workspace.Runtime;
import org.eclipse.che.api.core.model.workspace.Warning;
import org.eclipse.che.api.core.model.workspace.runtime.Machine;

/**
Expand All @@ -36,6 +37,17 @@ public RuntimeImpl(String activeEnv, Map<String, ? extends Machine> machines, St
this.owner = owner;
}

public RuntimeImpl(
String activeEnv,
Map<String, ? extends Machine> machines,
String owner,
List<? extends Warning> warnings) {
this.activeEnv = activeEnv;
this.machines = machines;
this.owner = owner;
this.warnings = warnings.stream().map(WarningImpl::new).collect(Collectors.toList());
}

public RuntimeImpl(Runtime runtime) {
this.activeEnv = runtime.getActiveEnv();
this.machines =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,9 @@ public List<? extends Warning> getWarnings() {
protected abstract Map<String, ? extends Machine> getInternalMachines()
throws InfrastructureException;

/**
* Returns workspace status.
*
* <p>Note that by default status is STARTING since context preparing is a part of workspace
* start.
*/
/** Returns workspace status. */
public WorkspaceStatus getStatus() throws InfrastructureException {
return status == null ? WorkspaceStatus.STARTING : status;
return status == null ? WorkspaceStatus.STOPPED : status;
}

/**
Expand Down Expand Up @@ -227,7 +222,7 @@ private Map<String, Server> rewriteExternalServers(
* @throws InfrastructureException when any other exception occurs
*/
protected void markStarting() throws InfrastructureException {
if (status != null && status != WorkspaceStatus.STOPPED) {
if (status != null) { // && status != WorkspaceStatus.STOPPED
throw new StateException("Runtime already started");
}
this.status = WorkspaceStatus.STARTING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.che.api.workspace.server;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
Expand All @@ -31,9 +32,7 @@
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -45,8 +44,8 @@
import static org.testng.Assert.assertTrue;
import static org.testng.util.Strings.isNullOrEmpty;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.eclipse.che.account.api.AccountManager;
Expand All @@ -55,27 +54,23 @@
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.Page;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.workspace.Runtime;
import org.eclipse.che.api.core.model.workspace.Warning;
import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.core.model.workspace.WorkspaceConfig;
import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
import org.eclipse.che.api.core.model.workspace.runtime.Machine;
import org.eclipse.che.api.core.model.workspace.runtime.MachineStatus;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl;
import org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl;
import org.eclipse.che.api.workspace.server.model.impl.MachineImpl;
import org.eclipse.che.api.workspace.server.model.impl.RecipeImpl;
import org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl;
import org.eclipse.che.api.workspace.server.model.impl.RuntimeImpl;
import org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl;
import org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.api.workspace.server.spi.InternalRuntime;
import org.eclipse.che.api.workspace.server.spi.RuntimeContext;
import org.eclipse.che.api.workspace.server.spi.RuntimeInfrastructure;
import org.eclipse.che.api.workspace.server.spi.WorkspaceDao;
import org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.commons.subject.Subject;
import org.eclipse.che.commons.subject.SubjectImpl;
Expand Down Expand Up @@ -216,7 +211,7 @@ public void getsWorkspacesAvailableForUserWithRuntimes() throws Exception {

final WorkspaceImpl workspace1 = createAndMockWorkspace(config, NAMESPACE_1);
final WorkspaceImpl workspace2 = createAndMockWorkspace(config, NAMESPACE_2);
final TestInternalRuntime runtime2 = mockRuntime(workspace2, RUNNING);
final TestRuntime runtime2 = mockRuntime(workspace2, RUNNING);
when(workspaceDao.getWorkspaces(eq(NAMESPACE_1), anyInt(), anyLong()))
.thenReturn(new Page<>(asList(workspace1, workspace2), 0, 2, 2));

Expand All @@ -233,7 +228,8 @@ public void getsWorkspacesAvailableForUserWithRuntimes() throws Exception {
res2.getStatus(),
RUNNING,
"Workspace status wasn't changed to the runtime instance status");
assertEquals(res2.getRuntime(), runtime2, "Workspace doesn't have expected runtime");
assertEquals(
res2.getRuntime(), new RuntimeImpl(runtime2), "Workspace doesn't have expected runtime");
assertFalse(res2.isTemporary(), "Workspace must be permanent");
}

Expand Down Expand Up @@ -299,7 +295,7 @@ public void getsWorkspacesByNamespaceWithoutRuntimes() throws Exception {
public void getsWorkspacesByNamespaceWithRuntimes() throws Exception {
// given
final WorkspaceImpl workspace = createAndMockWorkspace();
final TestInternalRuntime runtime = mockRuntime(workspace, RUNNING);
final TestRuntime runtime = mockRuntime(workspace, RUNNING);

// when
final Page<WorkspaceImpl> result =
Expand All @@ -313,7 +309,8 @@ public void getsWorkspacesByNamespaceWithRuntimes() throws Exception {
res1.getStatus(),
RUNNING,
"Workspace status wasn't changed to the runtime instance status");
assertEquals(res1.getRuntime(), runtime, "Workspace doesn't have expected runtime");
assertEquals(
res1.getRuntime(), new RuntimeImpl(runtime), "Workspace doesn't have expected runtime");
assertFalse(res1.isTemporary(), "Workspace must be permanent");
}

Expand Down Expand Up @@ -525,32 +522,29 @@ private void mockRuntimeStatus(WorkspaceImpl workspace, WorkspaceStatus status)
when(runtimes.getStatus(workspace.getId())).thenReturn(status);
}

private TestInternalRuntime mockRuntime(WorkspaceImpl workspace, WorkspaceStatus status)
private TestRuntime mockRuntime(WorkspaceImpl workspace, WorkspaceStatus status)
throws Exception {
RuntimeIdentity identity =
new RuntimeIdentityImpl(workspace.getId(), workspace.getConfig().getDefaultEnv(), "id");
// doAnswer(inv -> {
// final WorkspaceImpl ws = (WorkspaceImpl)inv.getArguments()[0];
// ws.setStatus(status);
// return ws;
// }).when(runtimes).injectStatus(workspace);
MachineImpl machine1 = spy(createMachine());
MachineImpl machine2 = spy(createMachine());
Map<String, Machine> machines = new HashMap<>();
machines.put("machine1", machine1);
machines.put("machine2", machine2);
TestInternalRuntime runtime = new TestInternalRuntime(mockContext(identity), machines);
TestRuntime runtime = new TestRuntime(machines);
doAnswer(
inv -> {
workspace.setStatus(status);
workspace.setRuntime(
new RuntimeImpl(
runtime.getActiveEnv(), runtime.getMachines(), runtime.getOwner()));
runtime.getActiveEnv(),
runtime.getMachines(),
runtime.getOwner(),
runtime.getWarnings()));
return null;
})
.when(runtimes)
.injectRuntime(workspace);
when(runtimes.isAnyRunning()).thenReturn(true);

return runtime;
}

Expand Down Expand Up @@ -625,40 +619,32 @@ private MachineImpl createMachine() {
return new MachineImpl(emptyMap(), emptyMap(), MachineStatus.RUNNING);
}

private RuntimeContext mockContext(RuntimeIdentity identity) throws Exception {
RuntimeContext context = mock(RuntimeContext.class);
doReturn(context).when(infrastructure).prepare(eq(identity), any(InternalEnvironment.class));
when(context.getInfrastructure()).thenReturn(infrastructure);
when(context.getIdentity()).thenReturn(identity);
return context;
}
private static class TestRuntime implements Runtime {

private static class TestInternalRuntime extends InternalRuntime<RuntimeContext> {
final Map<String, Machine> machines;

TestInternalRuntime(RuntimeContext context, Map<String, Machine> machines) {
super(context, null, null, STOPPED);
TestRuntime(Map<String, Machine> machines) {
this.machines = machines;
}

@Override
protected Map<String, Machine> getInternalMachines() {
return machines;
public String getActiveEnv() {
return "default";
}

@Override
public Map<String, String> getProperties() {
return Collections.emptyMap();
public Map<String, ? extends Machine> getMachines() {
return machines;
}

@Override
protected void internalStop(Map stopOptions) throws InfrastructureException {
throw new UnsupportedOperationException();
public String getOwner() {
return "owner";
}

@Override
protected void internalStart(Map startOptions) throws InfrastructureException {
throw new UnsupportedOperationException();
public List<? extends Warning> getWarnings() {
return emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

// TODO Adapt tests
/** @author Alexander Garagatyi */
public class InternalRuntimeTest {
private static final long SECOND_IN_MILLISECONDS = 1_000L;
Expand Down Expand Up @@ -635,7 +634,7 @@ public TestInternalRuntime(URLRewriter urlRewriter, boolean running)
new TestRuntimeContext(null, new RuntimeIdentityImpl("ws", "env", "id"), null),
urlRewriter,
emptyList(),
WorkspaceStatus.RUNNING);
running ? WorkspaceStatus.RUNNING : null);
}

@Override
Expand Down

0 comments on commit 27a3dca

Please sign in to comment.