Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generic init script filename when copying it into a Cassandra container #9606

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

import java.io.File;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Optional;

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

private static final String DEFAULT_LOCAL_DATACENTER = "datacenter1";

private static final String DEFAULT_INIT_SCRIPT_FILENAME = "init.cql";

private static final String CONTAINER_CONFIG_LOCATION = "/etc/cassandra";

private static final String USERNAME = "cassandra";
Expand Down Expand Up @@ -82,20 +81,17 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
private void runInitScriptIfRequired() {
if (initScriptPath != null) {
try {
URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
if (resource == null) {
logger().warn("Could not load classpath init script: {}", initScriptPath);
throw new ScriptLoadException(
"Could not load classpath init script: " + initScriptPath + ". Resource not found."
);
}
// The init script is executed as is by the cqlsh command, so copy it into the container.
String targetInitScriptName = new File(resource.toURI()).getName();
copyFileToContainer(MountableFile.forClasspathResource(initScriptPath), targetInitScriptName);
new CassandraDatabaseDelegate(this).execute(null, targetInitScriptName, -1, false, false);
} catch (URISyntaxException e) {
logger().warn("Could not copy init script into container: {}", initScriptPath);
throw new ScriptLoadException("Could not copy init script into container: " + initScriptPath, e);
final MountableFile originalInitScript = MountableFile.forClasspathResource(initScriptPath);
// The init script is executed as is by the cqlsh command, so copy it into the container. The name
// of the script is generic since it's not important to keep the original name.
copyFileToContainer(originalInitScript, DEFAULT_INIT_SCRIPT_FILENAME);
new CassandraDatabaseDelegate(this).execute(null, DEFAULT_INIT_SCRIPT_FILENAME, -1, false, false);
} catch (IllegalArgumentException e) {
// MountableFile.forClasspathResource will throw an IllegalArgumentException if the resource cannot
// be found.
logger().warn("Could not load classpath init script: {}", initScriptPath);
throw new ScriptLoadException(
"Could not load classpath init script: " + initScriptPath + ". Resource not found.", e);
} catch (ScriptUtils.ScriptStatementFailedException e) {
logger().error("Error while executing init script: {}", initScriptPath, e);
throw new ScriptUtils.UncategorizedScriptException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

public class CassandraContainerTest {

Expand Down Expand Up @@ -81,6 +82,17 @@ public void testInitScript() {
}
}

@Test
public void testNonexistentInitScript() {
try (
CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE)
.withInitScript("unknown_script.cql")
) {
assertThat(catchThrowable(cassandraContainer::start))
.isInstanceOf(ContainerLaunchException.class);
}
}

@Test
public void testInitScriptWithRequiredAuthentication() {
try (
Expand Down
Loading