|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import org.testcontainers.containers.delegate.YugabyteDBYCQLDelegate; |
| 5 | +import org.testcontainers.containers.strategy.YugabyteDBYCQLWaitStrategy; |
| 6 | +import org.testcontainers.ext.ScriptUtils; |
| 7 | +import org.testcontainers.utility.DockerImageName; |
| 8 | + |
| 9 | +import java.net.InetSocketAddress; |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +/** |
| 15 | + * Testcontainers implementation for YugabyteDB YCQL API. |
| 16 | + * |
| 17 | + * @author srinivasa-vasu |
| 18 | + * @see <a href="https://docs.yugabyte.com/stable/api/ycql/">YCQL API</a> |
| 19 | + */ |
| 20 | +public class YugabyteDBYCQLContainer extends GenericContainer<YugabyteDBYCQLContainer> { |
| 21 | + |
| 22 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("yugabytedb/yugabyte"); |
| 23 | + |
| 24 | + private static final Integer YCQL_PORT = 9042; |
| 25 | + |
| 26 | + private static final Integer MASTER_DASHBOARD_PORT = 7000; |
| 27 | + |
| 28 | + private static final Integer TSERVER_DASHBOARD_PORT = 9000; |
| 29 | + |
| 30 | + private static final String ENTRYPOINT = "bin/yugabyted start --background=false"; |
| 31 | + |
| 32 | + private static final String LOCAL_DC = "datacenter1"; |
| 33 | + |
| 34 | + private String keyspace; |
| 35 | + |
| 36 | + private String username; |
| 37 | + |
| 38 | + private String password; |
| 39 | + |
| 40 | + private String initScript; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param imageName image name |
| 44 | + */ |
| 45 | + public YugabyteDBYCQLContainer(final String imageName) { |
| 46 | + this(DockerImageName.parse(imageName)); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param imageName image name |
| 51 | + */ |
| 52 | + public YugabyteDBYCQLContainer(final DockerImageName imageName) { |
| 53 | + super(imageName); |
| 54 | + imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 55 | + withExposedPorts(YCQL_PORT, MASTER_DASHBOARD_PORT, TSERVER_DASHBOARD_PORT); |
| 56 | + waitingFor(new YugabyteDBYCQLWaitStrategy(this).withStartupTimeout(Duration.ofSeconds(60))); |
| 57 | + withCommand(ENTRYPOINT); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Set<Integer> getLivenessCheckPortNumbers() { |
| 62 | + return Collections.singleton(getMappedPort(YCQL_PORT)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Configures the environment variables. Setting up these variables would create the |
| 67 | + * custom objects. Setting {@link #withKeyspaceName(String)}, |
| 68 | + * {@link #withUsername(String)}, {@link #withPassword(String)} these parameters will |
| 69 | + * initilaize the database with those custom values |
| 70 | + */ |
| 71 | + @Override |
| 72 | + protected void configure() { |
| 73 | + addEnv("YCQL_KEYSPACE", keyspace); |
| 74 | + addEnv("YCQL_USER", username); |
| 75 | + addEnv("YCQL_PASSWORD", password); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param initScript path of the initialization script file |
| 80 | + * @return {@link YugabyteDBYCQLContainer} instance |
| 81 | + */ |
| 82 | + public YugabyteDBYCQLContainer withInitScript(String initScript) { |
| 83 | + this.initScript = initScript; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Setting this would create the keyspace |
| 89 | + * @param keyspace keyspace |
| 90 | + * @return {@link YugabyteDBYCQLContainer} instance |
| 91 | + */ |
| 92 | + public YugabyteDBYCQLContainer withKeyspaceName(final String keyspace) { |
| 93 | + this.keyspace = keyspace; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Setting this would create the custom user role |
| 99 | + * @param username user name |
| 100 | + * @return {@link YugabyteDBYCQLContainer} instance |
| 101 | + */ |
| 102 | + public YugabyteDBYCQLContainer withUsername(final String username) { |
| 103 | + this.username = username; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Setting this along with {@link #withUsername(String)} would enable authentication |
| 109 | + * @param password password |
| 110 | + * @return {@link YugabyteDBYCQLContainer} instance |
| 111 | + */ |
| 112 | + public YugabyteDBYCQLContainer withPassword(final String password) { |
| 113 | + this.password = password; |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Executes the initilization script |
| 119 | + * @param containerInfo containerInfo |
| 120 | + */ |
| 121 | + @Override |
| 122 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 123 | + if (this.initScript != null) { |
| 124 | + ScriptUtils.runInitScript(new YugabyteDBYCQLDelegate(this), initScript); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns a {@link InetSocketAddress} representation of YCQL's contact point info |
| 130 | + * @return contactpoint |
| 131 | + */ |
| 132 | + public InetSocketAddress getContactPoint() { |
| 133 | + return new InetSocketAddress(getHost(), getMappedPort(YCQL_PORT)); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns the local datacenter name |
| 138 | + * @return localdc name |
| 139 | + */ |
| 140 | + public String getLocalDc() { |
| 141 | + return LOCAL_DC; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Username getter method |
| 146 | + * @return username |
| 147 | + */ |
| 148 | + public String getUsername() { |
| 149 | + return this.username; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Password getter method |
| 154 | + * @return password |
| 155 | + */ |
| 156 | + public String getPassword() { |
| 157 | + return this.password; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Keyspace getter method |
| 162 | + * @return keyspace |
| 163 | + */ |
| 164 | + public String getKeyspace() { |
| 165 | + return this.keyspace; |
| 166 | + } |
| 167 | +} |
0 commit comments