Skip to content

Commit bfbfc05

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 f587c5b commit bfbfc05

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
@@ -8,6 +8,7 @@ chown -R 1001:1001 .
88
export DEVELOCITY_CACHE_USERNAME=${DEVELOCITY_CACHE_USR}
99
export DEVELOCITY_CACHE_PASSWORD=${DEVELOCITY_CACHE_PSW}
1010
export JENKINS_USER=${JENKINS_USER_NAME}
11+
export SDN_FORCE_REUSE_OF_CONTAINERS=true
1112

1213
# The environment variable to configure access key is still GRADLE_ENTERPRISE_ACCESS_KEY
1314
export GRADLE_ENTERPRISE_ACCESS_KEY=${DEVELOCITY_ACCESS_KEY}

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

+16-17
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,9 @@
3838

3939
import java.lang.reflect.Field;
4040
import java.lang.reflect.Modifier;
41-
import java.util.Arrays;
42-
import java.util.Collections;
43-
import java.util.HashMap;
44-
import java.util.HashSet;
4541
import java.util.List;
4642
import java.util.Locale;
43+
import java.util.Map;
4744
import java.util.Optional;
4845
import java.util.Set;
4946

@@ -82,11 +79,12 @@ public class Neo4jExtension implements BeforeAllCallback, BeforeEachCallback {
8279
private static final String SYS_PROPERTY_NEO4J_ACCEPT_COMMERCIAL_EDITION = "SDN_NEO4J_ACCEPT_COMMERCIAL_EDITION";
8380
private static final String SYS_PROPERTY_NEO4J_REPOSITORY = "SDN_NEO4J_REPOSITORY";
8481
private static final String SYS_PROPERTY_NEO4J_VERSION = "SDN_NEO4J_VERSION";
82+
private static final String SYS_PROPERTY_FORCE_CONTAINER_REUSE = "SDN_FORCE_REUSE_OF_CONTAINERS";
8583
private static final Log log = org.apache.commons.logging.LogFactory.getLog(Neo4jExtension.class);
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 {
@@ -295,35 +293,36 @@ public void close() {
295293

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

298-
private final String repository = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_REPOSITORY)).orElse("neo4j");
296+
private static final String repository = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_REPOSITORY)).orElse("neo4j");
299297

300-
private final String imageVersion = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_VERSION)).orElse("5");
298+
private static final String imageVersion = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_VERSION)).orElse("5");
301299

302-
private final boolean containerReuseSupported = TestcontainersConfiguration
300+
private static final boolean containerReuseSupported = TestcontainersConfiguration
303301
.getInstance().environmentSupportsReuse();
304302

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

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

319316
public void start() {
320-
neo4jContainer.start();
317+
if (!neo4jContainer.isRunning()) {
318+
neo4jContainer.start();
319+
}
321320
}
322321

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

0 commit comments

Comments
 (0)