Search in sources :

Example 6 with ServerImpl

use of org.eclipse.che.api.machine.server.model.impl.ServerImpl in project che by eclipse.

the class DefaultServerEvaluationStrategyTest method getExpectedServers.

private Map<String, ServerImpl> getExpectedServers(String externalAddress, String internalAddress) {
    String port1;
    String port2;
    port1 = ":32100";
    port2 = ":32103";
    Map<String, ServerImpl> 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;
}
Also used : ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) HashMap(java.util.HashMap) ServerPropertiesImpl(org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl)

Example 7 with ServerImpl

use of org.eclipse.che.api.machine.server.model.impl.ServerImpl in project che by eclipse.

the class DockerInstanceRuntimeInfoTest method shouldPassCommonServerConfigsOnGetServersForNonDevMachine.

@Test
public void shouldPassCommonServerConfigsOnGetServersForNonDevMachine() throws Exception {
    // given
    Set<ServerConf> 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<ServerConf> devSystemServersConfigs = singleton(new ServerConfImpl("devSysServer1-tcp", "4305/tcp", "http", null));
    List<ServerConf> 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, DEFAULT_HOSTNAME, provider, devSystemServersConfigs, commonSystemServersConfigs);
    // when
    Map<String, ServerImpl> servers = runtimeInfo.getServers();
    // then
    assertEquals(servers, serversMap);
    verify(serverEvaluationStrategy).getServers(eq(containerInfo), eq(DEFAULT_HOSTNAME), serversCaptor.capture());
    assertEquals(serversCaptor.getValue(), serversToMap(commonSystemServersConfigs, serversConfFromMachineConf));
}
Also used : ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) Matchers.anyString(org.mockito.Matchers.anyString) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 8 with ServerImpl

use of org.eclipse.che.api.machine.server.model.impl.ServerImpl in project che by eclipse.

the class ServerEvaluationStrategy method getServers.

/**
     * Constructs a map of {@link ServerImpl} from provided parameters, using selected strategy
     * for evaluating addresses and ports.
     *
     * <p>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.
     * @return a Map of the servers exposed by the container.
     */
public Map<String, ServerImpl> getServers(ContainerInfo containerInfo, String internalHost, Map<String, ServerConfImpl> serverConfMap) {
    Map<String, List<PortBinding>> portBindings;
    Map<String, String> labels = Collections.emptyMap();
    if (containerInfo.getNetworkSettings() != null && containerInfo.getNetworkSettings().getPorts() != null) {
        portBindings = containerInfo.getNetworkSettings().getPorts();
    } else {
        // If we can't get PortBindings, we can't return servers.
        return Collections.emptyMap();
    }
    if (containerInfo.getConfig() != null && containerInfo.getConfig().getLabels() != null) {
        labels = containerInfo.getConfig().getLabels();
    }
    Map<String, String> internalAddressesAndPorts = getInternalAddressesAndPorts(containerInfo, internalHost);
    Map<String, String> externalAddressesAndPorts = getExternalAddressesAndPorts(containerInfo, internalHost);
    Map<String, ServerImpl> servers = new LinkedHashMap<>();
    for (String portProtocol : portBindings.keySet()) {
        String internalAddressAndPort = internalAddressesAndPorts.get(portProtocol);
        String externalAddressAndPort = externalAddressesAndPorts.get(portProtocol);
        ServerConfImpl serverConf = getServerConfImpl(portProtocol, labels, serverConfMap);
        // Add protocol and path to internal/external address, if applicable
        String internalUrl = null;
        String externalUrl = null;
        if (serverConf.getProtocol() != null) {
            String pathSuffix = serverConf.getPath();
            if (pathSuffix != null && !pathSuffix.isEmpty()) {
                if (pathSuffix.charAt(0) != '/') {
                    pathSuffix = "/" + pathSuffix;
                }
            } else {
                pathSuffix = "";
            }
            internalUrl = serverConf.getProtocol() + "://" + internalAddressAndPort + pathSuffix;
            externalUrl = serverConf.getProtocol() + "://" + externalAddressAndPort + pathSuffix;
        }
        ServerProperties properties = new ServerPropertiesImpl(serverConf.getPath(), internalAddressAndPort, internalUrl);
        servers.put(portProtocol, new ServerImpl(serverConf.getRef(), serverConf.getProtocol(), externalAddressAndPort, externalUrl, properties));
    }
    return servers;
}
Also used : ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) ServerProperties(org.eclipse.che.api.core.model.machine.ServerProperties) List(java.util.List) ServerPropertiesImpl(org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with ServerImpl

use of org.eclipse.che.api.machine.server.model.impl.ServerImpl in project che by eclipse.

the class DockerInstanceRuntimeInfoTest method shouldPassCommonAndDevServerConfigsOnGetServersForNonDevMachine.

@Test
public void shouldPassCommonAndDevServerConfigsOnGetServersForNonDevMachine() throws Exception {
    Set<ServerConf> 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"));
    Set<ServerConf> 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"));
    List<ServerConf> 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, DEFAULT_HOSTNAME, provider, devSystemServersConfigs, commonSystemServersConfigs);
    Map<String, ServerImpl> servers = runtimeInfo.getServers();
    assertEquals(servers, serversMap);
    verify(serverEvaluationStrategy).getServers(eq(containerInfo), eq(DEFAULT_HOSTNAME), serversCaptor.capture());
    assertEquals(serversCaptor.getValue(), serversToMap(commonSystemServersConfigs, devSystemServersConfigs, serversConfFromMachineConf));
}
Also used : ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) Matchers.anyString(org.mockito.Matchers.anyString) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 10 with ServerImpl

