|
| 1 | +package org.testcontainers.cassandra; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import org.testcontainers.cassandra.delegate.CassandraDatabaseDelegate; |
| 5 | +import org.testcontainers.cassandra.wait.CassandraQueryWaitStrategy; |
| 6 | +import org.testcontainers.containers.GenericContainer; |
| 7 | +import org.testcontainers.ext.ScriptUtils; |
| 8 | +import org.testcontainers.ext.ScriptUtils.ScriptLoadException; |
| 9 | +import org.testcontainers.utility.DockerImageName; |
| 10 | +import org.testcontainers.utility.MountableFile; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.net.InetSocketAddress; |
| 14 | +import java.net.URISyntaxException; |
| 15 | +import java.net.URL; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +/** |
| 19 | + * Testcontainers implementation for Apache Cassandra. |
| 20 | + * <p> |
| 21 | + * Supported image: {@code cassandra} |
| 22 | + * <p> |
| 23 | + * Exposed ports: 9042 |
| 24 | + */ |
| 25 | +public class CassandraContainer extends GenericContainer<CassandraContainer> { |
| 26 | + |
| 27 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("cassandra"); |
| 28 | + |
| 29 | + private static final Integer CQL_PORT = 9042; |
| 30 | + |
| 31 | + private static final String DEFAULT_LOCAL_DATACENTER = "datacenter1"; |
| 32 | + |
| 33 | + private static final String CONTAINER_CONFIG_LOCATION = "/etc/cassandra"; |
| 34 | + |
| 35 | + private static final String USERNAME = "cassandra"; |
| 36 | + |
| 37 | + private static final String PASSWORD = "cassandra"; |
| 38 | + |
| 39 | + private String configLocation; |
| 40 | + |
| 41 | + private String initScriptPath; |
| 42 | + |
| 43 | + public CassandraContainer(String dockerImageName) { |
| 44 | + this(DockerImageName.parse(dockerImageName)); |
| 45 | + } |
| 46 | + |
| 47 | + public CassandraContainer(DockerImageName dockerImageName) { |
| 48 | + super(dockerImageName); |
| 49 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 50 | + |
| 51 | + addExposedPort(CQL_PORT); |
| 52 | + |
| 53 | + withEnv("CASSANDRA_SNITCH", "GossipingPropertyFileSnitch"); |
| 54 | + withEnv("JVM_OPTS", "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0"); |
| 55 | + withEnv("HEAP_NEWSIZE", "128M"); |
| 56 | + withEnv("MAX_HEAP_SIZE", "1024M"); |
| 57 | + withEnv("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch"); |
| 58 | + withEnv("CASSANDRA_DC", DEFAULT_LOCAL_DATACENTER); |
| 59 | + |
| 60 | + // Use the CassandraQueryWaitStrategy by default to avoid potential issues when the authentication is enabled. |
| 61 | + waitingFor(new CassandraQueryWaitStrategy()); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void configure() { |
| 66 | + // Map (effectively replace) directory in Docker with the content of resourceLocation if resource location is |
| 67 | + // not null. |
| 68 | + Optional |
| 69 | + .ofNullable(configLocation) |
| 70 | + .map(MountableFile::forClasspathResource) |
| 71 | + .ifPresent(mountableFile -> withCopyFileToContainer(mountableFile, CONTAINER_CONFIG_LOCATION)); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 76 | + runInitScriptIfRequired(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Load init script content and apply it to the database if initScriptPath is set |
| 81 | + */ |
| 82 | + private void runInitScriptIfRequired() { |
| 83 | + if (initScriptPath != null) { |
| 84 | + 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); |
| 99 | + } catch (ScriptUtils.ScriptStatementFailedException e) { |
| 100 | + logger().error("Error while executing init script: {}", initScriptPath, e); |
| 101 | + throw new ScriptUtils.UncategorizedScriptException( |
| 102 | + "Error while executing init script: " + initScriptPath, |
| 103 | + e |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Initialize Cassandra with the custom overridden Cassandra configuration |
| 111 | + * <p> |
| 112 | + * Be aware, that Docker effectively replaces all /etc/cassandra content with the content of config location, so if |
| 113 | + * Cassandra.yaml in configLocation is absent or corrupted, then Cassandra just won't launch |
| 114 | + * |
| 115 | + * @param configLocation relative classpath with the directory that contains cassandra.yaml and other configuration files |
| 116 | + */ |
| 117 | + public CassandraContainer withConfigurationOverride(String configLocation) { |
| 118 | + this.configLocation = configLocation; |
| 119 | + return self(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Initialize Cassandra with init CQL script |
| 124 | + * <p> |
| 125 | + * CQL script will be applied after container is started (see using WaitStrategy). |
| 126 | + * </p> |
| 127 | + * |
| 128 | + * @param initScriptPath relative classpath resource |
| 129 | + */ |
| 130 | + public CassandraContainer withInitScript(String initScriptPath) { |
| 131 | + this.initScriptPath = initScriptPath; |
| 132 | + return self(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get username |
| 137 | + * |
| 138 | + * By default, Cassandra has authenticator: AllowAllAuthenticator in cassandra.yaml |
| 139 | + * If username and password need to be used, then authenticator should be set as PasswordAuthenticator |
| 140 | + * (through custom Cassandra configuration) and through CQL with default cassandra-cassandra credentials |
| 141 | + * user management should be modified |
| 142 | + */ |
| 143 | + public String getUsername() { |
| 144 | + return USERNAME; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Get password |
| 149 | + * |
| 150 | + * By default, Cassandra has authenticator: AllowAllAuthenticator in cassandra.yaml |
| 151 | + * If username and password need to be used, then authenticator should be set as PasswordAuthenticator |
| 152 | + * (through custom Cassandra configuration) and through CQL with default cassandra-cassandra credentials |
| 153 | + * user management should be modified |
| 154 | + */ |
| 155 | + public String getPassword() { |
| 156 | + return PASSWORD; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Retrieve an {@link InetSocketAddress} for connecting to the Cassandra container via the driver. |
| 161 | + * |
| 162 | + * @return A InetSocketAddrss representation of this Cassandra container's host and port. |
| 163 | + */ |
| 164 | + public InetSocketAddress getContactPoint() { |
| 165 | + return new InetSocketAddress(getHost(), getMappedPort(CQL_PORT)); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Retrieve the Local Datacenter for connecting to the Cassandra container via the driver. |
| 170 | + * |
| 171 | + * @return The configured local Datacenter name. |
| 172 | + */ |
| 173 | + public String getLocalDatacenter() { |
| 174 | + return getEnvMap().getOrDefault("CASSANDRA_DC", DEFAULT_LOCAL_DATACENTER); |
| 175 | + } |
| 176 | +} |
0 commit comments