Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Upgrade to Selenium 3.12.0 (#574)
Browse files Browse the repository at this point in the history
* Upgrading to Selenium 3.12.0

* Moving logging statement back to debug

* Fix video file name generation.

* Using different port numbers when using plain docker, fixes #577
  • Loading branch information
diemol committed May 17, 2018
1 parent 7c8c44f commit e0e9fe1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@
</developers>

<properties>
<selenium-server.major-minor.version>3.11</selenium-server.major-minor.version>
<selenium-server.major-minor.version>3.12</selenium-server.major-minor.version>
<selenium-server.patch-level.version>0</selenium-server.patch-level.version>
<!-- This needs to match what is in the Selenium Server pom file -->
<jetty.version>9.4.8.v20171121</jetty.version>
<docker-client.version>8.11.2</docker-client.version>
<kubernetes-client.version>3.1.10</kubernetes-client.version>
<kubernetes-client.version>3.1.11</kubernetes-client.version>
<junit.version>4.12</junit.version>
<mockito.version>2.16.0</mockito.version>
<mockito.version>2.18.3</mockito.version>
<awaitility.version>3.1.0</awaitility.version>
<testng.version>6.14.2</testng.version>
<testng.version>6.14.3</testng.version>
<slf4j.version>1.7.25</slf4j.version>
<logstash-logback-encoder.version>5.0</logstash-logback-encoder.version>
<logstash-logback-encoder.version>5.1</logstash-logback-encoder.version>
<maven.compiler.source>1.8</maven.compiler.source>
<ch.qos.logback.version>1.2.3</ch.qos.logback.version>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void stopContainer(String containerId) {
try {
dockerClient.stopContainer(containerId, 5);
} catch (ContainerNotFoundException e) {
logger.info("Container {} does not exist - already shut down?.", containerId);
logger.debug("Container {} does not exist - already shut down?.", containerId);
} catch (DockerException | InterruptedException e) {
logger.warn(nodeId + " Error while stopping the container", e);
ga.trackException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.zalando.ep.zalenium.dashboard;

import com.google.common.base.Strings;
import de.zalando.ep.zalenium.util.CommonProxyUtilities;

import java.util.List;
Expand All @@ -10,7 +11,7 @@
*/
@SuppressWarnings("WeakerAccess")
public class TestInformation {
private static final String TEST_FILE_NAME_TEMPLATE = "{buildName}{proxyName}_{testName}_{browser}_{platform}_{timestamp}_{testStatus}";
private static final String TEST_FILE_NAME_TEMPLATE = "{proxyName}_{testName}_{browser}_{platform}_{timestamp}_{testStatus}";
private static final String FILE_NAME_TEMPLATE = "{fileName}{fileExtension}";
private static final String ZALENIUM_PROXY_NAME = "Zalenium";
private static final String SAUCE_LABS_PROXY_NAME = "SauceLabs";
Expand Down Expand Up @@ -122,7 +123,7 @@ public void setFileExtension(String fileExtension) {

public void buildVideoFileName() {
String buildName;
if ("N/A".equalsIgnoreCase(this.build) || this.build.trim().isEmpty()) {
if ("N/A".equalsIgnoreCase(this.build) || Strings.isNullOrEmpty(this.build)) {
buildName = "";
} else {
buildName = this.build.replaceAll("[^a-zA-Z0-9]", "_") + "/";
Expand All @@ -136,7 +137,7 @@ public void buildVideoFileName() {
.replace("{timestamp}", commonProxyUtilities.getCurrentDateAndTimeFormatted())
.replace("{testStatus}", getTestStatus().toString())
.replaceAll("[^a-zA-Z0-9]", "_");
this.testNameNoExtension = this.testNameNoExtension.replace("{buildName}", buildName);
this.testNameNoExtension = buildName.concat(this.testNameNoExtension);

this.fileName = FILE_NAME_TEMPLATE.replace("{fileName}", testNameNoExtension)
.replace("{fileExtension}", fileExtension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DockerSeleniumCapabilityMatcher() {

@Override
public boolean matches(Map<String, Object> nodeCapability, Map<String, Object> requestedCapability) {
logger.info(String.format("Validating %s in node with capabilities %s", requestedCapability,
logger.debug(String.format("Validating %s in node with capabilities %s", requestedCapability,
nodeCapability));

if (!requestedCapability.containsKey(CapabilityType.BROWSER_NAME)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -10,6 +11,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import de.zalando.ep.zalenium.container.DockerContainerClient;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomUtils;
import org.openqa.grid.common.RegistrationRequest;
Expand Down Expand Up @@ -53,10 +55,11 @@ public class DockeredSeleniumStarter {
private static final String DEFAULT_DOCKER_SELENIUM_IMAGE = "elgalu/selenium";
private static final String ZALENIUM_SELENIUM_IMAGE_NAME = "ZALENIUM_SELENIUM_IMAGE_NAME";
private static final int LOWER_PORT_BOUNDARY = 40000;
private static final int UPPER_PORT_BOUNDARY = 49999;
private static final ContainerClient defaultContainerClient = ContainerFactory.getContainerClient();
private static final Environment defaultEnvironment = new Environment();
private static final String LOGGING_PREFIX = "[DS] ";
private static final List<Integer> allocatedPorts = Collections.synchronizedList(new ArrayList<>());

private static List<MutableCapabilities> dockerSeleniumCapabilities = new ArrayList<>();
private static ContainerClient containerClient = defaultContainerClient;
private static Environment env = defaultEnvironment;
Expand Down Expand Up @@ -97,19 +100,6 @@ public static void readConfigurationFromEnvVariables() {
readConfigurationFromEnvVariables();
}

/*
* Updating the proxy's registration request information with the current DockerSelenium capabilities.
* If it is not possible to retrieve them, then we default to Chrome and Firefox in Linux.
*/
@VisibleForTesting
protected static RegistrationRequest updateDSCapabilities(RegistrationRequest registrationRequest) {
readConfigurationFromEnvVariables();
containerClient.setNodeId(LOGGING_PREFIX);
registrationRequest.getConfiguration().capabilities.clear();
registrationRequest.getConfiguration().capabilities.addAll(getCapabilities());
return registrationRequest;
}

@SuppressWarnings("ConstantConditions")
@VisibleForTesting
public static List<MutableCapabilities> getCapabilities() {
Expand Down Expand Up @@ -246,19 +236,21 @@ public ContainerCreationStatus startDockerSeleniumContainer(final TimeZone timeZ
String seleniumNodeParams = getSeleniumNodeParameters();
String latestImage = getLatestDownloadedImage(getDockerSeleniumImageName());

Map<String, String> envVars = buildEnvVars(effectiveTimeZone, effectiveScreenSize, hostIpAddress, sendAnonymousUsageInfo, nodePolling, nodeRegisterCycle, seleniumNodeParams);

ContainerCreationStatus creationStatus = containerClient
.createContainer(getContainerName(), latestImage, envVars, String.valueOf(LOWER_PORT_BOUNDARY));
int containerPort = LOWER_PORT_BOUNDARY;
if (containerClient instanceof DockerContainerClient) {
containerPort = findFreePortInRange();
}
Map<String, String> envVars = buildEnvVars(effectiveTimeZone, effectiveScreenSize, hostIpAddress, sendAnonymousUsageInfo,
nodePolling, nodeRegisterCycle, seleniumNodeParams, containerPort);

return creationStatus;
return containerClient.createContainer(getContainerName(), latestImage, envVars, String.valueOf(containerPort));
}

private Map<String, String> buildEnvVars(TimeZone timeZone, Dimension screenSize, String hostIpAddress,
boolean sendAnonymousUsageInfo, String nodePolling,
String nodeRegisterCycle, String seleniumNodeParams) {
final int noVncPort = LOWER_PORT_BOUNDARY + NO_VNC_PORT_GAP;
final int vncPort = LOWER_PORT_BOUNDARY + VNC_PORT_GAP;
boolean sendAnonymousUsageInfo, String nodePolling, String nodeRegisterCycle,
String seleniumNodeParams, int containerPort) {
final int noVncPort = containerPort + NO_VNC_PORT_GAP;
final int vncPort = containerPort + VNC_PORT_GAP;
Map<String, String> envVars = new HashMap<>();
envVars.put("ZALENIUM", "true");
envVars.put("SELENIUM_HUB_HOST", hostIpAddress);
Expand All @@ -281,7 +273,7 @@ private Map<String, String> buildEnvVars(TimeZone timeZone, Dimension screenSize
envVars.put("SEL_NODEPOLLING_MS", nodePolling);
envVars.put("SELENIUM_NODE_PROXY_PARAMS", "de.zalando.ep.zalenium.proxy.DockerSeleniumRemoteProxy");
envVars.put("MULTINODE", "true");
envVars.put("SELENIUM_MULTINODE_PORT", String.valueOf(LOWER_PORT_BOUNDARY));
envVars.put("SELENIUM_MULTINODE_PORT", String.valueOf(containerPort));
envVars.put("CHROME", "false");
envVars.put("FIREFOX", "false");
envVars.put("SELENIUM_NODE_PARAMS", seleniumNodeParams);
Expand Down Expand Up @@ -349,4 +341,34 @@ private TimeZone getConfiguredTimeZoneFromCapabilities(Map<String, Object> reque

return timeZone;
}

/*
Method adapted from https://gist.github.com/vorburger/3429822
*/
private int findFreePortInRange() {
/*
If the list size is this big (~9800), it means that almost all ports have been used, but
probably many have been released already. The list is cleared so ports can be reused.
If by any chance one of the first allocated ports is still used, it will be skipped by the
existing validation.
*/
synchronized (allocatedPorts){
if (allocatedPorts.size() > (UPPER_PORT_BOUNDARY - LOWER_PORT_BOUNDARY - 200)) {
allocatedPorts.clear();
LOGGER.info("Cleaning allocated ports list.");
}
for (int portNumber = LOWER_PORT_BOUNDARY; portNumber <= UPPER_PORT_BOUNDARY; portNumber++) {
int noVncPortNumber = portNumber + NO_VNC_PORT_GAP;
int vncPortNumber = portNumber + VNC_PORT_GAP;
if (!allocatedPorts.contains(portNumber) && !allocatedPorts.contains(noVncPortNumber)
&& !allocatedPorts.contains(vncPortNumber)) {
allocatedPorts.add(portNumber);
allocatedPorts.add(noVncPortNumber);
allocatedPorts.add(vncPortNumber);
return portNumber;
}
}
}
return -1;
}
}

0 comments on commit e0e9fe1

Please sign in to comment.