Skip to content

Commit 279ba10

Browse files
michael-simonsmp911de
authored andcommitted
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
1 parent 0f54c6f commit 279ba10

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

ci/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mkdir -p /tmp/jenkins-home
66
chown -R 1001:1001 .
77

88
export JENKINS_USER=${JENKINS_USER_NAME}
9+
export SDN_FORCE_REUSE_OF_CONTAINERS=true
910

1011
MAVEN_OPTS="-Duser.name=${JENKINS_USER} -Duser.home=/tmp/jenkins-home -Dscan=false" \
1112
./mvnw -s settings.xml -P${PROFILE} clean dependency:list verify -Dsort -U -B -Dmaven.repo.local=/tmp/jenkins-home/.m2/spring-data-neo4j

src/test/java/org/springframework/data/neo4j/test/Neo4jExtension.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121

2222
import java.lang.reflect.Field;
2323
import java.lang.reflect.Modifier;
24-
import java.util.Arrays;
25-
import java.util.Collections;
26-
import java.util.HashMap;
27-
import java.util.HashSet;
2824
import java.util.List;
2925
import java.util.Locale;
26+
import java.util.Map;
3027
import java.util.Optional;
3128
import java.util.Set;
3229

@@ -84,10 +81,11 @@ public class Neo4jExtension implements BeforeAllCallback, BeforeEachCallback {
8481
private static final String SYS_PROPERTY_NEO4J_ACCEPT_COMMERCIAL_EDITION = "SDN_NEO4J_ACCEPT_COMMERCIAL_EDITION";
8582
private static final String SYS_PROPERTY_NEO4J_REPOSITORY = "SDN_NEO4J_REPOSITORY";
8683
private static final String SYS_PROPERTY_NEO4J_VERSION = "SDN_NEO4J_VERSION";
84+
private static final String SYS_PROPERTY_FORCE_CONTAINER_REUSE = "SDN_FORCE_REUSE_OF_CONTAINERS";
8785

88-
private static Set<String> COMMUNITY_EDITION_INDICATOR = Collections.singleton("community");
86+
private static Set<String> COMMUNITY_EDITION_INDICATOR = Set.of("community");
8987

90-
private static Set<String> COMMERCIAL_EDITION_INDICATOR = new HashSet<>(Arrays.asList("commercial", "enterprise"));
88+
private static Set<String> COMMERCIAL_EDITION_INDICATOR = Set.of("commercial", "enterprise");
9189

9290
@Override
9391
public void beforeAll(ExtensionContext context) throws Exception {
@@ -296,35 +294,36 @@ public void close() {
296294

297295
static class ContainerAdapter implements ExtensionContext.Store.CloseableResource {
298296

299-
private final String repository = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_REPOSITORY)).orElse("neo4j");
297+
private static final String repository = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_REPOSITORY)).orElse("neo4j");
300298

301-
private final String imageVersion = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_VERSION)).orElse("5");
299+
private static final String imageVersion = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_VERSION)).orElse("5");
302300

303-
private final boolean containerReuseSupported = TestcontainersConfiguration
301+
private static final boolean containerReuseSupported = TestcontainersConfiguration
304302
.getInstance().environmentSupportsReuse();
305303

306-
private final Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(repository + ":" + imageVersion)
304+
private static final boolean forceReuse = Boolean.parseBoolean(System.getenv(SYS_PROPERTY_FORCE_CONTAINER_REUSE));
305+
306+
private static final Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(repository + ":" + imageVersion)
307307
.withoutAuthentication()
308308
.withEnv("NEO4J_ACCEPT_LICENSE_AGREEMENT",
309309
Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_ACCEPT_COMMERCIAL_EDITION)).orElse("no"))
310-
.withTmpFs(new HashMap<String, String>() {{ // K.W. Gedächtnis-Double-Brace-Initialization
311-
put("/log", "rw");
312-
put("/data", "rw");
313-
}})
310+
.withTmpFs(Map.of("/log", "rw", "/data", "rw"))
314311
.withReuse(containerReuseSupported);
315312

316313
public String getBoltUrl() {
317314
return neo4jContainer.getBoltUrl();
318315
}
319316

320317
public void start() {
321-
neo4jContainer.start();
318+
if (!neo4jContainer.isRunning()) {
319+
neo4jContainer.start();
320+
}
322321
}
323322

324323
@Override
325324
public void close() {
326-
if (!containerReuseSupported) {
327-
this.neo4jContainer.close();
325+
if (!(containerReuseSupported || forceReuse)) {
326+
neo4jContainer.close();
328327
}
329328
}
330329
}

0 commit comments

Comments
 (0)