Skip to content

Commit

Permalink
Refine Testcontainer setup for Cassandra 5 startup.
Browse files Browse the repository at this point in the history
The container version is picked up from the env-variable first and then from our project properties.

Adding config file for v5.

See #1503
  • Loading branch information
mp911de committed Jul 24, 2024
1 parent b02492f commit 24e1564
Show file tree
Hide file tree
Showing 2 changed files with 2,243 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.data.cassandra.core.cql.SessionCallback;
import org.springframework.data.cassandra.support.CassandraConnectionProperties;
import org.springframework.data.cassandra.support.CqlDataSet;
import org.springframework.data.util.Optionals;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import org.testcontainers.containers.CassandraContainer;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
Expand Down Expand Up @@ -273,6 +278,14 @@ private void runTestcontainerCassandra() {

container = getCassandraDockerImageName().map(CassandraContainer::new).orElseGet(CassandraContainer::new);

DockerImageName imageName = DockerImageName.parse(container.getDockerImageName());
String versionPart = imageName.getVersionPart();
Version version = Version.parse(versionPart);

if (version.getMajor() >= 5) {
container.withCopyToContainer(MountableFile.forClasspathResource("testcontainer-cassandra-v5.yaml"),
"/etc/cassandra/cassandra.yaml");
}
container.start();

log.info("Running with Cassandra Docker Testcontainer Image Name [{}]", container.getDockerImageName());
Expand All @@ -290,8 +303,8 @@ private Optional<String> getCassandraDockerImageName() {

private Optional<String> resolveCassandraVersion() {

return Optional.ofNullable(System.getProperty("cassandra.version", System.getenv("CASSANDRA_VERSION")))
.filter(StringUtils::hasText);
return Optionals.firstNonEmpty(() -> Optional.ofNullable(System.getenv("CASSANDRA_VERSION")),
() -> Optional.ofNullable(System.getProperty("cassandra.version"))).filter(StringUtils::hasText);
}

private synchronized void initializeConnection() {
Expand Down Expand Up @@ -392,7 +405,7 @@ private void executeAfterHooks() {

private synchronized void releaseConnection() {

if (resourceHolder == null) {
if (resourceHolder == null && this.session != null) {
this.session.close();
}

Expand Down
Loading

0 comments on commit 24e1564

Please sign in to comment.