Skip to content

Commit 3a49757

Browse files
committed
Use generic init script filename when copying it into a Cassandra container
fixes #9574
1 parent 8ce2045 commit 3a49757

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
import org.testcontainers.utility.DockerImageName;
1010
import org.testcontainers.utility.MountableFile;
1111

12-
import java.io.File;
1312
import java.net.InetSocketAddress;
14-
import java.net.URISyntaxException;
15-
import java.net.URL;
1613
import java.util.Optional;
1714

1815
/**
@@ -30,6 +27,8 @@ public class CassandraContainer extends GenericContainer<CassandraContainer> {
3027

3128
private static final String DEFAULT_LOCAL_DATACENTER = "datacenter1";
3229

30+
private static final String DEFAULT_INIT_SCRIPT_FILENAME = "init.cql";
31+
3332
private static final String CONTAINER_CONFIG_LOCATION = "/etc/cassandra";
3433

3534
private static final String USERNAME = "cassandra";
@@ -82,20 +81,17 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
8281
private void runInitScriptIfRequired() {
8382
if (initScriptPath != null) {
8483
try {
85-
URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
86-
if (resource == null) {
87-
logger().warn("Could not load classpath init script: {}", initScriptPath);
88-
throw new ScriptLoadException(
89-
"Could not load classpath init script: " + initScriptPath + ". Resource not found."
90-
);
91-
}
92-
// The init script is executed as is by the cqlsh command, so copy it into the container.
93-
String targetInitScriptName = new File(resource.toURI()).getName();
94-
copyFileToContainer(MountableFile.forClasspathResource(initScriptPath), targetInitScriptName);
95-
new CassandraDatabaseDelegate(this).execute(null, targetInitScriptName, -1, false, false);
96-
} catch (URISyntaxException e) {
97-
logger().warn("Could not copy init script into container: {}", initScriptPath);
98-
throw new ScriptLoadException("Could not copy init script into container: " + initScriptPath, e);
84+
final MountableFile originalInitScript = MountableFile.forClasspathResource(initScriptPath);
85+
// The init script is executed as is by the cqlsh command, so copy it into the container. The name
86+
// of the script is generic since it's not important to keep the original name.
87+
copyFileToContainer(originalInitScript, DEFAULT_INIT_SCRIPT_FILENAME);
88+
new CassandraDatabaseDelegate(this).execute(null, DEFAULT_INIT_SCRIPT_FILENAME, -1, false, false);
89+
} catch (IllegalArgumentException e) {
90+
// MountableFile.forClasspathResource will throw an IllegalArgumentException if the resource cannot
91+
// be found.
92+
logger().warn("Could not load classpath init script: {}", initScriptPath);
93+
throw new ScriptLoadException(
94+
"Could not load classpath init script: " + initScriptPath + ". Resource not found.", e);
9995
} catch (ScriptUtils.ScriptStatementFailedException e) {
10096
logger().error("Error while executing init script: {}", initScriptPath, e);
10197
throw new ScriptUtils.UncategorizedScriptException(

modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.testcontainers.utility.DockerImageName;
99

1010
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.catchThrowable;
1112

1213
public class CassandraContainerTest {
1314

@@ -81,6 +82,17 @@ public void testInitScript() {
8182
}
8283
}
8384

85+
@Test
86+
public void testNonexistentInitScript() {
87+
try (
88+
CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE)
89+
.withInitScript("unknown_script.cql")
90+
) {
91+
assertThat(catchThrowable(cassandraContainer::start))
92+
.isInstanceOf(ContainerLaunchException.class);
93+
}
94+
}
95+
8496
@Test
8597
public void testInitScriptWithRequiredAuthentication() {
8698
try (

0 commit comments

Comments
 (0)