-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add InfluxDB v3 support #10596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sbushmelev
wants to merge
5
commits into
testcontainers:main
Choose a base branch
from
sbushmelev:influx3-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−2
Open
Add InfluxDB v3 support #10596
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ac59b23
Add Influx db v3 support
sbushmelev fe09508
Corrected the comments for org.testcontainers.influxdb.InfluxDBContainer
sbushmelev 0a5296f
Add docs and fix comments
sbushmelev d861707
Merge branch 'main' into influx3-support
eddumelendez f711917
Change image version, fix tests
sbushmelev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV3.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| package org.testcontainers.containers; | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.HttpResponseException; | ||
| import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.classic.methods.HttpPost; | ||
| import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
| import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.HttpClients; | ||
| import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpStatus; | ||
| import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; | ||
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| import static java.lang.String.format; | ||
|
|
||
| /** | ||
| * Testcontainers implementation for InfluxDB 3 (InfluxDB IOx). | ||
| * <p> | ||
| * This container provides a instance of InfluxDB 3.x for integration testing. | ||
| * It supports both authenticated and non-authenticated modes. | ||
| * </p> | ||
| * | ||
| * <p> | ||
| * <strong>Example usage:</strong> | ||
| * <pre>{@code | ||
| * try (InfluxDBContainerV3<?> influxDB = new InfluxDBContainerV3<>("influxdb:3-core")) { | ||
| * influxDB.start(); | ||
| * String url = influxDB.getUrl(); | ||
| * String token = influxDB.getToken(); | ||
| * // Use InfluxDB client with the obtained URL and token | ||
| * } | ||
| * }</pre> | ||
| * </p> | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public class InfluxDBContainerV3<SELF extends InfluxDBContainerV3<SELF>> extends GenericContainer<SELF> { | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * The default port exposed by InfluxDB 3. | ||
| */ | ||
| public static final Integer INFLUXDB_PORT = 8181; | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); | ||
|
|
||
| /** | ||
| * The authentication token for InfluxDB 3. Lazily initialized and thread-safe. | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| private volatile String token; | ||
|
|
||
| /** | ||
| * Flag indicating whether authentication is disabled. | ||
| */ | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private boolean isAuthDisable; | ||
|
|
||
| /** | ||
| * Creates a new InfluxDB 3 container using the specified Docker image. | ||
| * | ||
| * @param dockerImageName the name of the Docker image | ||
| */ | ||
| public InfluxDBContainerV3(final DockerImageName dockerImageName) { | ||
| super(dockerImageName); | ||
| dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); | ||
|
|
||
| this.waitStrategy = | ||
| new HttpWaitStrategy() | ||
| .forPath("/health") | ||
| .forStatusCodeMatching(stausCode -> stausCode.equals(200) || stausCode.equals(401)); | ||
|
|
||
| withCommand("influxdb3 serve --node-id local01 --object-store file --data-dir /home/influxdb3/.influxdb3"); | ||
|
|
||
| addExposedPort(INFLUXDB_PORT); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an admin authentication token by making an HTTP request to the InfluxDB 3 instance. | ||
| * | ||
| * @return the generated authentication token | ||
| * @throws IllegalArgumentException if the token cannot be created due to HTTP or IO errors | ||
| * @throws HttpResponseException if the InfluxDB server returns a non-201 status code | ||
| */ | ||
| private String createToken() { | ||
sbushmelev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| HttpPost httpPost = new HttpPost(format("%s/api/v3/configure/token/admin", getUrl())); | ||
|
|
||
| httpPost.setHeader("Accept", "application/json"); | ||
| httpPost.setHeader("Content-Type", "application/json"); | ||
|
|
||
| try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||
| return httpClient.execute(httpPost, classicHttpResponse -> { | ||
| if (classicHttpResponse.getCode() != HttpStatus.SC_CREATED) { | ||
| throw new HttpResponseException( | ||
| classicHttpResponse.getCode(), | ||
| "Failed to get token" | ||
| ); | ||
| } | ||
| try (InputStream content = classicHttpResponse.getEntity().getContent()) { | ||
| return new ObjectMapper().readTree(content).get("token").asText(); | ||
| } | ||
| }); | ||
| } catch (IOException e) { | ||
| throw new IllegalArgumentException("Cannot get token", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Configures environment variables for the InfluxDB 3 container. | ||
| * <p> | ||
| * This is automatically called by Testcontainers during container startup. | ||
| * </p> | ||
| */ | ||
| @Override | ||
| protected void configure() { | ||
| addEnv("INFLUXDB3_START_WITHOUT_AUTH", Boolean.toString(isAuthDisable)); | ||
| } | ||
|
|
||
| /** | ||
| * @return a singleton set containing the mapped InfluxDB port | ||
| */ | ||
| @Override | ||
| public Set<Integer> getLivenessCheckPortNumbers() { | ||
| return Collections.singleton(getMappedPort(INFLUXDB_PORT)); | ||
| } | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Disables authentication for this InfluxDB instance. | ||
| * <p> | ||
| * When authentication is disabled, no token is required to access the database. | ||
| * </p> | ||
| * | ||
| * @return this container instance for method chaining | ||
| */ | ||
| public InfluxDBContainerV3<SELF> withDisableAuth() { | ||
| isAuthDisable = true; | ||
| return this; | ||
| } | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Gets the URL for connecting to this InfluxDB instance. | ||
| * <p> | ||
| * The URL includes the host and mapped port (since the actual port may change). | ||
| * </p> | ||
| * | ||
| * @return the HTTP URL to access InfluxDB (e.g., "http://localhost:32768") | ||
| */ | ||
| public String getUrl() { | ||
| return "http://" + getHost() + ":" + getMappedPort(INFLUXDB_PORT); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the authentication token for this InfluxDB instance. | ||
| * <p> | ||
| * The token is lazily initialized on first use and cached for subsequent calls. | ||
| * This method is thread-safe. | ||
| * </p> | ||
| * | ||
| * @return the authentication token | ||
| * @throws IllegalArgumentException if authentication is disabled or token creation fails | ||
| */ | ||
| public String getToken() { | ||
| String localToken = token; | ||
| if (localToken == null) { | ||
| synchronized (this) { | ||
| localToken = token; | ||
| if (localToken == null) { | ||
| token = localToken = createToken(); | ||
| } | ||
| } | ||
| } | ||
| return localToken; | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV3Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package org.testcontainers.containers; | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import com.influxdb.v3.client.InfluxDBClient; | ||
| import com.influxdb.v3.client.Point; | ||
| import org.junit.Test; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.time.Instant; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.assertj.core.api.Assertions.fail; | ||
|
|
||
| public class InfluxDBContainerV3Test { | ||
sbushmelev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| public void createInfluxDBContainerV3WithAuthTokenTest() { | ||
| try (InfluxDBContainerV3<?> container = new InfluxDBContainerV3<>(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE)) { | ||
| container.start(); | ||
| try (InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), container.getToken().toCharArray(), "test")) { | ||
| assertThat(client).isNotNull(); | ||
| assertThat(client.getServerVersion()).isEqualTo("3.3.0"); | ||
| } catch (Exception e) { | ||
| fail("Cannot get instance of influxdb v3", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void createInfluxDBContainerV3WithDisableAuthTokenTest() { | ||
| try (final InfluxDBContainerV3<?> container = new InfluxDBContainerV3<>(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE).withDisableAuth()) { | ||
| container.start(); | ||
| try (InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), null, "test")) { | ||
| assertThat(client).isNotNull(); | ||
| assertThat(client.getServerVersion()).isEqualTo("3.3.0"); | ||
| } catch (Exception e) { | ||
| fail("Cannot get instance of influxdb v3", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void tryToGetTokenWithAuthDisableTest() { | ||
| try (final InfluxDBContainerV3<?> container = new InfluxDBContainerV3<>(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE).withDisableAuth()) { | ||
| container.start(); | ||
| assertThatThrownBy(container::getToken).isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void writeAndReadResultTest() { | ||
| try (InfluxDBContainerV3<?> container = new InfluxDBContainerV3<>(InfluxDBTestUtils.INFLUXDB_V3_TEST_IMAGE)) { | ||
| container.start(); | ||
| try (InfluxDBClient client = InfluxDBClient.getInstance(container.getUrl(), container.getToken().toCharArray(), "test")) { | ||
| String location = "west"; | ||
| Double value = 55.15; | ||
| Point point = Point.measurement("temperature") | ||
| .setTag("location", location) | ||
| .setField("value", value) | ||
| .setTimestamp(Instant.now()); | ||
| client.writePoint(point); | ||
| String query = "select time,location,value from temperature"; | ||
| try (Stream<Object[]> result = client.query(query)) { | ||
| List<Object[]> rows = result.collect(Collectors.toList()); | ||
| assertThat(rows).hasSize(1); | ||
| Object[] row = rows.get(0); | ||
| assertThat(row[0]).isNotNull().isInstanceOf(BigInteger.class); | ||
| assertThat((String) row[1]).isEqualTo(location); | ||
| assertThat((Double) row[2]).isEqualTo(value); | ||
| } | ||
| } catch (Exception e) { | ||
| fail("Cannot write or read data from influxdb v3", e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this really needed? I don't think other modules need that.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this this "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED" unnecessary but
influxdb3-java use
https://arrow.apache.org/docs/java/install.html Here is the reason why you should use --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be only in effect, if we would use the module path?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this
when I try to read data from the database, I get the errors described here https://arrow.apache.org/docs/java/install.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kiview do you have any other ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eddumelendez Hello! Can you review it again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eddumelendez Good day, are there any problems left?