Skip to content

Commit cbc7ffa

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 5100eb4 commit cbc7ffa

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

ci/test.sh

+1
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

+16-17
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

@@ -83,10 +80,11 @@ public class Neo4jExtension implements BeforeAllCallback, BeforeEachCallback {
8380
private static final String SYS_PROPERTY_NEO4J_ACCEPT_COMMERCIAL_EDITION = "SDN_NEO4J_ACCEPT_COMMERCIAL_EDITION";
8481
private static final String SYS_PROPERTY_NEO4J_REPOSITORY = "SDN_NEO4J_REPOSITORY";
8582
private static final String SYS_PROPERTY_NEO4J_VERSION = "SDN_NEO4J_VERSION";
83+
private static final String SYS_PROPERTY_FORCE_CONTAINER_REUSE = "SDN_FORCE_REUSE_OF_CONTAINERS";
8684

87-
private static Set<String> COMMUNITY_EDITION_INDICATOR = Collections.singleton("community");
85+
private static Set<String> COMMUNITY_EDITION_INDICATOR = Set.of("community");
8886

89-
private static Set<String> COMMERCIAL_EDITION_INDICATOR = new HashSet<>(Arrays.asList("commercial", "enterprise"));
87+
private static Set<String> COMMERCIAL_EDITION_INDICATOR = Set.of("commercial", "enterprise");
9088

9189
@Override
9290
public void beforeAll(ExtensionContext context) throws Exception {
@@ -291,35 +289,36 @@ public void close() {
291289

292290
static class ContainerAdapter implements ExtensionContext.Store.CloseableResource {
293291

294-
private final String repository = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_REPOSITORY)).orElse("neo4j");
292+
private static final String repository = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_REPOSITORY)).orElse("neo4j");
295293

296-
private final String imageVersion = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_VERSION)).orElse("5");
294+
private static final String imageVersion = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_VERSION)).orElse("5");
297295

298-
private final boolean containerReuseSupported = TestcontainersConfiguration
296+
private static final boolean containerReuseSupported = TestcontainersConfiguration
299297
.getInstance().environmentSupportsReuse();
300298

301-
private final Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(repository + ":" + imageVersion)
299+
private static final boolean forceReuse = Boolean.parseBoolean(System.getenv(SYS_PROPERTY_FORCE_CONTAINER_REUSE));
300+
301+
private static final Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(repository + ":" + imageVersion)
302302
.withoutAuthentication()
303303
.withEnv("NEO4J_ACCEPT_LICENSE_AGREEMENT",
304304
Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_ACCEPT_COMMERCIAL_EDITION)).orElse("no"))
305-
.withTmpFs(new HashMap<String, String>() {{ // K.W. Gedächtnis-Double-Brace-Initialization
306-
put("/log", "rw");
307-
put("/data", "rw");
308-
}})
305+
.withTmpFs(Map.of("/log", "rw", "/data", "rw"))
309306
.withReuse(containerReuseSupported);
310307

311308
public String getBoltUrl() {
312309
return neo4jContainer.getBoltUrl();
313310
}
314311

315312
public void start() {
316-
neo4jContainer.start();
313+
if (!neo4jContainer.isRunning()) {
314+
neo4jContainer.start();
315+
}
317316
}
318317

319318
@Override
320319
public void close() {
321-
if (!containerReuseSupported) {
322-
this.neo4jContainer.close();
320+
if (!(containerReuseSupported || forceReuse)) {
321+
neo4jContainer.close();
323322
}
324323
}
325324
}

0 commit comments

Comments
 (0)