|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.HttpResponseException; |
| 5 | +import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.classic.methods.HttpPost; |
| 6 | +import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 7 | +import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.HttpClients; |
| 8 | +import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpStatus; |
| 9 | +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; |
| 10 | +import org.testcontainers.utility.DockerImageName; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +import static java.lang.String.format; |
| 18 | + |
| 19 | +/** |
| 20 | + * Testcontainers implementation for InfluxDB 3 (InfluxDB IOx). |
| 21 | + * <p> |
| 22 | + * This container provides a instance of InfluxDB 3.x for integration testing. |
| 23 | + * It supports both authenticated and non-authenticated modes. |
| 24 | + * </p> |
| 25 | + * |
| 26 | + * <p> |
| 27 | + * <strong>Example usage:</strong> |
| 28 | + * <pre>{@code |
| 29 | + * try (InfluxDBContainerV3<?> influxDB = new InfluxDBContainerV3<>("influxdb:3-core")) { |
| 30 | + * influxDB.start(); |
| 31 | + * String url = influxDB.getUrl(); |
| 32 | + * String token = influxDB.getToken(); |
| 33 | + * // Use InfluxDB client with the obtained URL and token |
| 34 | + * } |
| 35 | + * }</pre> |
| 36 | + * </p> |
| 37 | + */ |
| 38 | +public class InfluxDBContainerV3<SELF extends InfluxDBContainerV3<SELF>> extends GenericContainer<SELF> { |
| 39 | + |
| 40 | + /** |
| 41 | + * The default port exposed by InfluxDB 3. |
| 42 | + */ |
| 43 | + public static final Integer INFLUXDB_PORT = 8181; |
| 44 | + |
| 45 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); |
| 46 | + |
| 47 | + /** |
| 48 | + * The authentication token for InfluxDB 3. Lazily initialized and thread-safe. |
| 49 | + */ |
| 50 | + private volatile String token; |
| 51 | + |
| 52 | + /** |
| 53 | + * Flag indicating whether authentication is disabled. |
| 54 | + */ |
| 55 | + private boolean isAuthDisable; |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new InfluxDB 3 container using the specified Docker image. |
| 59 | + * |
| 60 | + * @param dockerImageName the name of the Docker image |
| 61 | + */ |
| 62 | + public InfluxDBContainerV3(final DockerImageName dockerImageName) { |
| 63 | + super(dockerImageName); |
| 64 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 65 | + |
| 66 | + this.waitStrategy = |
| 67 | + new HttpWaitStrategy() |
| 68 | + .forPath("/health") |
| 69 | + .forStatusCodeMatching(stausCode -> stausCode.equals(200) || stausCode.equals(401)); |
| 70 | + |
| 71 | + withCommand("influxdb3 serve --node-id local01 --object-store file --data-dir /home/influxdb3/.influxdb3"); |
| 72 | + |
| 73 | + addExposedPort(INFLUXDB_PORT); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Creates an admin authentication token by making an HTTP request to the InfluxDB 3 instance. |
| 78 | + * |
| 79 | + * @return the generated authentication token |
| 80 | + * @throws IllegalArgumentException if the token cannot be created due to HTTP or IO errors |
| 81 | + * @throws HttpResponseException if the InfluxDB server returns a non-201 status code |
| 82 | + */ |
| 83 | + private String createToken() { |
| 84 | + HttpPost httpPost = new HttpPost(format("%s/api/v3/configure/token/admin", getUrl())); |
| 85 | + |
| 86 | + httpPost.setHeader("Accept", "application/json"); |
| 87 | + httpPost.setHeader("Content-Type", "application/json"); |
| 88 | + |
| 89 | + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
| 90 | + return httpClient.execute(httpPost, classicHttpResponse -> { |
| 91 | + if (classicHttpResponse.getCode() != HttpStatus.SC_CREATED) { |
| 92 | + throw new HttpResponseException( |
| 93 | + classicHttpResponse.getCode(), |
| 94 | + "Failed to get token" |
| 95 | + ); |
| 96 | + } |
| 97 | + try (InputStream content = classicHttpResponse.getEntity().getContent()) { |
| 98 | + return new ObjectMapper().readTree(content).get("token").asText(); |
| 99 | + } |
| 100 | + }); |
| 101 | + } catch (IOException e) { |
| 102 | + throw new IllegalArgumentException("Cannot get token", e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Configures environment variables for the InfluxDB 3 container. |
| 108 | + * <p> |
| 109 | + * This is automatically called by Testcontainers during container startup. |
| 110 | + * </p> |
| 111 | + */ |
| 112 | + @Override |
| 113 | + protected void configure() { |
| 114 | + addEnv("INFLUXDB3_START_WITHOUT_AUTH", Boolean.toString(isAuthDisable)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return a singleton set containing the mapped InfluxDB port |
| 119 | + */ |
| 120 | + @Override |
| 121 | + public Set<Integer> getLivenessCheckPortNumbers() { |
| 122 | + return Collections.singleton(getMappedPort(INFLUXDB_PORT)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Disables authentication for this InfluxDB instance. |
| 127 | + * <p> |
| 128 | + * When authentication is disabled, no token is required to access the database. |
| 129 | + * </p> |
| 130 | + * |
| 131 | + * @return this container instance for method chaining |
| 132 | + */ |
| 133 | + public InfluxDBContainerV3<SELF> withDisableAuth() { |
| 134 | + isAuthDisable = true; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Gets the URL for connecting to this InfluxDB instance. |
| 140 | + * <p> |
| 141 | + * The URL includes the host and mapped port (since the actual port may change). |
| 142 | + * </p> |
| 143 | + * |
| 144 | + * @return the HTTP URL to access InfluxDB (e.g., "http://localhost:32768") |
| 145 | + */ |
| 146 | + public String getUrl() { |
| 147 | + return "http://" + getHost() + ":" + getMappedPort(INFLUXDB_PORT); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Gets the authentication token for this InfluxDB instance. |
| 152 | + * <p> |
| 153 | + * The token is lazily initialized on first use and cached for subsequent calls. |
| 154 | + * This method is thread-safe. |
| 155 | + * </p> |
| 156 | + * |
| 157 | + * @return the authentication token |
| 158 | + * @throws IllegalArgumentException if authentication is disabled or token creation fails |
| 159 | + */ |
| 160 | + public String getToken() { |
| 161 | + String localToken = token; |
| 162 | + if (localToken == null) { |
| 163 | + synchronized (this) { |
| 164 | + localToken = token; |
| 165 | + if (localToken == null) { |
| 166 | + token = localToken = createToken(); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + return localToken; |
| 171 | + } |
| 172 | +} |
0 commit comments