Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1107 Allow override of selenium containers and use real DinD info #1113

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.arquillian.cube.impl.client.enricher.HostIpTestEnricher;
import org.arquillian.cube.impl.client.enricher.HostPortTestEnricher;
import org.arquillian.cube.impl.client.enricher.StandaloneCubeUrlResourceProvider;
import org.arquillian.cube.impl.client.observer.BeforeClassEnricherObserver;
import org.arquillian.cube.impl.reporter.DockerReportKey;
import org.arquillian.cube.impl.reporter.TakeCubeInformation;
import org.arquillian.reporter.api.model.StringKey;
Expand All @@ -29,6 +30,7 @@ public void register(ExtensionBuilder builder) {
.observer(CubeLifecycleController.class)
//.observer(CubeSuiteLifecycleController.class)
.observer(ClientCubeControllerCreator.class)
.observer(BeforeClassEnricherObserver.class)
.observer(ForceStopDockerContainersShutdownHook.class);

builder.service(ResourceProvider.class, CubeControllerProvider.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package org.arquillian.cube.impl.client.enricher;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.arquillian.cube.CubeIp;
import org.arquillian.cube.impl.util.ReflectionUtil;
import org.arquillian.cube.spi.Cube;
Expand All @@ -16,6 +9,14 @@
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.test.spi.TestEnricher;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CubeIpTestEnricher implements TestEnricher {

private static final Logger logger = Logger.getLogger(CubeIpTestEnricher.class.getName());
Expand Down Expand Up @@ -43,7 +44,7 @@ public void enrich(Object testCase) {
if (ip != null) {
dockerContainerIpField.set(testCase, ip);
} else {
logger.log(Level.WARNING, String.format("There is no container with id %s.", containerName));
logger.log(Level.WARNING, String.format("There is no container to enrich with id %s.", containerName));
}
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
Expand All @@ -69,7 +70,7 @@ public Object[] resolve(Method method) {
if (ip != null) {
values[i] = ip;
} else {
logger.log(Level.WARNING, String.format("There is no container with id %s.", containerName));
logger.log(Level.WARNING, String.format("There was no IP to resolve for container '%s'", containerName));
}
}
}
Expand Down Expand Up @@ -104,18 +105,24 @@ private String getContainerIp(String containerName, boolean internal) {
final Cube cube = getCube(containerName);

if (cube == null) {
logger.log(Level.WARNING, String.format("There is no container with id %s.", containerName));
return null;
}

if (cube.hasMetadata(HasPortBindings.class)) {
final HasPortBindings metadata = (HasPortBindings) cube.getMetadata(HasPortBindings.class);

final Cube.State state = cube.state();
logger.log(Level.INFO, String.format("Container '%s' has state '%s' in '%s'", containerName, state, cube.getClass().getName()));

if (internal) {
return metadata.getInternalIP();
} else {
return metadata.getContainerIP();
}
} else {
return null;
logger.log(Level.WARNING, String.format("Container '%s' does not have port bindings, falling back to localhost", containerName));
return "localhost";
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.arquillian.cube.impl.client.observer;

import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.core.spi.ServiceLoader;
import org.jboss.arquillian.test.spi.TestEnricher;
import org.jboss.arquillian.test.spi.event.suite.BeforeClass;

import java.util.Collection;

public class BeforeClassEnricherObserver {

@Inject
Instance<ServiceLoader> serviceLoaderInstance;

public void executeEnrichers(@Observes BeforeClass beforeClass) throws Exception {

if (null != serviceLoaderInstance) {
final Collection<TestEnricher> all = serviceLoaderInstance.get().all(TestEnricher.class);
final Class<?> javaClass = beforeClass.getTestClass().getJavaClass();

for (final TestEnricher testEnricher : all) {
testEnricher.enrich(javaClass.newInstance());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
final CubeIpTestEnricherTest.MyTestExternal testCase = new CubeIpTestEnricherTest.MyTestExternal();
cubeIpTestEnricher.enrich(testCase);
assertThat(testCase.ip, is("192.168.99.101"));
assertThat(CubeIpTestEnricherTest.MyTestExternal.ipStatic, is("192.168.99.101"));

final String ip = cubeIpTestEnricher.cubeRegistryInstance.get().getCube("test").getMetadata(HasPortBindings.class).getContainerIP();
assertThat(ip, is("192.168.99.101"));
}

@Test
Expand Down Expand Up @@ -71,6 +75,10 @@ public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
final CubeIpTestEnricherTest.MyTest testCase = new CubeIpTestEnricherTest.MyTest();
cubeIpTestEnricher.enrich(testCase);
assertThat(testCase.ip, is("192.168.99.100"));
assertThat(MyTest.ipStatic, is("192.168.99.100"));

final String ip = cubeIpTestEnricher.cubeRegistryInstance.get().getCube("test").getMetadata(HasPortBindings.class).getInternalIP();
assertThat(ip, is("192.168.99.100"));
}

@Test
Expand Down Expand Up @@ -105,6 +113,8 @@ public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
public static class MyTest {
@CubeIp(containerName = "test")
String ip;
@CubeIp(containerName = "test")
static String ipStatic;

public void myMethod(String first, @CubeIp(containerName = "test") String ip) {

Expand All @@ -114,5 +124,7 @@ public void myMethod(String first, @CubeIp(containerName = "test") String ip) {
public static class MyTestExternal {
@CubeIp(containerName = "test", internal = false)
String ip;
@CubeIp(containerName = "test", internal = false)
static String ipStatic;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void shouldGetCachedResult() throws IOException {
CacheUrlAsset cacheUrlAsset = new CacheUrlAsset(new URL("http://arquillian.org/images/arq.txt"));
CacheUrlAsset.TEMP_LOCATION = newFolder.getAbsolutePath();
final Path path = Paths.get(newFolder.getAbsolutePath(), "arq.txt");
Files.write(path, "Hello".getBytes("UTF-8"));
Files.write(path, "Hello".getBytes(StandardCharsets.UTF_8));
InputStream is = cacheUrlAsset.openStream();
String content = slurp(is);

Expand All @@ -62,7 +63,7 @@ public void shouldGetCachedResult() throws IOException {
public void shouldDownloadFileIfExpired() throws IOException, InterruptedException {
final File newFolder = temporaryFolder.newFolder();
final Path path = Paths.get(newFolder.getAbsolutePath(), "arquillian_crown_icon_glossy_256.png");
Files.write(path, "invalidchunk".getBytes("UTF-8"));
Files.write(path, "invalidchunk".getBytes(StandardCharsets.UTF_8));
Thread.sleep(3000);
CacheUrlAsset cacheUrlAsset =
new CacheUrlAsset(new URL("http://arquillian.org/images/arquillian_crown_icon_glossy_256.png"), 2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.arquillian.cube.docker.impl.await;

import java.util.logging.Logger;
import org.arquillian.cube.docker.impl.client.config.Await;
import org.arquillian.cube.docker.impl.client.config.CubeContainer;
import org.arquillian.cube.docker.impl.docker.DockerClientExecutor;
import org.arquillian.cube.docker.impl.util.BindingUtil;
import org.arquillian.cube.spi.Cube;
import org.arquillian.cube.spi.await.AwaitStrategy;

import java.util.logging.Logger;

public class AwaitStrategyFactory {

private static final Logger log = Logger.getLogger(AwaitStrategyFactory.class.getName());
Expand All @@ -15,40 +17,70 @@ private AwaitStrategyFactory() {
super();
}

public static final AwaitStrategy create(DockerClientExecutor dockerClientExecutor, Cube<?> cube,
CubeContainer options) {
/**
* Build an await strategy, assuming that the environment is not docker-in-docker
*
* @param dockerClientExecutor Docker client
* @param cube The cube for which the strategy is required
* @param options The container options
* @return The configure await strategy, or the default polling strategy
*/
public static AwaitStrategy create(final DockerClientExecutor dockerClientExecutor,
final Cube<?> cube,
final CubeContainer options) {
return create(dockerClientExecutor, cube, options, false);
}

/**
* Build an await strategy with the option of defining that the environment is not docker-in-docker
*
* @param dockerClientExecutor Docker client
* @param cube The cube for which the strategy is required
* @param options The container options
* @param dind Is the docker daemon docker-in-docker (ie. dockerHost is NOT local)
* @return The configure await strategy, ot the default polling strategy
*/
public static AwaitStrategy create(final DockerClientExecutor dockerClientExecutor,
final Cube<?> cube,
final CubeContainer options,
final boolean dind) {

if (options.getAwait() != null) {
Await await = options.getAwait();

if (await.getStrategy() != null) {

if (dind) {
final String containerIp = BindingUtil.getContainerIp(dockerClientExecutor, cube.getId());
await.setIp(containerIp);
}

String strategy = await.getStrategy().toLowerCase();
switch (strategy) {
case PollingAwaitStrategy.TAG:
return new PollingAwaitStrategy(cube, dockerClientExecutor, await);
return new PollingAwaitStrategy(cube, dockerClientExecutor, await, dind);
case LogScanningAwaitStrategy.TAG:
return new LogScanningAwaitStrategy(cube, dockerClientExecutor, await);
case NativeAwaitStrategy.TAG:
return new NativeAwaitStrategy(cube, dockerClientExecutor);
case StaticAwaitStrategy.TAG:
return new StaticAwaitStrategy(cube, await);
return new StaticAwaitStrategy(cube, dockerClientExecutor, await, dind);
case SleepingAwaitStrategy.TAG:
return new SleepingAwaitStrategy(cube, await);
case HttpAwaitStrategy.TAG:
return new HttpAwaitStrategy(cube, dockerClientExecutor, await);
return new HttpAwaitStrategy(cube, dockerClientExecutor, await, dind);
case DockerHealthAwaitStrategy.TAG:
return new DockerHealthAwaitStrategy(cube, dockerClientExecutor, await);
default:
return new CustomAwaitStrategyInstantiator(cube, dockerClientExecutor, await);
}
} else {
log.fine("No await strategy is set and Polling one is going to be used.");
return new PollingAwaitStrategy(cube, dockerClientExecutor, new Await());
}
} else {
log.fine("No await strategy is set and Polling strategy is going to be used.");
return new PollingAwaitStrategy(cube, dockerClientExecutor, new Await());
}

return new PollingAwaitStrategy(cube, dockerClientExecutor, new Await(), dind);
}
}
Loading