|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import org.apache.commons.io.IOUtils; |
| 5 | +import org.testcontainers.containers.delegate.ScyllaDBDatabaseDelegate; |
| 6 | +import org.testcontainers.delegate.DatabaseDelegate; |
| 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.IOException; |
| 13 | +import java.net.InetSocketAddress; |
| 14 | +import java.net.URL; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +import javax.script.ScriptException; |
| 19 | + |
| 20 | +/** |
| 21 | + * Testcontainers implementation for ScyllaDB. |
| 22 | + * <p> |
| 23 | + * Supported image: {@code scylladb} |
| 24 | + * <p> |
| 25 | + * Exposed ports: 9042 |
| 26 | + */ |
| 27 | +public class ScyllaDBContainer<SELF extends ScyllaDBContainer<SELF>> extends GenericContainer<SELF> { |
| 28 | + |
| 29 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("scylladb/scylla:5.2.9"); |
| 30 | + |
| 31 | + public static final Integer CQL_PORT = 9042; |
| 32 | + |
| 33 | + private static final String DEFAULT_LOCAL_DATACENTER = "datacenter1"; |
| 34 | + |
| 35 | + private static final String CONTAINER_CONFIG_LOCATION = "/etc/scylla"; |
| 36 | + |
| 37 | + private static final String USERNAME = "scylladb"; |
| 38 | + |
| 39 | + private static final String PASSWORD = "scylladb"; |
| 40 | + |
| 41 | + private String configLocation; |
| 42 | + |
| 43 | + private String initScriptPath; |
| 44 | + |
| 45 | + private boolean enableJmxReporting; |
| 46 | + |
| 47 | + public ScyllaDBContainer(String dockerImageName) { |
| 48 | + this(DockerImageName.parse(dockerImageName)); |
| 49 | + } |
| 50 | + |
| 51 | + public ScyllaDBContainer(DockerImageName dockerImageName) { |
| 52 | + super(dockerImageName); |
| 53 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 54 | + |
| 55 | + addExposedPort(CQL_PORT); |
| 56 | + this.enableJmxReporting = false; |
| 57 | + |
| 58 | + withEnv("CASSANDRA_SNITCH", "GossipingPropertyFileSnitch"); |
| 59 | + withEnv("JVM_OPTS", "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0"); |
| 60 | + withEnv("HEAP_NEWSIZE", "128M"); |
| 61 | + withEnv("MAX_HEAP_SIZE", "1024M"); |
| 62 | + withEnv("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch"); |
| 63 | + withEnv("CASSANDRA_DC", DEFAULT_LOCAL_DATACENTER); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void configure() { |
| 68 | + optionallyMapResourceParameterAsVolume(CONTAINER_CONFIG_LOCATION, configLocation); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 73 | + runInitScriptIfRequired(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Load init script content and apply it to the database if initScriptPath is set |
| 78 | + */ |
| 79 | + private void runInitScriptIfRequired() { |
| 80 | + if (initScriptPath != null) { |
| 81 | + try { |
| 82 | + URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath); |
| 83 | + if (resource == null) { |
| 84 | + logger().warn("Could not load classpath init script: {}", initScriptPath); |
| 85 | + throw new ScriptLoadException( |
| 86 | + "Could not load classpath init script: " + initScriptPath + ". Resource not found." |
| 87 | + ); |
| 88 | + } |
| 89 | + String cql = IOUtils.toString(resource, StandardCharsets.UTF_8); |
| 90 | + DatabaseDelegate databaseDelegate = new ScyllaDBDatabaseDelegate(this); |
| 91 | + ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, cql); |
| 92 | + } catch (IOException e) { |
| 93 | + logger().warn("Could not load classpath init script: {}", initScriptPath); |
| 94 | + throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e); |
| 95 | + } catch (ScriptException e) { |
| 96 | + logger().error("Error while executing init script: {}", initScriptPath, e); |
| 97 | + throw new ScriptUtils.UncategorizedScriptException( |
| 98 | + "Error while executing init script: " + initScriptPath, |
| 99 | + e |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Map (effectively replace) directory in Docker with the content of resourceLocation if resource location is not null |
| 107 | + * |
| 108 | + * Protected to allow for changing implementation by extending the class |
| 109 | + * |
| 110 | + * @param pathNameInContainer path in docker |
| 111 | + * @param resourceLocation relative classpath to resource |
| 112 | + */ |
| 113 | + protected void optionallyMapResourceParameterAsVolume(String pathNameInContainer, String resourceLocation) { |
| 114 | + Optional |
| 115 | + .ofNullable(resourceLocation) |
| 116 | + .map(MountableFile::forClasspathResource) |
| 117 | + .ifPresent(mountableFile -> withCopyFileToContainer(mountableFile, pathNameInContainer)); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Initialize ScyllaDB with the custom overridden ScyllaDB configuration |
| 122 | + * <p> |
| 123 | + * Be aware, that Docker effectively replaces all /etc/sylladb content with the content of config location, so if |
| 124 | + * scylladb.yaml in configLocation is absent or corrupted, then ScyllaDB just won't launch |
| 125 | + * |
| 126 | + * @param configLocation relative classpath with the directory that contains cassandra.yaml and other configuration files |
| 127 | + */ |
| 128 | + public SELF withConfigurationOverride(String configLocation) { |
| 129 | + this.configLocation = configLocation; |
| 130 | + return self(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Initialize ScyllaDB with init CQL script |
| 135 | + * <p> |
| 136 | + * CQL script will be applied after container is started (see using WaitStrategy) |
| 137 | + * |
| 138 | + * @param initScriptPath relative classpath resource |
| 139 | + */ |
| 140 | + public SELF withInitScript(String initScriptPath) { |
| 141 | + this.initScriptPath = initScriptPath; |
| 142 | + return self(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Initialize ScyllaDB client with JMX reporting enabled or disabled |
| 147 | + */ |
| 148 | + public SELF withJmxReporting(boolean enableJmxReporting) { |
| 149 | + this.enableJmxReporting = enableJmxReporting; |
| 150 | + return self(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Get username |
| 155 | + * |
| 156 | + * By default ScyllaDB has authenticator: AllowAllAuthenticator in scylladb.yaml |
| 157 | + * If username and password need to be used, then authenticator should be set as PasswordAuthenticator |
| 158 | + * (through custom ScyllaDB configuration) and through CQL with default scylladb-scylladb credentials |
| 159 | + * user management should be modified |
| 160 | + */ |
| 161 | + public String getUsername() { |
| 162 | + return USERNAME; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Get password |
| 167 | + * |
| 168 | + * By default ScyllaDB has authenticator: AllowAllAuthenticator in scylladb.yaml |
| 169 | + * If username and password need to be used, then authenticator should be set as PasswordAuthenticator |
| 170 | + * (through custom Cassandra configuration) and through CQL with default scylladb-scylladb credentials |
| 171 | + * user management should be modified |
| 172 | + */ |
| 173 | + public String getPassword() { |
| 174 | + return PASSWORD; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Retrieve an {@link InetSocketAddress} for connecting to the ScyllaDB container via the driver. |
| 179 | + * |
| 180 | + * @return A InetSocketAddrss representation of this ScyllaDB container's host and port. |
| 181 | + */ |
| 182 | + public InetSocketAddress getContactPoint() { |
| 183 | + return new InetSocketAddress(getHost(), getMappedPort(CQL_PORT)); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Retrieve the Local Datacenter for connecting to the ScyllaDB container via the driver. |
| 188 | + * |
| 189 | + * @return The configured local Datacenter name. |
| 190 | + */ |
| 191 | + public String getLocalDatacenter() { |
| 192 | + return getEnvMap().getOrDefault("SCYLLADB_DC", DEFAULT_LOCAL_DATACENTER); |
| 193 | + } |
| 194 | +} |
0 commit comments