Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Workspace.Next model classes to latest updates in the Workspace.Next vision #10246

Merged
merged 3 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public KubernetesWorkspaceNextApplier(
}

@Override
public void apply(InternalEnvironment internalEnvironment, Collection<ChePlugin> cheServices)
public void apply(InternalEnvironment internalEnvironment, Collection<ChePlugin> chePlugins)
throws InfrastructureException {
if (cheServices.isEmpty()) {
if (chePlugins.isEmpty()) {
return;
}
KubernetesEnvironment kubernetesEnvironment = (KubernetesEnvironment) internalEnvironment;
Expand All @@ -72,8 +72,8 @@ public void apply(InternalEnvironment internalEnvironment, Collection<ChePlugin>
"Workspace.Next configuration can be applied to a workspace with one pod only");
}
Pod pod = pods.values().iterator().next();
for (ChePlugin cheService : cheServices) {
for (CheContainer container : cheService.getContainers()) {
for (ChePlugin chePlugin : chePlugins) {
for (CheContainer container : chePlugin.getContainers()) {
io.fabric8.kubernetes.api.model.Container k8sContainer =
addContainer(pod, container.getImage(), container.getEnv());

Expand All @@ -83,7 +83,7 @@ public void apply(InternalEnvironment internalEnvironment, Collection<ChePlugin>
addMachine(
kubernetesEnvironment,
machineName,
getContainerEndpoints(container.getPorts(), cheService.getEndpoints()),
getContainerEndpoints(container.getPorts(), chePlugin.getEndpoints()),
container.getVolumes());

normalizeMemory(k8sContainer, machineConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void setUp() {
}

@Test
public void doesNothingIfServicesListIsEmpty() throws Exception {
public void doesNothingIfChePluginsListIsEmpty() throws Exception {
applier.apply(internalEnvironment, emptyList());

verifyZeroInteractions(internalEnvironment);
Expand All @@ -112,8 +112,8 @@ public void addToolingContainerToAPod() throws Exception {
}

@Test
public void canAddMultipleToolingContainersToAPodFromOneService() throws Exception {
applier.apply(internalEnvironment, singletonList(testServiceWith2Containers()));
public void canAddMultipleToolingContainersToAPodFromOnePlugin() throws Exception {
applier.apply(internalEnvironment, singletonList(testChePluginWith2Containers()));

assertEquals(containers.size(), 2);
for (Container container : containers) {
Expand All @@ -122,7 +122,7 @@ public void canAddMultipleToolingContainersToAPodFromOneService() throws Excepti
}

@Test
public void canAddMultipleToolingContainersToAPodFromSeveralServices() throws Exception {
public void canAddMultipleToolingContainersToAPodFromSeveralPlugins() throws Exception {
applier.apply(internalEnvironment, ImmutableList.of(testChePlugin(), testChePlugin()));

assertEquals(containers.size(), 2);
Expand Down Expand Up @@ -168,6 +168,87 @@ public void addsTwoServersForContainers() throws Exception {
80, "test-port", emptyMap(), true, 8090, "another-test-port", emptyMap(), false));
}

@Test
public void addsMachineWithServersThatUseSamePortButDifferentNames() throws Exception {
ChePlugin chePlugin = testChePlugin();
addPortToSingleContainerPlugin(chePlugin, 80, "test-port/http", emptyMap(), true);
addPortToSingleContainerPlugin(chePlugin, 80, "test-port/ws", emptyMap(), true);
applier.apply(internalEnvironment, singletonList(chePlugin));

InternalMachineConfig machineConfig = getOneAndOnlyMachine(internalEnvironment);
assertEquals(
machineConfig.getServers(),
expectedTwoServers(
80, "test-port/http", emptyMap(), true, 80, "test-port/ws", emptyMap(), true));
}

@Test
public void addsMachineWithServersThatSetProtocolAndPath() throws Exception {
ChePlugin chePlugin = testChePlugin();
addPortToSingleContainerPlugin(
chePlugin,
443,
"test-port",
ImmutableMap.of("path", "/path/1", "protocol", "https", "attr1", "value1"),
true);
applier.apply(internalEnvironment, singletonList(chePlugin));

InternalMachineConfig machineConfig = getOneAndOnlyMachine(internalEnvironment);
assertEquals(
machineConfig.getServers(),
expectedSingleServer(
443, "test-port", singletonMap("attr1", "value1"), true, "https", "/path/1"));
}

@Test
public void setsDefaultMemoryLimitForMachineAssociatedWithContainer() throws Exception {
applier.apply(internalEnvironment, singletonList(testChePlugin()));

InternalMachineConfig machineConfig = getOneAndOnlyMachine(internalEnvironment);
String memoryLimitAttribute = machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE);
assertEquals(memoryLimitAttribute, Integer.toString(MEMORY_LIMIT_MB * 1024 * 1024));
}

private ChePlugin testChePlugin() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, testChePlugin sounds like it should test ChePlugin, but in facts, it creates test ChePlugin object. It does not make much sense since it is test sources but maybe just createChePlugin would be clearer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will change it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ChePlugin plugin = new ChePlugin();
plugin.setName("some-name");
plugin.setId("some-id");
plugin.setVersion("0.0.3");
plugin.setContainers(singletonList(testContainer()));
return plugin;
}

private ChePlugin testChePluginWith2Containers() {
ChePlugin plugin = new ChePlugin();
plugin.setName("some-name");
plugin.setId("some-id");
plugin.setVersion("0.0.3");
plugin.setContainers(asList(testContainer(), testContainer()));
return plugin;
}

private CheContainer testContainer() {
CheContainer cheContainer = new CheContainer();
cheContainer.setImage(TEST_IMAGE);
cheContainer.setEnv(singletonList(new EnvVar().name(ENV_VAR).value(ENV_VAR_VALUE)));
cheContainer.setVolumes(
singletonList(new Volume().name(VOLUME_NAME).mountPath(VOLUME_MOUNT_PATH)));
return cheContainer;
}

private void verifyContainer(Container toolingContainer) {
assertEquals(toolingContainer.getImage(), TEST_IMAGE);
assertEquals(
toolingContainer.getEnv(),
singletonList(new io.fabric8.kubernetes.api.model.EnvVar(ENV_VAR, ENV_VAR_VALUE, null)));
}

private InternalMachineConfig getOneAndOnlyMachine(InternalEnvironment internalEnvironment) {
Map<String, InternalMachineConfig> machines = internalEnvironment.getMachines();
assertEquals(machines.size(), 1);
return machines.values().iterator().next();
}

private void addPortToSingleContainerPlugin(
ChePlugin plugin,
int port,
Expand All @@ -193,13 +274,15 @@ private void addPortToSingleContainerPlugin(
}
}

@SuppressWarnings("SameParameterValue")
private Map<String, ServerConfig> expectedSingleServer(
int port, String portName, Map<String, String> attributes, boolean isExternal) {
Map<String, ServerConfig> servers = new HashMap<>();
addExpectedServer(servers, port, portName, attributes, isExternal, null, null);
return servers;
}

@SuppressWarnings("SameParameterValue")
private Map<String, ServerConfig> expectedSingleServer(
int port,
String portName,
Expand All @@ -212,6 +295,7 @@ private Map<String, ServerConfig> expectedSingleServer(
return servers;
}

@SuppressWarnings("SameParameterValue")
private Map<String, ServerConfig> expectedTwoServers(
int port,
String portName,
Expand Down Expand Up @@ -241,85 +325,4 @@ private void addExpectedServer(
portName,
new ServerConfigImpl(Integer.toString(port) + "/tcp", protocol, path, serverAttributes));
}

@Test
public void addsMachineWithServersThatUseSamePortButDifferentNames() throws Exception {
ChePlugin chePlugin = testChePlugin();
addPortToSingleContainerPlugin(chePlugin, 80, "test-port/http", emptyMap(), true);
addPortToSingleContainerPlugin(chePlugin, 80, "test-port/ws", emptyMap(), true);
applier.apply(internalEnvironment, singletonList(chePlugin));

InternalMachineConfig machineConfig = getOneAndOnlyMachine(internalEnvironment);
assertEquals(
machineConfig.getServers(),
expectedTwoServers(
80, "test-port/http", emptyMap(), true, 80, "test-port/ws", emptyMap(), true));
}

@Test
public void addsMachineWithServersThatSetProtocolAndPath() throws Exception {
ChePlugin chePlugin = testChePlugin();
addPortToSingleContainerPlugin(
chePlugin,
443,
"test-port",
ImmutableMap.of("path", "/path/1", "protocol", "https", "attr1", "value1"),
true);
applier.apply(internalEnvironment, singletonList(chePlugin));

InternalMachineConfig machineConfig = getOneAndOnlyMachine(internalEnvironment);
assertEquals(
machineConfig.getServers(),
expectedSingleServer(
443, "test-port", singletonMap("attr1", "value1"), true, "https", "/path/1"));
}

@Test
public void setsDefaultMemoryLimitForMachineAssociatedWithContainer() throws Exception {
applier.apply(internalEnvironment, singletonList(testChePlugin()));

InternalMachineConfig machineConfig = getOneAndOnlyMachine(internalEnvironment);
String memoryLimitAttribute = machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE);
assertEquals(memoryLimitAttribute, Integer.toString(MEMORY_LIMIT_MB * 1024 * 1024));
}

private ChePlugin testChePlugin() {
ChePlugin service = new ChePlugin();
service.setName("some-name");
service.setId("some-id");
service.setVersion("0.0.3");
service.setContainers(singletonList(testContainer()));
return service;
}

private ChePlugin testServiceWith2Containers() {
ChePlugin service = new ChePlugin();
service.setName("some-name");
service.setId("some-id");
service.setVersion("0.0.3");
service.setContainers(asList(testContainer(), testContainer()));
return service;
}

private CheContainer testContainer() {
CheContainer cheContainer = new CheContainer();
cheContainer.setImage(TEST_IMAGE);
cheContainer.setEnv(singletonList(new EnvVar().name(ENV_VAR).value(ENV_VAR_VALUE)));
cheContainer.setVolumes(
singletonList(new Volume().name(VOLUME_NAME).mountPath(VOLUME_MOUNT_PATH)));
return cheContainer;
}

private void verifyContainer(Container toolingContainer) {
assertEquals(toolingContainer.getImage(), TEST_IMAGE);
assertEquals(
toolingContainer.getEnv(),
singletonList(new io.fabric8.kubernetes.api.model.EnvVar(ENV_VAR, ENV_VAR_VALUE, null)));
}

private InternalMachineConfig getOneAndOnlyMachine(InternalEnvironment internalEnvironment) {
Map<String, InternalMachineConfig> machines = internalEnvironment.getMachines();
assertEquals(machines.size(), 1);
return machines.values().iterator().next();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -760,16 +760,16 @@ private void applyWorkspaceNext(
Map<String, String> workspaceAttributes,
String recipeType)
throws InfrastructureException {
Collection<ChePlugin> cheServices = workspaceNextObjectsRetriever.get(workspaceAttributes);
if (cheServices.isEmpty()) {
Collection<ChePlugin> chePlugins = workspaceNextObjectsRetriever.get(workspaceAttributes);
if (chePlugins.isEmpty()) {
return;
}
WorkspaceNextApplier wsNext = workspaceNextAppliers.get(recipeType);
if (wsNext == null) {
throw new InfrastructureException(
"Workspace.Next features are not supported for recipe type " + recipeType);
}
wsNext.apply(internalEnvironment, cheServices);
wsNext.apply(internalEnvironment, chePlugins);
}

private String sessionUserNameOr(String nameIfNoUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public interface WorkspaceNextApplier {
*
* @param internalEnvironment infrastructure specific representation of workspace runtime
* environment
* @param cheServices Workspace.Next configuration to apply to {@code internalEnvironment}
* @param chePlugins Workspace.Next configuration to apply to {@code internalEnvironment}
* @throws InfrastructureException when applying Workspace.Next objects fails
*/
void apply(InternalEnvironment internalEnvironment, Collection<ChePlugin> cheServices)
void apply(InternalEnvironment internalEnvironment, Collection<ChePlugin> chePlugins)
throws InfrastructureException;
}
Loading