ElasticContainer: Create with static method

Move creation of the container and handling of failure into a static
method in ElasticContainer, which is then called from the tests.

Change-Id: I176acdc05bbc6e06ce8c1fd76acbc9b3902c06be
This commit is contained in:
David Pursehouse
2018-05-25 15:54:16 +09:00
parent 5f40a6adff
commit ba6fc47f5f
5 changed files with 20 additions and 32 deletions

View File

@@ -42,13 +42,11 @@ public class ReindexIT extends StandaloneSiteTest {
elasticsearchTest = true; elasticsearchTest = true;
if (elasticNodeInfo == null) { if (elasticNodeInfo == null) {
try { try {
container = new ElasticContainer<>(); container = ElasticContainer.createAndStart();
container.start(); elasticNodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort());
} catch (Throwable t) { } catch (Throwable t) {
return null; return null;
} }
elasticNodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort());
} }
String indicesPrefix = UUID.randomUUID().toString(); String indicesPrefix = UUID.randomUUID().toString();
Config cfg = new Config(); Config cfg = new Config();

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.elasticsearch.testing;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import java.util.Set; import java.util.Set;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.junit.internal.AssumptionViolatedException;
import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.GenericContainer;
/* Helper class for running ES integration tests in docker container */ /* Helper class for running ES integration tests in docker container */
@@ -25,11 +26,23 @@ public class ElasticContainer<SELF extends ElasticContainer<SELF>> extends Gener
private static final String VERSION = "2.4.6-alpine"; private static final String VERSION = "2.4.6-alpine";
private static final int ELASTICSEARCH_DEFAULT_PORT = 9200; private static final int ELASTICSEARCH_DEFAULT_PORT = 9200;
public ElasticContainer() { public static ElasticContainer<?> createAndStart() {
// Assumption violation is not natively supported by Testcontainers.
// See https://github.com/testcontainers/testcontainers-java/issues/343
try {
ElasticContainer<?> container = new ElasticContainer<>();
container.start();
return container;
} catch (Throwable t) {
throw new AssumptionViolatedException("Unable to start container[might be docker related]");
}
}
private ElasticContainer() {
this(NAME + ":" + VERSION); this(NAME + ":" + VERSION);
} }
public ElasticContainer(String dockerImageName) { private ElasticContainer(String dockerImageName) {
super(dockerImageName); super(dockerImageName);
} }

View File

@@ -24,7 +24,6 @@ import com.google.inject.Injector;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.internal.AssumptionViolatedException;
public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest { public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
private static ElasticNodeInfo nodeInfo; private static ElasticNodeInfo nodeInfo;
@@ -37,15 +36,7 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
return; return;
} }
// Assumption violation is not natively supported by Testcontainers. container = ElasticContainer.createAndStart();
// See https://github.com/testcontainers/testcontainers-java/issues/343
try {
container = new ElasticContainer<>();
container.start();
} catch (Throwable t) {
throw new AssumptionViolatedException("Unable to start container[might be docker related]");
}
nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort());
} }

View File

@@ -28,7 +28,6 @@ import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import org.junit.rules.TestName; import org.junit.rules.TestName;
public class ElasticQueryChangesTest extends AbstractQueryChangesTest { public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
@@ -44,13 +43,7 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
return; return;
} }
try { container = ElasticContainer.createAndStart();
container = new ElasticContainer<>();
container.start();
} catch (Throwable t) {
throw new AssumptionViolatedException("Unable to start container[might be docker related]");
}
nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort());
} }

View File

@@ -24,7 +24,6 @@ import com.google.inject.Injector;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.internal.AssumptionViolatedException;
public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest { public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest {
private static ElasticNodeInfo nodeInfo; private static ElasticNodeInfo nodeInfo;
@@ -37,13 +36,7 @@ public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest {
return; return;
} }
try { container = ElasticContainer.createAndStart();
container = new ElasticContainer<>();
container.start();
} catch (Throwable t) {
throw new AssumptionViolatedException("Unable to start container[might be docker related]");
}
nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort()); nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort());
} }