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

@@ -17,6 +17,7 @@ package com.google.gerrit.elasticsearch.testing;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
import org.apache.http.HttpHost;
import org.junit.internal.AssumptionViolatedException;
import org.testcontainers.containers.GenericContainer;
/* 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 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);
}
public ElasticContainer(String dockerImageName) {
private ElasticContainer(String dockerImageName) {
super(dockerImageName);
}

View File

@@ -24,7 +24,6 @@ import com.google.inject.Injector;
import org.eclipse.jgit.lib.Config;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.internal.AssumptionViolatedException;
public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
private static ElasticNodeInfo nodeInfo;
@@ -37,15 +36,7 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
return;
}
// Assumption violation is not natively supported by Testcontainers.
// 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]");
}
container = ElasticContainer.createAndStart();
nodeInfo = new ElasticNodeInfo(container.getHttpHost().getPort());
}

View File

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