Skip to content
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 oak-it-osgi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<artifactId>testcontainers-elasticsearch</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion oak-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<tika.version>1.28.5</tika.version>
<derby.version>10.16.1.1</derby.version>
<jackson.version>2.19.4</jackson.version>
<testcontainers.version>1.21.4</testcontainers.version>
<testcontainers.version>2.0.3</testcontainers.version>
<pax-exam.version>4.14.0</pax-exam.version>
<pax-url.version>2.6.17</pax-url.version>
<groovy.version>3.0.25</groovy.version>
Expand Down
4 changes: 2 additions & 2 deletions oak-run-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>toxiproxy</artifactId>
<artifactId>testcontainers-toxiproxy</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<artifactId>testcontainers-mongodb</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import org.apache.jackrabbit.oak.stats.Clock;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
Expand Down Expand Up @@ -90,17 +91,9 @@ public static Collection<Object[]> parameters() {
final boolean parallelDump;
final boolean testUpdateContent;

@Rule
public final Network network = Network.newNetwork();
@Rule
public final MongoDBContainer mongoDBContainer = new MongoDBContainer(MongoDockerRule.getDockerImageName())
.withNetwork(network)
.withNetworkAliases("mongo")
.withExposedPorts(MONGODB_DEFAULT_PORT);
@Rule
public final ToxiproxyContainer toxiproxy = new ToxiproxyContainer(TOXIPROXY_IMAGE)
.withStartupAttempts(3)
.withNetwork(network);
private Network network;
private MongoDBContainer mongoDBContainer;
private ToxiproxyContainer toxiproxy;
@Rule
public final DocumentMKBuilderProvider builderProvider = new DocumentMKBuilderProvider();
@Rule
Expand All @@ -125,6 +118,16 @@ public static void setup() throws IOException {

@Before
public void before() throws Exception {
network = Network.newNetwork();
mongoDBContainer = new MongoDBContainer(MongoDockerRule.getDockerImageName())
.withNetwork(network)
.withNetworkAliases("mongo")
.withExposedPorts(MONGODB_DEFAULT_PORT);
mongoDBContainer.start();
toxiproxy = new ToxiproxyContainer(TOXIPROXY_IMAGE)
.withStartupAttempts(3)
.withNetwork(network);
toxiproxy.start();
ToxiproxyClient toxiproxyClient = new ToxiproxyClient(toxiproxy.getHost(), toxiproxy.getControlPort());
// For the logic under test
this.proxy = toxiproxyClient.createProxy("mongo", "0.0.0.0:8666", "mongo:" + MONGODB_DEFAULT_PORT);
Expand All @@ -142,6 +145,19 @@ public void before() throws Exception {
}
}

@After
public void after() {
if (toxiproxy != null) {
toxiproxy.stop();
}
if (mongoDBContainer != null) {
mongoDBContainer.stop();
}
if (network != null) {
network.close();
}
}

@Test
public void mongoDisconnectTest() throws Exception {
// Testcontainers' MongoDBContainer starts a Mongo replica set of one node. In this configuration we cannot
Expand Down
2 changes: 1 addition & 1 deletion oak-run-elastic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<artifactId>testcontainers-elasticsearch</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoDatabase;
import org.apache.commons.collections4.set.ListOrderedSet;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.commons.Compression;
Expand Down Expand Up @@ -71,7 +72,6 @@
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.rules.TemporaryFolder;
import org.testcontainers.shaded.org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
Expand Down
4 changes: 2 additions & 2 deletions oak-search-elastic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<artifactId>testcontainers-elasticsearch</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>toxiproxy</artifactId>
<artifactId>testcontainers-toxiproxy</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.utility.MountableFile;

import co.elastic.clients.transport.Version;

import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class ElasticTestServer implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(ElasticTestServer.class);
private static final String ELASTIC_DOCKER_IMAGE_VERSION = System.getProperty("elasticDockerImageVersion");

private static final ElasticTestServer SERVER = new ElasticTestServer();
private static volatile ElasticsearchContainer CONTAINER;
private static volatile Network network;

private ElasticTestServer() {
}
Expand All @@ -62,39 +64,41 @@
return CONTAINER;
}

@SuppressWarnings("resource")
private synchronized void setup() {
String esDockerImageVersion = ELASTIC_DOCKER_IMAGE_VERSION != null ? ELASTIC_DOCKER_IMAGE_VERSION : Version.VERSION.toString();
LOG.info("Elasticsearch test Docker image version: {}.", esDockerImageVersion);
checkIfDockerClientAvailable();
Network network = Network.newNetwork();
network = Network.newNetwork();
CONTAINER = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:" + esDockerImageVersion)
.withEnv("ES_JAVA_OPTS", "-Xms1g -Xmx1g")
.withCopyFileToContainer(
MountableFile.forClasspathResource("elasticsearch.yml"),
"/usr/share/elasticsearch/config/elasticsearch.yml")
.withEnv("network.host", "0.0.0.0")
.withEnv("ingest.geoip.downloader.enabled", "false")
.withEnv("xpack.security.enabled", "false")
.withEnv("xpack.security.http.ssl.enabled", "false")
.withEnv("action.destructive_requires_name", "false")
.withNetwork(network)
.withNetworkAliases("elasticsearch")
.waitingFor(Wait.forHttp("/").forPort(9200).forStatusCode(200))
// Default is 30 seconds, which might not be enough on environments with limited resources or network latency
.withStartupTimeout(Duration.ofMinutes(3))
.withStartupAttempts(3);
CONTAINER.start();
verifyConnectivity();

Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOG).withSeparateOutputStreams();
CONTAINER.followOutput(logConsumer);
}

@Override
public void close() {
if (this == SERVER) {
// Closed with a shutdown hook
return;
}

if (CONTAINER != null) {
CONTAINER.stop();
}
CONTAINER = null;
if (network != null) {
network.close();
}
network = null;
}

private void checkIfDockerClientAvailable() {
Expand All @@ -108,6 +112,31 @@
assumeNotNull(client);
}

private void verifyConnectivity() {
// Ensure the container is actually reachable before tests proceed.
try (ElasticConnection connection = ElasticConnection.newBuilder()
.withIndexPrefix("elastic_test_bootstrap")
.withConnectionParameters(ElasticConnection.DEFAULT_SCHEME, CONTAINER.getHost(),
CONTAINER.getMappedPort(ElasticConnection.DEFAULT_PORT))
.build()) {
long deadline = System.nanoTime() + TimeUnit.MINUTES.toNanos(1);
while (System.nanoTime() < deadline) {
if (connection.isAvailable()) {
return;
}
try {
Thread.sleep(1000);

Check warning on line 128 in oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticTestServer.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this use of "Thread.sleep()".

See more on https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&issues=AZ84fVLH_IpUmErFHrOT&open=AZ84fVLH_IpUmErFHrOT&pullRequest=3002
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted while waiting for Elasticsearch readiness", e);
}
}
throw new IllegalStateException("Elasticsearch test container started but did not become reachable via HTTP");
} catch (IOException e) {
LOG.debug("Error closing bootstrap Elastic connection", e);
}
}

/**
* Launches an Elasticsearch Test Server to re-use among several test executions.
*/
Expand Down
24 changes: 0 additions & 24 deletions oak-search-elastic/src/test/resources/elasticsearch.yml

This file was deleted.

4 changes: 2 additions & 2 deletions oak-store-document/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,13 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>toxiproxy</artifactId>
<artifactId>testcontainers-toxiproxy</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<artifactId>testcontainers-mongodb</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.containers.Network;
Expand All @@ -59,19 +58,11 @@ public class LeaseUpdateSocketTimeoutIT {

private static final int LEASE_SO_TIMEOUT = 50;

@Rule
public Network network = Network.newNetwork();
private Network network;

@Rule
public MongoDBContainer mongoDBContainer = new MongoDBContainer(MongoDockerRule.getDockerImageName())
.withNetwork(network)
.withNetworkAliases("mongo")
.withExposedPorts(MONGODB_DEFAULT_PORT);
private MongoDBContainer mongoDBContainer;

@Rule
public ToxiproxyContainer tp = new ToxiproxyContainer(TOXIPROXY_IMAGE)
.withStartupAttempts(3)
.withNetwork(network);
private ToxiproxyContainer tp;

private Proxy proxy;

Expand All @@ -89,6 +80,16 @@ public static void dockerAvailable() {

@Before
public void before() throws Exception {
network = Network.newNetwork();
mongoDBContainer = new MongoDBContainer(MongoDockerRule.getDockerImageName())
.withNetwork(network)
.withNetworkAliases("mongo")
.withExposedPorts(MONGODB_DEFAULT_PORT);
mongoDBContainer.start();
tp = new ToxiproxyContainer(TOXIPROXY_IMAGE)
.withStartupAttempts(3)
.withNetwork(network);
tp.start();
clock = new Clock.Virtual();
clock.waitUntil(System.currentTimeMillis());
setClusterNodeInfoClock(clock);
Expand All @@ -103,7 +104,18 @@ public void before() throws Exception {

@After
public void after() {
store.dispose();
if (store != null) {
store.dispose();
}
if (tp != null) {
tp.stop();
}
if (mongoDBContainer != null) {
mongoDBContainer.stop();
}
if (network != null) {
network.close();
}
TestUtils.resetClusterNodeInfoClockToDefault();
}

Expand Down
Loading