From 279ba10388e3d1c11ba1d4d2f6e962bbfcbbafd9 Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Tue, 28 Nov 2023 12:26:25 +0100 Subject: [PATCH] Allow enforcement to reuse a container in the CI build. This orders reusable containers on wish and is meant for scenarios in which a CI does not allow "native" use of reusable test containers but will make sure to clean them up after CI ran. Export a variable `SDN_FORCE_REUSE_OF_CONTAINERS` as `true` and SDN tests won't close the container, regardless whether the underlying Testcontainers supports reuse or not. Closes #2837 --- ci/test.sh | 1 + .../data/neo4j/test/Neo4jExtension.java | 33 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ci/test.sh b/ci/test.sh index 73aeb0364..9b89971b2 100755 --- a/ci/test.sh +++ b/ci/test.sh @@ -6,6 +6,7 @@ mkdir -p /tmp/jenkins-home chown -R 1001:1001 . export JENKINS_USER=${JENKINS_USER_NAME} +export SDN_FORCE_REUSE_OF_CONTAINERS=true MAVEN_OPTS="-Duser.name=${JENKINS_USER} -Duser.home=/tmp/jenkins-home -Dscan=false" \ ./mvnw -s settings.xml -P${PROFILE} clean dependency:list verify -Dsort -U -B -Dmaven.repo.local=/tmp/jenkins-home/.m2/spring-data-neo4j diff --git a/src/test/java/org/springframework/data/neo4j/test/Neo4jExtension.java b/src/test/java/org/springframework/data/neo4j/test/Neo4jExtension.java index fa682fbf4..72e9d819b 100644 --- a/src/test/java/org/springframework/data/neo4j/test/Neo4jExtension.java +++ b/src/test/java/org/springframework/data/neo4j/test/Neo4jExtension.java @@ -21,12 +21,9 @@ import lombok.extern.apachecommons.CommonsLog; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Optional; import java.util.Set; @@ -84,10 +81,11 @@ public class Neo4jExtension implements BeforeAllCallback, BeforeEachCallback { private static final String SYS_PROPERTY_NEO4J_ACCEPT_COMMERCIAL_EDITION = "SDN_NEO4J_ACCEPT_COMMERCIAL_EDITION"; private static final String SYS_PROPERTY_NEO4J_REPOSITORY = "SDN_NEO4J_REPOSITORY"; private static final String SYS_PROPERTY_NEO4J_VERSION = "SDN_NEO4J_VERSION"; + private static final String SYS_PROPERTY_FORCE_CONTAINER_REUSE = "SDN_FORCE_REUSE_OF_CONTAINERS"; - private static Set COMMUNITY_EDITION_INDICATOR = Collections.singleton("community"); + private static Set COMMUNITY_EDITION_INDICATOR = Set.of("community"); - private static Set COMMERCIAL_EDITION_INDICATOR = new HashSet<>(Arrays.asList("commercial", "enterprise")); + private static Set COMMERCIAL_EDITION_INDICATOR = Set.of("commercial", "enterprise"); @Override public void beforeAll(ExtensionContext context) throws Exception { @@ -296,21 +294,20 @@ public class Neo4jExtension implements BeforeAllCallback, BeforeEachCallback { static class ContainerAdapter implements ExtensionContext.Store.CloseableResource { - private final String repository = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_REPOSITORY)).orElse("neo4j"); + private static final String repository = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_REPOSITORY)).orElse("neo4j"); - private final String imageVersion = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_VERSION)).orElse("5"); + private static final String imageVersion = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_VERSION)).orElse("5"); - private final boolean containerReuseSupported = TestcontainersConfiguration + private static final boolean containerReuseSupported = TestcontainersConfiguration .getInstance().environmentSupportsReuse(); - private final Neo4jContainer neo4jContainer = new Neo4jContainer<>(repository + ":" + imageVersion) + private static final boolean forceReuse = Boolean.parseBoolean(System.getenv(SYS_PROPERTY_FORCE_CONTAINER_REUSE)); + + private static final Neo4jContainer neo4jContainer = new Neo4jContainer<>(repository + ":" + imageVersion) .withoutAuthentication() .withEnv("NEO4J_ACCEPT_LICENSE_AGREEMENT", Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_ACCEPT_COMMERCIAL_EDITION)).orElse("no")) - .withTmpFs(new HashMap() {{ // K.W. Gedächtnis-Double-Brace-Initialization - put("/log", "rw"); - put("/data", "rw"); - }}) + .withTmpFs(Map.of("/log", "rw", "/data", "rw")) .withReuse(containerReuseSupported); public String getBoltUrl() { @@ -318,13 +315,15 @@ public class Neo4jExtension implements BeforeAllCallback, BeforeEachCallback { } public void start() { - neo4jContainer.start(); + if (!neo4jContainer.isRunning()) { + neo4jContainer.start(); + } } @Override public void close() { - if (!containerReuseSupported) { - this.neo4jContainer.close(); + if (!(containerReuseSupported || forceReuse)) { + neo4jContainer.close(); } } }