Skip to content

Commit

Permalink
Utilities for arquillian integration tests (apache#1310)
Browse files Browse the repository at this point in the history
* Utilities for arquillian integration tests

A set of utilities for working with clusters set up in arquillian.

- Utilities to work with docker
- Utilities to work with arquillian pulsar clusters
- Await strategies that arquillian uses to know if a node is up
- Stop actions to copy logs at the end of a test suite

* Move to log4j2

* Pull in extra dependencies for log4j yml configuration
  • Loading branch information
ivankelly authored and merlimat committed Mar 3, 2018
1 parent 973fa3a commit 44e0663
Show file tree
Hide file tree
Showing 9 changed files with 781 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/integration-tests-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.pulsar.tests</groupId>
<artifactId>tests-parent</artifactId>
<version>2.0.0-incubating-SNAPSHOT</version>
</parent>

<groupId>org.apache.pulsar.tests</groupId>
<artifactId>integration-tests-utils</artifactId>
<packaging>jar</packaging>

<name>Apache Pulsar :: Tests :: Utility module for Arquillian based integration tests</name>

<properties>
<arquillian-cube.version>1.15.1</arquillian-cube.version>
<commons-compress.version>1.15</commons-compress.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>${commons-compress.version}</version>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<dependency>
<groupId>org.arquillian.cube</groupId>
<artifactId>arquillian-cube-docker</artifactId>
<version>${arquillian-cube.version}</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.tests;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.InspectExecResponse;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.ContainerNetwork;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DockerUtils {
private static final Logger LOG = LoggerFactory.getLogger(DockerUtils.class);

private static File getTargetDirectory(String containerId) {
String base = System.getProperty("maven.buildDirectory");
if (base == null) {
base = "target";
}
File directory = new File(base + "/container-logs/" + containerId);
if (!directory.exists() && !directory.mkdirs()) {
LOG.error("Error creating directory for container logs.");
}
return directory;
}

public static void dumpContainerLogToTarget(DockerClient docker, String containerId) {
File output = new File(getTargetDirectory(containerId), "docker.log");
try (FileOutputStream os = new FileOutputStream(output)) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
docker.logContainerCmd(containerId).withStdOut(true)
.withStdErr(true).withTimestamps(true).exec(new ResultCallback<Frame>() {
@Override
public void close() {}

@Override
public void onStart(Closeable closeable) {}

@Override
public void onNext(Frame object) {
try {
os.write(object.getPayload());
} catch (IOException e) {
onError(e);
}
}

@Override
public void onError(Throwable throwable) {
future.completeExceptionally(throwable);
}

@Override
public void onComplete() {
future.complete(true);
}
});
future.get();
} catch (RuntimeException|ExecutionException|IOException e) {
LOG.error("Error dumping log for {}", containerId, e);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
LOG.info("Interrupted dumping log from container {}", containerId, ie);
}
}

public static void dumpContainerLogDirToTarget(DockerClient docker, String containerId, String path) {
final int READ_BLOCK_SIZE = 10000;

try (InputStream dockerStream = docker.copyArchiveFromContainerCmd(containerId, path).exec();
TarArchiveInputStream stream = new TarArchiveInputStream(dockerStream)) {
TarArchiveEntry entry = stream.getNextTarEntry();
while (entry != null) {
if (entry.isFile()) {
File output = new File(getTargetDirectory(containerId), entry.getName().replace("/", "-"));
try (FileOutputStream os = new FileOutputStream(output)) {
byte[] block = new byte[READ_BLOCK_SIZE];
int read = stream.read(block, 0, READ_BLOCK_SIZE);
while (read > -1) {
os.write(block, 0, read);
read = stream.read(block, 0, READ_BLOCK_SIZE);
}
}
}
entry = stream.getNextTarEntry();
}
} catch (RuntimeException|IOException e) {
LOG.error("Error reading logs from container {}", containerId, e);
}
}

public static String getContainerIP(DockerClient docker, String containerId) {
for (Map.Entry<String, ContainerNetwork> e : docker.inspectContainerCmd(containerId)
.exec().getNetworkSettings().getNetworks().entrySet()) {
return e.getValue().getIpAddress();
}
throw new IllegalArgumentException("Container " + containerId + " has no networks");
}

public static String getContainerHostname(DockerClient docker, String containerId) {
return runCommand(docker, containerId, "hostname").trim();
}

public static String runCommand(DockerClient docker, String containerId, String... cmd) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
String execid = docker.execCreateCmd(containerId).withCmd(cmd)
.withAttachStderr(true).withAttachStdout(true).exec().getId();
String cmdString = Arrays.stream(cmd).collect(Collectors.joining(" "));
StringBuffer output = new StringBuffer();
docker.execStartCmd(execid).withDetach(false)
.exec(new ResultCallback<Frame>() {
@Override
public void close() {}

@Override
public void onStart(Closeable closeable) {
LOG.info("DOCKER.exec({}:{}): Executing...", containerId, cmdString);
}

@Override
public void onNext(Frame object) {
LOG.info("DOCKER.exec({}:{}): {}", containerId, cmdString, object);
output.append(new String(object.getPayload()));
}

@Override
public void onError(Throwable throwable) {
future.completeExceptionally(throwable);
}

@Override
public void onComplete() {
LOG.info("DOCKER.exec({}:{}): Done", containerId, cmdString);
future.complete(true);
}
});
future.join();

InspectExecResponse resp = docker.inspectExecCmd(execid).exec();
while (resp.isRunning()) {
try {
Thread.sleep(200);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
resp = docker.inspectExecCmd(execid).exec();
}
int retCode = resp.getExitCode();
if (retCode != 0) {
throw new RuntimeException(
String.format("cmd(%s) failed on %s with exitcode %d",
cmdString, containerId, retCode));
} else {
LOG.info("DOCKER.exec({}:{}): completed with {}", containerId, cmdString, retCode);
}
return output.toString();
}

public static Optional<String> getContainerCluster(DockerClient docker, String containerId) {
return Optional.ofNullable(docker.inspectContainerCmd(containerId)
.exec().getConfig().getLabels().get("cluster"));
}

public static Set<String> allCubeIds() {
Pattern pattern = Pattern.compile("^arq.cube.docker.([^.]*).ip$");
return System.getProperties().keySet().stream()
.map(k -> pattern.matcher(k.toString()))
.filter(m -> m.matches())
.map(m -> m.group(1))
.collect(Collectors.toSet());
}

public static Set<String> cubeIdsWithLabels(DockerClient docker, Map<String,String> labels) {
return allCubeIds().stream()
.filter(id -> {
Map<String,String> configuredLabels = docker.inspectContainerCmd(id).exec().getConfig().getLabels();
return labels.entrySet().stream()
.map(e -> configuredLabels.containsKey(e.getKey())
&& configuredLabels.get(e.getKey()).equals(e.getValue()))
.reduce(true, (acc, res) -> acc && res);
})
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.tests;

import org.arquillian.cube.docker.impl.docker.DockerClientExecutor;
import org.arquillian.cube.impl.model.CubeId;
import org.arquillian.cube.spi.beforeStop.BeforeStopAction;

public class LogToTargetDirStopAction implements BeforeStopAction {
private DockerClientExecutor dockerClientExecutor;
private CubeId containerID;

public void setDockerClientExecutor(DockerClientExecutor executor) {
this.dockerClientExecutor = executor;
}

public void setContainerID(CubeId containerID) {
this.containerID = containerID;
}

@Override
public void doBeforeStop() {
DockerUtils.dumpContainerLogToTarget(dockerClientExecutor.getDockerClient(), containerID.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.tests;

import org.arquillian.cube.spi.await.AwaitStrategy;

public class NoopAwaitStrategy implements AwaitStrategy {
@Override
public boolean await() {
return true;
}
}
Loading

0 comments on commit 44e0663

Please sign in to comment.