use of org.eclipse.che.api.machine.server.model.impl.ServerImpl in project che by eclipse.

the class ServerEvaluationStrategyTest method shouldPreferMachineConfOverDockerLabels.

@Test
public void shouldPreferMachineConfOverDockerLabels() throws Exception {
    // given
    prepareStrategyAndContainerInfoMocks();
    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<String, ServerImpl> expectedServers = new HashMap<>();
    expectedServers.put("8080/tcp", new ServerImpl("myserv1conf", "http", DEFAULT_HOSTNAME + ":32100", "http://" + DEFAULT_HOSTNAME + ":32100", new ServerPropertiesImpl(null, DEFAULT_HOSTNAME + ":32100", "http://" + DEFAULT_HOSTNAME + ":32100")));
    expectedServers.put("9090/udp", new ServerImpl("myserv2label", "dhcp", DEFAULT_HOSTNAME + ":32101", "dhcp://" + DEFAULT_HOSTNAME + ":32101/path", new ServerPropertiesImpl("/path", DEFAULT_HOSTNAME + ":32101", "dhcp://" + DEFAULT_HOSTNAME + ":32101/path")));
    // when
    final Map<String, ServerImpl> servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs);
    // then
    assertEquals(servers, expectedServers);
}
Also used : ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) HashMap(java.util.HashMap) ServerPropertiesImpl(org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) Test(org.testng.annotations.Test)

Aggregations

ServerImpl (org.eclipse.che.api.machine.server.model.impl.ServerImpl)15 ServerPropertiesImpl (org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl)11 HashMap (java.util.HashMap)10 Test (org.testng.annotations.Test)10 ServerConfImpl (org.eclipse.che.api.machine.server.model.impl.ServerConfImpl)7 HashSet (java.util.HashSet)3 ServerConf (org.eclipse.che.api.core.model.machine.ServerConf)3 Matchers.anyString (org.mockito.Matchers.anyString)3 List (java.util.List)2 MachineRuntimeInfoImpl (org.eclipse.che.api.machine.server.model.impl.MachineRuntimeInfoImpl)2 Response (com.jayway.restassured.response.Response)1 URI (java.net.URI)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 ServerProperties (org.eclipse.che.api.core.model.machine.ServerProperties)1 MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)1 MachineImpl (org.eclipse.che.api.machine.server.model.impl.MachineImpl)1 MachineLimitsImpl (org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl)1 MachineSourceImpl (org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl)1