You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ba...@apache.org on 2024/03/01 13:24:08 UTC

(jackrabbit-oak) 01/02: OAK-10464: Use Testcontainers instead of com.arakelian:docker-junit-rule (#1143)

This is an automated email from the ASF dual-hosted git repository.

baedke pushed a commit to branch issue/oak-10680
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit 1a715b61dce20ec4d03a60c973d0451e8a12693c
Author: t-rana <14...@users.noreply.github.com>
AuthorDate: Tue Oct 31 19:44:26 2023 +0530

    OAK-10464: Use Testcontainers instead of com.arakelian:docker-junit-rule (#1143)
    
    * OAK-10464 testcontainers dependency
    
    * OAK-10464 testcontainers dependency
    
    * modified blob endpoint, logback, add testcontainer dependency
    
    * migrate mongo docker rule to testcontainer
    
    * minor dependency changes
    
    * minor changes
    
    * refactor dependency, removed unused ones
    
    * revert logback changes
    
    * revert logback changes
    
    * removed test container dependency from child modules
    
    * remove unused dependency
    
    ---------
    
    Co-authored-by: smiroslav <mi...@apache.org>
---
 oak-blob-cloud-azure/pom.xml                       |   4 +-
 .../cloud/azure/blobstorage/AzuriteDockerRule.java | 122 ++++++++++++++++++++
 oak-it/pom.xml                                     |   4 +-
 oak-jcr/pom.xml                                    |   4 +-
 oak-lucene/pom.xml                                 |   4 +-
 oak-parent/pom.xml                                 |   8 +-
 oak-pojosr/pom.xml                                 |   4 +-
 oak-run-commons/pom.xml                            |   4 +-
 oak-run/pom.xml                                    |   4 +-
 oak-segment-azure/pom.xml                          |   4 +-
 oak-store-document/pom.xml                         |   5 -
 .../plugins/document/MongoConnectionFactory.java   |   2 +-
 .../plugins/document/mongo/MongoDockerRule.java    | 127 +++++++++++++++------
 oak-upgrade/pom.xml                                |   8 +-
 14 files changed, 239 insertions(+), 65 deletions(-)

diff --git a/oak-blob-cloud-azure/pom.xml b/oak-blob-cloud-azure/pom.xml
index 7540889f68..0c9121ef7e 100644
--- a/oak-blob-cloud-azure/pom.xml
+++ b/oak-blob-cloud-azure/pom.xml
@@ -176,8 +176,8 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.mockito</groupId>
-            <artifactId>mockito-core</artifactId>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>testcontainers</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
diff --git a/oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzuriteDockerRule.java b/oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzuriteDockerRule.java
new file mode 100644
index 0000000000..3eaf27980a
--- /dev/null
+++ b/oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzuriteDockerRule.java
@@ -0,0 +1,122 @@
+/*
+ * 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.jackrabbit.oak.blob.cloud.azure.blobstorage;
+
+import com.microsoft.azure.storage.CloudStorageAccount;
+import com.microsoft.azure.storage.StorageException;
+import com.microsoft.azure.storage.blob.CloudBlobClient;
+import com.microsoft.azure.storage.blob.CloudBlobContainer;
+import org.junit.Assume;
+import org.junit.rules.ExternalResource;
+import org.junit.runner.Description;
+import org.junit.runners.model.MultipleFailureException;
+import org.junit.runners.model.Statement;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.utility.DockerImageName;
+
+import java.net.URISyntaxException;
+import java.security.InvalidKeyException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+
+public class AzuriteDockerRule extends ExternalResource {
+
+    private static final DockerImageName DOCKER_IMAGE_NAME = DockerImageName.parse("mcr.microsoft.com/azure-storage/azurite:3.19.0");
+    public static final String ACCOUNT_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
+    public static final String ACCOUNT_NAME = "devstoreaccount1";
+    private static final AtomicReference<Exception> STARTUP_EXCEPTION = new AtomicReference<>();
+
+    private GenericContainer<?> azuriteContainer;
+
+    @Override
+    protected void before() throws Throwable {
+        azuriteContainer = new GenericContainer<>(DOCKER_IMAGE_NAME)
+                .withExposedPorts(10000)
+                .withEnv(Map.of("executable", "blob"))
+                .withStartupTimeout(Duration.ofSeconds(30));
+
+        try {
+            azuriteContainer.start();
+        } catch (IllegalStateException e) {
+            STARTUP_EXCEPTION.set(e);
+            throw e;
+        }
+    }
+
+    @Override
+    protected void after() {
+        if (azuriteContainer != null) {
+            azuriteContainer.stop();
+        }
+    }
+
+    @Override
+    public Statement apply(Statement base, Description description) {
+        return new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                try {
+                    before();
+                } catch (IllegalStateException e) {
+                    Assume.assumeNoException(STARTUP_EXCEPTION.get());
+                    throw e;
+                }
+
+                List<Throwable> errors = new ArrayList<Throwable>();
+                try {
+                    base.evaluate();
+                } catch (Throwable t) {
+                    errors.add(t);
+                } finally {
+                    try {
+                        after();
+                    } catch (Throwable t) {
+                        errors.add(t);
+                    }
+                }
+                MultipleFailureException.assertEmpty(errors);
+            }
+        };
+    }
+
+    public String getBlobEndpoint() {
+        return "http://127.0.0.1:" + getMappedPort() + "/devstoreaccount1";
+    }
+
+    public CloudBlobContainer getContainer(String name) throws URISyntaxException, StorageException, InvalidKeyException {
+        CloudStorageAccount cloud = getCloudStorageAccount();
+        CloudBlobClient cloudBlobClient = cloud.createCloudBlobClient();
+        CloudBlobContainer container = cloudBlobClient.getContainerReference(name);
+        container.deleteIfExists();
+        container.create();
+        return container;
+    }
+
+    public CloudStorageAccount getCloudStorageAccount() throws URISyntaxException, InvalidKeyException {
+        String blobEndpoint = "BlobEndpoint=" + getBlobEndpoint();
+        String accountName = "AccountName=" + ACCOUNT_NAME;
+        String accountKey = "AccountKey=" + ACCOUNT_KEY;
+        return CloudStorageAccount.parse("DefaultEndpointsProtocol=http;" + ";" + accountName + ";" + accountKey + ";" + blobEndpoint);
+    }
+
+    public int getMappedPort() {
+        return azuriteContainer.getMappedPort(10000);
+    }
+}
diff --git a/oak-it/pom.xml b/oak-it/pom.xml
index 7a0010ac17..9ec2cf2196 100644
--- a/oak-it/pom.xml
+++ b/oak-it/pom.xml
@@ -220,8 +220,8 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>com.arakelian</groupId>
-            <artifactId>docker-junit-rule</artifactId>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>testcontainers</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/oak-jcr/pom.xml b/oak-jcr/pom.xml
index d4dc1bfabb..f02d67453c 100644
--- a/oak-jcr/pom.xml
+++ b/oak-jcr/pom.xml
@@ -498,8 +498,8 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>com.arakelian</groupId>
-      <artifactId>docker-junit-rule</artifactId>
+      <groupId>org.testcontainers</groupId>
+      <artifactId>testcontainers</artifactId>
       <scope>test</scope>
     </dependency>
   </dependencies>
diff --git a/oak-lucene/pom.xml b/oak-lucene/pom.xml
index cbde5da851..920c340505 100644
--- a/oak-lucene/pom.xml
+++ b/oak-lucene/pom.xml
@@ -430,8 +430,8 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>com.arakelian</groupId>
-      <artifactId>docker-junit-rule</artifactId>
+      <groupId>org.testcontainers</groupId>
+      <artifactId>testcontainers</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>
diff --git a/oak-parent/pom.xml b/oak-parent/pom.xml
index 7ab36157cc..8dee1e76c5 100644
--- a/oak-parent/pom.xml
+++ b/oak-parent/pom.xml
@@ -681,10 +681,12 @@
         <artifactId>httpmime</artifactId>
         <version>4.5.14</version>
       </dependency>
+      <!-- Testcontainers dependency -->
       <dependency>
-        <groupId>com.arakelian</groupId>
-        <artifactId>docker-junit-rule</artifactId>
-        <version>2.3.0</version>
+        <groupId>org.testcontainers</groupId>
+        <artifactId>testcontainers</artifactId>
+        <version>${testcontainers.version}</version>
+        <scope>test</scope>
       </dependency>
       <dependency>
         <groupId>com.microsoft.azure</groupId>
diff --git a/oak-pojosr/pom.xml b/oak-pojosr/pom.xml
index cd6d1e5372..03fa7ac322 100644
--- a/oak-pojosr/pom.xml
+++ b/oak-pojosr/pom.xml
@@ -369,8 +369,8 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>com.arakelian</groupId>
-      <artifactId>docker-junit-rule</artifactId>
+      <groupId>org.testcontainers</groupId>
+      <artifactId>testcontainers</artifactId>
       <scope>test</scope>
     </dependency>
   </dependencies>
diff --git a/oak-run-commons/pom.xml b/oak-run-commons/pom.xml
index 3af6591a4e..6b190a6650 100644
--- a/oak-run-commons/pom.xml
+++ b/oak-run-commons/pom.xml
@@ -149,8 +149,8 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>com.arakelian</groupId>
-            <artifactId>docker-junit-rule</artifactId>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>testcontainers</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
diff --git a/oak-run/pom.xml b/oak-run/pom.xml
index 7cb5fc32ca..ab82bc0086 100644
--- a/oak-run/pom.xml
+++ b/oak-run/pom.xml
@@ -447,8 +447,8 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>com.arakelian</groupId>
-      <artifactId>docker-junit-rule</artifactId>
+      <groupId>org.testcontainers</groupId>
+      <artifactId>testcontainers</artifactId>
       <scope>test</scope>
     </dependency>
   </dependencies>
diff --git a/oak-segment-azure/pom.xml b/oak-segment-azure/pom.xml
index 44215631d4..22d2bf0753 100644
--- a/oak-segment-azure/pom.xml
+++ b/oak-segment-azure/pom.xml
@@ -176,8 +176,8 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>com.arakelian</groupId>
-            <artifactId>docker-junit-rule</artifactId>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>testcontainers</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/oak-store-document/pom.xml b/oak-store-document/pom.xml
index e9b93ce162..b10bf91a57 100644
--- a/oak-store-document/pom.xml
+++ b/oak-store-document/pom.xml
@@ -318,11 +318,6 @@
       <artifactId>metrics-core</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>com.arakelian</groupId>
-      <artifactId>docker-junit-rule</artifactId>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>org.testcontainers</groupId>
       <artifactId>testcontainers</artifactId>
diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java
index 3ffefb8cf6..dd571a9105 100644
--- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java
+++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java
@@ -55,7 +55,7 @@ public class MongoConnectionFactory extends ExternalResource {
         MongoConnection c = MongoUtils.getConnection(dbName);
         if (c == null && MongoDockerRule.isDockerAvailable()) {
             // fall back to docker if available
-            c = new MongoConnection("localhost", mongo.getPort(), dbName);
+            c = new MongoConnection(mongo.getHost(), mongo.getPort(), dbName);
         }
         assumeNotNull(c);
         if (c != null) {
diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDockerRule.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDockerRule.java
index 3339b6f6c5..c7d204b69d 100644
--- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDockerRule.java
+++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDockerRule.java
@@ -16,60 +16,117 @@
  */
 package org.apache.jackrabbit.oak.plugins.document.mongo;
 
-import java.util.UUID;
-
-import com.arakelian.docker.junit.DockerRule;
-import com.arakelian.docker.junit.model.ImmutableDockerConfig;
-import com.spotify.docker.client.DefaultDockerClient;
-import com.spotify.docker.client.auth.FixedRegistryAuthSupplier;
-
-import org.apache.jackrabbit.oak.plugins.document.MongoUtils;
+import org.junit.Assume;
+import org.junit.rules.ExternalResource;
+import org.junit.runner.Description;
+import org.junit.runners.model.MultipleFailureException;
+import org.junit.runners.model.Statement;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.testcontainers.DockerClientFactory;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.images.RemoteDockerImage;
+import org.testcontainers.utility.DockerImageName;
+
+import java.time.Duration;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
- * A MongoDB {@link DockerRule}.
+ * A MongoDB {@link GenericContainer}.
  */
-public class MongoDockerRule extends DockerRule {
+public class MongoDockerRule extends ExternalResource {
 
     private static final Logger LOG = LoggerFactory.getLogger(MongoDockerRule.class);
 
-    private static final String CONFIG_NAME = "MongoDB-" + UUID.randomUUID().toString().substring(0, 8);
-
     private static final String VERSION = System.getProperty("mongo.version", "3.6");
-
-    private static final String IMAGE = "mongo:" + VERSION;
-
+    private static final String MONGO_IMAGE = "mongo:" + VERSION;
+    private static final AtomicReference<Exception> STARTUP_EXCEPTION = new AtomicReference<>();
+    private static final int DEFAULT_MONGO_PORT = 27017;
+    private static final DockerImageName DOCKER_IMAGE_NAME = DockerImageName.parse(MONGO_IMAGE);
     private static final boolean DOCKER_AVAILABLE;
+    private static GenericContainer<?> mongoContainer;
 
     static {
-        boolean available = false;
-        try (DefaultDockerClient client = DefaultDockerClient.fromEnv()
-                .connectTimeoutMillis(5000L).readTimeoutMillis(20000L)
-                .registryAuthSupplier(new FixedRegistryAuthSupplier())
-                .build()) {
-            client.ping();
-            client.pull(IMAGE);
-            available = true;
+
+        boolean dockerAvailable = false;
+        boolean imageAvailable = false;
+        try {
+            dockerAvailable = checkDockerAvailability();
+            if (dockerAvailable) {
+                imageAvailable = checkImageAvailability();
+            } else {
+                LOG.info("docker not available");
+            }
         } catch (Throwable t) {
-            LOG.info("Cannot connect to docker or pull image", t);
+            LOG.error("not able to pull specified mongo image: {}, error: ", MONGO_IMAGE, t);
         }
-        DOCKER_AVAILABLE = available;
+        DOCKER_AVAILABLE = dockerAvailable && imageAvailable;
     }
 
-    public MongoDockerRule() {
-        super(ImmutableDockerConfig.builder()
-                .name(CONFIG_NAME)
-                .image(IMAGE)
-                .ports("27017")
-                .allowRunningBetweenUnitTests(true)
-                .alwaysRemoveContainer(true)
-                .addStartedListener(container -> container.waitForPort("27017/tcp"))
-                .build());
+    @Override
+    protected void before() throws Throwable {
+        if (mongoContainer != null && mongoContainer.isRunning()) {
+            return;
+        }
+        mongoContainer = new GenericContainer<>(DOCKER_IMAGE_NAME)
+                .withExposedPorts(DEFAULT_MONGO_PORT)
+                .withStartupTimeout(Duration.ofMinutes(1));
+
+        try {
+            long startTime = Instant.now().toEpochMilli();
+            mongoContainer.start();
+            LOG.info("mongo container started in: " + (Instant.now().toEpochMilli() - startTime) + " ms");
+        } catch (Exception e) {
+            LOG.error("error while starting mongoDb container, error: ", e);
+            STARTUP_EXCEPTION.set(e);
+            throw e;
+        }
+    }
+
+    @Override
+    public Statement apply(Statement base, Description description) {
+        return new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                try {
+                    before();
+                } catch (Throwable e) {
+                    Assume.assumeNoException(STARTUP_EXCEPTION.get());
+                    throw e;
+                }
+
+                List<Throwable> errors = new ArrayList<>();
+                try {
+                    base.evaluate();
+                } catch (Throwable t) {
+                    errors.add(t);
+                }
+                MultipleFailureException.assertEmpty(errors);
+            }
+        };
+    }
+
+    private static boolean checkImageAvailability() throws TimeoutException {
+        RemoteDockerImage remoteDockerImage = new RemoteDockerImage(DOCKER_IMAGE_NAME);
+        remoteDockerImage.get(1, TimeUnit.MINUTES);
+        return true;
+    }
+
+    private static boolean checkDockerAvailability() {
+        return DockerClientFactory.instance().isDockerAvailable();
     }
 
     public int getPort() {
-        return getContainer().getPortBinding("27017/tcp").getPort();
+        return mongoContainer.getMappedPort(DEFAULT_MONGO_PORT);
+    }
+
+    public String getHost() {
+        return mongoContainer.getHost();
     }
 
     public static boolean isDockerAvailable() {
diff --git a/oak-upgrade/pom.xml b/oak-upgrade/pom.xml
index 011c382819..3f94cb31e4 100644
--- a/oak-upgrade/pom.xml
+++ b/oak-upgrade/pom.xml
@@ -220,11 +220,9 @@
       <type>test-jar</type>
       <scope>test</scope>
     </dependency>
-
-	<dependency>
-      <groupId>com.arakelian</groupId>
-      <artifactId>docker-junit-rule</artifactId>
-      <version>2.1.0</version>
+    <dependency>
+      <groupId>org.testcontainers</groupId>
+      <artifactId>testcontainers</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>