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

[BUA-695] Video recording #17

Merged
merged 4 commits into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

<properties>
<selenium-server.version>2.53.1</selenium-server.version>
<docker-client.version>5.0.2</docker-client.version>
<docker-client.version>6.0.0</docker-client.version>
<junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<awaitility.version>2.0.0</awaitility.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package de.zalando.tip.zalenium.proxy;

import com.google.common.annotations.VisibleForTesting;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ExecCreation;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
Expand All @@ -11,6 +20,12 @@
import org.openqa.grid.internal.TestSession;
import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -29,6 +44,23 @@ public class DockerSeleniumRemoteProxy extends DefaultRemoteProxy {

private DockerSeleniumNodePoller dockerSeleniumNodePollerThread = null;

private static final DockerClient defaultDockerClient = new DefaultDockerClient("unix:///var/run/docker.sock");
private static DockerClient dockerClient = defaultDockerClient;

public enum VideoRecordingAction {
START_RECORDING("start-video"), STOP_RECORDING("stop-video");

private String recordingAction;

VideoRecordingAction(String action) {
recordingAction = action;
}

public String getRecordingAction() {
return recordingAction;
}
}


public DockerSeleniumRemoteProxy(RegistrationRequest request, Registry registry) {
super(request, registry);
Expand All @@ -47,7 +79,9 @@ public TestSession getNewSession(Map<String, Object> requestedCapability) {
return null;
}
if (increaseCounter()) {
return super.getNewSession(requestedCapability);
TestSession newSession = super.getNewSession(requestedCapability);
videoRecording(VideoRecordingAction.START_RECORDING);
return newSession;
}
LOGGER.log(Level.FINE, "{0} No more sessions allowed", getNodeIpAndPort());
return null;
Expand Down Expand Up @@ -101,6 +135,58 @@ public int getAmountOfExecutedTests() {
return amountOfExecutedTests;
}

public void videoRecording(final VideoRecordingAction action) {
try {
List<Container> containerList = dockerClient.listContainers(DockerClient.ListContainersParam.allContainers());
for (Container container : containerList) {
String containerName = "/ZALENIUM_" + getRemoteHost().getPort();
if (containerName.equalsIgnoreCase(container.names().get(0))) {
processVideoAction(action, container.id());
}
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, getNodeIpAndPort() + e.toString(), e);
}
}

private void processVideoAction(final VideoRecordingAction action, final String containerId) throws DockerException,
InterruptedException, IOException, URISyntaxException {
final String[] command = {"bash", "-c", action.getRecordingAction()};
final ExecCreation execCreation = dockerClient.execCreate(containerId, command,
DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr());
final LogStream output = dockerClient.execStart(execCreation.id());
LOGGER.log(Level.INFO, "{0} {1} {2}", new Object[]{getNodeIpAndPort(),
action.getRecordingAction(), output.readFully()});

if (VideoRecordingAction.STOP_RECORDING == action) {
copyVideos(containerId);
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private void copyVideos(final String containerId) throws IOException, DockerException, InterruptedException, URISyntaxException {
File jarLocation = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
String localPath = jarLocation.getParent();
LOGGER.log(Level.INFO, "{0} Copying files to: {1}", new Object[]{getNodeIpAndPort(), localPath});
try(TarArchiveInputStream tarStream = new TarArchiveInputStream(dockerClient.archiveContainer(containerId,
"/videos/"))) {
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
File curFile = new File(localPath, entry.getName());
File parent = curFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
OutputStream outputStream = new FileOutputStream(curFile);
IOUtils.copy(tarStream, outputStream);
outputStream.close();
}
}
}

public DockerSeleniumNodePoller getDockerSeleniumNodePollerThread() {
return dockerSeleniumNodePollerThread;
}
Expand Down Expand Up @@ -137,6 +223,7 @@ public void run() {
then the node executes its teardown.
*/
if (!dockerSeleniumRemoteProxy.isBusy() && dockerSeleniumRemoteProxy.isTestSessionLimitReached()) {
dockerSeleniumRemoteProxy.videoRecording(VideoRecordingAction.STOP_RECORDING);
shutdownNode();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ protected void startDockerSeleniumContainer(String browser) {
.build();

try {
final ContainerCreation dockerSeleniumContainer = dockerClient.createContainer(containerConfig);
String containerName = String.format("%s_%s", "ZALENIUM", nodePort);
final ContainerCreation dockerSeleniumContainer = dockerClient.createContainer(containerConfig,
containerName);
dockerClient.startContainer(dockerSeleniumContainer.id());
} catch (Exception e) {
LOGGER.log(Level.SEVERE, LOGGING_PREFIX + e.toString(), e);
Expand Down