From f2ca9a844501131663142ea64cb81069a58e182f Mon Sep 17 00:00:00 2001 From: Alexander Garagatyi Date: Mon, 16 Jan 2017 14:53:34 +0200 Subject: [PATCH] CHE-3686: fix docker machines addresses setting Set internal machine address by CLI from CHE_IP. Add and fix docs in che.env. Change behavior of default machine server address evaluation to be similar to the previous state. Code cleanup. Signed-off-by: Alexander Garagatyi --- .../assembly-main/src/assembly/bin/che.sh | 4 +- .../che/api/core/model/machine/Server.java | 2 - dockerfiles/che/entrypoint.sh | 4 +- dockerfiles/init/manifests/che.env | 24 +- dockerfiles/launcher/launcher.sh | 4 +- dockerfiles/launcher/launcher_cmds.sh | 4 +- dockerfiles/launcher/launcher_funcs.sh | 4 +- .../DefaultServerEvaluationStrategy.java | 44 +- .../machine/DockerInstanceRuntimeInfo.java | 6 +- .../LocalDockerServerEvaluationStrategy.java | 25 +- .../machine/ServerEvaluationStrategy.java | 100 +++- .../DefaultServerEvaluationStrategyTest.java | 120 ++-- .../DockerInstanceRuntimeInfoTest.java | 566 ++++-------------- .../machine/ServerEvaluationStrategyTest.java | 280 +++++---- 14 files changed, 450 insertions(+), 737 deletions(-) diff --git a/assembly/assembly-main/src/assembly/bin/che.sh b/assembly/assembly-main/src/assembly/bin/che.sh index 16cd0ced26fe..ad73199308aa 100644 --- a/assembly/assembly-main/src/assembly/bin/che.sh +++ b/assembly/assembly-main/src/assembly/bin/che.sh @@ -184,9 +184,9 @@ usage () { set_environment_variables () { ### Set value of derived environment variables. - # CHE_DOCKER_MACHINE_HOST is used internally by Che to set its IP address + # CHE_DOCKER_IP is used internally by Che to set its IP address if [[ -n "${CHE_IP}" ]]; then - export CHE_DOCKER_MACHINE_HOST="${CHE_IP}" + export CHE_DOCKER_IP="${CHE_IP}" fi #if [ "${WIN}" == "true" ] && [ ! -z "${JAVA_HOME}" ]; then diff --git a/core/che-core-api-model/src/main/java/org/eclipse/che/api/core/model/machine/Server.java b/core/che-core-api-model/src/main/java/org/eclipse/che/api/core/model/machine/Server.java index 5dce50b4480f..d9babad0aaf6 100644 --- a/core/che-core-api-model/src/main/java/org/eclipse/che/api/core/model/machine/Server.java +++ b/core/che-core-api-model/src/main/java/org/eclipse/che/api/core/model/machine/Server.java @@ -28,8 +28,6 @@ public interface Server { * External address of the server in form hostname:port. *

* This address is used by the browser to communicate with the server. - * hostname can be configured using property machine.docker.local_node_host.external - * or environment variable CHE_DOCKER_MACHINE_HOST_EXTERNAL. * port is the external port and cannot be configured. * If not explicitly configured that address is set using {@link ServerProperties#getInternalAddress()} */ diff --git a/dockerfiles/che/entrypoint.sh b/dockerfiles/che/entrypoint.sh index d4b1f65fcf2f..a27d8787bc70 100755 --- a/dockerfiles/che/entrypoint.sh +++ b/dockerfiles/che/entrypoint.sh @@ -79,9 +79,9 @@ usage () { set_environment_variables () { ### Set value of derived environment variables. - # CHE_DOCKER_MACHINE_HOST is used internally by Che to set its IP address + # CHE_DOCKER_IP is used internally by Che to set its IP address if [[ -n "${CHE_IP}" ]]; then - export CHE_DOCKER_MACHINE_HOST="${CHE_IP}" + export CHE_DOCKER_IP="${CHE_IP}" fi # Convert Tomcat environment variables to POSIX format. diff --git a/dockerfiles/init/manifests/che.env b/dockerfiles/init/manifests/che.env index 6cb0d7943f5c..961b02d359f8 100644 --- a/dockerfiles/init/manifests/che.env +++ b/dockerfiles/init/manifests/che.env @@ -134,16 +134,24 @@ # Che Server --> Workspace Connection (see Workspace Address Resolution Strategy, below): # - If CHE_DOCKER_SERVER__EVALUATION__STRATEGY is 'default': # 1. Use the value of che.docker.ip -# 2. Else, use address of docker0 bridge network, if available -# 3. Else, use localhost +# 2. Else, if server connects over Unix socket, then use localhost +# 3. Else, use DOCKER_HOST # - If CHE_DOCKER_SERVER__EVALUATION__STRATEGY is 'docker-local': -# 1. Use the address of the workspace container within the docker network -# 2. If address cannot be read, try localhost +# 1. Use the address of the workspace container within the docker network and exposed ports +# 2. If address is missing, if server connects over Unix socket, then use localhost and published ports +# 3. Else, use DOCKER_HOST and published ports # -# Browser --> Workspace Connection: -# 1. Use the value of che.docker.ip -# 2. Else, if server connects over Unix socket, then use localhost -# 3. Else, use DOCKER_HOST +# Browser --> Workspace Connection (see Workspace Address Resolution Strategy, below): +# - If CHE_DOCKER_SERVER__EVALUATION__STRATEGY is 'default': +# 1. If set use the value of che.docker.ip.external +# 2. Else if set use the value of che.docker.ip +# 3. Else, if server connects over Unix socket, then use localhost +# 4. Else, use DOCKER_HOST +# - If CHE_DOCKER_SERVER__EVALUATION__STRATEGY is 'docker-local': +# 1. If set use the value of che.docker.ip.external +# 2. Else use the address of the workspace container within the docker network, if it is set +# 3. If address is missing, if server connects over Unix socket, then use localhost +# 4. Else, use DOCKER_HOST # # Workspace Agent --> Che Server # 1. Default is http://che-host:${SERVER_PORT}/wsmaster/api, where che-host is IP of server. diff --git a/dockerfiles/launcher/launcher.sh b/dockerfiles/launcher/launcher.sh index dce1c963dca2..00f43f73887b 100755 --- a/dockerfiles/launcher/launcher.sh +++ b/dockerfiles/launcher/launcher.sh @@ -62,7 +62,7 @@ init_global_variables() { DEFAULT_CHE_DEBUG_SERVER="false" DEFAULT_CHE_DEBUG_SERVER_PORT="8000" DEFAULT_CHE_DEBUG_SERVER_SUSPEND="false" - DEFAULT_CHE_DOCKER_MACHINE_HOST_EXTERNAL=$(get_docker_external_hostname) + DEFAULT_CHE_DOCKER_IP_EXTERNAL=$(get_docker_external_hostname) # Clean eventual user provided paths # This is a user-provided override @@ -96,7 +96,7 @@ init_global_variables() { CHE_DEBUG_SERVER=${CHE_DEBUG_SERVER:-${DEFAULT_CHE_DEBUG_SERVER}} CHE_DEBUG_SERVER_PORT=${CHE_DEBUG_SERVER_PORT:-${DEFAULT_CHE_DEBUG_SERVER_PORT}} CHE_DEBUG_SERVER_SUSPEND=${CHE_DEBUG_SERVER_SUSPEND:-${DEFAULT_CHE_DEBUG_SERVER_SUSPEND}} - CHE_DOCKER_MACHINE_HOST_EXTERNAL=${CHE_DOCKER_MACHINE_HOST_EXTERNAL:-${DEFAULT_CHE_DOCKER_MACHINE_HOST_EXTERNAL}} + CHE_DOCKER_IP_EXTERNAL=${CHE_DOCKER_IP_EXTERNAL:-${DEFAULT_CHE_DOCKER_IP_EXTERNAL}} USAGE=" Usage: diff --git a/dockerfiles/launcher/launcher_cmds.sh b/dockerfiles/launcher/launcher_cmds.sh index 185f5de9e250..5a3ef93a21ba 100644 --- a/dockerfiles/launcher/launcher_cmds.sh +++ b/dockerfiles/launcher/launcher_cmds.sh @@ -112,7 +112,7 @@ print_debug_info() { info "DOCKER_INSTALL_TYPE = ${DOCKER_INSTALL_TYPE}" info "DOCKER_HOST_OS = $(get_docker_host_os)" info "DOCKER_HOST_IP = ${DEFAULT_DOCKER_HOST_IP}" - info "DOCKER_HOST_EXTERNAL_IP = ${DEFAULT_CHE_DOCKER_MACHINE_HOST_EXTERNAL:-not set}" + info "DOCKER_HOST_EXTERNAL_IP = ${DEFAULT_CHE_DOCKER_IP_EXTERNAL:-not set}" info "DOCKER_DAEMON_VERSION = $(get_docker_daemon_version)" info "" info "" @@ -160,4 +160,4 @@ print_debug_info() { info "CHE_HOSTNAME = ${CHE_HOSTNAME}" info "CHE_SERVER_CONTAINER_NAME = ${CHE_SERVER_CONTAINER_NAME}" info "CHE_SERVER_IMAGE_NAME = ${CHE_SERVER_IMAGE_NAME}" -} \ No newline at end of file +} diff --git a/dockerfiles/launcher/launcher_funcs.sh b/dockerfiles/launcher/launcher_funcs.sh index af7a6fe9ae16..79eefe07cfcc 100644 --- a/dockerfiles/launcher/launcher_funcs.sh +++ b/dockerfiles/launcher/launcher_funcs.sh @@ -165,7 +165,7 @@ docker_run_with_conf() { docker_run_with_external_hostname() { if has_external_hostname; then - docker_run_with_conf -e "CHE_DOCKER_MACHINE_HOST_EXTERNAL=${CHE_DOCKER_MACHINE_HOST_EXTERNAL}" "$@" + docker_run_with_conf -e "CHE_DOCKER_IP_EXTERNAL=${CHE_DOCKER_IP_EXTERNAL}" "$@" else docker_run_with_conf "$@" fi @@ -217,7 +217,7 @@ has_assembly() { } has_external_hostname() { - if [ "${CHE_DOCKER_MACHINE_HOST_EXTERNAL}" = "" ]; then + if [ "${CHE_DOCKER_IP_EXTERNAL}" = "" ]; then return 1 else return 0 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 51b2197d5ece..1f5eba9e94ea 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 @@ -8,22 +8,16 @@ * Contributors: * Red Hat Inc. - initial API and implementation *******************************************************************************/ - package org.eclipse.che.plugin.docker.machine; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import com.google.inject.Inject; +import com.google.inject.name.Named; 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 com.google.inject.Inject; -import com.google.inject.name.Named; -import static com.google.common.base.Strings.isNullOrEmpty; +import java.util.Map; /** * Represents the default server evaluation strategy. By default, calling @@ -34,6 +28,7 @@ * {@code che.docker.ip} and {@code che.docker.ip.external}, respectively. * * @author Angel Misevski + * @author Alexander Garagatyi * @see ServerEvaluationStrategy */ public class DefaultServerEvaluationStrategy extends ServerEvaluationStrategy { @@ -49,48 +44,29 @@ public class DefaultServerEvaluationStrategy extends ServerEvaluationStrategy { protected String externalAddressProperty; @Inject - public DefaultServerEvaluationStrategy (@Nullable @Named("che.docker.ip") String internalAddress, - @Nullable @Named("che.docker.ip.external") String externalAddress) { + 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 internalAddressContainer = containerInfo.getNetworkSettings().getGateway(); - String internalAddress = internalAddressProperty != null ? internalAddressProperty : - !isNullOrEmpty(internalAddressContainer) ? - internalAddressContainer : internalHost; - Map> portBindings = containerInfo.getNetworkSettings().getPorts(); - - return getAddressesAndPorts(internalAddress, portBindings); + return getExposedPortsToAddressPorts(internalAddress, containerInfo.getNetworkSettings().getPorts()); } @Override protected Map getExternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) { - String externalAddressContainer = containerInfo.getNetworkSettings().getGateway(); - String externalAddress = externalAddressProperty != null ? externalAddressProperty : - !isNullOrEmpty(externalAddressContainer) ? - externalAddressContainer : + internalAddressProperty != null ? + internalAddressProperty : internalHost; - Map> portBindings = containerInfo.getNetworkSettings().getPorts(); - - return getAddressesAndPorts(externalAddress, portBindings); - } - - private Map getAddressesAndPorts(String address, Map> ports) { - Map addressesAndPorts = new HashMap<>(); - for (String portKey : ports.keySet()) { - String port = ports.get(portKey).get(0).getHostPort(); - addressesAndPorts.put(portKey, address + ":" + port); - } - return addressesAndPorts; + return super.getExposedPortsToAddressPorts(externalAddress, containerInfo.getNetworkSettings().getPorts()); } } 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 924497c933bb..6a49ef6386ae 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 @@ -74,9 +74,9 @@ public class DockerInstanceRuntimeInfo implements MachineRuntimeInfo { */ public static final String USER_TOKEN = "USER_TOKEN"; - private final ContainerInfo info; - private final Map serversConf; - private final String internalHost; + private final ContainerInfo info; + private final Map serversConf; + private final String internalHost; private final ServerEvaluationStrategyProvider provider; @Inject 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 b7390feeab4a..e89e0d643949 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 @@ -8,20 +8,20 @@ * Contributors: * Red Hat Inc. - initial API and implementation *******************************************************************************/ - package org.eclipse.che.plugin.docker.machine; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import com.google.inject.Inject; +import com.google.inject.name.Named; 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 com.google.inject.Inject; -import com.google.inject.name.Named; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static com.google.common.base.Strings.isNullOrEmpty; /** @@ -49,8 +49,8 @@ public class LocalDockerServerEvaluationStrategy extends ServerEvaluationStrateg protected String externalAddressProperty; @Inject - public LocalDockerServerEvaluationStrategy (@Nullable @Named("che.docker.ip") String internalAddress, - @Nullable @Named("che.docker.ip.external") String externalAddress) { + public LocalDockerServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String internalAddress, + @Nullable @Named("che.docker.ip.external") String externalAddress) { this.internalAddressProperty = internalAddress; this.externalAddressProperty = externalAddress; } @@ -93,13 +93,6 @@ protected Map getExternalAddressesAndPorts(ContainerInfo contain externalAddressContainer : internalHost; - Map> portBindings = containerInfo.getNetworkSettings().getPorts(); - - Map addressesAndPorts = new HashMap<>(); - for (String portKey : portBindings.keySet()) { - String port = portBindings.get(portKey).get(0).getHostPort(); - addressesAndPorts.put(portKey, externalAddress + ":" + port); - } - return addressesAndPorts; + return getExposedPortsToAddressPorts(externalAddress, containerInfo.getNetworkSettings().getPorts()); } } 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 fcbe98eca0f8..e244ff4caef7 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 @@ -8,24 +8,20 @@ * Contributors: * Red Hat Inc. - initial API and implementation *******************************************************************************/ - package org.eclipse.che.plugin.docker.machine; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - import org.eclipse.che.api.core.model.machine.ServerProperties; import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; import org.eclipse.che.api.machine.server.model.impl.ServerImpl; import org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl; -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 com.google.inject.Inject; -import com.google.inject.name.Named; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; /** * Represents a strategy for resolving Servers associated with workspace containers. @@ -33,6 +29,7 @@ * {@link ServerImpl} objects. * * @author Angel Misevski + * @author Alexander Garagatyi * @see ServerEvaluationStrategyProvider */ public abstract class ServerEvaluationStrategy { @@ -45,9 +42,11 @@ public abstract class ServerEvaluationStrategy { * Gets a map of all internal addresses exposed by the container in the form of * {@code "

:"} * - * @param containerInfo the ContainerInfo object that describes the container. - * @param internalAddress address passed into {@code getServers}; used as fallback if address cannot be - * retrieved from containerInfo. + * @param containerInfo + * the ContainerInfo object that describes the container. + * @param internalAddress + * address passed into {@code getServers}; used as fallback if address cannot be + * retrieved from containerInfo. * @return a Map of port protocol (e.g. "4401/tcp") to address (e.g. "172.17.0.1:32317") */ protected abstract Map getInternalAddressesAndPorts(ContainerInfo containerInfo, @@ -57,9 +56,11 @@ protected abstract Map getInternalAddressesAndPorts(ContainerInf * Gets a map of all external addresses exposed by the container in the form of * {@code "
:"} * - * @param containerInfo the ContainerInfo object that describes the container. - * @param internalAddress address passed into {@code getServers}; used as fallback if address cannot be - * retrieved from containerInfo. + * @param containerInfo + * the ContainerInfo object that describes the container. + * @param internalAddress + * address passed into {@code getServers}; used as fallback if address cannot be + * retrieved from containerInfo. * @return a Map of port protocol (e.g. "4401/tcp") to address (e.g. "localhost:32317") */ protected abstract Map getExternalAddressesAndPorts(ContainerInfo containerInfo, @@ -72,10 +73,13 @@ protected abstract Map getExternalAddressesAndPorts(ContainerInf *

Keys consist of port number and transport protocol (tcp or udp) separated by * a forward slash (e.g. 8080/tcp) * - * @param containerInfo the {@link ContainerInfo} describing the container. - * @param internalHost alternative hostname to use, if address cannot be obtained from containerInfo - * @param serverConfMap additional Map of {@link ServerConfImpl}. Configurations here override those found - * in containerInfo. + * @param containerInfo + * the {@link ContainerInfo} describing the container. + * @param internalHost + * alternative hostname to use, if address cannot be obtained from containerInfo + * @param serverConfMap + * additional Map of {@link ServerConfImpl}. Configurations here override those found + * in containerInfo. * @return a Map of the servers exposed by the container. */ public Map getServers(ContainerInfo containerInfo, @@ -83,7 +87,7 @@ public Map getServers(ContainerInfo containerInfo, Map serverConfMap) { Map> portBindings; - Map labels = Collections.emptyMap();; + Map labels = Collections.emptyMap(); if (containerInfo.getNetworkSettings() != null && containerInfo.getNetworkSettings().getPorts() != null) { portBindings = containerInfo.getNetworkSettings().getPorts(); @@ -141,13 +145,16 @@ public Map getServers(ContainerInfo containerInfo, * *

{@code che:server::[ref|path|protocol]} * - * @param portProtocol the port binding associated with the server - * @param labels a map holding the relevant values for reference, protocol, and path - * for the given {@code portProtocol} - * @param serverConfMap a map of {@link ServerConfImpl} with {@code portProtocol} as - * keys. + * @param portProtocol + * the port binding associated with the server + * @param labels + * a map holding the relevant values for reference, protocol, and path + * for the given {@code portProtocol} + * @param serverConfMap + * a map of {@link ServerConfImpl} with {@code portProtocol} as + * keys. * @return {@code ServerConfImpl}, obtained from {@code serverConfMap} if possible, - * or from {@code labels} if there is no entry in {@code serverConfMap}. + * or from {@code labels} if there is no entry in {@code serverConfMap}. */ private ServerConfImpl getServerConfImpl(String portProtocol, Map labels, @@ -188,4 +195,45 @@ private ServerConfImpl getServerConfImpl(String portProtocol, } return serverConf; } + + /** + * Transforms address and server ports into map where + * key is port and optional transport protocol and value is address port of server. + * + *

Example: + * When method accepts address my-host.com and ports: + *

{@code
+     * {
+     *     "7070" : [
+     *         "hostIp" : "127.0.0.1",
+     *         "hostPort" : "32720"
+     *     ],
+     *     "8080/tcp" : [
+     *         "hostIp" : "127.0.0.1",
+     *         "hostPort" : "32721"
+     *     ],
+     *     "9090/udp" : [
+     *         "hostIp" : "127.0.0.1",
+     *         "hostPort" : "32722"
+     *     ]
+     * }
+     * }
+ * this method returns: + *
{@code
+     * {
+     *     "7070" : "my-host.com:32720",
+     *     "8080/tcp" : "my-host.com:32721",
+     *     "9090/udp" : "my-host.com:32722"
+     * }
+     * }
+ */ + protected Map getExposedPortsToAddressPorts(String address, Map> ports) { + Map addressesAndPorts = new HashMap<>(); + for (Map.Entry> portEntry : ports.entrySet()) { + // there is one value always + String port = portEntry.getValue().get(0).getHostPort(); + addressesAndPorts.put(portEntry.getKey(), address + ":" + port); + } + return addressesAndPorts; + } } diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategyTest.java b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategyTest.java index 3f3cd102592f..d4c5d2677989 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategyTest.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategyTest.java @@ -8,18 +8,8 @@ * Contributors: * Red Hat Inc. - initial API and implementation *******************************************************************************/ - package org.eclipse.che.plugin.docker.machine; -import static org.mockito.Mockito.when; -import static org.testng.Assert.assertEquals; - -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.eclipse.che.api.core.model.machine.MachineConfig; import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; import org.eclipse.che.api.machine.server.model.impl.ServerImpl; import org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl; @@ -33,21 +23,29 @@ import org.testng.annotations.Listeners; import org.testng.annotations.Test; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; + +/** + * @author Angel Misevski + * @author Alexander Garagatyi + */ @Listeners(MockitoTestNGListener.class) public class DefaultServerEvaluationStrategyTest { private static final String CHE_DOCKER_IP = "container-host.com"; private static final String CHE_DOCKER_IP_EXTERNAL = "container-host-ext.com"; private static final String ALL_IP_ADDRESS = "0.0.0.0"; - private static final String CONTAINERINFO_GATEWAY = "172.17.0.1"; - private static final String CONTAINERINFO_IP_ADDRESS = "172.17.0.200"; private static final String DEFAULT_HOSTNAME = "localhost"; @Mock private ContainerInfo containerInfo; @Mock - private MachineConfig machineConfig; - @Mock private ContainerConfig containerConfig; @Mock private NetworkSettings networkSettings; @@ -56,8 +54,6 @@ public class DefaultServerEvaluationStrategyTest { private Map serverConfs; - private Map> ports; - @BeforeMethod public void setUp() { @@ -65,58 +61,35 @@ public void setUp() { serverConfs.put("4301/tcp", new ServerConfImpl("sysServer1-tcp", "4301/tcp", "http", "/some/path1")); serverConfs.put("4305/udp", new ServerConfImpl("devSysServer1-udp", "4305/udp", null, "some/path4")); - ports = new HashMap<>(); + Map> ports = new HashMap<>(); ports.put("4301/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); + .withHostPort("32100"))); ports.put("4305/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32103"))); + .withHostPort("32103"))); when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); - when(networkSettings.getGateway()).thenReturn(CONTAINERINFO_GATEWAY); - when(networkSettings.getIpAddress()).thenReturn(CONTAINERINFO_IP_ADDRESS); when(networkSettings.getPorts()).thenReturn(ports); when(containerInfo.getConfig()).thenReturn(containerConfig); when(containerConfig.getLabels()).thenReturn(Collections.emptyMap()); } /** - * Test: che.docker.ip property takes highest precedence for internal address - * @throws Exception - */ - @Test - public void defaultStrategyShouldUseInternalIpPropertyToOverrideContainerInfo() throws Exception { - // given - strategy = new DefaultServerEvaluationStrategy(CHE_DOCKER_IP, null); - - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - CHE_DOCKER_IP, - false); - - // when - final Map servers = strategy.getServers(containerInfo, - null, - serverConfs); - - // then - assertEquals(servers, expectedServers); - } - - /** - * Test: If che.docker.ip is null, containerInfo.getGateway() is used for internal address + * Test: If che.docker.ip is null, and che.docker.ip.external is null or empty, should use provided + * internalHostname value as internal and external addresses in this case. * @throws Exception */ @Test - public void defaultStrategyShouldUseContainerInfoWhenInternalIpPropertyIsNull() throws Exception { + public void defaultStrategyShouldUseInternalHostWhenBothIpPropertyAreNull() throws Exception { // given strategy = new DefaultServerEvaluationStrategy(null, null); - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - CONTAINERINFO_GATEWAY, - false); + final Map expectedServers = getExpectedServers(DEFAULT_HOSTNAME, + DEFAULT_HOSTNAME + ); // when final Map servers = strategy.getServers(containerInfo, - null, + DEFAULT_HOSTNAME, serverConfs); // then @@ -124,19 +97,17 @@ public void defaultStrategyShouldUseContainerInfoWhenInternalIpPropertyIsNull() } /** - * Test: If che.docker.ip is null, and containerInfo.getGateway() is null or empty, should use provided - * internalHostname value. Also tests that this value is used for external address in this case. + * Test: If che.docker.ip.external and che.docker.ip are not null, these values should take precedence for external and internal addresses. * @throws Exception */ @Test - public void defaultStrategyShouldUseInternalHostWhenBothIpPropertyAndContainerInfoAreNull() throws Exception { + public void defaultStrategyShouldUseIpPropertiesWhenAvailable() throws Exception { // given - strategy = new DefaultServerEvaluationStrategy(null, null); - when(networkSettings.getGateway()).thenReturn(""); + strategy = new DefaultServerEvaluationStrategy(CHE_DOCKER_IP, CHE_DOCKER_IP_EXTERNAL); - final Map expectedServers = getExpectedServers(DEFAULT_HOSTNAME, - DEFAULT_HOSTNAME, - false); + final Map expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, + CHE_DOCKER_IP + ); // when final Map servers = strategy.getServers(containerInfo, @@ -148,17 +119,16 @@ public void defaultStrategyShouldUseInternalHostWhenBothIpPropertyAndContainerIn } /** - * Test: If che.docker.ip.external is not null, that should take precedence for external address. + * Test: If che.docker.ip.external is null, che.docker.ip is used as external address if it is not null. * @throws Exception */ @Test - public void defaultStrategyShouldUseExtenalIpPropertyWhenAvailable() throws Exception { + public void defaultStrategyShouldUseInternalIpPropertyAsExternalIfExternalIpPropertyIsNull() throws Exception { // given - strategy = new DefaultServerEvaluationStrategy(null, CHE_DOCKER_IP_EXTERNAL); + strategy = new DefaultServerEvaluationStrategy(CHE_DOCKER_IP, null); - final Map expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, - CONTAINERINFO_GATEWAY, - false); + final Map expectedServers = getExpectedServers(CHE_DOCKER_IP, + CHE_DOCKER_IP); // when final Map servers = strategy.getServers(containerInfo, @@ -170,17 +140,17 @@ public void defaultStrategyShouldUseExtenalIpPropertyWhenAvailable() throws Exce } /** - * Test: If che.docker.ip.external is null, should use containerInfo.getGateway() + * Test: If che.docker.ip.external is not null and che.docker.ip is null, + * internal address is taken from provided internalAddress and external address is taken from property. * @throws Exception */ @Test - public void defaultStrategyShouldUseContainerInfoForExternalWhenPropertyIsNull() throws Exception { + public void defaultStrategyShouldUseProvidedInternalIpIfInternalIpPropertyIsNull() throws Exception { // given - strategy = new DefaultServerEvaluationStrategy(null, null); + strategy = new DefaultServerEvaluationStrategy(null, CHE_DOCKER_IP_EXTERNAL); - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - CONTAINERINFO_GATEWAY, - false); + final Map expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, + DEFAULT_HOSTNAME); // when final Map servers = strategy.getServers(containerInfo, @@ -192,17 +162,11 @@ public void defaultStrategyShouldUseContainerInfoForExternalWhenPropertyIsNull() } private Map getExpectedServers(String externalAddress, - String internalAddress, - boolean useExposedPorts) { + String internalAddress) { String port1; String port2; - if (useExposedPorts) { - port1 = ":4301"; - port2 = ":4305"; - } else { - port1 = ":32100"; - port2 = ":32103"; - } + port1 = ":32100"; + port2 = ":32103"; Map expectedServers = new HashMap<>(); expectedServers.put("4301/tcp", new ServerImpl("sysServer1-tcp", "http", diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfoTest.java b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfoTest.java index 0a7e425c04ef..7c37fc0a5d13 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfoTest.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfoTest.java @@ -15,18 +15,17 @@ import org.eclipse.che.api.core.model.machine.ServerConf; import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; import org.eclipse.che.api.machine.server.model.impl.ServerImpl; -import org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl; import org.eclipse.che.plugin.docker.client.json.ContainerConfig; import org.eclipse.che.plugin.docker.client.json.ContainerInfo; -import org.eclipse.che.plugin.docker.client.json.NetworkSettings; -import org.eclipse.che.plugin.docker.client.json.PortBinding; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.Mock; import org.mockito.testng.MockitoTestNGListener; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Listeners; import org.testng.annotations.Test; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -34,35 +33,43 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; - -import static org.eclipse.che.plugin.docker.machine.DefaultServerEvaluationStrategy.SERVER_CONF_LABEL_PATH_KEY; -import static org.eclipse.che.plugin.docker.machine.DefaultServerEvaluationStrategy.SERVER_CONF_LABEL_PROTOCOL_KEY; -import static org.eclipse.che.plugin.docker.machine.DefaultServerEvaluationStrategy.SERVER_CONF_LABEL_REF_KEY; -import static org.mockito.Mockito.doReturn; +import java.util.stream.Stream; + +import static java.util.Collections.singleton; +import static java.util.Collections.singletonList; +import static java.util.stream.Collectors.toMap; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; +/** + * @author Angel Misevski + * @author Alexander Garagatyi + */ @Listeners(MockitoTestNGListener.class) public class DockerInstanceRuntimeInfoTest { - private static final String ALL_IP_ADDRESS = "0.0.0.0"; - private static final String CONTAINERINFO_GATEWAY = "172.17.0.1"; - private static final String CONTAINERINFO_IP_ADDRESS = "172.17.0.200"; private static final String DEFAULT_HOSTNAME = "localhost"; @Mock - private ContainerInfo containerInfo; + private ContainerInfo containerInfo; + @Mock + private MachineConfig machineConfig; @Mock - private MachineConfig machineConfig; + private ContainerConfig containerConfig; @Mock - private ContainerConfig containerConfig; + private ServerEvaluationStrategy serverEvaluationStrategy; @Mock - private NetworkSettings networkSettings; + private ServerEvaluationStrategyProvider provider; + @Mock + private Map serversMap; + @Captor + private ArgumentCaptor> serversCaptor; private DockerInstanceRuntimeInfo runtimeInfo; - @Mock - private static ServerEvaluationStrategyProvider provider; - @BeforeMethod public void setUp() { @@ -74,13 +81,9 @@ public void setUp() { Collections.emptySet()); when(containerInfo.getConfig()).thenReturn(containerConfig); - when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); - when(networkSettings.getGateway()).thenReturn(CONTAINERINFO_GATEWAY); - when(networkSettings.getIpAddress()).thenReturn(CONTAINERINFO_IP_ADDRESS); when(machineConfig.getServers()).thenReturn(Collections.emptyList()); - when(containerConfig.getLabels()).thenReturn(Collections.emptyMap()); - - when(provider.get()).thenReturn(new DefaultServerEvaluationStrategy(null, null)); + when(provider.get()).thenReturn(serverEvaluationStrategy); + when(serverEvaluationStrategy.getServers(any(), anyString(), any())).thenReturn(serversMap); } @Test @@ -93,9 +96,10 @@ public void shouldReturnEnvVars() throws Exception { when(containerConfig.getEnv()).thenReturn(expectedVariables.entrySet() .stream() - .map(stringStringEntry -> stringStringEntry.getKey() + - "=" + - stringStringEntry.getValue()) + .map(stringStringEntry -> + stringStringEntry.getKey() + + "=" + + stringStringEntry.getValue()) .collect(Collectors.toList()) .toArray(new String[expectedVariables.size()])); @@ -140,363 +144,38 @@ public void shouldReturnNullProjectsRootIfNoAppropriateEnvVarFound() throws Exce } @Test - public void shouldReturnServerForEveryExposedPort() throws Exception { - // given - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("100100/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32101"))); - ports.put("8080/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - - // when - final Map servers = runtimeInfo.getServers(); - - // then - assertEquals(servers.keySet(), ports.keySet()); - } - - @Test - public void shouldAddDefaultReferenceIfReferenceIsNotSet() throws Exception { - // given - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("100100/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32101"))); - ports.put("8080/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - final HashMap expectedServers = new HashMap<>(); - expectedServers.put("8080/tcp", new ServerImpl("Server-8080-tcp", - null, - CONTAINERINFO_GATEWAY + ":32100", - null, - new ServerPropertiesImpl(null, CONTAINERINFO_GATEWAY + ":32100", null))); - expectedServers.put("100100/udp", new ServerImpl("Server-100100-udp", - null, - CONTAINERINFO_GATEWAY + ":32101", - null, - new ServerPropertiesImpl(null, CONTAINERINFO_GATEWAY + ":32101", null))); - expectedServers.put("8080/udp", new ServerImpl("Server-8080-udp", - null, - CONTAINERINFO_GATEWAY + ":32102", - null, - new ServerPropertiesImpl(null, CONTAINERINFO_GATEWAY + ":32102", null))); - - // when - final Map servers = runtimeInfo.getServers(); - - // then - assertEquals(servers, expectedServers); - } - - @Test - public void shouldAddRefUrlProtocolPathToServerFromMachineConfig() throws Exception { - // given - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - List serversConfigs = new ArrayList<>(); - doReturn(serversConfigs).when(machineConfig).getServers(); - ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("100100/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32101"))); - ports.put("8080/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - ports.put("8000/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32103"))); - serversConfigs.add(new ServerConfImpl("myserv1", "8080/tcp", "http", null)); - serversConfigs.add(new ServerConfImpl("myserv1-tftp", "8080/udp", "tftp", "/some/path")); - serversConfigs.add(new ServerConfImpl("myserv2", "100100/udp", "dhcp", "/some")); - serversConfigs.add(new ServerConfImpl(null, "8000/tcp", "tcp", "/path")); - runtimeInfo = new DockerInstanceRuntimeInfo(containerInfo, - machineConfig, - DEFAULT_HOSTNAME, - provider, - Collections.emptySet(), - Collections.emptySet()); - final HashMap expectedServers = new HashMap<>(); - expectedServers.put("8080/tcp", new ServerImpl("myserv1", - "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100"))); - expectedServers.put("100100/udp", new ServerImpl("myserv2", - "dhcp", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some", - new ServerPropertiesImpl("/some", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some"))); - expectedServers.put("8080/udp", new ServerImpl("myserv1-tftp", - "tftp", - CONTAINERINFO_GATEWAY + ":32102", - "tftp://" + CONTAINERINFO_GATEWAY + ":32102/some/path", - new ServerPropertiesImpl("/some/path", - CONTAINERINFO_GATEWAY + ":32102", - "tftp://" + CONTAINERINFO_GATEWAY + ":32102/some/path"))); - expectedServers.put("8000/tcp", new ServerImpl("Server-8000-tcp", - "tcp", - CONTAINERINFO_GATEWAY + ":32103", - "tcp://" + CONTAINERINFO_GATEWAY + ":32103/path", - new ServerPropertiesImpl("/path", - CONTAINERINFO_GATEWAY + ":32103", - "tcp://" + CONTAINERINFO_GATEWAY + ":32103/path"))); - - // when - final Map servers = runtimeInfo.getServers(); - - // then - assertEquals(servers, expectedServers); - } - - @Test - public void shouldAllowToUsePortFromMachineConfigWithoutTransportProtocol() throws Exception { - // given - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - List serversConfigs = new ArrayList<>(); - doReturn(serversConfigs).when(machineConfig).getServers(); - ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("8080/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - serversConfigs.add(new ServerConfImpl("myserv1", "8080", "http", "/some")); - serversConfigs.add(new ServerConfImpl("myserv1-tftp", "8080/udp", "tftp", "path")); - runtimeInfo = new DockerInstanceRuntimeInfo(containerInfo, - machineConfig, - DEFAULT_HOSTNAME, - provider, - Collections.emptySet(), - Collections.emptySet()); - final HashMap expectedServers = new HashMap<>(); - expectedServers.put("8080/tcp", new ServerImpl("myserv1", - "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some", - new ServerPropertiesImpl("/some", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some"))); - expectedServers.put("8080/udp", new ServerImpl("myserv1-tftp", - "tftp", - CONTAINERINFO_GATEWAY + ":32102", - "tftp://" + CONTAINERINFO_GATEWAY + ":32102/path", - new ServerPropertiesImpl("path", - CONTAINERINFO_GATEWAY + ":32102", - "tftp://" + CONTAINERINFO_GATEWAY + ":32102/path"))); - - // when - final Map servers = runtimeInfo.getServers(); - - // then - assertEquals(servers, expectedServers); - } - - @Test - public void shouldAddRefUrlPathToServerFromLabels() throws Exception { - // given - runtimeInfo = new DockerInstanceRuntimeInfo(containerInfo, - machineConfig, - DEFAULT_HOSTNAME, - provider, - Collections.emptySet(), - Collections.emptySet()); - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - Map labels = new HashMap<>(); - when(containerConfig.getLabels()).thenReturn(labels); - ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("100100/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32101"))); - ports.put("8080/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - ports.put("8000/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32103"))); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/tcp"), "myserv1"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080/tcp"), "http"); - labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "8080/tcp"), "/some/path"); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/udp"), "myserv1-tftp"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080/udp"), "tftp"); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "100100/udp"), "myserv2"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "100100/udp"), "dhcp"); - labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "100100/udp"), "some/path"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8000/tcp"), "tcp"); - final HashMap expectedServers = new HashMap<>(); - expectedServers.put("8080/tcp", new ServerImpl("myserv1", - "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path", - new ServerPropertiesImpl("/some/path", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path"))); - expectedServers.put("100100/udp", new ServerImpl("myserv2", - "dhcp", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path", - new ServerPropertiesImpl("some/path", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path"))); - expectedServers.put("8080/udp", new ServerImpl("myserv1-tftp", - "tftp", - CONTAINERINFO_GATEWAY + ":32102", - "tftp://" + CONTAINERINFO_GATEWAY + ":32102", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32102", - "tftp://" + CONTAINERINFO_GATEWAY + ":32102"))); - expectedServers.put("8000/tcp", new ServerImpl("Server-8000-tcp", - "tcp", - CONTAINERINFO_GATEWAY + ":32103", - "tcp://" + CONTAINERINFO_GATEWAY + ":32103", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32103", - "tcp://" + CONTAINERINFO_GATEWAY + ":32103"))); - - // when - final Map servers = runtimeInfo.getServers(); - - // then - assertEquals(servers, expectedServers); - } - - @Test - public void shouldAllowToUsePortFromDockerLabelsWithoutTransportProtocol() throws Exception { - // given - runtimeInfo = new DockerInstanceRuntimeInfo(containerInfo, - machineConfig, - DEFAULT_HOSTNAME, - provider, - Collections.emptySet(), - Collections.emptySet()); - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - Map labels = new HashMap<>(); - when(containerConfig.getLabels()).thenReturn(labels); - ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("8080/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - ports.put("8000/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32103"))); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080"), "myserv1"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080"), "http"); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/udp"), "myserv1-tftp"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080/udp"), "tftp"); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8000"), "myserv2"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8000/tcp"), "tcp"); - final HashMap expectedServers = new HashMap<>(); - expectedServers.put("8080/tcp", new ServerImpl("myserv1", - "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100"))); - expectedServers.put("8080/udp", new ServerImpl("myserv1-tftp", - "tftp", - CONTAINERINFO_GATEWAY + ":32102", - "tftp://" + CONTAINERINFO_GATEWAY + ":32102", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32102", - "tftp://" + CONTAINERINFO_GATEWAY + ":32102"))); - expectedServers.put("8000/tcp", new ServerImpl("myserv2", - "tcp", - CONTAINERINFO_GATEWAY + ":32103", - "tcp://" + CONTAINERINFO_GATEWAY + ":32103", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32103", - "tcp://" + CONTAINERINFO_GATEWAY + ":32103"))); - - // when - final Map servers = runtimeInfo.getServers(); - - // then - assertEquals(servers, expectedServers); - } - - @Test - public void shouldPreferMachineConfOverDockerLabels() throws Exception { - // given - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - Map labels = new HashMap<>(); - when(containerConfig.getLabels()).thenReturn(labels); - List serversConfigs = new ArrayList<>(); - doReturn(serversConfigs).when(machineConfig).getServers(); - ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("100100/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32101"))); - ports.put("8080/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/tcp"), "myserv1label"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080/tcp"), "https"); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/udp"), "myserv1-tftp"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080/udp"), "tftp"); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "100100/udp"), "myserv2label"); - labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "100100/udp"), "dhcp"); - labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "100100/udp"), "/path"); - serversConfigs.add(new ServerConfImpl("myserv1conf", "8080/tcp", "http", null)); - serversConfigs.add(new ServerConfImpl(null, "8080/udp", null, "some/path")); - runtimeInfo = new DockerInstanceRuntimeInfo(containerInfo, - machineConfig, - DEFAULT_HOSTNAME, - provider, - Collections.emptySet(), - Collections.emptySet()); - final HashMap expectedServers = new HashMap<>(); - expectedServers.put("8080/tcp", new ServerImpl("myserv1conf", - "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100"))); - expectedServers.put("100100/udp", new ServerImpl("myserv2label", - "dhcp", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/path", - new ServerPropertiesImpl("/path", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/path"))); - expectedServers.put("8080/udp", new ServerImpl("Server-8080-udp", - null, - CONTAINERINFO_GATEWAY + ":32102", - null, - new ServerPropertiesImpl("some/path", CONTAINERINFO_GATEWAY + ":32102", null))); - - // when - final Map servers = runtimeInfo.getServers(); + public void shouldBeAbleToGetServers() throws Exception { + Map servers = runtimeInfo.getServers(); - // then - assertEquals(servers, expectedServers); + assertEquals(servers, serversMap); + verify(serverEvaluationStrategy).getServers(eq(containerInfo), eq(DEFAULT_HOSTNAME), any()); } @Test - public void shouldAddOnlyCommonSystemServersConfigToNonDevMachine() throws Exception { + public void shouldPassCommonServerConfigsOnGetServersForNonDevMachine() throws Exception { // given - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - ports.put("4301/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("4302/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32101"))); - ports.put("4301/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - // add user defined server - ports.put("4305/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32103"))); Set commonSystemServersConfigs = new HashSet<>(); - commonSystemServersConfigs.add(new ServerConfImpl("sysServer1-tcp", "4301/tcp", "http", "/some/path")); - commonSystemServersConfigs.add(new ServerConfImpl("sysServer2-udp", "4302/udp", "dhcp", null)); - commonSystemServersConfigs.add(new ServerConfImpl("sysServer1-udp", "4301/udp", null, "some/path")); - Set devSystemServersConfigs = new HashSet<>(); - devSystemServersConfigs.add(new ServerConfImpl("devSysServer1-tcp", "4305/tcp", "http", null)); + commonSystemServersConfigs.add(new ServerConfImpl("sysServer1-tcp", + "4301/tcp", + "http", + "/some/path")); + commonSystemServersConfigs.add(new ServerConfImpl("sysServer2-udp", + "4302/udp", + "dhcp", + null)); + commonSystemServersConfigs.add(new ServerConfImpl("sysServer1-udp", + "4301/udp", + null, + "some/path")); + Set devSystemServersConfigs = singleton(new ServerConfImpl("devSysServer1-tcp", + "4305/tcp", + "http", + null)); + List serversConfFromMachineConf = singletonList(new ServerConfImpl("machineConfServer1-tcp", + "4306/tcp", + "http", + null)); + when(machineConfig.getServers()).thenAnswer(invocation -> serversConfFromMachineConf); when(machineConfig.isDev()).thenReturn(false); runtimeInfo = new DockerInstanceRuntimeInfo(containerInfo, machineConfig, @@ -504,60 +183,41 @@ public void shouldAddOnlyCommonSystemServersConfigToNonDevMachine() throws Excep provider, devSystemServersConfigs, commonSystemServersConfigs); - final HashMap expectedServers = new HashMap<>(); - expectedServers.put("4301/tcp", new ServerImpl("sysServer1-tcp", - "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path", - new ServerPropertiesImpl("/some/path", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path"))); - expectedServers.put("4302/udp", new ServerImpl("sysServer2-udp", - "dhcp", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101"))); - expectedServers.put("4301/udp", new ServerImpl("sysServer1-udp", - null, - CONTAINERINFO_GATEWAY + ":32102", - null, - new ServerPropertiesImpl("some/path", - CONTAINERINFO_GATEWAY + ":32102", - null))); - expectedServers.put("4305/tcp", new ServerImpl("Server-4305-tcp", - null, - CONTAINERINFO_GATEWAY + ":32103", - null, - new ServerPropertiesImpl(null, CONTAINERINFO_GATEWAY + ":32103", null))); // when - final Map servers = runtimeInfo.getServers(); + Map servers = runtimeInfo.getServers(); // then - assertEquals(servers, expectedServers); + assertEquals(servers, serversMap); + verify(serverEvaluationStrategy).getServers(eq(containerInfo), eq(DEFAULT_HOSTNAME), serversCaptor.capture()); + assertEquals(serversCaptor.getValue(), serversToMap(commonSystemServersConfigs, serversConfFromMachineConf)); } @Test - public void shouldAddCommonAndDevSystemServersConfigToDevMachine() throws Exception { - // given - Map> ports = new HashMap<>(); - when(networkSettings.getPorts()).thenReturn(ports); - ports.put("4301/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("4302/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32101"))); - ports.put("4305/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32102"))); - ports.put("4305/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32103"))); + public void shouldPassCommonAndDevServerConfigsOnGetServersForNonDevMachine() throws Exception { Set commonSystemServersConfigs = new HashSet<>(); - commonSystemServersConfigs.add(new ServerConfImpl("sysServer1-tcp", "4301/tcp", "http", "/some/path1")); - commonSystemServersConfigs.add(new ServerConfImpl("sysServer2-udp", "4302/udp", "dhcp", "some/path2")); + commonSystemServersConfigs.add(new ServerConfImpl("sysServer1-tcp", + "4301/tcp", + "http", + "/some/path1")); + commonSystemServersConfigs.add(new ServerConfImpl("sysServer2-udp", + "4302/udp", + "dhcp", + "some/path2")); Set devSystemServersConfigs = new HashSet<>(); - devSystemServersConfigs.add(new ServerConfImpl("devSysServer1-tcp", "4305/tcp", "http", "/some/path3")); - devSystemServersConfigs.add(new ServerConfImpl("devSysServer1-udp", "4305/udp", null, "some/path4")); + devSystemServersConfigs.add(new ServerConfImpl("devSysServer1-tcp", + "4305/tcp", + "http", + "/some/path3")); + devSystemServersConfigs.add(new ServerConfImpl("devSysServer1-udp", + "4305/udp", + null, + "some/path4")); + List serversConfFromMachineConf = singletonList(new ServerConfImpl("machineConfServer1-tcp", + "4306/tcp", + "http", + null)); + when(machineConfig.getServers()).thenAnswer(invocation -> serversConfFromMachineConf); when(machineConfig.isDev()).thenReturn(true); runtimeInfo = new DockerInstanceRuntimeInfo(containerInfo, machineConfig, @@ -565,40 +225,32 @@ public void shouldAddCommonAndDevSystemServersConfigToDevMachine() throws Except provider, devSystemServersConfigs, commonSystemServersConfigs); - final HashMap expectedServers = new HashMap<>(); - expectedServers.put("4301/tcp", new ServerImpl("sysServer1-tcp", - "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path1", - new ServerPropertiesImpl("/some/path1", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path1"))); - expectedServers.put("4302/udp", new ServerImpl("sysServer2-udp", - "dhcp", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path2", - new ServerPropertiesImpl("some/path2", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path2"))); - expectedServers.put("4305/tcp", new ServerImpl("devSysServer1-tcp", - "http", - CONTAINERINFO_GATEWAY + ":32102", - "http://" + CONTAINERINFO_GATEWAY + ":32102/some/path3", - new ServerPropertiesImpl("/some/path3", - CONTAINERINFO_GATEWAY + ":32102", - "http://" + CONTAINERINFO_GATEWAY + ":32102/some/path3"))); - expectedServers.put("4305/udp", new ServerImpl("devSysServer1-udp", - null, - CONTAINERINFO_GATEWAY + ":32103", - null, - new ServerPropertiesImpl("some/path4", - CONTAINERINFO_GATEWAY + ":32103", - null))); - // when - final Map servers = runtimeInfo.getServers(); + Map servers = runtimeInfo.getServers(); - // then - assertEquals(servers, expectedServers); + assertEquals(servers, serversMap); + verify(serverEvaluationStrategy).getServers(eq(containerInfo), eq(DEFAULT_HOSTNAME), serversCaptor.capture()); + assertEquals(serversCaptor.getValue(), serversToMap(commonSystemServersConfigs, + devSystemServersConfigs, + serversConfFromMachineConf)); + } + + private Map serversToMap(Collection serverConfigs, + Collection serverConfigs2) { + return serversToMap(Stream.concat(serverConfigs.stream(), serverConfigs2.stream())); + } + + private Map serversToMap(Collection serverConfigs, + Collection serverConfigs2, + Collection serverConfigs3) { + return serversToMap(Stream.concat(Stream.concat(serverConfigs.stream(), serverConfigs2.stream()), + serverConfigs3.stream())); + } + + private Map serversToMap(Stream serverConfigs) { + return serverConfigs.collect(toMap(srvConf -> srvConf.getPort().contains("/") ? + srvConf.getPort() : + srvConf.getPort() + "/tcp", + ServerConfImpl::new)); } } 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 fcac39c12b39..af22597eb4b0 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 @@ -8,22 +8,8 @@ * Contributors: * Red Hat Inc. - initial API and implementation *******************************************************************************/ - package org.eclipse.che.plugin.docker.machine; -import org.testng.annotations.Test; - -import static org.eclipse.che.plugin.docker.machine.ServerEvaluationStrategy.SERVER_CONF_LABEL_PATH_KEY; -import static org.eclipse.che.plugin.docker.machine.ServerEvaluationStrategy.SERVER_CONF_LABEL_PROTOCOL_KEY; -import static org.eclipse.che.plugin.docker.machine.ServerEvaluationStrategy.SERVER_CONF_LABEL_REF_KEY; -import static org.mockito.Mockito.when; -import static org.testng.Assert.assertEquals; - -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; import org.eclipse.che.api.machine.server.model.impl.ServerImpl; import org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl; @@ -35,13 +21,30 @@ import org.mockito.testng.MockitoTestNGListener; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Listeners; +import org.testng.annotations.Test; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.eclipse.che.plugin.docker.machine.ServerEvaluationStrategy.SERVER_CONF_LABEL_PATH_KEY; +import static org.eclipse.che.plugin.docker.machine.ServerEvaluationStrategy.SERVER_CONF_LABEL_PROTOCOL_KEY; +import static org.eclipse.che.plugin.docker.machine.ServerEvaluationStrategy.SERVER_CONF_LABEL_REF_KEY; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; + +/** + * @author Angel Misevski + * @author Alexander Garagatyi + */ @Listeners(MockitoTestNGListener.class) public class ServerEvaluationStrategyTest { - private static final String ALL_IP_ADDRESS = "0.0.0.0"; - private static final String CONTAINERINFO_GATEWAY = "172.17.0.1"; - private static final String DEFAULT_HOSTNAME = "localhost"; + private static final String ALL_IP_ADDRESS = "0.0.0.0"; + private static final String DEFAULT_HOSTNAME = "localhost"; @Mock private ContainerInfo containerInfo; @@ -58,30 +61,63 @@ public class ServerEvaluationStrategyTest { @BeforeMethod public void setUp() { - strategy = new DefaultServerEvaluationStrategy(null, null); + strategy = spy(new TestServerEvaluationStrategyImpl()); serverConfs = new HashMap<>(); labels = new HashMap<>(); when(containerInfo.getConfig()).thenReturn(containerConfig); when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); - when(networkSettings.getGateway()).thenReturn(CONTAINERINFO_GATEWAY); when(containerConfig.getLabels()).thenReturn(labels); } - private Map> getPorts() { + @Test + public void shouldConvertAddressAndExposedPortsInMapOfExposedPortToAddressPort() throws Exception { + // given Map> ports = new HashMap<>(); ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS) .withHostPort("32100"))); ports.put("9090/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS) .withHostPort("32101"))); - return ports; + Map expected = new HashMap<>(); + expected.put("8080/tcp", DEFAULT_HOSTNAME + ":" + "32100"); + expected.put("9090/udp", DEFAULT_HOSTNAME + ":" + "32101"); + + // when + Map actual = strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports); + + // then + assertEquals(actual, expected); + } + + @Test + public void shouldIgnoreMultiplePortBindingEntries() throws Exception { + // given + Map> ports = new HashMap<>(); + ports.put("8080/tcp", Arrays.asList(new PortBinding().withHostIp(ALL_IP_ADDRESS) + .withHostPort("32100"), + new PortBinding().withHostIp(DEFAULT_HOSTNAME) + .withHostPort("32102"))); + ports.put("9090/udp", Arrays.asList(new PortBinding().withHostIp(ALL_IP_ADDRESS) + .withHostPort("32101"), + new PortBinding().withHostIp(ALL_IP_ADDRESS) + .withHostPort("32103"), + new PortBinding().withHostIp(DEFAULT_HOSTNAME) + .withHostPort("32104"))); + Map expected = new HashMap<>(); + expected.put("8080/tcp", DEFAULT_HOSTNAME + ":" + "32100"); + expected.put("9090/udp", DEFAULT_HOSTNAME + ":" + "32101"); + + // when + Map actual = strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports); + + // then + assertEquals(actual, expected); } @Test public void shouldReturnServerForEveryExposedPort() throws Exception { // given - Map> ports = getPorts(); - when(networkSettings.getPorts()).thenReturn(ports); + Map> ports = prepareStrategyAndContainerInfoMocks(); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, @@ -93,20 +129,21 @@ public void shouldReturnServerForEveryExposedPort() throws Exception { @Test public void shouldAddDefaultReferenceIfReferenceIsNotSet() throws Exception { // given - Map> ports = getPorts(); - when(networkSettings.getPorts()).thenReturn(ports); + prepareStrategyAndContainerInfoMocks(); final HashMap expectedServers = new HashMap<>(); expectedServers.put("8080/tcp", new ServerImpl("Server-8080-tcp", null, - CONTAINERINFO_GATEWAY + ":32100", + DEFAULT_HOSTNAME + ":32100", null, - new ServerPropertiesImpl(null, CONTAINERINFO_GATEWAY + ":32100", null))); + new ServerPropertiesImpl(null, DEFAULT_HOSTNAME + ":32100", + null))); expectedServers.put("9090/udp", new ServerImpl("Server-9090-udp", null, - CONTAINERINFO_GATEWAY + ":32101", + DEFAULT_HOSTNAME + ":32101", null, - new ServerPropertiesImpl(null, CONTAINERINFO_GATEWAY + ":32101", null))); + new ServerPropertiesImpl(null, DEFAULT_HOSTNAME + ":32101", + null))); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); @@ -117,8 +154,7 @@ public void shouldAddDefaultReferenceIfReferenceIsNotSet() throws Exception { @Test public void shouldAddRefUrlProtocolPathToServerFromMachineConfig() throws Exception { // given - Map> ports = getPorts(); - when(networkSettings.getPorts()).thenReturn(ports); + prepareStrategyAndContainerInfoMocks(); serverConfs.put("8080/tcp", new ServerConfImpl("myserv1", "8080/tcp", "http", null)); serverConfs.put("9090/udp", new ServerConfImpl("myserv2", "9090/udp", "dhcp", "/some/path")); @@ -126,18 +162,20 @@ public void shouldAddRefUrlProtocolPathToServerFromMachineConfig() throws Except final HashMap expectedServers = new HashMap<>(); expectedServers.put("8080/tcp", new ServerImpl("myserv1", "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100", - new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100"))); + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + ":32100", + new ServerPropertiesImpl(null, + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + + ":32100"))); expectedServers.put("9090/udp", new ServerImpl("myserv2", "dhcp", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path", + DEFAULT_HOSTNAME + ":32101", + "dhcp://" + DEFAULT_HOSTNAME + ":32101/some/path", new ServerPropertiesImpl("/some/path", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path"))); + DEFAULT_HOSTNAME + ":32101", + "dhcp://" + DEFAULT_HOSTNAME + + ":32101/some/path"))); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); @@ -149,27 +187,28 @@ public void shouldAddRefUrlProtocolPathToServerFromMachineConfig() throws Except @Test public void shouldAllowToUsePortFromMachineConfigWithoutTransportProtocol() throws Exception { // given - Map> ports = getPorts(); - when(networkSettings.getPorts()).thenReturn(ports); + prepareStrategyAndContainerInfoMocks(); - serverConfs.put("8080", new ServerConfImpl("myserv1", "8080", "http", "/some")); + serverConfs.put("8080", new ServerConfImpl("myserv1", "8080", "http", "/some")); serverConfs.put("9090/udp", new ServerConfImpl("myserv1-tftp", "9090/udp", "tftp", "/path")); final HashMap expectedServers = new HashMap<>(); expectedServers.put("8080/tcp", new ServerImpl("myserv1", "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some", + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + ":32100/some", new ServerPropertiesImpl("/some", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some"))); + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + + ":32100/some"))); expectedServers.put("9090/udp", new ServerImpl("myserv1-tftp", "tftp", - CONTAINERINFO_GATEWAY + ":32101", - "tftp://" + CONTAINERINFO_GATEWAY + ":32101/path", + DEFAULT_HOSTNAME + ":32101", + "tftp://" + DEFAULT_HOSTNAME + ":32101/path", new ServerPropertiesImpl("/path", - CONTAINERINFO_GATEWAY + ":32101", - "tftp://" + CONTAINERINFO_GATEWAY + ":32101/path"))); + DEFAULT_HOSTNAME + ":32101", + "tftp://" + DEFAULT_HOSTNAME + + ":32101/path"))); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); @@ -181,36 +220,37 @@ public void shouldAllowToUsePortFromMachineConfigWithoutTransportProtocol() thro @Test public void shouldAddRefUrlPathToServerFromLabels() throws Exception { // given - Map> ports = getPorts(); - when(networkSettings.getPorts()).thenReturn(ports); + Map> ports = prepareStrategyAndContainerInfoMocks(); Map labels = new HashMap<>(); when(containerConfig.getLabels()).thenReturn(labels); - ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) + ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS) .withHostPort("32100"))); - ports.put("9090/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32101"))); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/tcp"), "myserv1"); + ports.put("9090/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS) + .withHostPort("32101"))); + labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/tcp"), "myserv1"); labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080/tcp"), "http"); - labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "8080/tcp"), "/some/path"); + labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "8080/tcp"), "/some/path"); labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "9090/udp"), "dhcp"); - labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "9090/udp"), "some/path"); + labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "9090/udp"), "some/path"); final HashMap expectedServers = new HashMap<>(); expectedServers.put("8080/tcp", new ServerImpl("myserv1", "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path", + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + ":32100/some/path", new ServerPropertiesImpl("/some/path", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path"))); + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + + ":32100/some/path"))); expectedServers.put("9090/udp", new ServerImpl("Server-9090-udp", "dhcp", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path", + DEFAULT_HOSTNAME + ":32101", + "dhcp://" + DEFAULT_HOSTNAME + ":32101/some/path", new ServerPropertiesImpl("some/path", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path"))); + DEFAULT_HOSTNAME + ":32101", + "dhcp://" + DEFAULT_HOSTNAME + + ":32101/some/path"))); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); @@ -222,30 +262,31 @@ public void shouldAddRefUrlPathToServerFromLabels() throws Exception { @Test public void shouldAllowToUsePortFromDockerLabelsWithoutTransportProtocol() throws Exception { // given - Map> ports = getPorts(); - when(networkSettings.getPorts()).thenReturn(ports); + prepareStrategyAndContainerInfoMocks(); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080"), "myserv1"); + labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080"), "myserv1"); labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080"), "http"); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "9090/udp"), "myserv1-tftp"); + labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "9090/udp"), "myserv1-tftp"); labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "9090/udp"), "tftp"); final HashMap expectedServers = new HashMap<>(); expectedServers.put("8080/tcp", new ServerImpl("myserv1", "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100", + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + ":32100", new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100"))); + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + + ":32100"))); expectedServers.put("9090/udp", new ServerImpl("myserv1-tftp", "tftp", - CONTAINERINFO_GATEWAY + ":32101", - "tftp://" + CONTAINERINFO_GATEWAY + ":32101", + DEFAULT_HOSTNAME + ":32101", + "tftp://" + DEFAULT_HOSTNAME + ":32101", new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32101", - "tftp://" + CONTAINERINFO_GATEWAY + ":32101"))); + DEFAULT_HOSTNAME + ":32101", + "tftp://" + DEFAULT_HOSTNAME + + ":32101"))); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); @@ -257,33 +298,34 @@ public void shouldAllowToUsePortFromDockerLabelsWithoutTransportProtocol() throw @Test public void shouldPreferMachineConfOverDockerLabels() throws Exception { // given - Map> ports = getPorts(); - when(networkSettings.getPorts()).thenReturn(ports); + prepareStrategyAndContainerInfoMocks(); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/tcp"), "myserv1label"); + labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "8080/tcp"), "myserv1label"); labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "8080/tcp"), "https"); - labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "9090/udp"), "myserv2label"); + labels.put(String.format(SERVER_CONF_LABEL_REF_KEY, "9090/udp"), "myserv2label"); labels.put(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, "9090/udp"), "dhcp"); - labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "9090/udp"), "/path"); + labels.put(String.format(SERVER_CONF_LABEL_PATH_KEY, "9090/udp"), "/path"); serverConfs.put("8080/tcp", new ServerConfImpl("myserv1conf", "8080/tcp", "http", null)); final HashMap expectedServers = new HashMap<>(); expectedServers.put("8080/tcp", new ServerImpl("myserv1conf", "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100", + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + ":32100", new ServerPropertiesImpl(null, - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100"))); + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + + ":32100"))); expectedServers.put("9090/udp", new ServerImpl("myserv2label", "dhcp", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/path", + DEFAULT_HOSTNAME + ":32101", + "dhcp://" + DEFAULT_HOSTNAME + ":32101/path", new ServerPropertiesImpl("/path", - CONTAINERINFO_GATEWAY + ":32101", - "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/path"))); + DEFAULT_HOSTNAME + ":32101", + "dhcp://" + DEFAULT_HOSTNAME + + ":32101/path"))); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); @@ -295,27 +337,28 @@ public void shouldPreferMachineConfOverDockerLabels() throws Exception { @Test public void shouldAddPathCorrectlyWithoutLeadingSlash() throws Exception { // given - Map> ports = getPorts(); - when(networkSettings.getPorts()).thenReturn(ports); + prepareStrategyAndContainerInfoMocks(); - serverConfs.put("8080", new ServerConfImpl("myserv1", "8080", "http", "some")); + serverConfs.put("8080", new ServerConfImpl("myserv1", "8080", "http", "some")); serverConfs.put("9090/udp", new ServerConfImpl("myserv1-tftp", "9090/udp", "tftp", "some/path")); final HashMap expectedServers = new HashMap<>(); expectedServers.put("8080/tcp", new ServerImpl("myserv1", "http", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some", + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + ":32100/some", new ServerPropertiesImpl("some", - CONTAINERINFO_GATEWAY + ":32100", - "http://" + CONTAINERINFO_GATEWAY + ":32100/some"))); + DEFAULT_HOSTNAME + ":32100", + "http://" + DEFAULT_HOSTNAME + + ":32100/some"))); expectedServers.put("9090/udp", new ServerImpl("myserv1-tftp", "tftp", - CONTAINERINFO_GATEWAY + ":32101", - "tftp://" + CONTAINERINFO_GATEWAY + ":32101/some/path", + DEFAULT_HOSTNAME + ":32101", + "tftp://" + DEFAULT_HOSTNAME + ":32101/some/path", new ServerPropertiesImpl("some/path", - CONTAINERINFO_GATEWAY + ":32101", - "tftp://" + CONTAINERINFO_GATEWAY + ":32101/some/path"))); + DEFAULT_HOSTNAME + ":32101", + "tftp://" + DEFAULT_HOSTNAME + + ":32101/some/path"))); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); @@ -323,4 +366,35 @@ public void shouldAddPathCorrectlyWithoutLeadingSlash() throws Exception { // then assertEquals(servers, expectedServers); } + + private Map> prepareStrategyAndContainerInfoMocks() { + Map> ports = new HashMap<>(); + ports.put("8080/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS) + .withHostPort("32100"))); + ports.put("9090/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS) + .withHostPort("32101"))); + when(networkSettings.getPorts()).thenReturn(ports); + Map exposedPortsToAddressPorts = + strategy.getExposedPortsToAddressPorts(DEFAULT_HOSTNAME, ports); + when(strategy.getExternalAddressesAndPorts(containerInfo, DEFAULT_HOSTNAME)) + .thenReturn(exposedPortsToAddressPorts); + when(strategy.getInternalAddressesAndPorts(containerInfo, DEFAULT_HOSTNAME)) + .thenReturn(exposedPortsToAddressPorts); + + return ports; + } + + private static class TestServerEvaluationStrategyImpl extends ServerEvaluationStrategy { + @Override + protected Map getInternalAddressesAndPorts(ContainerInfo containerInfo, + String internalAddress) { + return null; + } + + @Override + protected Map getExternalAddressesAndPorts(ContainerInfo containerInfo, + String internalAddress) { + return null; + } + } }