diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/CustomServerEvaluationStrategy.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/CustomServerEvaluationStrategy.java index 82ac8505d1b..e7dbdbd5a24 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/CustomServerEvaluationStrategy.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/CustomServerEvaluationStrategy.java @@ -40,7 +40,7 @@ * @author Florent Benoit * @see ServerEvaluationStrategy */ -public class CustomServerEvaluationStrategy extends DefaultServerEvaluationStrategy { +public class CustomServerEvaluationStrategy extends ServerEvaluationStrategy { /** * Regexp to extract port (under the form 22/tcp or 4401/tcp, etc.) from label references @@ -57,6 +57,32 @@ public class CustomServerEvaluationStrategy extends DefaultServerEvaluationStrat */ public static final String CHE_MACHINE_NAME_PROPERTY = "CHE_MACHINE_NAME="; + /** + * Name of the property to get the property that indicates if the machine is the dev machine + */ + public static final String CHE_IS_DEV_MACHINE_PROPERTY = "CHE_IS_DEV_MACHINE="; + + /** + * Prefix added in front of the generated name to build the workspaceId + */ + public static final String CHE_WORKSPACE_ID_PREFIX = "workspace"; + + + /** + * name of the macro that indicates if the machine is the dev machine + */ + public static final String IS_DEV_MACHINE_MACRO = "isDevMachine"; + + /** + * Used to store the address set by property {@code che.docker.ip}, if applicable. + */ + protected String cheDockerIp; + + /** + * Used to store the address set by property {@code che.docker.ip.external}. if applicable. + */ + protected String cheDockerIpExternal; + /** * The current port of che. */ @@ -72,7 +98,18 @@ public class CustomServerEvaluationStrategy extends DefaultServerEvaluationStrat */ private String cheDockerCustomExternalTemplate; - + /** + * Option to enable the use of the container address, when searching for addresses. + */ + private boolean useContainerAddress; + + + /** + * Option to tell if an exception should be thrown when the host defined in the `externalAddress` isn't known + * and cannot be converted into a valid IP. + */ + private boolean throwOnUnknownHost = true; + /** * Default constructor */ @@ -82,12 +119,49 @@ public CustomServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String c @Nullable @Named("che.docker.server_evaluation_strategy.custom.template") String cheDockerCustomExternalTemplate, @Nullable @Named("che.docker.server_evaluation_strategy.custom.external.protocol") String cheDockerCustomExternalProtocol, @Named("che.port") String chePort) { - super(cheDockerIp, cheDockerIpExternal); + this(cheDockerIp, cheDockerIpExternal, cheDockerCustomExternalTemplate, cheDockerCustomExternalProtocol, chePort, false); + } + + /** + * Constructor to be called by derived strategies + */ + public CustomServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String cheDockerIp, + @Nullable @Named("che.docker.ip.external") String cheDockerIpExternal, + @Nullable @Named("che.docker.server_evaluation_strategy.custom.template") String cheDockerCustomExternalTemplate, + @Nullable @Named("che.docker.server_evaluation_strategy.custom.external.protocol") String cheDockerCustomExternalProtocol, + @Named("che.port") String chePort, + boolean useContainerAddress) { + this.cheDockerIp = cheDockerIp; + this.cheDockerIpExternal = cheDockerIpExternal; this.chePort = chePort; this.cheDockerCustomExternalTemplate = cheDockerCustomExternalTemplate; this.cheDockerCustomExternalProtocol = cheDockerCustomExternalProtocol; + this.useContainerAddress = useContainerAddress; } + @Override + protected Map getInternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) { + final String internalAddressContainer = containerInfo.getNetworkSettings().getIpAddress(); + + final String internalAddress; + + if (useContainerAddress) { + internalAddress = !isNullOrEmpty(internalAddressContainer) ? + internalAddressContainer : + internalHost; + } else { + internalAddress = + cheDockerIp != null ? + cheDockerIp : + internalHost; + } + + boolean useExposedPorts = useContainerAddress && internalAddress != internalHost; + + return getExposedPortsToAddressPorts(internalAddress, containerInfo.getNetworkSettings().getPorts(), useExposedPorts); + } + + /** * Override the host for all ports by using the external template. */ @@ -100,6 +174,10 @@ protected Map getExternalAddressesAndPorts(ContainerInfo contain // get current ports Map> ports = containerInfo.getNetworkSettings().getPorts(); + if (isNullOrEmpty(cheDockerCustomExternalTemplate)) { + return getExposedPortsToAddressPorts(renderingEvaluation.getExternalAddress(), ports, false); + } + return ports.keySet().stream() .collect(Collectors.toMap(portKey -> portKey, portKey -> renderingEvaluation.render(cheDockerCustomExternalTemplate, portKey))); @@ -180,6 +258,11 @@ public interface RenderingEvaluation { * @return the rendering of the template */ String render(String template, String port); + + /** + * Gets default external address. + */ + String getExternalAddress(); } /** @@ -202,12 +285,20 @@ protected OnlineRenderingEvaluation withInternalHost(String internalHost) { } @Override - protected String getExternalAddress() { - return externalAddressProperty != null ? - externalAddressProperty : - !isNullOrEmpty(gatewayAddressContainer) ? - gatewayAddressContainer : - this.internalHost; + public String getExternalAddress() { + if (useContainerAddress) { + return cheDockerIpExternal != null ? + cheDockerIpExternal : + !isNullOrEmpty(gatewayAddressContainer) ? + gatewayAddressContainer : + this.internalHost; + } + + return cheDockerIpExternal != null ? + cheDockerIpExternal : + cheDockerIp != null ? + cheDockerIp : + this.internalHost; } } @@ -292,7 +383,7 @@ protected void initPortMapping() { // add to this map only port without a known ref name Map portsToUnkownRefName = exposedPorts.stream().filter((port) -> !portsToKnownRefName.containsKey(port)) - .collect(Collectors.toMap(p -> p, p -> "Server-" + p.replace('/', '-'))); + .collect(Collectors.toMap(p -> p, p -> "server-" + p.replace('/', '-'))); // list of all ports with refName (known/unknown) this.portsToRefName = new HashMap(portsToKnownRefName); @@ -302,9 +393,9 @@ protected void initPortMapping() { /** * Gets default external address. */ - protected String getExternalAddress() { - return externalAddressProperty != null ? - externalAddressProperty : internalAddressProperty; + public String getExternalAddress() { + return cheDockerIpExternal != null ? + cheDockerIpExternal : cheDockerIp; } /** @@ -313,14 +404,16 @@ protected String getExternalAddress() { protected void populateGlobalProperties() { String externalAddress = getExternalAddress(); String externalIP = getExternalIp(externalAddress); - globalPropertiesMap.put("internalIp", internalAddressProperty); + globalPropertiesMap.put("internalIp", cheDockerIp); globalPropertiesMap.put("externalAddress", externalAddress); globalPropertiesMap.put("externalIP", externalIP); globalPropertiesMap.put("workspaceId", getWorkspaceId()); + globalPropertiesMap.put("workspaceIdWithoutPrefix", getWorkspaceId().replaceFirst(CHE_WORKSPACE_ID_PREFIX,"")); globalPropertiesMap.put("machineName", getMachineName()); globalPropertiesMap.put("wildcardNipDomain", getWildcardNipDomain(externalAddress)); globalPropertiesMap.put("wildcardXipDomain", getWildcardXipDomain(externalAddress)); globalPropertiesMap.put("chePort", chePort); + globalPropertiesMap.put(IS_DEV_MACHINE_MACRO, getIsDevMachine()); } /** @@ -333,11 +426,25 @@ public String render(String template, String port) { this.initialized = true; } ST stringTemplate = new ST(template); - globalPropertiesMap.forEach((key, value) -> stringTemplate.add(key, value)); + globalPropertiesMap.forEach((key, value) -> stringTemplate.add(key, + IS_DEV_MACHINE_MACRO.equals(key) ? + Boolean.parseBoolean(value) + : value)); stringTemplate.add("serverName", portsToRefName.get(port)); return stringTemplate.render(); } + /** + * returns if the current machine is the dev machine + * + * @return true if the curent machine is the dev machine + */ + protected String getIsDevMachine() { + return Arrays.stream(env).filter(env -> env.startsWith(CHE_IS_DEV_MACHINE_PROPERTY)) + .map(s -> s.substring(CHE_IS_DEV_MACHINE_PROPERTY.length())) + .findFirst().get(); + } + /** * Gets the workspace ID from the config of the given container * @@ -369,8 +476,11 @@ protected String getExternalIp(String externalAddress) { try { return InetAddress.getByName(externalAddress).getHostAddress(); } catch (UnknownHostException e) { - throw new UnsupportedOperationException("Unable to find the IP for the address '" + externalAddress + "'", e); + if (throwOnUnknownHost) { + throw new UnsupportedOperationException("Unable to find the IP for the address '" + externalAddress + "'", e); + } } + return null; } /** @@ -393,4 +503,13 @@ protected String getWildcardXipDomain(String externalAddress) { } + @Override + protected boolean useHttpsForExternalUrls() { + return "https".equals(cheDockerCustomExternalProtocol); + } + + public CustomServerEvaluationStrategy withThrowOnUnknownHost(boolean throwOnUnknownHost) { + this.throwOnUnknownHost = throwOnUnknownHost; + return this; + } } diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategy.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategy.java index b5578b8d543..836c6bfe4f7 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategy.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategy.java @@ -31,48 +31,11 @@ * @author Alexander Garagatyi * @see ServerEvaluationStrategy */ -public class DefaultServerEvaluationStrategy extends ServerEvaluationStrategy { - - /** - * Used to store the address set by property {@code che.docker.ip}, if applicable. - */ - protected String internalAddressProperty; - - /** - * Used to store the address set by property {@code che.docker.ip.external}. if applicable. - */ - protected String externalAddressProperty; +public class DefaultServerEvaluationStrategy extends CustomServerEvaluationStrategy { @Inject public DefaultServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String internalAddress, @Nullable @Named("che.docker.ip.external") String externalAddress) { - this.internalAddressProperty = internalAddress; - this.externalAddressProperty = externalAddress; - } - - @Override - protected Map getInternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) { - String internalAddress = internalAddressProperty != null ? - internalAddressProperty : - internalHost; - - return getExposedPortsToAddressPorts(internalAddress, containerInfo.getNetworkSettings().getPorts()); - } - - @Override - protected Map getExternalAddressesAndPorts(ContainerInfo containerInfo, - String internalHost) { - String externalAddress = externalAddressProperty != null ? - externalAddressProperty : - internalAddressProperty != null ? - internalAddressProperty : - internalHost; - - return super.getExposedPortsToAddressPorts(externalAddress, containerInfo.getNetworkSettings().getPorts()); - } - - @Override - protected boolean useHttpsForExternalUrls() { - return false; + super(internalAddress, externalAddress, null, null, null, false); } } diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfo.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfo.java index 0cb32f70ac9..bb1ad218318 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfo.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfo.java @@ -68,6 +68,11 @@ public class DockerInstanceRuntimeInfo implements MachineRuntimeInfo { */ public static final String CHE_MACHINE_NAME = "CHE_MACHINE_NAME"; + /** + * Environment variable that will contain Name of the machine + */ + public static final String CHE_IS_DEV_MACHINE = "CHE_IS_DEV_MACHINE"; + /** * Default HOSTNAME that will be added in all docker containers that are started. This host will container the Docker host's ip * reachable inside the container. diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerServerEvaluationStrategy.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerServerEvaluationStrategy.java index 4b88e0b0823..0c22ede05d4 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerServerEvaluationStrategy.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerServerEvaluationStrategy.java @@ -16,14 +16,9 @@ import org.eclipse.che.api.machine.server.model.impl.ServerImpl; import org.eclipse.che.commons.annotation.Nullable; import org.eclipse.che.plugin.docker.client.json.ContainerInfo; -import org.eclipse.che.plugin.docker.client.json.PortBinding; -import java.util.HashMap; -import java.util.List; import java.util.Map; -import static com.google.common.base.Strings.isNullOrEmpty; - /** * Represents a server evaluation strategy for the configuration where the workspace server and * workspace containers are running on the same Docker network. Calling @@ -36,72 +31,11 @@ * @author Angel Misevski * @see ServerEvaluationStrategy */ -public class LocalDockerServerEvaluationStrategy extends ServerEvaluationStrategy { - - /** - * Used to store the address set by property {@code che.docker.ip}, if applicable. - */ - protected String internalAddressProperty; - - /** - * Used to store the address set by property {@code che.docker.ip.external}. if applicable. - */ - protected String externalAddressProperty; +public class LocalDockerServerEvaluationStrategy extends CustomServerEvaluationStrategy { @Inject public LocalDockerServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String internalAddress, @Nullable @Named("che.docker.ip.external") String externalAddress) { - this.internalAddressProperty = internalAddress; - this.externalAddressProperty = externalAddress; - } - - @Override - protected Map getInternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) { - String internalAddressContainer = containerInfo.getNetworkSettings().getIpAddress(); - - String internalAddress; - boolean useExposedPorts = true; - if (!isNullOrEmpty(internalAddressContainer)) { - internalAddress = internalAddressContainer; - } else { - internalAddress = internalHost; - useExposedPorts = false; - } - - Map> portBindings = containerInfo.getNetworkSettings().getPorts(); - - Map addressesAndPorts = new HashMap<>(); - for (Map.Entry> portEntry : portBindings.entrySet()) { - String exposedPort = portEntry.getKey().split("/")[0]; - String ephemeralPort = portEntry.getValue().get(0).getHostPort(); - if (useExposedPorts) { - addressesAndPorts.put(portEntry.getKey(), internalAddress + ":" + exposedPort); - } else { - addressesAndPorts.put(portEntry.getKey(), internalAddress + ":" + ephemeralPort); - } - } - return addressesAndPorts; + super(internalAddress, externalAddress, null, null, null, true); } - - @Override - protected Map getExternalAddressesAndPorts(ContainerInfo containerInfo, - String internalHost) { - String cheExternalAddress = getCheExternalAddress(containerInfo, internalHost); - return getExposedPortsToAddressPorts(cheExternalAddress, containerInfo.getNetworkSettings().getPorts()); - } - - protected String getCheExternalAddress(ContainerInfo containerInfo, String internalHost) { - String externalAddressContainer = containerInfo.getNetworkSettings().getGateway(); - return externalAddressProperty != null ? - externalAddressProperty : - !isNullOrEmpty(externalAddressContainer) ? - externalAddressContainer : - internalHost; - } - - @Override - protected boolean useHttpsForExternalUrls() { - return false; - } - } diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java index b32b7144d37..ea032ed63ae 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java @@ -13,17 +13,9 @@ import com.google.inject.Inject; import com.google.inject.name.Named; -import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; + import org.eclipse.che.api.machine.server.model.impl.ServerImpl; import org.eclipse.che.commons.annotation.Nullable; -import org.eclipse.che.plugin.docker.client.json.ContainerInfo; -import org.eclipse.che.plugin.docker.client.json.PortBinding; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Stream; /** @@ -41,57 +33,13 @@ * @author Mario Loriedo * @see ServerEvaluationStrategy */ -public class LocalDockerSinglePortServerEvaluationStrategy extends LocalDockerServerEvaluationStrategy { - - private static final String CHE_WORKSPACE_ID_ENV_VAR = "CHE_WORKSPACE_ID"; - - private boolean secureExternalUrls; +public class LocalDockerSinglePortServerEvaluationStrategy extends CustomServerEvaluationStrategy { @Inject public LocalDockerSinglePortServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String internalAddress, @Nullable @Named("che.docker.ip.external") String externalAddress, - @Named("che.docker.server_evaluation_strategy.secure.external.urls") boolean secureExternalUrls) { - super(internalAddress, externalAddress); - this.secureExternalUrls = secureExternalUrls; - } - - @Override - protected boolean useHttpsForExternalUrls() { - return this.secureExternalUrls; - } - - @Override - protected Map getExternalAddressesAndPorts(ContainerInfo containerInfo, - String internalHost) { - String cheExternalAddress = getCheExternalAddress(containerInfo, internalHost); - String workspaceID = getWorkspaceID(containerInfo.getConfig().getEnv()); - Map labels= containerInfo.getConfig().getLabels(); - - Map> portBindings = containerInfo.getNetworkSettings().getPorts(); - Map addressesAndPorts = new HashMap<>(); - for (String serverKey : portBindings.keySet()) { - String serverName = getWorkspaceServerName(labels, serverKey); - String serverURL = serverName + "-" + workspaceID + "-" + cheExternalAddress; - addressesAndPorts.put(serverKey, serverURL); - } - - return addressesAndPorts; - } - - private String getWorkspaceServerName(Map labels, String portKey) { - ServerConfImpl serverConf = getServerConfImpl(portKey, labels, new HashMap<>()); - if (serverConf == null) { - return "server-" + portKey.split("/",2)[0]; - } - return serverConf.getRef(); - } - - private String getWorkspaceID(String[] env) { - Stream envStream = Arrays.stream(env); - return envStream.filter(v -> v.startsWith(CHE_WORKSPACE_ID_ENV_VAR) && v.contains("=")). - map(v -> v.split("=",2)[1]). - findFirst(). - orElse("unknown-ws"). - replaceFirst("workspace",""); + @Named("che.docker.server_evaluation_strategy.secure.external.urls") boolean secureExternalUrls + ) { + super(internalAddress, externalAddress, "--", secureExternalUrls ? "https" : "http", null, true); } } diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/MachineProviderImpl.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/MachineProviderImpl.java index 05e79e58312..0e36ebfc6d3 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/MachineProviderImpl.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/MachineProviderImpl.java @@ -666,6 +666,7 @@ private void addSystemWideContainerSettings(String workspaceId, // register workspace ID and Machine Name env.put(DockerInstanceRuntimeInfo.CHE_WORKSPACE_ID, workspaceId); env.put(DockerInstanceRuntimeInfo.CHE_MACHINE_NAME, machineName); + env.put(DockerInstanceRuntimeInfo.CHE_IS_DEV_MACHINE, Boolean.toString(isDev)); composeService.getExpose().addAll(portsToExpose); composeService.getEnvironment().putAll(env); diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/ServerEvaluationStrategy.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/ServerEvaluationStrategy.java index 6a7887b9070..8f2147638b4 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/ServerEvaluationStrategy.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/ServerEvaluationStrategy.java @@ -45,7 +45,10 @@ public abstract class ServerEvaluationStrategy { /** * @return true if external addresses need to be exposed against https, false otherwise */ - protected abstract boolean useHttpsForExternalUrls(); + protected boolean useHttpsForExternalUrls() { + return false; + } + /** * Gets a map of all internal addresses exposed by the container in the form of @@ -240,17 +243,28 @@ protected ServerConfImpl getServerConfImpl(String portProtocol, * "9090/udp" : "my-host.com:32722" * } * } + * */ - protected Map getExposedPortsToAddressPorts(String address, Map> ports) { + protected Map getExposedPortsToAddressPorts(String address, Map> ports, boolean useExposedPorts) { Map addressesAndPorts = new HashMap<>(); for (Map.Entry> portEntry : ports.entrySet()) { + String exposedPort = portEntry.getKey().split("/")[0]; // there is one value always - String port = portEntry.getValue().get(0).getHostPort(); - addressesAndPorts.put(portEntry.getKey(), address + ":" + port); + String ephemeralPort = portEntry.getValue().get(0).getHostPort(); + if (useExposedPorts) { + addressesAndPorts.put(portEntry.getKey(), address + ":" + exposedPort); + } else { + addressesAndPorts.put(portEntry.getKey(), address + ":" + ephemeralPort); + } } return addressesAndPorts; } + protected Map getExposedPortsToAddressPorts(String address, Map> ports) { + return getExposedPortsToAddressPorts(address, ports, false); + } + + /** * @param protocolForInternalUrl * @return https, if {@link #useHttpsForExternalUrls()} method in sub-class returns true and protocol for internal Url is http diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/CustomServerEvaluationStrategyTest.java b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/CustomServerEvaluationStrategyTest.java index 1e8fc9aef07..4200cb14c20 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/CustomServerEvaluationStrategyTest.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/CustomServerEvaluationStrategyTest.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.Set; +import static org.eclipse.che.plugin.docker.machine.CustomServerEvaluationStrategy.CHE_WORKSPACE_ID_PREFIX; import static org.mockito.Mockito.when; /** @@ -43,11 +44,19 @@ public class CustomServerEvaluationStrategyTest { private static final String ALL_IP_ADDRESS = "0.0.0.0"; - private static final String WORKSPACE_ID_VALUE = "work123"; - private static final String WORKSPACE_ID_PROPERTY = "CHE_WORKSPACE_ID=" + WORKSPACE_ID_VALUE; + private static final String WORKSPACE_ID_WITHOUT_PREFIX_VALUE = "ABCDEFG"; + + private static final String WORKSPACE_ID_VALUE = CHE_WORKSPACE_ID_PREFIX + WORKSPACE_ID_WITHOUT_PREFIX_VALUE; + private static final String WORKSPACE_ID_PROPERTY_PREFIX = "CHE_WORKSPACE_ID="; + private static final String WORKSPACE_ID_PROPERTY = WORKSPACE_ID_PROPERTY_PREFIX + WORKSPACE_ID_VALUE; private static final String MACHINE_NAME_VALUE = "myMachine"; - private static final String MACHINE_NAME_PROPERTY = "CHE_MACHINE_NAME=" + MACHINE_NAME_VALUE; + private static final String MACHINE_NAME_PROPERTY_PREFIX = "CHE_MACHINE_NAME="; + private static final String MACHINE_NAME_PROPERTY = MACHINE_NAME_PROPERTY_PREFIX + MACHINE_NAME_VALUE; + + private static final String IS_DEV_MACHINE_PROPERTY_PREFIX = "CHE_IS_DEV_MACHINE="; + private static final String IS_DEV_MACHINE_PROPERTY_TRUE = IS_DEV_MACHINE_PROPERTY_PREFIX + "true"; + private static final String IS_DEV_MACHINE_PROPERTY_FALSE = IS_DEV_MACHINE_PROPERTY_PREFIX + "false"; @Mock private ContainerConfig containerConfig; @@ -76,7 +85,7 @@ protected void setup() throws Exception { when(containerConfig.getLabels()).thenReturn(containerLabels); when(containerConfig.getExposedPorts()).thenReturn(containerExposedPorts); - envContainerConfig = new String[]{WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY}; + envContainerConfig = new String[]{WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY, IS_DEV_MACHINE_PROPERTY_TRUE}; when(containerConfig.getEnv()).thenReturn(envContainerConfig); when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); @@ -126,6 +135,52 @@ public void testWorkspaceIdRule() throws Throwable { Assert.assertEquals(portMapping.get("4401/tcp"), WORKSPACE_ID_VALUE); } + /** + * Check workspace Id without prefix template + */ + @Test + public void testWorkspaceIdWithoutPrefixRule() throws Throwable { + this.customServerEvaluationStrategy = + new CustomServerEvaluationStrategy("10.0.0.1", "192.168.1.1", "", "http", "8080"); + + Map portMapping = this.customServerEvaluationStrategy.getExternalAddressesAndPorts(containerInfo, "localhost"); + + Assert.assertTrue(portMapping.containsKey("4401/tcp")); + Assert.assertEquals(portMapping.get("4401/tcp"), WORKSPACE_ID_WITHOUT_PREFIX_VALUE); + } + + /** + * Check the isDevMachine macro in template + */ + @Test + public void testIsDevMachineWhenTrue() throws Throwable { + this.customServerEvaluationStrategy = + new CustomServerEvaluationStrategy("10.0.0.1", "192.168.1.1", + "", "http", "8080"); + + Map portMapping = this.customServerEvaluationStrategy.getExternalAddressesAndPorts(containerInfo, "localhost"); + + Assert.assertTrue(portMapping.containsKey("4401/tcp")); + Assert.assertEquals(portMapping.get("4401/tcp"), WORKSPACE_ID_VALUE); + } + + /** + * Check the isDevMachine macro in template + */ + @Test + public void testIsDevMachineWhenFalse() throws Throwable { + this.envContainerConfig = new String[]{WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY, IS_DEV_MACHINE_PROPERTY_FALSE}; + when(containerConfig.getEnv()).thenReturn(envContainerConfig); + + this.customServerEvaluationStrategy = + new CustomServerEvaluationStrategy("10.0.0.1", "192.168.1.1", + "", "http", "8080"); + + Map portMapping = this.customServerEvaluationStrategy.getExternalAddressesAndPorts(containerInfo, "localhost"); + + Assert.assertTrue(portMapping.containsKey("4401/tcp")); + Assert.assertEquals(portMapping.get("4401/tcp"), MACHINE_NAME_VALUE); + } /** * Check workspace Id template @@ -213,7 +268,7 @@ public void testOffline() throws Throwable { exposedPorts.add("4401/tcp"); exposedPorts.add("4411/tcp"); exposedPorts.add("8080/tcp"); - List env = Arrays.asList(WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY); + List env = Arrays.asList(WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY, IS_DEV_MACHINE_PROPERTY_TRUE); this.customServerEvaluationStrategy = new CustomServerEvaluationStrategy("127.0.0.1", null, "-", "https", "8080"); CustomServerEvaluationStrategy.RenderingEvaluation renderingEvaluation = this.customServerEvaluationStrategy @@ -233,7 +288,7 @@ public void testOfflineExternal() throws Throwable { exposedPorts.add("4401/tcp"); exposedPorts.add("4411/tcp"); exposedPorts.add("8080/tcp"); - List env = Arrays.asList(WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY); + List env = Arrays.asList(WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY, IS_DEV_MACHINE_PROPERTY_TRUE); this.customServerEvaluationStrategy = new CustomServerEvaluationStrategy("127.0.0.1", "127.0.0.1", "-", "https", "8080"); CustomServerEvaluationStrategy.RenderingEvaluation renderingEvaluation = this.customServerEvaluationStrategy @@ -253,7 +308,7 @@ public void testOfflineInvalidExternal() throws Throwable { exposedPorts.add("4401/tcp"); exposedPorts.add("4411/tcp"); exposedPorts.add("8080/tcp"); - List env = Arrays.asList(WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY); + List env = Arrays.asList(WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY, IS_DEV_MACHINE_PROPERTY_TRUE); this.customServerEvaluationStrategy = new CustomServerEvaluationStrategy("127.0.0.1", "300.300.300.300", "-", "https", "8080"); CustomServerEvaluationStrategy.RenderingEvaluation renderingEvaluation = this.customServerEvaluationStrategy diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategyTest.java b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategyTest.java index 203d2bd2146..1b1c4535f9b 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategyTest.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategyTest.java @@ -40,6 +40,15 @@ public class LocalDockerSinglePortServerEvaluationStrategyTest { private static final String CONTAINERCONFIG_HOSTNAME = "che-ws-y6jwknht0efzczit-4086112300-fm0aj"; private static final String WORKSPACE_ID = "79rfwhqaztq2ru2k"; + private static final String WORKSPACE_ID_VALUE = WORKSPACE_ID; + private static final String WORKSPACE_ID_PROPERTY = "CHE_WORKSPACE_ID=" + WORKSPACE_ID_VALUE; + + private static final String MACHINE_NAME_VALUE = "myMachine"; + private static final String MACHINE_NAME_PROPERTY = "CHE_MACHINE_NAME=" + MACHINE_NAME_VALUE; + + private static final String IS_DEV_MACHINE_VALUE = "true"; + private static final String IS_DEV_MACHINE_PROPERTY = "CHE_IS_DEV_MACHINE=" + IS_DEV_MACHINE_VALUE; + @Mock private ContainerInfo containerInfo; @Mock @@ -56,6 +65,9 @@ public class LocalDockerSinglePortServerEvaluationStrategyTest { private Map labels; private String[] env; + + private String[] envContainerConfig; + @BeforeMethod public void setUp() { @@ -83,6 +95,10 @@ public void setUp() { when(containerConfig.getHostname()).thenReturn(CONTAINERCONFIG_HOSTNAME); when(containerConfig.getEnv()).thenReturn(env); when(containerConfig.getLabels()).thenReturn(labels); + + envContainerConfig = new String[]{WORKSPACE_ID_PROPERTY, MACHINE_NAME_PROPERTY, IS_DEV_MACHINE_PROPERTY}; + when(containerConfig.getEnv()).thenReturn(envContainerConfig); + } /** @@ -92,7 +108,7 @@ public void setUp() { @Test public void shouldUseServerRefToBuildAddressWhenAvailable() throws Exception { // given - strategy = new LocalDockerSinglePortServerEvaluationStrategy(null, null, false); + strategy = new LocalDockerSinglePortServerEvaluationStrategy(null, null, false).withThrowOnUnknownHost(false); final Map expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, CONTAINERCONFIG_HOSTNAME, diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/ServerEvaluationStrategyTest.java b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/ServerEvaluationStrategyTest.java index bb1898a4a01..f9bf96e14b7 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/ServerEvaluationStrategyTest.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/ServerEvaluationStrategyTest.java @@ -83,7 +83,7 @@ public void shouldConvertAddressAndExposedPortsInMapOfExposedPortToAddressPort() expected.put("9090/udp", DEFAULT_HOSTNAME + ":" + "32101"); // when - Map actual = strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports); + Map actual = strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports, false); // then assertEquals(actual, expected); @@ -108,7 +108,7 @@ public void shouldIgnoreMultiplePortBindingEntries() throws Exception { expected.put("9090/udp", DEFAULT_HOSTNAME + ":" + "32101"); // when - Map actual = strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports); + Map actual = strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports, false); // then assertEquals(actual, expected); @@ -375,7 +375,7 @@ private Map> prepareStrategyAndContainerInfoMocks() { .withHostPort("32101"))); when(networkSettings.getPorts()).thenReturn(ports); Map exposedPortsToAddressPorts = - strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports); + strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports, false); when(strategy.getExternalAddressesAndPorts(containerInfo, DEFAULT_HOSTNAME)) .thenReturn(exposedPortsToAddressPorts); when(strategy.getInternalAddressesAndPorts(containerInfo, DEFAULT_HOSTNAME))