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 59bdd3ad1de..5a710345b71 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 @@ -162,14 +162,17 @@ public Map getServers(ContainerInfo containerInfo, private ServerConfImpl getServerConfImpl(String portProtocol, Map labels, Map serverConfMap) { + // Label can be specified without protocol -- e.g. 4401 refers to 4401/tcp + String port = portProtocol.substring(0, portProtocol.length() - 4); + ServerConfImpl serverConf; // provided serverConf map takes precedence if (serverConfMap.get(portProtocol) != null) { serverConf = serverConfMap.get(portProtocol); + } else if (serverConfMap.get(port) != null) { + serverConf = serverConfMap.get(port); } else { String ref, protocol, path; - // Label can be specified without protocol -- e.g. 4401 refers to 4401/tcp - String port = portProtocol.substring(0, portProtocol.length() - 4); ref = labels.get(String.format(SERVER_CONF_LABEL_REF_KEY, portProtocol)); if (ref == null) { 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 new file mode 100644 index 00000000000..b1adacec1ad --- /dev/null +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategyTest.java @@ -0,0 +1,223 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * 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; +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.Mock; +import org.mockito.testng.MockitoTestNGListener; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +@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; + + private ServerEvaluationStrategy strategy; + + private Map serverConfs; + + private Map> ports; + + @BeforeMethod + public void setUp() { + + serverConfs = new HashMap<>(); + 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<>(); + ports.put("4301/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) + .withHostPort("32100"))); + ports.put("4305/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) + .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 + * @throws Exception + */ + @Test + public void defaultStrategyShouldUseContainerInfoWhenInternalIpPropertyIsNull() throws Exception { + // given + strategy = new DefaultServerEvaluationStrategy(null, null); + + final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, + CONTAINERINFO_GATEWAY, + false); + + // when + final Map servers = strategy.getServers(containerInfo, + null, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + /** + * 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. + * @throws Exception + */ + @Test + public void defaultStrategyShouldUseInternalHostWhenBothIpPropertyAndContainerInfoAreNull() throws Exception { + // given + strategy = new DefaultServerEvaluationStrategy(null, null); + when(networkSettings.getGateway()).thenReturn(""); + + final Map expectedServers = getExpectedServers(DEFAULT_HOSTNAME, + DEFAULT_HOSTNAME, + false); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + /** + * Test: If che.docker.ip.external is not null, that should take precedence for external address. + * @throws Exception + */ + @Test + public void defaultStrategyShouldUseExtenalIpPropertyWhenAvailable() throws Exception { + // given + strategy = new DefaultServerEvaluationStrategy(null, CHE_DOCKER_IP_EXTERNAL); + + final Map expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, + CONTAINERINFO_GATEWAY, + false); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + /** + * Test: If che.docker.ip.external is null, should use containerInfo.getGateway() + * @throws Exception + */ + @Test + public void defaultStrategyShouldUseContainerInfoForExternalWhenPropertyIsNull() throws Exception { + // given + strategy = new DefaultServerEvaluationStrategy(null, null); + + final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, + CONTAINERINFO_GATEWAY, + false); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + private Map getExpectedServers(String externalAddress, + String internalAddress, + boolean useExposedPorts) { + String port1; + String port2; + if (useExposedPorts) { + port1 = ":4301"; + port2 = ":4305"; + } else { + port1 = ":32100"; + port2 = ":32103"; + } + Map expectedServers = new HashMap<>(); + expectedServers.put("4301/tcp", new ServerImpl("sysServer1-tcp", + "http", + externalAddress + ":32100", + "http://" + externalAddress + ":32100/some/path1", + new ServerPropertiesImpl("/some/path1", + internalAddress + port1, + "http://" + internalAddress + port1 + "/some/path1"))); + expectedServers.put("4305/udp", new ServerImpl("devSysServer1-udp", + null, + externalAddress + ":32103", + null, + new ServerPropertiesImpl("some/path4", + internalAddress + port2, + null))); + return expectedServers; + } +} diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/LocalDockerServerEvaluationStrategyTest.java b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/LocalDockerServerEvaluationStrategyTest.java new file mode 100644 index 00000000000..256b999a21c --- /dev/null +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/LocalDockerServerEvaluationStrategyTest.java @@ -0,0 +1,246 @@ +/******************************************************************************* + * Copyright (c) 2016 Red Hat Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * 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; +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.Mock; +import org.mockito.testng.MockitoTestNGListener; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +@Listeners(MockitoTestNGListener.class) +public class LocalDockerServerEvaluationStrategyTest { + + 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; + + private ServerEvaluationStrategy strategy; + + private Map serverConfs; + + private Map> ports; + + @BeforeMethod + public void setUp() { + + serverConfs = new HashMap<>(); + 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<>(); + ports.put("4301/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) + .withHostPort("32100"))); + ports.put("4305/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) + .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: local docker strategy should use internal container address when it can. + * @throws Exception + */ + @Test + public void localDockerStrategyShouldUseContainerIpAddressWhenAvailable() throws Exception { + // given + strategy = new LocalDockerServerEvaluationStrategy(null, null); + + final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, + CONTAINERINFO_IP_ADDRESS, + true); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + /** + * Test: local docker strategy should ignore che.docker.ip property. + * @throws Exception + */ + @Test + public void localDockerStrategyShouldIgnoreCheDockerIpProperty() throws Exception { + // given + strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, null); + + final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, + CONTAINERINFO_IP_ADDRESS, + true); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + /** + * Test: local docker strategy should use passed parameter internalHost when containerInfo.getIpAddress() is null. + * @throws Exception + */ + @Test + public void localDockerStrategyShouldUseProvidedInternalHostWhenContainerInfoUnavailable() throws Exception { + // given + strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, null); + when(networkSettings.getIpAddress()).thenReturn(""); + + final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, + DEFAULT_HOSTNAME, + false); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + /** + * Test: local docker strategy should let che.docker.ip.external take precedence if available + * @throws Exception + */ + @Test + public void localDockerStrategyShouldUseExternalDockerIpPropertyIfAvailable() throws Exception { + // given + strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, CHE_DOCKER_IP_EXTERNAL); + + final Map expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, + CONTAINERINFO_IP_ADDRESS, + true); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + /** + * Test: local docker strategy should use containerInfo for externalHost if property is null + * @throws Exception + */ + @Test + public void localDockerStrategyShouldUseContainerInfoForExternalWhenPropertyIsNull() throws Exception { + // given + strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, null); + + final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, + CONTAINERINFO_IP_ADDRESS, + true); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + /** + * Test: local docker strategy should use containerInfo for externalHost if property is null + * @throws Exception + */ + @Test + public void localDockerStrategyShouldUseInternalHostWhenContainerInfoIsUnavailable() throws Exception { + // given + strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, null); + when(networkSettings.getGateway()).thenReturn(""); + + final Map expectedServers = getExpectedServers(DEFAULT_HOSTNAME, + CONTAINERINFO_IP_ADDRESS, + true); + + // when + final Map servers = strategy.getServers(containerInfo, + DEFAULT_HOSTNAME, + serverConfs); + + // then + assertEquals(servers, expectedServers); + } + + private Map getExpectedServers(String externalAddress, + String internalAddress, + boolean useExposedPorts) { + String port1; + String port2; + if (useExposedPorts) { + port1 = ":4301"; + port2 = ":4305"; + } else { + port1 = ":32100"; + port2 = ":32103"; + } + Map expectedServers = new HashMap<>(); + expectedServers.put("4301/tcp", new ServerImpl("sysServer1-tcp", + "http", + externalAddress + ":32100", + "http://" + externalAddress + ":32100/some/path1", + new ServerPropertiesImpl("/some/path1", + internalAddress + port1, + "http://" + internalAddress + port1 + "/some/path1"))); + expectedServers.put("4305/udp", new ServerImpl("devSysServer1-udp", + null, + externalAddress + ":32103", + null, + new ServerPropertiesImpl("some/path4", + internalAddress + port2, + null))); + return expectedServers; + } + +} 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 24d6843ab81..01a9ccd25e1 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 @@ -13,6 +13,9 @@ 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; @@ -21,7 +24,6 @@ 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; @@ -37,322 +39,288 @@ @Listeners(MockitoTestNGListener.class) public class ServerEvaluationStrategyTest { - 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; - private ServerEvaluationStrategy strategy; - private Map serverConfs; - private Map> ports; + private Map labels; + + private ServerEvaluationStrategy strategy; @BeforeMethod public void setUp() { - + strategy = new DefaultServerEvaluationStrategy(null, null); serverConfs = new HashMap<>(); - 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<>(); - ports.put("4301/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32100"))); - ports.put("4305/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) - .withHostPort("32103"))); + labels = new HashMap<>(); + when(containerInfo.getConfig()).thenReturn(containerConfig); 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()); + when(containerConfig.getLabels()).thenReturn(labels); } - /** - * 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 - * @throws Exception - */ - @Test - public void defaultStrategyShouldUseContainerInfoWhenInternalIpPropertyIsNull() throws Exception { - // given - strategy = new DefaultServerEvaluationStrategy(null, null); - - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - CONTAINERINFO_GATEWAY, - false); - - // when - final Map servers = strategy.getServers(containerInfo, - null, - serverConfs); - - // then - assertEquals(servers, expectedServers); - } - - /** - * 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. - * @throws Exception - */ - @Test - public void defaultStrategyShouldUseInternalHostWhenBothIpPropertyAndContainerInfoAreNull() throws Exception { - // given - strategy = new DefaultServerEvaluationStrategy(null, null); - when(networkSettings.getGateway()).thenReturn(""); - - final Map expectedServers = getExpectedServers(DEFAULT_HOSTNAME, - DEFAULT_HOSTNAME, - false); - - // when - final Map servers = strategy.getServers(containerInfo, - DEFAULT_HOSTNAME, - serverConfs); - - // then - assertEquals(servers, expectedServers); + private Map> getPorts() { + 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; } - /** - * Test: If che.docker.ip.external is not null, that should take precedence for external address. - * @throws Exception - */ @Test - public void defaultStrategyShouldUseExtenalIpPropertyWhenAvailable() throws Exception { + public void shouldReturnServerForEveryExposedPort() throws Exception { // given - strategy = new DefaultServerEvaluationStrategy(null, CHE_DOCKER_IP_EXTERNAL); - - final Map expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, - CONTAINERINFO_GATEWAY, - false); - + Map> ports = getPorts(); + when(networkSettings.getPorts()).thenReturn(ports); // when final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); - // then - assertEquals(servers, expectedServers); + assertEquals(servers.keySet(), ports.keySet()); } - /** - * Test: If che.docker.ip.external is null, should use containerInfo.getGateway() - * @throws Exception - */ @Test - public void defaultStrategyShouldUseContainerInfoForExternalWhenPropertyIsNull() throws Exception { + public void shouldAddDefaultReferenceIfReferenceIsNotSet() throws Exception { // given - strategy = new DefaultServerEvaluationStrategy(null, null); - - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - CONTAINERINFO_GATEWAY, - false); + Map> ports = getPorts(); + when(networkSettings.getPorts()).thenReturn(ports); + 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("9090/udp", new ServerImpl("Server-9090-udp", + null, + CONTAINERINFO_GATEWAY + ":32101", + null, + new ServerPropertiesImpl(null, CONTAINERINFO_GATEWAY + ":32101", null))); // when - final Map servers = strategy.getServers(containerInfo, - DEFAULT_HOSTNAME, - serverConfs); + final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); // then assertEquals(servers, expectedServers); } - /** - * Test: local docker strategy should use internal container address when it can. - * @throws Exception - */ @Test - public void localDockerStrategyShouldUseContainerIpAddressWhenAvailable() throws Exception { + public void shouldAddRefUrlProtocolPathToServerFromMachineConfig() throws Exception { // given - strategy = new LocalDockerServerEvaluationStrategy(null, null); + Map> ports = getPorts(); + when(networkSettings.getPorts()).thenReturn(ports); - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - CONTAINERINFO_IP_ADDRESS, - true); + serverConfs.put("8080/tcp", new ServerConfImpl("myserv1", "8080/tcp", "http", null)); + serverConfs.put("9090/udp", new ServerConfImpl("myserv2", "9090/udp", "dhcp", "/some/path")); + + 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("9090/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"))); // when - final Map servers = strategy.getServers(containerInfo, - DEFAULT_HOSTNAME, - serverConfs); + final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); // then assertEquals(servers, expectedServers); } - /** - * Test: local docker strategy should ignore che.docker.ip property. - * @throws Exception - */ @Test - public void localDockerStrategyShouldIgnoreCheDockerIpProperty() throws Exception { + public void shouldAllowToUsePortFromMachineConfigWithoutTransportProtocol() throws Exception { // given - strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, null); + Map> ports = getPorts(); + when(networkSettings.getPorts()).thenReturn(ports); - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - CONTAINERINFO_IP_ADDRESS, - true); + 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", + new ServerPropertiesImpl("/some", + CONTAINERINFO_GATEWAY + ":32100", + "http://" + CONTAINERINFO_GATEWAY + ":32100/some"))); + expectedServers.put("9090/udp", new ServerImpl("myserv1-tftp", + "tftp", + CONTAINERINFO_GATEWAY + ":32101", + "tftp://" + CONTAINERINFO_GATEWAY + ":32101/path", + new ServerPropertiesImpl("/path", + CONTAINERINFO_GATEWAY + ":32101", + "tftp://" + CONTAINERINFO_GATEWAY + ":32101/path"))); // when - final Map servers = strategy.getServers(containerInfo, - DEFAULT_HOSTNAME, - serverConfs); + final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); // then assertEquals(servers, expectedServers); } - /** - * Test: local docker strategy should use passed parameter internalHost when containerInfo.getIpAddress() is null. - * @throws Exception - */ @Test - public void localDockerStrategyShouldUseProvidedInternalHostWhenContainerInfoUnavailable() throws Exception { + public void shouldAddRefUrlPathToServerFromLabels() throws Exception { // given - strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, null); - when(networkSettings.getIpAddress()).thenReturn(""); - - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - DEFAULT_HOSTNAME, - false); + Map> ports = getPorts(); + 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("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_PROTOCOL_KEY, "9090/udp"), "dhcp"); + 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", + new ServerPropertiesImpl("/some/path", + CONTAINERINFO_GATEWAY + ":32100", + "http://" + CONTAINERINFO_GATEWAY + ":32100/some/path"))); + expectedServers.put("9090/udp", new ServerImpl("Server-9090-udp", + "dhcp", + CONTAINERINFO_GATEWAY + ":32101", + "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path", + new ServerPropertiesImpl("some/path", + CONTAINERINFO_GATEWAY + ":32101", + "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/some/path"))); // when - final Map servers = strategy.getServers(containerInfo, - DEFAULT_HOSTNAME, - serverConfs); + final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); // then assertEquals(servers, expectedServers); } - /** - * Test: local docker strategy should let che.docker.ip.external take precedence if available - * @throws Exception - */ @Test - public void localDockerStrategyShouldUseExternalDockerIpPropertyIfAvailable() throws Exception { + public void shouldAllowToUsePortFromDockerLabelsWithoutTransportProtocol() throws Exception { // given - strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, CHE_DOCKER_IP_EXTERNAL); + Map> ports = getPorts(); + when(networkSettings.getPorts()).thenReturn(ports); - final Map expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, - CONTAINERINFO_IP_ADDRESS, - true); + 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_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", + new ServerPropertiesImpl(null, + CONTAINERINFO_GATEWAY + ":32100", + "http://" + CONTAINERINFO_GATEWAY + ":32100"))); + expectedServers.put("9090/udp", new ServerImpl("myserv1-tftp", + "tftp", + CONTAINERINFO_GATEWAY + ":32101", + "tftp://" + CONTAINERINFO_GATEWAY + ":32101", + new ServerPropertiesImpl(null, + CONTAINERINFO_GATEWAY + ":32101", + "tftp://" + CONTAINERINFO_GATEWAY + ":32101"))); // when - final Map servers = strategy.getServers(containerInfo, - DEFAULT_HOSTNAME, - serverConfs); + final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); // then assertEquals(servers, expectedServers); } - /** - * Test: local docker strategy should use containerInfo for externalHost if property is null - * @throws Exception - */ @Test - public void localDockerStrategyShouldUseContainerInfoForExternalWhenPropertyIsNull() throws Exception { + public void shouldPreferMachineConfOverDockerLabels() throws Exception { // given - strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, null); + Map> ports = getPorts(); + when(networkSettings.getPorts()).thenReturn(ports); - final Map expectedServers = getExpectedServers(CONTAINERINFO_GATEWAY, - CONTAINERINFO_IP_ADDRESS, - true); + 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_PROTOCOL_KEY, "9090/udp"), "dhcp"); + 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", + new ServerPropertiesImpl(null, + CONTAINERINFO_GATEWAY + ":32100", + "http://" + CONTAINERINFO_GATEWAY + ":32100"))); + expectedServers.put("9090/udp", new ServerImpl("myserv2label", + "dhcp", + CONTAINERINFO_GATEWAY + ":32101", + "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/path", + new ServerPropertiesImpl("/path", + CONTAINERINFO_GATEWAY + ":32101", + "dhcp://" + CONTAINERINFO_GATEWAY + ":32101/path"))); // when - final Map servers = strategy.getServers(containerInfo, - DEFAULT_HOSTNAME, - serverConfs); + final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); // then assertEquals(servers, expectedServers); } - /** - * Test: local docker strategy should use containerInfo for externalHost if property is null - * @throws Exception - */ @Test - public void localDockerStrategyShouldUseInternalHostWhenContainerInfoIsUnavailable() throws Exception { + public void shouldAddPathCorrectlyWithoutLeadingSlash() throws Exception { // given - strategy = new LocalDockerServerEvaluationStrategy(CHE_DOCKER_IP, null); - when(networkSettings.getGateway()).thenReturn(""); + Map> ports = getPorts(); + when(networkSettings.getPorts()).thenReturn(ports); - final Map expectedServers = getExpectedServers(DEFAULT_HOSTNAME, - CONTAINERINFO_IP_ADDRESS, - true); + 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", + new ServerPropertiesImpl("some", + CONTAINERINFO_GATEWAY + ":32100", + "http://" + CONTAINERINFO_GATEWAY + ":32100/some"))); + expectedServers.put("9090/udp", new ServerImpl("myserv1-tftp", + "tftp", + CONTAINERINFO_GATEWAY + ":32101", + "tftp://" + CONTAINERINFO_GATEWAY + ":32101/some/path", + new ServerPropertiesImpl("some/path", + CONTAINERINFO_GATEWAY + ":32101", + "tftp://" + CONTAINERINFO_GATEWAY + ":32101/some/path"))); // when - final Map servers = strategy.getServers(containerInfo, - DEFAULT_HOSTNAME, - serverConfs); + final Map servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs); // then assertEquals(servers, expectedServers); } - - private Map getExpectedServers(String externalAddress, - String internalAddress, - boolean useExposedPorts) { - String port1; - String port2; - if (useExposedPorts) { - port1 = ":4301"; - port2 = ":4305"; - } else { - port1 = ":32100"; - port2 = ":32103"; - } - Map expectedServers = new HashMap<>(); - expectedServers.put("4301/tcp", new ServerImpl("sysServer1-tcp", - "http", - externalAddress + ":32100", - "http://" + externalAddress + ":32100/some/path1", - new ServerPropertiesImpl("/some/path1", - internalAddress + port1, - "http://" + internalAddress + port1 + "/some/path1"))); - expectedServers.put("4305/udp", new ServerImpl("devSysServer1-udp", - null, - externalAddress + ":32103", - null, - new ServerPropertiesImpl("some/path4", - internalAddress + port2, - null))); - return expectedServers; - } }