Skip to content

Commit b3bcf36

Browse files
Use generic init script filename when copying it into a CassandraContainer (#9606)
Fixes #9574 --------- Co-authored-by: Eddú Meléndez Gonzales <eddu.melendez@gmail.com>
1 parent 8ad5c41 commit b3bcf36

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

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

Lines changed: 18 additions & 20 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";
@@ -80,26 +79,25 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
8079
* Load init script content and apply it to the database if initScriptPath is set
8180
*/
8281
private void runInitScriptIfRequired() {
83-
if (initScriptPath != null) {
82+
if (this.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(this.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: {}", this.initScriptPath);
93+
throw new ScriptLoadException(
94+
"Could not load classpath init script: " + this.initScriptPath + ". Resource not found.",
95+
e
96+
);
9997
} catch (ScriptUtils.ScriptStatementFailedException e) {
100-
logger().error("Error while executing init script: {}", initScriptPath, e);
98+
logger().error("Error while executing init script: {}", this.initScriptPath, e);
10199
throw new ScriptUtils.UncategorizedScriptException(
102-
"Error while executing init script: " + initScriptPath,
100+
"Error while executing init script: " + this.initScriptPath,
103101
e
104102
);
105103
}

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

Lines changed: 11 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,16 @@ 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)).isInstanceOf(ContainerLaunchException.class);
92+
}
93+
}
94+
8495
@Test
8596
public void testInitScriptWithRequiredAuthentication() {
8697
try (

0 commit comments

Comments
 (0)