|
| 1 | +package org.testcontainers.scylladb; |
| 2 | + |
| 3 | +import org.testcontainers.containers.GenericContainer; |
| 4 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 5 | +import org.testcontainers.utility.DockerImageName; |
| 6 | +import org.testcontainers.utility.MountableFile; |
| 7 | + |
| 8 | +import java.net.InetSocketAddress; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +/** |
| 12 | + * Testcontainers implementation for ScyllaDB. |
| 13 | + * <p> |
| 14 | + * Supported image: {@code scylladb/scylla} |
| 15 | + * <p> |
| 16 | + * Exposed ports: |
| 17 | + * <ul> |
| 18 | + * <li>CQL Port: 9042</li> |
| 19 | + * <li>Shard Aware Port: 19042</li> |
| 20 | + * <li>Alternator Port: 8000</li> |
| 21 | + * </ul> |
| 22 | + */ |
| 23 | +public class ScyllaDBContainer extends GenericContainer<ScyllaDBContainer> { |
| 24 | + |
| 25 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("scylladb/scylla"); |
| 26 | + |
| 27 | + private static final Integer CQL_PORT = 9042; |
| 28 | + |
| 29 | + private static final Integer SHARD_AWARE_PORT = 19042; |
| 30 | + |
| 31 | + private static final Integer ALTERNATOR_PORT = 8000; |
| 32 | + |
| 33 | + private static final String COMMAND = "--developer-mode=1 --overprovisioned=1"; |
| 34 | + |
| 35 | + private static final String CONTAINER_CONFIG_LOCATION = "/etc/scylla"; |
| 36 | + |
| 37 | + private boolean alternatorEnabled = false; |
| 38 | + |
| 39 | + private String configLocation; |
| 40 | + |
| 41 | + public ScyllaDBContainer(String dockerImageName) { |
| 42 | + this(DockerImageName.parse(dockerImageName)); |
| 43 | + } |
| 44 | + |
| 45 | + public ScyllaDBContainer(DockerImageName dockerImageName) { |
| 46 | + super(dockerImageName); |
| 47 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 48 | + |
| 49 | + withExposedPorts(CQL_PORT, SHARD_AWARE_PORT); |
| 50 | + |
| 51 | + withCommand(COMMAND); |
| 52 | + waitingFor(Wait.forLogMessage(".*initialization completed..*", 1)); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void configure() { |
| 57 | + if (this.alternatorEnabled) { |
| 58 | + addExposedPort(8000); |
| 59 | + String newCommand = |
| 60 | + COMMAND + " --alternator-port=" + ALTERNATOR_PORT + " --alternator-write-isolation=always"; |
| 61 | + withCommand(newCommand); |
| 62 | + } |
| 63 | + |
| 64 | + // Map (effectively replace) directory in Docker with the content of resourceLocation if resource location is |
| 65 | + // not null. |
| 66 | + Optional |
| 67 | + .ofNullable(configLocation) |
| 68 | + .map(MountableFile::forClasspathResource) |
| 69 | + .ifPresent(mountableFile -> withCopyFileToContainer(mountableFile, CONTAINER_CONFIG_LOCATION)); |
| 70 | + } |
| 71 | + |
| 72 | + public ScyllaDBContainer withConfigurationOverride(String configLocation) { |
| 73 | + this.configLocation = configLocation; |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + public ScyllaDBContainer withSsl(MountableFile certificate, MountableFile keyfile, MountableFile truststore) { |
| 78 | + withCopyFileToContainer(certificate, "/etc/scylla/scylla.cer.pem"); |
| 79 | + withCopyFileToContainer(keyfile, "/etc/scylla/scylla.key.pem"); |
| 80 | + withCopyFileToContainer(truststore, "/etc/scylla/scylla.truststore"); |
| 81 | + withEnv("SSL_CERTFILE", "/etc/scylla/scylla.cer.pem"); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public ScyllaDBContainer withAlternator() { |
| 86 | + this.alternatorEnabled = true; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Retrieve an {@link InetSocketAddress} for connecting to the ScyllaDB container via the driver. |
| 92 | + * |
| 93 | + * @return A InetSocketAddress representation of this ScyllaDB container's host and port. |
| 94 | + */ |
| 95 | + public InetSocketAddress getContactPoint() { |
| 96 | + return new InetSocketAddress(getHost(), getMappedPort(CQL_PORT)); |
| 97 | + } |
| 98 | + |
| 99 | + public InetSocketAddress getShardAwareContactPoint() { |
| 100 | + return new InetSocketAddress(getHost(), getMappedPort(SHARD_AWARE_PORT)); |
| 101 | + } |
| 102 | + |
| 103 | + public String getAlternatorEndpoint() { |
| 104 | + if (!this.alternatorEnabled) { |
| 105 | + throw new IllegalStateException("Alternator is not enabled"); |
| 106 | + } |
| 107 | + return "http://" + getHost() + ":" + getMappedPort(ALTERNATOR_PORT); |
| 108 | + } |
| 109 | +} |
0 commit comments