From 1d7a20fc460271e1fae82c83313eb128e08c07a4 Mon Sep 17 00:00:00 2001 From: raminqaf Date: Thu, 7 Jan 2021 13:39:20 +0100 Subject: [PATCH 01/51] update InfluxDBContainer class for v2+ --- modules/influxdb/build.gradle | 8 +- .../containers/InfluxDBContainer.java | 196 ++++++++---------- .../containers/InfluxDBContainerTest.java | 46 ++-- .../InfluxDBContainerWithUserTest.java | 104 ++++++---- .../containers/InfluxDBTestImages.java | 2 +- .../src/test/resources/influx-setup.sh | 8 + 6 files changed, 199 insertions(+), 165 deletions(-) create mode 100644 modules/influxdb/src/test/resources/influx-setup.sh diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 2e735e1137a..f2b8e38c85d 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -1,8 +1,12 @@ description = "Testcontainers :: InfluxDB" +ext { + influxdbClientVersion = '1.14.0' +} + dependencies { compile project(':testcontainers') - compileOnly 'org.influxdb:influxdb-java:2.21' - testCompile 'org.influxdb:influxdb-java:2.21' + compileOnly group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbClientVersion}" + testCompile group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbClientVersion}" } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 7ebfbff1a86..506cc7f251c 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -1,163 +1,149 @@ package org.testcontainers.containers; -import org.influxdb.InfluxDB; -import org.influxdb.InfluxDBFactory; +import static org.junit.Assert.assertEquals; + +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientFactory; +import com.influxdb.client.InfluxDBClientOptions; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import lombok.SneakyThrows; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitAllStrategy; +import org.testcontainers.lifecycle.Startables; import org.testcontainers.utility.DockerImageName; - -import java.util.Collections; -import java.util.Set; +import org.testcontainers.utility.MountableFile; /** - * See https://store.docker.com/images/influxdb + * See + * https://docs.influxdata.com/influxdb/v2.0/get-started/#download-and-run-influxdb-v2-0 */ public class InfluxDBContainer> extends GenericContainer { public static final Integer INFLUXDB_PORT = 8086; - private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); - private static final String DEFAULT_TAG = "1.4.3"; - - @Deprecated - public static final String VERSION = DEFAULT_TAG; - - private boolean authEnabled = true; - private String admin = "admin"; - private String adminPassword = "password"; - - private String database; - private String username = "any"; - private String password = "any"; - - /** - * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead - */ - @Deprecated - public InfluxDBContainer() { - this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); + private static final String REGISTRY = "quay.io"; + private static final String REPOSITORY = "influxdb/influxdb"; + private static final String TAG = "v2.0.0"; + private static final DockerImageName DEFAULT_IMAGE_NAME = + DockerImageName.parse(String.format("%s/%s:%s", REGISTRY, REPOSITORY, TAG)); + private static final int NO_CONTENT_STATUS_CODE = 204; + private static final String INFLUX_SETUP_SH = "influx-setup.sh"; + + private String username = "test-user"; + private String password = "test-password"; + private String bucket = "test-bucket"; + private String organization = "test-org"; + + private InfluxDBContainer(final DockerImageName imageName) { + super(imageName); + imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); + this.waitStrategy = (new WaitAllStrategy()) + .withStrategy(Wait + .forHttp("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE)) + .withStrategy(Wait.forListeningPort()); + this.addExposedPort(INFLUXDB_PORT); } - /** - * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead - */ - @Deprecated - public InfluxDBContainer(final String version) { - this(DEFAULT_IMAGE_NAME.withTag(version)); + public static InfluxDBContainer createWithDefaultTag() { + return new InfluxDBContainer<>(DEFAULT_IMAGE_NAME); } - public InfluxDBContainer(final DockerImageName dockerImageName) { - super(dockerImageName); - - dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - - waitStrategy = new WaitAllStrategy() - .withStrategy(Wait.forHttp("/ping").withBasicCredentials(username, password).forStatusCode(204)) - .withStrategy(Wait.forListeningPort()); - - addExposedPort(INFLUXDB_PORT); + public static InfluxDBContainer createWithSpecificTag(final DockerImageName imageName) { + return new InfluxDBContainer<>(imageName); } @Override protected void configure() { - addEnv("INFLUXDB_ADMIN_USER", admin); - addEnv("INFLUXDB_ADMIN_PASSWORD", adminPassword); - - addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(authEnabled)); - - addEnv("INFLUXDB_DB", database); - addEnv("INFLUXDB_USER", username); - addEnv("INFLUXDB_USER_PASSWORD", password); + this.addEnv("INFLUXDB_USER", this.username); + this.addEnv("INFLUXDB_PASSWORD", this.password); + this.addEnv("INFLUXDB_BUCKET", this.bucket); + this.addEnv("INFLUXDB_ORG", this.organization); } @Override - public Set getLivenessCheckPortNumbers() { - return Collections.singleton(getMappedPort(INFLUXDB_PORT)); + @SneakyThrows({InterruptedException.class, IOException.class, ExecutionException.class}) + public void start() { + this.withCopyFileToContainer(MountableFile.forClasspathResource(INFLUX_SETUP_SH), + String.format("%s", INFLUX_SETUP_SH)); + if (this.containerId != null) { + return; + } + Startables.deepStart(this.dependencies).get(); + // trigger LazyDockerClient's resolve so that we fail fast here and not in getDockerImageName() + this.dockerClient.authConfig(); + this.doStart(); + final Container.ExecResult execResult = this.execInContainer("chmod", "-x", "/influx-setup.sh"); + assertEquals(execResult.getExitCode(), 0); + final Container.ExecResult writeResult = this.execInContainer("/bin/bash", "/influx-setup.sh"); + assertEquals(writeResult.getExitCode(), 0); } /** - * Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`. - * - * @param authEnabled Enables authentication. - * @return a reference to this container instance - */ - public SELF withAuthEnabled(final boolean authEnabled) { - this.authEnabled = authEnabled; - return self(); - } - - /** - * Set env variable `INFLUXDB_ADMIN_USER`. + * Set env variable `INFLUXDB_USER`. * - * @param admin The name of the admin user to be created. If this is unset, no admin user is created. + * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will + * be granted read and write permissions for that database. * @return a reference to this container instance */ - public SELF withAdmin(final String admin) { - this.admin = admin; - return self(); + public SELF withUsername(final String username) { + this.username = username; + return this.self(); } /** - * Set env variable `INFLUXDB_ADMIN_PASSWORD`. + * Set env variable `INFLUXDB_PASSWORD`. * - * @param adminPassword TThe password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a - * random password is generated and printed to standard out. + * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is + * generated and printed to standard out. * @return a reference to this container instance */ - public SELF withAdminPassword(final String adminPassword) { - this.adminPassword = adminPassword; - return self(); + public SELF withPassword(final String password) { + this.password = password; + return this.self(); } /** - * Set env variable `INFLUXDB_DB`. + * Set env variable `INFLUXDB_BUCKET`. * - * @param database Automatically initializes a database with the name of this environment variable. + * @param bucket Automatically initializes a bucket with the name of this environment variable. * @return a reference to this container instance */ - public SELF withDatabase(final String database) { - this.database = database; - return self(); + public SELF withBucket(final String bucket) { + this.bucket = bucket; + return this.self(); } /** - * Set env variable `INFLUXDB_USER`. + * Set env variable `INFLUXDB_ORGANIZATION`. * - * @param username The name of a user to be created with no privileges. If `INFLUXDB_DB` is set, this user will - * be granted read and write permissions for that database. + * @param organization The organization for the initial setup of influxDB. * @return a reference to this container instance */ - public SELF withUsername(final String username) { - this.username = username; - return self(); + public SELF withOrganization(final String organization) { + this.organization = organization; + return this.self(); } /** - * Set env variable `INFLUXDB_USER_PASSWORD`. - * - * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password - * is generated and printed to standard out. - * @return a reference to this container instance + * @return a influxDb client */ - public SELF withPassword(final String password) { - this.password = password; - return self(); + public InfluxDBClient getNewInfluxDB() { + final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() + .url(this.getUrl()) + .authenticate(this.username, this.password.toCharArray()) + .bucket(this.bucket) + .org(this.organization) + .build(); + return InfluxDBClientFactory.create(influxDBClientOptions); } - /** * @return a url to influxDb */ - public String getUrl() { - return "http://" + getHost() + ":" + getLivenessCheckPort(); - } - - /** - * @return a influxDb client - */ - public InfluxDB getNewInfluxDB() { - InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), username, password); - influxDB.setDatabase(database); - return influxDB; + String getUrl() { + return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index 7acf030b5e4..aadca36bb94 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -1,43 +1,65 @@ package org.testcontainers.containers; -import org.influxdb.InfluxDB; -import org.junit.ClassRule; -import org.junit.Test; - import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.domain.HealthCheck.StatusEnum; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + public class InfluxDBContainerTest { + private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); + @ClassRule - public static InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBTestImages.INFLUXDB_TEST_IMAGE); + public static final InfluxDBContainer influxDBContainer = InfluxDBContainer + .createWithSpecificTag(InfluxDBTestImages.INFLUXDB_TEST_IMAGE); + + private InfluxDBClient client = null; + + @Before + public void setUp() { + this.client = influxDBContainer.getNewInfluxDB(); + } + + @After + public void tearDown() { + this.client.close(); + } @Test public void getUrl() { - String actual = influxDBContainer.getUrl(); + final String actual = influxDBContainer.getUrl(); assertThat(actual, notNullValue()); } @Test public void getNewInfluxDB() { - InfluxDB actual = influxDBContainer.getNewInfluxDB(); + final InfluxDBClient actual = influxDBContainer.getNewInfluxDB(); assertThat(actual, notNullValue()); - assertThat(actual.ping(), notNullValue()); + assertThat(actual.health().getStatus(), is(StatusEnum.PASS)); } @Test - public void getLivenessCheckPort() { - Integer actual = influxDBContainer.getLivenessCheckPort(); + public void checkVersion() { + assertThat(this.client, notNullValue()); - assertThat(actual, notNullValue()); + assertThat(this.client.health().getStatus(), is(StatusEnum.PASS)); + + final String actualVersion = String.format("v%s", this.client.health().getVersion()); + + assertThat(actualVersion, is(TEST_VERSION)); } @Test public void isRunning() { - boolean actual = influxDBContainer.isRunning(); + final boolean actual = influxDBContainer.isRunning(); assertThat(actual, is(true)); } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java index b045ca297c1..d57666b47ee 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java @@ -1,72 +1,86 @@ package org.testcontainers.containers; -import org.influxdb.InfluxDB; -import org.influxdb.dto.Point; -import org.influxdb.dto.Query; -import org.influxdb.dto.QueryResult; -import org.junit.Rule; -import org.junit.Test; - -import java.util.concurrent.TimeUnit; - -import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.QueryApi; +import com.influxdb.client.WriteApi; +import com.influxdb.client.domain.Bucket; +import com.influxdb.client.domain.WritePrecision; +import com.influxdb.client.write.Point; +import com.influxdb.query.FluxRecord; +import com.influxdb.query.FluxTable; +import java.time.Instant; +import java.util.List; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + public class InfluxDBContainerWithUserTest { - private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); - private static final String DATABASE = "test"; - private static final String USER = "test-user"; - private static final String PASSWORD = "test-password"; + private static final String BUCKET = "new-test-bucket"; + private static final String USER = "new-test-user"; + private static final String PASSWORD = "new-test-password"; + private static final String ORG = "new-test-org"; + + private InfluxDBClient client = null; - @Rule - public InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBTestImages.INFLUXDB_TEST_IMAGE) - .withDatabase(DATABASE) + @ClassRule + public static final InfluxDBContainer influxDBContainer = InfluxDBContainer + .createWithDefaultTag() + .withBucket(BUCKET) .withUsername(USER) - .withPassword(PASSWORD); + .withPassword(PASSWORD) + .withOrganization(ORG); - @Test - public void describeDatabases() { - InfluxDB actual = influxDBContainer.getNewInfluxDB(); + @Before + public void setUp() { + this.client = influxDBContainer.getNewInfluxDB(); + } - assertThat(actual, notNullValue()); - assertThat(actual.describeDatabases(), hasItem(DATABASE)); + @After + public void tearDown() { + this.client.close(); } @Test - public void checkVersion() { - InfluxDB actual = influxDBContainer.getNewInfluxDB(); - - assertThat(actual, notNullValue()); + public void getBucket() { + assertThat(this.client, notNullValue()); - assertThat(actual.ping(), notNullValue()); - assertThat(actual.ping().getVersion(), is(TEST_VERSION)); + final Bucket bucket = this.client.getBucketsApi().findBucketByName(BUCKET); + assertThat(bucket, notNullValue()); - assertThat(actual.version(), is(TEST_VERSION)); + assertThat(bucket.getName(), is(BUCKET)); } @Test public void queryForWriteAndRead() { - InfluxDB influxDB = influxDBContainer.getNewInfluxDB(); + try (final WriteApi writeApi = this.client.getWriteApi()) { + + // + // Write by Data Point + // + final Point point = Point.measurement("temperature") + .addTag("location", "west") + .addField("value", 55D) + .time(Instant.now().toEpochMilli(), WritePrecision.MS); + + writeApi.writePoint(point); - Point point = Point.measurement("cpu") - .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) - .addField("idle", 90L) - .addField("user", 9L) - .addField("system", 1L) - .build(); - influxDB.write(point); + } - Query query = new Query("SELECT idle FROM cpu", DATABASE); - QueryResult actual = influxDB.query(query); + // + // Query data + // + final String flux = String.format("from(bucket:\"%s\") |> range(start: 0)", BUCKET); - assertThat(actual, notNullValue()); - assertThat(actual.getError(), nullValue()); - assertThat(actual.getResults(), notNullValue()); - assertThat(actual.getResults().size(), is(1)); + final QueryApi queryApi = this.client.getQueryApi(); + final FluxTable fluxTable = queryApi.query(flux).get(0); + final List records = fluxTable.getRecords(); + assertThat(records.size(), is(1)); } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java index a0e2d31bbdf..3bb6a6aadfc 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java @@ -3,5 +3,5 @@ import org.testcontainers.utility.DockerImageName; public interface InfluxDBTestImages { - DockerImageName INFLUXDB_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); + DockerImageName INFLUXDB_TEST_IMAGE = DockerImageName.parse("quay.io/influxdb/influxdb:v2.0.0"); } diff --git a/modules/influxdb/src/test/resources/influx-setup.sh b/modules/influxdb/src/test/resources/influx-setup.sh new file mode 100644 index 00000000000..4a5d16ba8d8 --- /dev/null +++ b/modules/influxdb/src/test/resources/influx-setup.sh @@ -0,0 +1,8 @@ +#!/bin/bash +influx setup \ + -f \ + -u "${INFLUXDB_USER}" \ + -p "${INFLUXDB_PASSWORD}" \ + -b "${INFLUXDB_BUCKET}" \ + -o "${INFLUXDB_ORG}" \ + -r 0 From 60d8970452c74ee6b2aceee0a3b1aa8350bb6ad5 Mon Sep 17 00:00:00 2001 From: raminqaf Date: Thu, 7 Jan 2021 16:30:21 +0100 Subject: [PATCH 02/51] update influxdb.md --- docs/modules/databases/influxdb.md | 47 +++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index 740cf69982c..82a7326db92 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -2,21 +2,54 @@ Testcontainers module for InfluxData [InfluxDB](https://github.com/influxdata/influxdb). -## Usage example +## Importat Note +This is the test container for influxDB v2 and does not support influxDB 1.x. Influx Data migrated to quay.io as their new [container registry](https://quay.io/repository/influxdb/influxdb). +## Usage example Running influxDbContainer as a stand-in for InfluxDB in a test: ```java public class SomeTest { - @Rule - public InfluxDBContainer influxDbContainer = new InfluxDBContainer(); - + @ClassRule + public static final InfluxDBContainer influxDbContainer = InfluxDBContainer.createWithDefaultTag(); + + @Test + public void someTestMethod() { + final InfluxDBClient influxDB = influxDbContainer.getNewInfluxDB(); + // rest of the test + } +} +``` +This will create a InfluxDB container with tag v2.0.0. The influxDB will be setup with the following data:
+ +| Property | Value | +| ------------- |:-------------:| +| User | test-user | +| Password | test-password | +| Bucket | test-bucket | +| Organization | test-org | + +It is possible to override the default values and start the container with a newer tag: +```java +public class SomeTest { + @ClassRule + public static final InfluxDBContainer influxDBContainer = InfluxDBContainer + .createWithSpecificTag(DockerImageName.parse("quay.io/influxdb/influxdb:v2.0.3")) + .withBucket(BUCKET) + .withUsername(USER) + .withPassword(PASSWORD) + .withOrganization(ORG); + @Test public void someTestMethod() { - InfluxDB influxDB = influxDbContainer.getNewInfluxDB(); - ... + final InfluxDBClient influxDB = influxDbContainer.getNewInfluxDB(); + // rest of the test + } +} ``` +**NOTE**: The `createWithSpecificTag` static factory needs a valid registry and tag name. You can find the latest tags [here](https://quay.io/repository/influxdb/influxdb?tab=tags).
+**NOTE**: You can find the latest documentation about the influx client [here](https://github.com/influxdata/influxdb-client-java). ## Adding this module to your project dependencies @@ -35,5 +68,5 @@ testCompile "org.testcontainers:influxdb:{{latest_version}}" ``` -!!! hint +**Hint:** Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency. From 3aa16a5dc9a9f8873b9df5e2ef27b69f03ad38f6 Mon Sep 17 00:00:00 2001 From: raminqaf Date: Thu, 7 Jan 2021 17:15:02 +0100 Subject: [PATCH 03/51] add retention time and update influxdb.md --- docs/modules/databases/influxdb.md | 7 +++-- .../containers/InfluxDBContainer.java | 27 +++++++++++++++++++ .../containers/RetentionUnits.java | 18 +++++++++++++ .../src/test/resources/influx-setup.sh | 12 ++++----- 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index 82a7326db92..eb0a89b7523 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -2,7 +2,7 @@ Testcontainers module for InfluxData [InfluxDB](https://github.com/influxdata/influxdb). -## Importat Note +## Important Note This is the test container for influxDB v2 and does not support influxDB 1.x. Influx Data migrated to quay.io as their new [container registry](https://quay.io/repository/influxdb/influxdb). ## Usage example @@ -23,12 +23,15 @@ public class SomeTest { ``` This will create a InfluxDB container with tag v2.0.0. The influxDB will be setup with the following data:
-| Property | Value | +| Property | Default Value | | ------------- |:-------------:| | User | test-user | | Password | test-password | | Bucket | test-bucket | | Organization | test-org | +| Retention | 0 (infinite) | +| Retention Unit| ns | +For more details about the InfluxDB setup go to the official [docs](https://docs.influxdata.com/influxdb/v2.0/reference/cli/influx/setup/). It is possible to override the default values and start the container with a newer tag: ```java diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 506cc7f251c..89c746e18e3 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -34,6 +34,8 @@ public class InfluxDBContainer> extends Gen private String password = "test-password"; private String bucket = "test-bucket"; private String organization = "test-org"; + private int retention = 0; + private String retentionUnit = RetentionUnits.NANOSECONDS.label; private InfluxDBContainer(final DockerImageName imageName) { super(imageName); @@ -61,6 +63,8 @@ protected void configure() { this.addEnv("INFLUXDB_PASSWORD", this.password); this.addEnv("INFLUXDB_BUCKET", this.bucket); this.addEnv("INFLUXDB_ORG", this.organization); + this.addEnv("INFLUXDB_RETENTION", String.valueOf(this.retention)); + this.addEnv("INFLUXDB_RETENTION_UNIT", this.retentionUnit); } @Override @@ -127,6 +131,29 @@ public SELF withOrganization(final String organization) { return this.self(); } + /** + * Set env variable `INFLUXDB_RETENTION`. + * + * @param retention Duration bucket will retain data (0 is infinite, default is 0). + * @return a reference to this container instance + */ + public SELF withRetention(final int retention) { + this.retention = retention; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_RETENTION_UNIT`. + * + * @param retentionUnit The retention unit (ns, us, ms, etc.). + * @return a reference to this container instance + */ + public SELF withRetentionUnit(final RetentionUnits retentionUnit) { + this.retentionUnit = retentionUnit.label; + return this.self(); + } + + /** * @return a influxDb client */ diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java b/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java new file mode 100644 index 00000000000..613109ba0a8 --- /dev/null +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java @@ -0,0 +1,18 @@ +package org.testcontainers.containers; + +public enum RetentionUnits { + NANOSECONDS("ns"), + MICROSECONDS("us"), + MILLISECONDS("ms"), + SECONDS("s"), + MINUTES("m"), + HOURS("h"), + DAYS("d"), + WEEKS("w"); + + public final String label; + + RetentionUnits(final String label) { + this.label = label; + } +} diff --git a/modules/influxdb/src/test/resources/influx-setup.sh b/modules/influxdb/src/test/resources/influx-setup.sh index 4a5d16ba8d8..ebd8c2d6582 100644 --- a/modules/influxdb/src/test/resources/influx-setup.sh +++ b/modules/influxdb/src/test/resources/influx-setup.sh @@ -1,8 +1,8 @@ #!/bin/bash influx setup \ - -f \ - -u "${INFLUXDB_USER}" \ - -p "${INFLUXDB_PASSWORD}" \ - -b "${INFLUXDB_BUCKET}" \ - -o "${INFLUXDB_ORG}" \ - -r 0 + --force \ + --username "${INFLUXDB_USER}" \ + --password "${INFLUXDB_PASSWORD}" \ + --bucket "${INFLUXDB_BUCKET}" \ + --org "${INFLUXDB_ORG}" \ + --retention "${INFLUXDB_RETENTION}""${INFLUXDB_RETENTION_UNIT}" From 898b4a9c410d439066dbc1b630a96c9040880536 Mon Sep 17 00:00:00 2001 From: raminqaf Date: Thu, 7 Jan 2021 17:53:23 +0100 Subject: [PATCH 04/51] made retentiion units lable package private --- .../main/java/org/testcontainers/containers/RetentionUnits.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java b/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java index 613109ba0a8..432e3c78aed 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java @@ -10,7 +10,7 @@ public enum RetentionUnits { DAYS("d"), WEEKS("w"); - public final String label; + final String label; RetentionUnits(final String label) { this.label = label; From 3ea55600e687d379feb86602bd58cd4e264127e0 Mon Sep 17 00:00:00 2001 From: raminqaf Date: Sun, 10 Jan 2021 14:35:26 +0100 Subject: [PATCH 05/51] add influxDB v1.x code --- docs/modules/databases/influxdb.md | 37 +++- modules/influxdb/build.gradle | 10 +- .../containers/InfluxDBContainer.java | 174 +---------------- .../containers/InfluxDBContainerV1.java | 164 ++++++++++++++++ .../containers/InfluxDBContainerV2.java | 176 ++++++++++++++++++ ...RetentionUnits.java => RetentionUnit.java} | 4 +- .../containers/InfluxDBContainerV1Test.java | 44 +++++ .../InfluxDBContainerV1WithUserTest.java | 72 +++++++ ...Test.java => InfluxDBContainerV2Test.java} | 16 +- ...a => InfluxDBContainerV2WithUserTest.java} | 6 +- .../containers/InfluxDBV1TestImages.java | 7 + ...tImages.java => InfluxDBV2TestImages.java} | 2 +- 12 files changed, 516 insertions(+), 196 deletions(-) create mode 100644 modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java create mode 100644 modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java rename modules/influxdb/src/main/java/org/testcontainers/containers/{RetentionUnits.java => RetentionUnit.java} (78%) create mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java create mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java rename modules/influxdb/src/test/java/org/testcontainers/containers/{InfluxDBContainerTest.java => InfluxDBContainerV2Test.java} (68%) rename modules/influxdb/src/test/java/org/testcontainers/containers/{InfluxDBContainerWithUserTest.java => InfluxDBContainerV2WithUserTest.java} (92%) create mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV1TestImages.java rename modules/influxdb/src/test/java/org/testcontainers/containers/{InfluxDBTestImages.java => InfluxDBV2TestImages.java} (82%) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index eb0a89b7523..02915a1428a 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -3,21 +3,22 @@ Testcontainers module for InfluxData [InfluxDB](https://github.com/influxdata/influxdb). ## Important Note -This is the test container for influxDB v2 and does not support influxDB 1.x. Influx Data migrated to quay.io as their new [container registry](https://quay.io/repository/influxdb/influxdb). +They are breaking changes in InfluxDB v2.x. For more information refer to the main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). +Influx Data migrated to quay.io as their new [container registry](https://quay.io/repository/influxdb/influxdb). -## Usage example +## InfluxDB V2.x Usage example Running influxDbContainer as a stand-in for InfluxDB in a test: ```java public class SomeTest { @ClassRule - public static final InfluxDBContainer influxDbContainer = InfluxDBContainer.createWithDefaultTag(); + public static final InfluxDBV2Container influxDbContainer = InfluxDBV2Container.createWithDefaultTag(); @Test public void someTestMethod() { final InfluxDBClient influxDB = influxDbContainer.getNewInfluxDB(); - // rest of the test + // Rest of the test } } ``` @@ -37,7 +38,7 @@ It is possible to override the default values and start the container with a new ```java public class SomeTest { @ClassRule - public static final InfluxDBContainer influxDBContainer = InfluxDBContainer + public static final InfluxDBContainerV2 influxDBContainer = InfluxDBContainerV2 .createWithSpecificTag(DockerImageName.parse("quay.io/influxdb/influxdb:v2.0.3")) .withBucket(BUCKET) .withUsername(USER) @@ -52,7 +53,28 @@ public class SomeTest { } ``` **NOTE**: The `createWithSpecificTag` static factory needs a valid registry and tag name. You can find the latest tags [here](https://quay.io/repository/influxdb/influxdb?tab=tags).
-**NOTE**: You can find the latest documentation about the influx client [here](https://github.com/influxdata/influxdb-client-java). +**NOTE**: You can find the latest documentation about the influxdb java client [here](https://github.com/influxdata/influxdb-client-java). + +**Hint:** + Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency. + +## InfluxDB V1.x Usage example (Deprecated) + +Running influxDbContainer as a stand-in for InfluxDB in a test: + +```java +public class SomeTest { + + @Rule + public InfluxDBContainer influxDbContainer = new InfluxDBContainer(); + + @Test + public void someTestMethod() { + final InfluxDB influxDB = this.influxDbContainer.getNewInfluxDB(); + // Rest of the test + } +} +``` ## Adding this module to your project dependencies @@ -70,6 +92,3 @@ testCompile "org.testcontainers:influxdb:{{latest_version}}" test ``` - -**Hint:** - Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency. diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index f2b8e38c85d..0c1d51f4cba 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -1,12 +1,16 @@ description = "Testcontainers :: InfluxDB" ext { - influxdbClientVersion = '1.14.0' + influxdbV1ClientVersion = '2.21' + influxdbV2ClientVersion = '1.14.0' } dependencies { compile project(':testcontainers') - compileOnly group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbClientVersion}" - testCompile group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbClientVersion}" + compileOnly group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" + testCompile group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" + + compileOnly group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" + testCompile group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 89c746e18e3..da91beadc0c 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -1,176 +1,10 @@ package org.testcontainers.containers; -import static org.junit.Assert.assertEquals; - -import com.influxdb.client.InfluxDBClient; -import com.influxdb.client.InfluxDBClientFactory; -import com.influxdb.client.InfluxDBClientOptions; -import java.io.IOException; -import java.util.concurrent.ExecutionException; -import lombok.SneakyThrows; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.containers.wait.strategy.WaitAllStrategy; -import org.testcontainers.lifecycle.Startables; import org.testcontainers.utility.DockerImageName; -import org.testcontainers.utility.MountableFile; - -/** - * See - * https://docs.influxdata.com/influxdb/v2.0/get-started/#download-and-run-influxdb-v2-0 - */ -public class InfluxDBContainer> extends GenericContainer { - - public static final Integer INFLUXDB_PORT = 8086; - - private static final String REGISTRY = "quay.io"; - private static final String REPOSITORY = "influxdb/influxdb"; - private static final String TAG = "v2.0.0"; - private static final DockerImageName DEFAULT_IMAGE_NAME = - DockerImageName.parse(String.format("%s/%s:%s", REGISTRY, REPOSITORY, TAG)); - private static final int NO_CONTENT_STATUS_CODE = 204; - private static final String INFLUX_SETUP_SH = "influx-setup.sh"; - - private String username = "test-user"; - private String password = "test-password"; - private String bucket = "test-bucket"; - private String organization = "test-org"; - private int retention = 0; - private String retentionUnit = RetentionUnits.NANOSECONDS.label; - - private InfluxDBContainer(final DockerImageName imageName) { - super(imageName); - imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - this.waitStrategy = (new WaitAllStrategy()) - .withStrategy(Wait - .forHttp("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE)) - .withStrategy(Wait.forListeningPort()); - this.addExposedPort(INFLUXDB_PORT); - } - - public static InfluxDBContainer createWithDefaultTag() { - return new InfluxDBContainer<>(DEFAULT_IMAGE_NAME); - } - - public static InfluxDBContainer createWithSpecificTag(final DockerImageName imageName) { - return new InfluxDBContainer<>(imageName); - } - - @Override - protected void configure() { - this.addEnv("INFLUXDB_USER", this.username); - this.addEnv("INFLUXDB_PASSWORD", this.password); - this.addEnv("INFLUXDB_BUCKET", this.bucket); - this.addEnv("INFLUXDB_ORG", this.organization); - this.addEnv("INFLUXDB_RETENTION", String.valueOf(this.retention)); - this.addEnv("INFLUXDB_RETENTION_UNIT", this.retentionUnit); - } - - @Override - @SneakyThrows({InterruptedException.class, IOException.class, ExecutionException.class}) - public void start() { - this.withCopyFileToContainer(MountableFile.forClasspathResource(INFLUX_SETUP_SH), - String.format("%s", INFLUX_SETUP_SH)); - if (this.containerId != null) { - return; - } - Startables.deepStart(this.dependencies).get(); - // trigger LazyDockerClient's resolve so that we fail fast here and not in getDockerImageName() - this.dockerClient.authConfig(); - this.doStart(); - final Container.ExecResult execResult = this.execInContainer("chmod", "-x", "/influx-setup.sh"); - assertEquals(execResult.getExitCode(), 0); - final Container.ExecResult writeResult = this.execInContainer("/bin/bash", "/influx-setup.sh"); - assertEquals(writeResult.getExitCode(), 0); - } - - /** - * Set env variable `INFLUXDB_USER`. - * - * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will - * be granted read and write permissions for that database. - * @return a reference to this container instance - */ - public SELF withUsername(final String username) { - this.username = username; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_PASSWORD`. - * - * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is - * generated and printed to standard out. - * @return a reference to this container instance - */ - public SELF withPassword(final String password) { - this.password = password; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_BUCKET`. - * - * @param bucket Automatically initializes a bucket with the name of this environment variable. - * @return a reference to this container instance - */ - public SELF withBucket(final String bucket) { - this.bucket = bucket; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_ORGANIZATION`. - * - * @param organization The organization for the initial setup of influxDB. - * @return a reference to this container instance - */ - public SELF withOrganization(final String organization) { - this.organization = organization; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_RETENTION`. - * - * @param retention Duration bucket will retain data (0 is infinite, default is 0). - * @return a reference to this container instance - */ - public SELF withRetention(final int retention) { - this.retention = retention; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_RETENTION_UNIT`. - * - * @param retentionUnit The retention unit (ns, us, ms, etc.). - * @return a reference to this container instance - */ - public SELF withRetentionUnit(final RetentionUnits retentionUnit) { - this.retentionUnit = retentionUnit.label; - return this.self(); - } - - - /** - * @return a influxDb client - */ - public InfluxDBClient getNewInfluxDB() { - final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() - .url(this.getUrl()) - .authenticate(this.username, this.password.toCharArray()) - .bucket(this.bucket) - .org(this.organization) - .build(); - return InfluxDBClientFactory.create(influxDBClientOptions); - } - /** - * @return a url to influxDb - */ - String getUrl() { - return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); +@Deprecated +public class InfluxDBContainer> extends InfluxDBContainerV1 { + public InfluxDBContainer(final DockerImageName influxdbTestImage) { + super(influxdbTestImage); } } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java new file mode 100644 index 00000000000..f7a69f3a5de --- /dev/null +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -0,0 +1,164 @@ +package org.testcontainers.containers; + +import java.util.Collections; +import java.util.Set; +import org.influxdb.InfluxDB; +import org.influxdb.InfluxDBFactory; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitAllStrategy; +import org.testcontainers.utility.DockerImageName; + +/** + * See https://store.docker.com/images/influxdb + */ +class InfluxDBContainerV1> extends GenericContainer { + + public static final Integer INFLUXDB_PORT = 8086; + + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); + private static final String DEFAULT_TAG = "1.4.3"; + + private static final int NO_CONTENT_STATUS_CODE = 204; + + @Deprecated + public static final String VERSION = DEFAULT_TAG; + + private boolean authEnabled = true; + private String admin = "admin"; + private String adminPassword = "password"; + + private String database; + private String username = "any"; + private String password = "any"; + + /** + * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + */ + @Deprecated + public InfluxDBContainerV1() { + this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); + } + + /** + * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + */ + @Deprecated + public InfluxDBContainerV1(final String version) { + this(DEFAULT_IMAGE_NAME.withTag(version)); + } + + public InfluxDBContainerV1(final DockerImageName dockerImageName) { + super(dockerImageName); + + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); + + this.waitStrategy = new WaitAllStrategy() + .withStrategy(Wait.forHttp("/ping").withBasicCredentials(this.username, this.password).forStatusCode(NO_CONTENT_STATUS_CODE)) + .withStrategy(Wait.forListeningPort()); + + this.addExposedPort(INFLUXDB_PORT); + } + + @Override + protected void configure() { + this.addEnv("INFLUXDB_ADMIN_USER", this.admin); + this.addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword); + + this.addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled)); + + this.addEnv("INFLUXDB_DB", this.database); + this.addEnv("INFLUXDB_USER", this.username); + this.addEnv("INFLUXDB_USER_PASSWORD", this.password); + } + + @Override + public Set getLivenessCheckPortNumbers() { + return Collections.singleton(this.getMappedPort(INFLUXDB_PORT)); + } + + /** + * Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`. + * + * @param authEnabled Enables authentication. + * @return a reference to this container instance + */ + public SELF withAuthEnabled(final boolean authEnabled) { + this.authEnabled = authEnabled; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_ADMIN_USER`. + * + * @param admin The name of the admin user to be created. If this is unset, no admin user is created. + * @return a reference to this container instance + */ + public SELF withAdmin(final String admin) { + this.admin = admin; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_ADMIN_PASSWORD`. + * + * @param adminPassword TThe password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a + * random password is generated and printed to standard out. + * @return a reference to this container instance + */ + public SELF withAdminPassword(final String adminPassword) { + this.adminPassword = adminPassword; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_DB`. + * + * @param database Automatically initializes a database with the name of this environment variable. + * @return a reference to this container instance + */ + public SELF withDatabase(final String database) { + this.database = database; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_USER`. + * + * @param username The name of a user to be created with no privileges. If `INFLUXDB_DB` is set, this user will be + * granted read and write permissions for that database. + * @return a reference to this container instance + */ + public SELF withUsername(final String username) { + this.username = username; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_USER_PASSWORD`. + * + * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is + * generated and printed to standard out. + * @return a reference to this container instance + */ + public SELF withPassword(final String password) { + this.password = password; + return this.self(); + } + + + /** + * @return a url to influxDb + */ + public String getUrl() { + return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); + } + + /** + * @return a influxDb client + */ + public InfluxDB getNewInfluxDB() { + final InfluxDB influxDB = InfluxDBFactory.connect(this.getUrl(), this.username, this.password); + influxDB.setDatabase(this.database); + return influxDB; + } +} diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java new file mode 100644 index 00000000000..c48f73a28cb --- /dev/null +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -0,0 +1,176 @@ +package org.testcontainers.containers; + +import static org.junit.Assert.assertEquals; + +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientFactory; +import com.influxdb.client.InfluxDBClientOptions; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import lombok.SneakyThrows; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitAllStrategy; +import org.testcontainers.lifecycle.Startables; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.MountableFile; + +/** + * See + * https://docs.influxdata.com/influxdb/v2.0/get-started/#download-and-run-influxdb-v2-0 + */ +public class InfluxDBContainerV2> extends GenericContainer { + + public static final Integer INFLUXDB_PORT = 8086; + + private static final String REGISTRY = "quay.io"; + private static final String REPOSITORY = "influxdb/influxdb"; + private static final String TAG = "v2.0.0"; + private static final DockerImageName DEFAULT_IMAGE_NAME = + DockerImageName.parse(String.format("%s/%s:%s", REGISTRY, REPOSITORY, TAG)); + private static final int NO_CONTENT_STATUS_CODE = 204; + private static final String INFLUX_SETUP_SH = "influx-setup.sh"; + + private String username = "test-user"; + private String password = "test-password"; + private String bucket = "test-bucket"; + private String organization = "test-org"; + private int retention = 0; + private String retentionUnit = RetentionUnit.NANOSECONDS.label; + + private InfluxDBContainerV2(final DockerImageName imageName) { + super(imageName); + imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); + this.waitStrategy = (new WaitAllStrategy()) + .withStrategy(Wait + .forHttp("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE)) + .withStrategy(Wait.forListeningPort()); + this.addExposedPort(INFLUXDB_PORT); + } + + public static InfluxDBContainerV2 createWithDefaultTag() { + return new InfluxDBContainerV2<>(DEFAULT_IMAGE_NAME); + } + + public static InfluxDBContainerV2 createWithSpecificTag(final DockerImageName imageName) { + return new InfluxDBContainerV2<>(imageName); + } + + @Override + protected void configure() { + this.addEnv("INFLUXDB_USER", this.username); + this.addEnv("INFLUXDB_PASSWORD", this.password); + this.addEnv("INFLUXDB_BUCKET", this.bucket); + this.addEnv("INFLUXDB_ORG", this.organization); + this.addEnv("INFLUXDB_RETENTION", String.valueOf(this.retention)); + this.addEnv("INFLUXDB_RETENTION_UNIT", this.retentionUnit); + } + + @Override + @SneakyThrows({InterruptedException.class, IOException.class, ExecutionException.class}) + public void start() { + this.withCopyFileToContainer(MountableFile.forClasspathResource(INFLUX_SETUP_SH), + String.format("%s", INFLUX_SETUP_SH)); + if (this.containerId != null) { + return; + } + Startables.deepStart(this.dependencies).get(); + // trigger LazyDockerClient's resolve so that we fail fast here and not in getDockerImageName() + this.dockerClient.authConfig(); + this.doStart(); + final Container.ExecResult execResult = this.execInContainer("chmod", "-x", "/influx-setup.sh"); + assertEquals(execResult.getExitCode(), 0); + final Container.ExecResult writeResult = this.execInContainer("/bin/bash", "/influx-setup.sh"); + assertEquals(writeResult.getExitCode(), 0); + } + + /** + * Set env variable `INFLUXDB_USER`. + * + * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will + * be granted read and write permissions for that database. + * @return a reference to this container instance + */ + public SELF withUsername(final String username) { + this.username = username; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_PASSWORD`. + * + * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is + * generated and printed to standard out. + * @return a reference to this container instance + */ + public SELF withPassword(final String password) { + this.password = password; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_BUCKET`. + * + * @param bucket Automatically initializes a bucket with the name of this environment variable. + * @return a reference to this container instance + */ + public SELF withBucket(final String bucket) { + this.bucket = bucket; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_ORGANIZATION`. + * + * @param organization The organization for the initial setup of influxDB. + * @return a reference to this container instance + */ + public SELF withOrganization(final String organization) { + this.organization = organization; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_RETENTION`. + * + * @param retention Duration bucket will retain data (0 is infinite, default is 0). + * @return a reference to this container instance + */ + public SELF withRetention(final int retention) { + this.retention = retention; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_RETENTION_UNIT`. + * + * @param retentionUnit The retention unit (ns, us, ms, etc.). + * @return a reference to this container instance + */ + public SELF withRetentionUnit(final RetentionUnit retentionUnit) { + this.retentionUnit = retentionUnit.label; + return this.self(); + } + + + /** + * @return a influxDb client + */ + public InfluxDBClient getNewInfluxDB() { + final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() + .url(this.getUrl()) + .authenticate(this.username, this.password.toCharArray()) + .bucket(this.bucket) + .org(this.organization) + .build(); + return InfluxDBClientFactory.create(influxDBClientOptions); + } + + /** + * @return a url to influxDb + */ + String getUrl() { + return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); + } +} diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java b/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnit.java similarity index 78% rename from modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java rename to modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnit.java index 432e3c78aed..24cbfc09f9a 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnits.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnit.java @@ -1,6 +1,6 @@ package org.testcontainers.containers; -public enum RetentionUnits { +public enum RetentionUnit { NANOSECONDS("ns"), MICROSECONDS("us"), MILLISECONDS("ms"), @@ -12,7 +12,7 @@ public enum RetentionUnits { final String label; - RetentionUnits(final String label) { + RetentionUnit(final String label) { this.label = label; } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java new file mode 100644 index 00000000000..97eb12446d0 --- /dev/null +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -0,0 +1,44 @@ +package org.testcontainers.containers; + +import org.influxdb.InfluxDB; +import org.junit.ClassRule; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +public class InfluxDBContainerV1Test { + + @ClassRule + public static InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE); + + @Test + public void getUrl() { + final String actual = influxDBContainer.getUrl(); + + assertThat(actual, notNullValue()); + } + + @Test + public void getNewInfluxDB() { + final InfluxDB actual = influxDBContainer.getNewInfluxDB(); + + assertThat(actual, notNullValue()); + assertThat(actual.ping(), notNullValue()); + } + + @Test + public void getLivenessCheckPort() { + final Integer actual = influxDBContainer.getLivenessCheckPort(); + + assertThat(actual, notNullValue()); + } + + @Test + public void isRunning() { + final boolean actual = influxDBContainer.isRunning(); + + assertThat(actual, is(true)); + } +} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java new file mode 100644 index 00000000000..2f3f7c9de3d --- /dev/null +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java @@ -0,0 +1,72 @@ +package org.testcontainers.containers; + +import org.influxdb.InfluxDB; +import org.influxdb.dto.Point; +import org.influxdb.dto.Query; +import org.influxdb.dto.QueryResult; +import org.junit.Rule; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +public class InfluxDBContainerV1WithUserTest { + + private static final String TEST_VERSION = InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); + private static final String DATABASE = "test"; + private static final String USER = "test-user"; + private static final String PASSWORD = "test-password"; + + @Rule + public InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE) + .withDatabase(DATABASE) + .withUsername(USER) + .withPassword(PASSWORD); + + @Test + public void describeDatabases() { + final InfluxDB actual = this.influxDBContainer.getNewInfluxDB(); + + assertThat(actual, notNullValue()); + assertThat(actual.describeDatabases(), hasItem(DATABASE)); + } + + @Test + public void checkVersion() { + final InfluxDB actual = this.influxDBContainer.getNewInfluxDB(); + + assertThat(actual, notNullValue()); + + assertThat(actual.ping(), notNullValue()); + assertThat(actual.ping().getVersion(), is(TEST_VERSION)); + + assertThat(actual.version(), is(TEST_VERSION)); + } + + @Test + public void queryForWriteAndRead() { + final InfluxDB influxDB = this.influxDBContainer.getNewInfluxDB(); + + final Point point = Point.measurement("cpu") + .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + .addField("idle", 90L) + .addField("user", 9L) + .addField("system", 1L) + .build(); + influxDB.write(point); + + final Query query = new Query("SELECT idle FROM cpu", DATABASE); + final QueryResult actual = influxDB.query(query); + + assertThat(actual, notNullValue()); + assertThat(actual.getError(), nullValue()); + assertThat(actual.getResults(), notNullValue()); + assertThat(actual.getResults().size(), is(1)); + + } +} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java similarity index 68% rename from modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java rename to modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java index aadca36bb94..5b0676004c0 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java @@ -11,19 +11,19 @@ import org.junit.ClassRule; import org.junit.Test; -public class InfluxDBContainerTest { +public class InfluxDBContainerV2Test { - private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); + private static final String TEST_VERSION = InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); @ClassRule - public static final InfluxDBContainer influxDBContainer = InfluxDBContainer - .createWithSpecificTag(InfluxDBTestImages.INFLUXDB_TEST_IMAGE); + public static final InfluxDBContainerV2 influxDBContainerV2 = InfluxDBContainerV2 + .createWithSpecificTag(InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE); private InfluxDBClient client = null; @Before public void setUp() { - this.client = influxDBContainer.getNewInfluxDB(); + this.client = influxDBContainerV2.getNewInfluxDB(); } @After @@ -33,14 +33,14 @@ public void tearDown() { @Test public void getUrl() { - final String actual = influxDBContainer.getUrl(); + final String actual = influxDBContainerV2.getUrl(); assertThat(actual, notNullValue()); } @Test public void getNewInfluxDB() { - final InfluxDBClient actual = influxDBContainer.getNewInfluxDB(); + final InfluxDBClient actual = influxDBContainerV2.getNewInfluxDB(); assertThat(actual, notNullValue()); assertThat(actual.health().getStatus(), is(StatusEnum.PASS)); @@ -59,7 +59,7 @@ public void checkVersion() { @Test public void isRunning() { - final boolean actual = influxDBContainer.isRunning(); + final boolean actual = influxDBContainerV2.isRunning(); assertThat(actual, is(true)); } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java similarity index 92% rename from modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java rename to modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java index d57666b47ee..c3f2ef41454 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java @@ -19,7 +19,7 @@ import org.junit.ClassRule; import org.junit.Test; -public class InfluxDBContainerWithUserTest { +public class InfluxDBContainerV2WithUserTest { private static final String BUCKET = "new-test-bucket"; private static final String USER = "new-test-user"; @@ -29,7 +29,7 @@ public class InfluxDBContainerWithUserTest { private InfluxDBClient client = null; @ClassRule - public static final InfluxDBContainer influxDBContainer = InfluxDBContainer + public static final InfluxDBContainerV2 influxDBContainerV2 = InfluxDBContainerV2 .createWithDefaultTag() .withBucket(BUCKET) .withUsername(USER) @@ -38,7 +38,7 @@ public class InfluxDBContainerWithUserTest { @Before public void setUp() { - this.client = influxDBContainer.getNewInfluxDB(); + this.client = influxDBContainerV2.getNewInfluxDB(); } @After diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV1TestImages.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV1TestImages.java new file mode 100644 index 00000000000..625a00b5b93 --- /dev/null +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV1TestImages.java @@ -0,0 +1,7 @@ +package org.testcontainers.containers; + +import org.testcontainers.utility.DockerImageName; + +public interface InfluxDBV1TestImages { + DockerImageName INFLUXDB_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); +} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java similarity index 82% rename from modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java rename to modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java index 3bb6a6aadfc..125a8ac7c43 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java @@ -2,6 +2,6 @@ import org.testcontainers.utility.DockerImageName; -public interface InfluxDBTestImages { +public interface InfluxDBV2TestImages { DockerImageName INFLUXDB_TEST_IMAGE = DockerImageName.parse("quay.io/influxdb/influxdb:v2.0.0"); } From 31ecbe35bbe0f5088b29356df9e38525129ebfd0 Mon Sep 17 00:00:00 2001 From: raminqaf Date: Sun, 28 Mar 2021 18:09:17 +0200 Subject: [PATCH 06/51] bump inflxudb client version up to 2.0.0 --- modules/influxdb/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 0c1d51f4cba..a703d515359 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -2,7 +2,7 @@ description = "Testcontainers :: InfluxDB" ext { influxdbV1ClientVersion = '2.21' - influxdbV2ClientVersion = '1.14.0' + influxdbV2ClientVersion = '2.0.0' } dependencies { From 11ed5d4eb680ac1379eb6b9b6af7e710c275786f Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 10:49:09 +0200 Subject: [PATCH 07/51] update influxdb2 client and container class and add review suggestions --- docs/modules/databases/influxdb.md | 53 ++++--- modules/influxdb/build.gradle | 2 +- .../containers/InfluxDBContainer.java | 12 ++ .../containers/InfluxDBContainerV1.java | 5 +- .../containers/InfluxDBContainerV2.java | 130 ++++++++---------- .../containers/RetentionUnit.java | 18 --- .../containers/InfluxDBContainerV1Test.java | 11 +- .../InfluxDBContainerV1WithUserTest.java | 15 +- .../containers/InfluxDBContainerV2Test.java | 10 +- .../InfluxDBContainerV2WithUserTest.java | 27 ++-- .../containers/InfluxDBV2TestImages.java | 2 +- .../src/test/resources/influx-setup.sh | 8 -- 12 files changed, 144 insertions(+), 149 deletions(-) delete mode 100644 modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnit.java delete mode 100644 modules/influxdb/src/test/resources/influx-setup.sh diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index 02915a1428a..356b4750199 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -1,62 +1,73 @@ # InfluxDB Module -Testcontainers module for InfluxData [InfluxDB](https://github.com/influxdata/influxdb). +Testcontainers module for InfluxData [InfluxDB](https://www.influxdata.com/products/influxdb/). ## Important Note -They are breaking changes in InfluxDB v2.x. For more information refer to the main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). -Influx Data migrated to quay.io as their new [container registry](https://quay.io/repository/influxdb/influxdb). + +They are breaking changes in InfluxDB v2.x. For more information refer to the +main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). InfluxDB +official [container registry](https://hub.docker.com/_/influxdb) on docker hub. ## InfluxDB V2.x Usage example + Running influxDbContainer as a stand-in for InfluxDB in a test: ```java public class SomeTest { @ClassRule - public static final InfluxDBV2Container influxDbContainer = InfluxDBV2Container.createWithDefaultTag(); + public static final InfluxDBV2Container influxDbContainer = + new InfluxDBContainerV2<>(DockerImageName.parse("influxdb")); @Test public void someTestMethod() { - final InfluxDBClient influxDB = influxDbContainer.getNewInfluxDB(); + final InfluxDBClient influxDB = influxDbContainer.getInfluxDBClient(); // Rest of the test } } ``` -This will create a InfluxDB container with tag v2.0.0. The influxDB will be setup with the following data:
+ +This will create a InfluxDB container with the latest tag ( +available [tags](https://hub.docker.com/_/influxdb?tab=tags&page=1&ordering=last_updated)). The influxDB will be setup +with the following data:
| Property | Default Value | | ------------- |:-------------:| -| User | test-user | +| Username | test-user | | Password | test-password | -| Bucket | test-bucket | | Organization | test-org | +| Bucket | test-bucket | | Retention | 0 (infinite) | -| Retention Unit| ns | -For more details about the InfluxDB setup go to the official [docs](https://docs.influxdata.com/influxdb/v2.0/reference/cli/influx/setup/). +| Admin Token | - | + +For more details about the InfluxDB setup go to the +official [docs](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials) +. It is possible to override the default values and start the container with a newer tag: + ```java public class SomeTest { @ClassRule - public static final InfluxDBContainerV2 influxDBContainer = InfluxDBContainerV2 - .createWithSpecificTag(DockerImageName.parse("quay.io/influxdb/influxdb:v2.0.3")) - .withBucket(BUCKET) - .withUsername(USER) - .withPassword(PASSWORD) - .withOrganization(ORG); + public static final InfluxDBContainerV2 influxDBContainer = + new InfluxDBContainerV2<>(InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE) + .withUsername(USERNAME) + .withPassword(PASSWORD) + .withOrganization(ORG) + .withBucket(BUCKET) + .withRetention(RETENTION) // Optional + .withAdminToken(ADMIN_TOKEN); // Optional @Test public void someTestMethod() { - final InfluxDBClient influxDB = influxDbContainer.getNewInfluxDB(); + final InfluxDBClient influxDB = influxDbContainer.getInfluxDBClient(); // rest of the test } } ``` -**NOTE**: The `createWithSpecificTag` static factory needs a valid registry and tag name. You can find the latest tags [here](https://quay.io/repository/influxdb/influxdb?tab=tags).
-**NOTE**: You can find the latest documentation about the influxdb java client [here](https://github.com/influxdata/influxdb-client-java). -**Hint:** - Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency. +**NOTE**: You can find the latest documentation about the influxdb java +client [here](https://github.com/influxdata/influxdb-client-java). ## InfluxDB V1.x Usage example (Deprecated) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index a703d515359..f23c3a77c77 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -2,7 +2,7 @@ description = "Testcontainers :: InfluxDB" ext { influxdbV1ClientVersion = '2.21' - influxdbV2ClientVersion = '2.0.0' + influxdbV2ClientVersion = '3.1.0' } dependencies { diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index da91beadc0c..bb54b92bbbc 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -2,8 +2,20 @@ import org.testcontainers.utility.DockerImageName; +/** + * @deprecated instead use {@link InfluxDBContainerV1#InfluxDBContainerV1(DockerImageName) } for InfluxDB 1.x or {@link + * InfluxDBContainerV2#InfluxDBContainerV2(DockerImageName)} for InfluxDB 2.x instead + */ @Deprecated public class InfluxDBContainer> extends InfluxDBContainerV1 { + public InfluxDBContainer() { + super(); + } + + public InfluxDBContainer(final String version) { + super(version); + } + public InfluxDBContainer(final DockerImageName influxdbTestImage) { super(influxdbTestImage); } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index f7a69f3a5de..30e9135a5b1 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -11,7 +11,7 @@ /** * See https://store.docker.com/images/influxdb */ -class InfluxDBContainerV1> extends GenericContainer { +public class InfluxDBContainerV1> extends GenericContainer { public static final Integer INFLUXDB_PORT = 8086; @@ -53,7 +53,8 @@ public InfluxDBContainerV1(final DockerImageName dockerImageName) { dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); this.waitStrategy = new WaitAllStrategy() - .withStrategy(Wait.forHttp("/ping").withBasicCredentials(this.username, this.password).forStatusCode(NO_CONTENT_STATUS_CODE)) + .withStrategy(Wait.forHttp("/ping").withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE)) .withStrategy(Wait.forListeningPort()); this.addExposedPort(INFLUXDB_PORT); diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index c48f73a28cb..aeef0ea7cce 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -1,43 +1,34 @@ package org.testcontainers.containers; -import static org.junit.Assert.assertEquals; - import com.influxdb.client.InfluxDBClient; import com.influxdb.client.InfluxDBClientFactory; import com.influxdb.client.InfluxDBClientOptions; -import java.io.IOException; -import java.util.concurrent.ExecutionException; -import lombok.SneakyThrows; +import java.util.Optional; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitAllStrategy; -import org.testcontainers.lifecycle.Startables; import org.testcontainers.utility.DockerImageName; -import org.testcontainers.utility.MountableFile; /** - * See - * https://docs.influxdata.com/influxdb/v2.0/get-started/#download-and-run-influxdb-v2-0 + * Refer to + * the official InfluxDB 2.x container repository + * on docker hub for detailed documentation and newest tags. */ public class InfluxDBContainerV2> extends GenericContainer { public static final Integer INFLUXDB_PORT = 8086; - private static final String REGISTRY = "quay.io"; - private static final String REPOSITORY = "influxdb/influxdb"; - private static final String TAG = "v2.0.0"; - private static final DockerImageName DEFAULT_IMAGE_NAME = - DockerImageName.parse(String.format("%s/%s:%s", REGISTRY, REPOSITORY, TAG)); + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); private static final int NO_CONTENT_STATUS_CODE = 204; - private static final String INFLUX_SETUP_SH = "influx-setup.sh"; private String username = "test-user"; private String password = "test-password"; private String bucket = "test-bucket"; private String organization = "test-org"; - private int retention = 0; - private String retentionUnit = RetentionUnit.NANOSECONDS.label; + private Optional retention = Optional.empty(); + private Optional adminToken = Optional.empty(); + - private InfluxDBContainerV2(final DockerImageName imageName) { + public InfluxDBContainerV2(final DockerImageName imageName) { super(imageName); imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); this.waitStrategy = (new WaitAllStrategy()) @@ -49,44 +40,39 @@ private InfluxDBContainerV2(final DockerImageName imageName) { this.addExposedPort(INFLUXDB_PORT); } - public static InfluxDBContainerV2 createWithDefaultTag() { - return new InfluxDBContainerV2<>(DEFAULT_IMAGE_NAME); - } - - public static InfluxDBContainerV2 createWithSpecificTag(final DockerImageName imageName) { - return new InfluxDBContainerV2<>(imageName); - } - + /** + *

+ * The InfluxDB image contains some extra functionality to automatically bootstrap the system. This functionality is + * enabled by setting the DOCKER_INFLUXDB_INIT_MODE environment variable to the value "setup" when running the + * container. Additional environment variables are used to configure the setup logic: + *

    + *
  • DOCKER_INFLUXDB_INIT_USERNAME: The username to set for the system's initial super-user (Required). + *
  • DOCKER_INFLUXDB_INIT_PASSWORD: The password to set for the system's initial super-user (Required). + *
  • DOCKER_INFLUXDB_INIT_ORG: The name to set for the system's initial organization (Required). + *
  • DOCKER_INFLUXDB_INIT_BUCKET: The name to set for the system's initial bucket (Required). + *
  • DOCKER_INFLUXDB_INIT_RETENTION: The duration the system's initial bucket should retain data. If not set, + * the initial bucket will retain data forever. + *
  • DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: The authentication token to associate with the system's initial + * super-user. If not set, a token will be auto-generated by the system. + *
+ *
+ * See + * + * full documentation + */ @Override protected void configure() { - this.addEnv("INFLUXDB_USER", this.username); - this.addEnv("INFLUXDB_PASSWORD", this.password); - this.addEnv("INFLUXDB_BUCKET", this.bucket); - this.addEnv("INFLUXDB_ORG", this.organization); - this.addEnv("INFLUXDB_RETENTION", String.valueOf(this.retention)); - this.addEnv("INFLUXDB_RETENTION_UNIT", this.retentionUnit); - } - - @Override - @SneakyThrows({InterruptedException.class, IOException.class, ExecutionException.class}) - public void start() { - this.withCopyFileToContainer(MountableFile.forClasspathResource(INFLUX_SETUP_SH), - String.format("%s", INFLUX_SETUP_SH)); - if (this.containerId != null) { - return; - } - Startables.deepStart(this.dependencies).get(); - // trigger LazyDockerClient's resolve so that we fail fast here and not in getDockerImageName() - this.dockerClient.authConfig(); - this.doStart(); - final Container.ExecResult execResult = this.execInContainer("chmod", "-x", "/influx-setup.sh"); - assertEquals(execResult.getExitCode(), 0); - final Container.ExecResult writeResult = this.execInContainer("/bin/bash", "/influx-setup.sh"); - assertEquals(writeResult.getExitCode(), 0); + this.addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); + this.addEnv("DOCKER_INFLUXDB_INIT_USERNAME", this.username); + this.addEnv("DOCKER_INFLUXDB_INIT_PASSWORD", this.password); + this.addEnv("DOCKER_INFLUXDB_INIT_ORG", this.organization); + this.addEnv("DOCKER_INFLUXDB_INIT_BUCKET", this.bucket); + this.retention.ifPresent(ret -> this.addEnv("DOCKER_INFLUXDB_INIT_RETENTION", ret)); + this.adminToken.ifPresent(token -> this.addEnv("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", token)); } /** - * Set env variable `INFLUXDB_USER`. + * Set env variable `DOCKER_INFLUXDB_INIT_USERNAME`. * * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will * be granted read and write permissions for that database. @@ -98,7 +84,7 @@ public SELF withUsername(final String username) { } /** - * Set env variable `INFLUXDB_PASSWORD`. + * Set env variable `DOCKER_INFLUXDB_INIT_PASSWORD`. * * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is * generated and printed to standard out. @@ -110,54 +96,58 @@ public SELF withPassword(final String password) { } /** - * Set env variable `INFLUXDB_BUCKET`. + * Set env variable `DOCKER_INFLUXDB_INIT_ORG`. * - * @param bucket Automatically initializes a bucket with the name of this environment variable. + * @param organization The organization for the initial setup of influxDB. * @return a reference to this container instance */ - public SELF withBucket(final String bucket) { - this.bucket = bucket; + public SELF withOrganization(final String organization) { + this.organization = organization; return this.self(); } /** - * Set env variable `INFLUXDB_ORGANIZATION`. + * Set env variable `DOCKER_INFLUXDB_INIT_BUCKET`. * - * @param organization The organization for the initial setup of influxDB. + * @param bucket Automatically initializes a bucket with the name of this environment variable. * @return a reference to this container instance */ - public SELF withOrganization(final String organization) { - this.organization = organization; + public SELF withBucket(final String bucket) { + this.bucket = bucket; return this.self(); } /** - * Set env variable `INFLUXDB_RETENTION`. + * Set env variable `DOCKER_INFLUXDB_INIT_RETENTION`. * * @param retention Duration bucket will retain data (0 is infinite, default is 0). * @return a reference to this container instance */ - public SELF withRetention(final int retention) { - this.retention = retention; + public SELF withRetention(final String retention) { + this.retention = Optional.of(retention); return this.self(); } /** - * Set env variable `INFLUXDB_RETENTION_UNIT`. + * Set env variable `DOCKER_INFLUXDB_INIT_ADMIN_TOKEN`. * - * @param retentionUnit The retention unit (ns, us, ms, etc.). + * @param adminToken Authentication token to associate with the admin user. * @return a reference to this container instance */ - public SELF withRetentionUnit(final RetentionUnit retentionUnit) { - this.retentionUnit = retentionUnit.label; + public SELF withAdminToken(final String adminToken) { + this.adminToken = Optional.of(adminToken); return this.self(); } /** - * @return a influxDb client + * This method uses the InfluxDBClientOptions builder to build a InfluxDB client + * + * @return a InfluxDB client + * @see InfluxDBClientOptions + * @see InfluxDBClient */ - public InfluxDBClient getNewInfluxDB() { + public InfluxDBClient getInfluxDBClient() { final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() .url(this.getUrl()) .authenticate(this.username, this.password.toCharArray()) @@ -170,7 +160,7 @@ public InfluxDBClient getNewInfluxDB() { /** * @return a url to influxDb */ - String getUrl() { + public String getUrl() { return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); } } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnit.java b/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnit.java deleted file mode 100644 index 24cbfc09f9a..00000000000 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/RetentionUnit.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.testcontainers.containers; - -public enum RetentionUnit { - NANOSECONDS("ns"), - MICROSECONDS("us"), - MILLISECONDS("ms"), - SECONDS("s"), - MINUTES("m"), - HOURS("h"), - DAYS("d"), - WEEKS("w"); - - final String label; - - RetentionUnit(final String label) { - this.label = label; - } -} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 97eb12446d0..53f30910598 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -1,17 +1,18 @@ package org.testcontainers.containers; -import org.influxdb.InfluxDB; -import org.junit.ClassRule; -import org.junit.Test; - import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; +import org.influxdb.InfluxDB; +import org.junit.ClassRule; +import org.junit.Test; + public class InfluxDBContainerV1Test { @ClassRule - public static InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE); + public static InfluxDBContainer influxDBContainer = + new InfluxDBContainer<>(InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE); @Test public void getUrl() { diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java index 2f3f7c9de3d..5970161f932 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java @@ -1,5 +1,12 @@ package org.testcontainers.containers; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import java.util.concurrent.TimeUnit; import org.influxdb.InfluxDB; import org.influxdb.dto.Point; import org.influxdb.dto.Query; @@ -7,14 +14,6 @@ import org.junit.Rule; import org.junit.Test; -import java.util.concurrent.TimeUnit; - -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - public class InfluxDBContainerV1WithUserTest { private static final String TEST_VERSION = InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java index 5b0676004c0..bafc1b65738 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java @@ -16,14 +16,14 @@ public class InfluxDBContainerV2Test { private static final String TEST_VERSION = InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); @ClassRule - public static final InfluxDBContainerV2 influxDBContainerV2 = InfluxDBContainerV2 - .createWithSpecificTag(InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE); + public static final InfluxDBContainerV2 influxDBContainerV2 = + new InfluxDBContainerV2<>(InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE); private InfluxDBClient client = null; @Before public void setUp() { - this.client = influxDBContainerV2.getNewInfluxDB(); + this.client = influxDBContainerV2.getInfluxDBClient(); } @After @@ -40,7 +40,7 @@ public void getUrl() { @Test public void getNewInfluxDB() { - final InfluxDBClient actual = influxDBContainerV2.getNewInfluxDB(); + final InfluxDBClient actual = influxDBContainerV2.getInfluxDBClient(); assertThat(actual, notNullValue()); assertThat(actual.health().getStatus(), is(StatusEnum.PASS)); @@ -52,7 +52,7 @@ public void checkVersion() { assertThat(this.client.health().getStatus(), is(StatusEnum.PASS)); - final String actualVersion = String.format("v%s", this.client.health().getVersion()); + final String actualVersion = String.format("%s", this.client.health().getVersion()); assertThat(actualVersion, is(TEST_VERSION)); } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java index c3f2ef41454..b39c145d88e 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java @@ -21,24 +21,29 @@ public class InfluxDBContainerV2WithUserTest { - private static final String BUCKET = "new-test-bucket"; - private static final String USER = "new-test-user"; + private static final String USERNAME = "new-test-user"; private static final String PASSWORD = "new-test-password"; private static final String ORG = "new-test-org"; + private static final String BUCKET = "new-test-bucket"; + private static final String RETENTION = "1w"; + private static final String ADMIN_TOKEN = "super-secret-token"; + private InfluxDBClient client = null; @ClassRule - public static final InfluxDBContainerV2 influxDBContainerV2 = InfluxDBContainerV2 - .createWithDefaultTag() - .withBucket(BUCKET) - .withUsername(USER) - .withPassword(PASSWORD) - .withOrganization(ORG); + public static final InfluxDBContainerV2 influxDBContainerV2 = + new InfluxDBContainerV2<>(InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE) + .withUsername(USERNAME) + .withPassword(PASSWORD) + .withOrganization(ORG) + .withBucket(BUCKET) + .withRetention(RETENTION) + .withAdminToken(ADMIN_TOKEN); @Before public void setUp() { - this.client = influxDBContainerV2.getNewInfluxDB(); + this.client = influxDBContainerV2.getInfluxDBClient(); } @After @@ -58,6 +63,8 @@ public void getBucket() { @Test public void queryForWriteAndRead() { + assertThat(this.client, notNullValue()); + try (final WriteApi writeApi = this.client.getWriteApi()) { // @@ -65,7 +72,7 @@ public void queryForWriteAndRead() { // final Point point = Point.measurement("temperature") .addTag("location", "west") - .addField("value", 55D) + .addField("value", 55.0D) .time(Instant.now().toEpochMilli(), WritePrecision.MS); writeApi.writePoint(point); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java index 125a8ac7c43..c7d5f9e9211 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java @@ -3,5 +3,5 @@ import org.testcontainers.utility.DockerImageName; public interface InfluxDBV2TestImages { - DockerImageName INFLUXDB_TEST_IMAGE = DockerImageName.parse("quay.io/influxdb/influxdb:v2.0.0"); + DockerImageName INFLUXDB_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); } diff --git a/modules/influxdb/src/test/resources/influx-setup.sh b/modules/influxdb/src/test/resources/influx-setup.sh deleted file mode 100644 index ebd8c2d6582..00000000000 --- a/modules/influxdb/src/test/resources/influx-setup.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -influx setup \ - --force \ - --username "${INFLUXDB_USER}" \ - --password "${INFLUXDB_PASSWORD}" \ - --bucket "${INFLUXDB_BUCKET}" \ - --org "${INFLUXDB_ORG}" \ - --retention "${INFLUXDB_RETENTION}""${INFLUXDB_RETENTION_UNIT}" From 56887b05a67a1f80e08421f39cde65c99ded5026 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 10:57:30 +0200 Subject: [PATCH 08/51] fix influxDB 1 dependency --- modules/influxdb/build.gradle | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index f23c3a77c77..90cbe1e915c 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -8,8 +8,7 @@ ext { dependencies { compile project(':testcontainers') - compileOnly group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" - testCompile group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" + implementation group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" compileOnly group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" testCompile group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" From 79d6dce9271b411f682256ea3999dafde4e5fb64 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 11:08:07 +0200 Subject: [PATCH 09/51] replace testCompile with testImplementation in build.gradle file --- modules/influxdb/build.gradle | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 73b4c24de03..af6c6f936f5 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -8,8 +8,7 @@ ext { dependencies { api project(':testcontainers') - implementation group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" + testImplementation group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" - compileOnly group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" - testCompile group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" + testImplementation group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" } From b4ba6bfc8d4b686848ed8ccc691f3058a4844195 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 11:13:22 +0200 Subject: [PATCH 10/51] implementation to build.gradle --- modules/influxdb/build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index af6c6f936f5..322d5de07b0 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -8,7 +8,9 @@ ext { dependencies { api project(':testcontainers') + implementation group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" testImplementation group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" + implementation group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" testImplementation group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" } From 9bc928e80eb978a765e0b5e310615012a0d3dd9f Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 13:27:29 +0200 Subject: [PATCH 11/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java Co-authored-by: Richard North --- .../java/org/testcontainers/containers/InfluxDBContainerV1.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index 30e9135a5b1..f3a64ee3822 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -32,7 +32,7 @@ public class InfluxDBContainerV1> extends private String password = "any"; /** - * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + * @deprecated use {@link InfluxDBContainerV1(DockerImageName)} instead */ @Deprecated public InfluxDBContainerV1() { From c4a23f35ba31db1c5cff1101c95095e60cb01ec4 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 13:27:39 +0200 Subject: [PATCH 12/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java Co-authored-by: Richard North --- .../java/org/testcontainers/containers/InfluxDBContainerV1.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index f3a64ee3822..fd12d636b40 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -40,7 +40,7 @@ public InfluxDBContainerV1() { } /** - * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + * @deprecated use {@link InfluxDBContainerV1(DockerImageName)} instead */ @Deprecated public InfluxDBContainerV1(final String version) { From 66e507ac05bc26966e999c0aa85079f2225e887e Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 13:50:03 +0200 Subject: [PATCH 13/51] update README and add review suggestions --- docs/modules/databases/influxdb.md | 81 ++++++------------- modules/influxdb/build.gradle | 13 +-- .../containers/InfluxDBContainerV1Test.java | 2 +- .../InfluxDBContainerV1WithUserTest.java | 4 +- .../containers/InfluxDBContainerV2Test.java | 4 +- .../InfluxDBContainerV2WithUserTest.java | 2 +- .../containers/InfluxDBTestImages.java | 8 ++ .../containers/InfluxDBV1TestImages.java | 7 -- .../containers/InfluxDBV2TestImages.java | 7 -- 9 files changed, 41 insertions(+), 87 deletions(-) create mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java delete mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV1TestImages.java delete mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index a002258db13..f515bf5b89f 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -12,80 +12,42 @@ official [container registry](https://hub.docker.com/_/influxdb) on docker hub. Running influxDbContainer as a stand-in for InfluxDB in a test: -```java -public class SomeTest { - - @ClassRule - public static final InfluxDBV2Container influxDbContainer = - new InfluxDBContainerV2<>(DockerImageName.parse("influxdb")); - - @Test - public void someTestMethod() { - final InfluxDBClient influxDB = influxDbContainer.getInfluxDBClient(); - // Rest of the test - } -} -``` + +[InfluxDBContainerV2Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java) + + -This will create a InfluxDB container with the latest tag ( -available [tags](https://hub.docker.com/_/influxdb?tab=tags&page=1&ordering=last_updated)). The influxDB will be setup -with the following data:
+The influxDB will be setup with the following data:
| Property | Default Value | | ------------- |:-------------:| -| Username | test-user | -| Password | test-password | -| Organization | test-org | -| Bucket | test-bucket | -| Retention | 0 (infinite) | -| Admin Token | - | +| username | test-user | +| password | test-password | +| organization | test-org | +| bucket | test-bucket | +| retention | 0 (infinite) | +| adminToken | - | For more details about the InfluxDB setup go to the official [docs](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials) . -It is possible to override the default values and start the container with a newer tag: - -```java -public class SomeTest { - @ClassRule - public static final InfluxDBContainerV2 influxDBContainer = - new InfluxDBContainerV2<>(InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE) - .withUsername(USERNAME) - .withPassword(PASSWORD) - .withOrganization(ORG) - .withBucket(BUCKET) - .withRetention(RETENTION) // Optional - .withAdminToken(ADMIN_TOKEN); // Optional - - @Test - public void someTestMethod() { - final InfluxDBClient influxDB = influxDbContainer.getInfluxDBClient(); - // rest of the test - } -} -``` +It is possible to override the default values, take a look at these tests: + + +[InfluxDBContainerV2WithUserTest](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java) + -**NOTE**: You can find the latest documentation about the influxdb java +**NOTE**: You can find the latest documentation about the influxdb v2.x java client [here](https://github.com/influxdata/influxdb-client-java). ## InfluxDB V1.x Usage example (Deprecated) Running influxDbContainer as a stand-in for InfluxDB in a test: -```java -public class SomeTest { - - @Rule - public InfluxDBContainer influxDbContainer = new InfluxDBContainer(); - - @Test - public void someTestMethod() { - final InfluxDB influxDB = this.influxDbContainer.getNewInfluxDB(); - // Rest of the test - } -} -``` + +[InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) + ## Adding this module to your project dependencies @@ -103,3 +65,6 @@ testImplementation "org.testcontainers:influxdb:{{latest_version}}" test ``` + +**Hint:** Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You +should ensure that your project also has a suitable database driver as a dependency. diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 322d5de07b0..86b3285c896 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -1,16 +1,11 @@ description = "Testcontainers :: InfluxDB" -ext { - influxdbV1ClientVersion = '2.21' - influxdbV2ClientVersion = '3.1.0' -} - dependencies { api project(':testcontainers') - implementation group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" - testImplementation group: 'org.influxdb', name: 'influxdb-java', version: "${influxdbV1ClientVersion}" + implementation "org.influxdb:influxdb-java:2.21" + implementation "org.influxdb:influxdb-java:2.21" - implementation group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" - testImplementation group:'com.influxdb', name:'influxdb-client-java', version:"${influxdbV2ClientVersion}" + implementation "com.influxdb:influxdb-client-java:3.1.0" + testImplementation "com.influxdb:influxdb-client-java:3.1.0" } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 53f30910598..0c52be9b19d 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -12,7 +12,7 @@ public class InfluxDBContainerV1Test { @ClassRule public static InfluxDBContainer influxDBContainer = - new InfluxDBContainer<>(InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE); + new InfluxDBContainer<>(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE); @Test public void getUrl() { diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java index 5970161f932..c5af2e830a3 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java @@ -16,13 +16,13 @@ public class InfluxDBContainerV1WithUserTest { - private static final String TEST_VERSION = InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); + private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE.getVersionPart(); private static final String DATABASE = "test"; private static final String USER = "test-user"; private static final String PASSWORD = "test-password"; @Rule - public InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBV1TestImages.INFLUXDB_TEST_IMAGE) + public InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java index bafc1b65738..a82b2de5267 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java @@ -13,11 +13,11 @@ public class InfluxDBContainerV2Test { - private static final String TEST_VERSION = InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE.getVersionPart(); + private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE.getVersionPart(); @ClassRule public static final InfluxDBContainerV2 influxDBContainerV2 = - new InfluxDBContainerV2<>(InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE); + new InfluxDBContainerV2<>(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE); private InfluxDBClient client = null; diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java index b39c145d88e..563efb36a52 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java @@ -33,7 +33,7 @@ public class InfluxDBContainerV2WithUserTest { @ClassRule public static final InfluxDBContainerV2 influxDBContainerV2 = - new InfluxDBContainerV2<>(InfluxDBV2TestImages.INFLUXDB_TEST_IMAGE) + new InfluxDBContainerV2<>(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java new file mode 100644 index 00000000000..8b12ce92ebd --- /dev/null +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java @@ -0,0 +1,8 @@ +package org.testcontainers.containers; + +import org.testcontainers.utility.DockerImageName; + +public interface InfluxDBTestImages { + DockerImageName INFLUXDB_V1_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); + DockerImageName INFLUXDB_V2_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); +} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV1TestImages.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV1TestImages.java deleted file mode 100644 index 625a00b5b93..00000000000 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV1TestImages.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.testcontainers.containers; - -import org.testcontainers.utility.DockerImageName; - -public interface InfluxDBV1TestImages { - DockerImageName INFLUXDB_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); -} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java deleted file mode 100644 index c7d5f9e9211..00000000000 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestImages.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.testcontainers.containers; - -import org.testcontainers.utility.DockerImageName; - -public interface InfluxDBV2TestImages { - DockerImageName INFLUXDB_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); -} From 5021425c7391c932ac9a8c2312cfcaa83eef263d Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 16:53:52 +0200 Subject: [PATCH 14/51] Update docs/modules/databases/influxdb.md Co-authored-by: Richard North --- docs/modules/databases/influxdb.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index f515bf5b89f..57e0fa6fa65 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -66,5 +66,5 @@ testImplementation "org.testcontainers:influxdb:{{latest_version}}" ``` -**Hint:** Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You -should ensure that your project also has a suitable database driver as a dependency. +!!! hint + Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency. From 0f5242310fb2ef3bffdc6961c4435473d98c1750 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 17:03:21 +0200 Subject: [PATCH 15/51] fix build.gradle --- modules/influxdb/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 86b3285c896..3b6868f7ebe 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -4,7 +4,7 @@ dependencies { api project(':testcontainers') implementation "org.influxdb:influxdb-java:2.21" - implementation "org.influxdb:influxdb-java:2.21" + testImplementation "org.influxdb:influxdb-java:2.21" implementation "com.influxdb:influxdb-client-java:3.1.0" testImplementation "com.influxdb:influxdb-client-java:3.1.0" From d606cb1fd153eeb2a066fb6591e95c6260f2e0c0 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 17:04:06 +0200 Subject: [PATCH 16/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java Co-authored-by: Sergei Egorov --- .../testcontainers/containers/InfluxDBContainerV1.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index fd12d636b40..7c38871d244 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -31,14 +31,6 @@ public class InfluxDBContainerV1> extends private String username = "any"; private String password = "any"; - /** - * @deprecated use {@link InfluxDBContainerV1(DockerImageName)} instead - */ - @Deprecated - public InfluxDBContainerV1() { - this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); - } - /** * @deprecated use {@link InfluxDBContainerV1(DockerImageName)} instead */ From 2f8fdbc11ae37de38b073b25d273e6ef69acb857 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 17:05:24 +0200 Subject: [PATCH 17/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java Co-authored-by: Sergei Egorov --- .../java/org/testcontainers/containers/InfluxDBContainerV1.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index 7c38871d244..cdb27233b37 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -11,7 +11,7 @@ /** * See
https://store.docker.com/images/influxdb */ -public class InfluxDBContainerV1> extends GenericContainer { +public class InfluxDBContainerV1 extends GenericContainer { public static final Integer INFLUXDB_PORT = 8086; From dfe23b6a63436cdce1a5c79946bb9a9be85cbdd9 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 17:05:36 +0200 Subject: [PATCH 18/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java Co-authored-by: Sergei Egorov --- .../java/org/testcontainers/containers/InfluxDBContainerV2.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index aeef0ea7cce..c6c8830a566 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -13,7 +13,7 @@ * the official InfluxDB 2.x container repository * on docker hub for detailed documentation and newest tags. */ -public class InfluxDBContainerV2> extends GenericContainer { +public class InfluxDBContainerV2 extends GenericContainer { public static final Integer INFLUXDB_PORT = 8086; From 1b4ad562cc15cf466a650ee4312787f6c982bedf Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 17:12:46 +0200 Subject: [PATCH 19/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java Co-authored-by: Sergei Egorov --- .../containers/InfluxDBContainerV2.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index c6c8830a566..e8efe3c2fd1 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -31,11 +31,13 @@ public class InfluxDBContainerV2 extends GenericContainer { public InfluxDBContainerV2(final DockerImageName imageName) { super(imageName); imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - this.waitStrategy = (new WaitAllStrategy()) - .withStrategy(Wait - .forHttp("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE)) + this.waitStrategy = new WaitAllStrategy() + .withStrategy( + Wait + .forHttp("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE) + ) .withStrategy(Wait.forListeningPort()); this.addExposedPort(INFLUXDB_PORT); } From e67fc5a1416437eea3fafe44fab214839d1a5416 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 17:31:43 +0200 Subject: [PATCH 20/51] add suggestions --- .../containers/InfluxDBContainer.java | 2 +- .../containers/InfluxDBContainerV1.java | 32 ++++++----- .../containers/InfluxDBContainerV2.java | 54 +++++++++++-------- .../containers/InfluxDBContainerV1Test.java | 12 ++--- .../InfluxDBContainerV1WithUserTest.java | 14 ++--- .../containers/InfluxDBContainerV2Test.java | 12 ++--- .../InfluxDBContainerV2WithUserTest.java | 12 ++--- 7 files changed, 79 insertions(+), 59 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index bb54b92bbbc..ec1652f8769 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -7,7 +7,7 @@ * InfluxDBContainerV2#InfluxDBContainerV2(DockerImageName)} for InfluxDB 2.x instead */ @Deprecated -public class InfluxDBContainer> extends InfluxDBContainerV1 { +public class InfluxDBContainer extends InfluxDBContainerV1 { public InfluxDBContainer() { super(); } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index cdb27233b37..e97b4a76be6 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -31,6 +31,14 @@ public class InfluxDBContainerV1 extends GenericContainer { private String username = "any"; private String password = "any"; + /** + * @deprecated use {@link InfluxDBContainerV1(DockerImageName)} instead + */ + @Deprecated + public InfluxDBContainerV1() { + this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); + } + /** * @deprecated use {@link InfluxDBContainerV1(DockerImageName)} instead */ @@ -75,9 +83,9 @@ public Set getLivenessCheckPortNumbers() { * @param authEnabled Enables authentication. * @return a reference to this container instance */ - public SELF withAuthEnabled(final boolean authEnabled) { + public InfluxDBContainerV1 withAuthEnabled(final boolean authEnabled) { this.authEnabled = authEnabled; - return this.self(); + return this; } /** @@ -86,9 +94,9 @@ public SELF withAuthEnabled(final boolean authEnabled) { * @param admin The name of the admin user to be created. If this is unset, no admin user is created. * @return a reference to this container instance */ - public SELF withAdmin(final String admin) { + public InfluxDBContainerV1 withAdmin(final String admin) { this.admin = admin; - return this.self(); + return this; } /** @@ -98,9 +106,9 @@ public SELF withAdmin(final String admin) { * random password is generated and printed to standard out. * @return a reference to this container instance */ - public SELF withAdminPassword(final String adminPassword) { + public InfluxDBContainerV1 withAdminPassword(final String adminPassword) { this.adminPassword = adminPassword; - return this.self(); + return this; } /** @@ -109,9 +117,9 @@ public SELF withAdminPassword(final String adminPassword) { * @param database Automatically initializes a database with the name of this environment variable. * @return a reference to this container instance */ - public SELF withDatabase(final String database) { + public InfluxDBContainerV1 withDatabase(final String database) { this.database = database; - return this.self(); + return this; } /** @@ -121,9 +129,9 @@ public SELF withDatabase(final String database) { * granted read and write permissions for that database. * @return a reference to this container instance */ - public SELF withUsername(final String username) { + public InfluxDBContainerV1 withUsername(final String username) { this.username = username; - return this.self(); + return this; } /** @@ -133,9 +141,9 @@ public SELF withUsername(final String username) { * generated and printed to standard out. * @return a reference to this container instance */ - public SELF withPassword(final String password) { + public InfluxDBContainerV1 withPassword(final String password) { this.password = password; - return this.self(); + return this; } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index c6c8830a566..25b0c68b537 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -28,16 +28,18 @@ public class InfluxDBContainerV2 extends GenericContainer { private Optional adminToken = Optional.empty(); + public InfluxDBContainerV2(final String imageName) { + super(DockerImageName.parse(imageName)); + final DockerImageName dockerImageName = DockerImageName.parse(imageName); + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); + this.setWaitStrategy(); + } + + public InfluxDBContainerV2(final DockerImageName imageName) { super(imageName); imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - this.waitStrategy = (new WaitAllStrategy()) - .withStrategy(Wait - .forHttp("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE)) - .withStrategy(Wait.forListeningPort()); - this.addExposedPort(INFLUXDB_PORT); + this.setWaitStrategy(); } /** @@ -57,8 +59,7 @@ public InfluxDBContainerV2(final DockerImageName imageName) { * *
* See - * - * full documentation + * full documentation */ @Override protected void configure() { @@ -78,9 +79,9 @@ protected void configure() { * be granted read and write permissions for that database. * @return a reference to this container instance */ - public SELF withUsername(final String username) { + public InfluxDBContainerV2 withUsername(final String username) { this.username = username; - return this.self(); + return this; } /** @@ -90,9 +91,9 @@ public SELF withUsername(final String username) { * generated and printed to standard out. * @return a reference to this container instance */ - public SELF withPassword(final String password) { + public InfluxDBContainerV2 withPassword(final String password) { this.password = password; - return this.self(); + return this; } /** @@ -101,9 +102,9 @@ public SELF withPassword(final String password) { * @param organization The organization for the initial setup of influxDB. * @return a reference to this container instance */ - public SELF withOrganization(final String organization) { + public InfluxDBContainerV2 withOrganization(final String organization) { this.organization = organization; - return this.self(); + return this; } /** @@ -112,9 +113,9 @@ public SELF withOrganization(final String organization) { * @param bucket Automatically initializes a bucket with the name of this environment variable. * @return a reference to this container instance */ - public SELF withBucket(final String bucket) { + public InfluxDBContainerV2 withBucket(final String bucket) { this.bucket = bucket; - return this.self(); + return this; } /** @@ -123,9 +124,9 @@ public SELF withBucket(final String bucket) { * @param retention Duration bucket will retain data (0 is infinite, default is 0). * @return a reference to this container instance */ - public SELF withRetention(final String retention) { + public InfluxDBContainerV2 withRetention(final String retention) { this.retention = Optional.of(retention); - return this.self(); + return this; } /** @@ -134,9 +135,9 @@ public SELF withRetention(final String retention) { * @param adminToken Authentication token to associate with the admin user. * @return a reference to this container instance */ - public SELF withAdminToken(final String adminToken) { + public InfluxDBContainerV2 withAdminToken(final String adminToken) { this.adminToken = Optional.of(adminToken); - return this.self(); + return this; } @@ -163,4 +164,15 @@ public InfluxDBClient getInfluxDBClient() { public String getUrl() { return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); } + + private void setWaitStrategy() { + this.waitStrategy = (new WaitAllStrategy()) + .withStrategy( + Wait + .forHttp("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE)) + .withStrategy(Wait.forListeningPort()); + this.addExposedPort(INFLUXDB_PORT); + } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 0c52be9b19d..b8518232f97 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -1,18 +1,18 @@ package org.testcontainers.containers; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - import org.influxdb.InfluxDB; import org.junit.ClassRule; import org.junit.Test; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + public class InfluxDBContainerV1Test { @ClassRule - public static InfluxDBContainer influxDBContainer = - new InfluxDBContainer<>(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE); + public static InfluxDBContainer influxDBContainer = + new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE); @Test public void getUrl() { diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java index c5af2e830a3..118ecba1408 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java @@ -1,11 +1,5 @@ package org.testcontainers.containers; -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - import java.util.concurrent.TimeUnit; import org.influxdb.InfluxDB; import org.influxdb.dto.Point; @@ -14,6 +8,12 @@ import org.junit.Rule; import org.junit.Test; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + public class InfluxDBContainerV1WithUserTest { private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE.getVersionPart(); @@ -22,7 +22,7 @@ public class InfluxDBContainerV1WithUserTest { private static final String PASSWORD = "test-password"; @Rule - public InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE) + public InfluxDBContainerV1 influxDBContainer = new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java index a82b2de5267..7b69289bc25 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java @@ -1,9 +1,5 @@ package org.testcontainers.containers; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - import com.influxdb.client.InfluxDBClient; import com.influxdb.client.domain.HealthCheck.StatusEnum; import org.junit.After; @@ -11,13 +7,17 @@ import org.junit.ClassRule; import org.junit.Test; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + public class InfluxDBContainerV2Test { private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE.getVersionPart(); @ClassRule - public static final InfluxDBContainerV2 influxDBContainerV2 = - new InfluxDBContainerV2<>(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE); + public static final InfluxDBContainerV2 influxDBContainerV2 = + new InfluxDBContainerV2(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE); private InfluxDBClient client = null; diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java index 563efb36a52..0b10ccf7f4f 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java @@ -1,9 +1,5 @@ package org.testcontainers.containers; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - import com.influxdb.client.InfluxDBClient; import com.influxdb.client.QueryApi; import com.influxdb.client.WriteApi; @@ -19,6 +15,10 @@ import org.junit.ClassRule; import org.junit.Test; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + public class InfluxDBContainerV2WithUserTest { private static final String USERNAME = "new-test-user"; @@ -32,8 +32,8 @@ public class InfluxDBContainerV2WithUserTest { private InfluxDBClient client = null; @ClassRule - public static final InfluxDBContainerV2 influxDBContainerV2 = - new InfluxDBContainerV2<>(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE) + public static final InfluxDBContainerV2 influxDBContainerV2 = + new InfluxDBContainerV2(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) From 104f88f0aaf803e53dd29ca4c716cef885400941 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 17:53:24 +0200 Subject: [PATCH 21/51] remove client code --- modules/influxdb/build.gradle | 1 - .../containers/InfluxDBContainerV2.java | 26 ++++--------------- .../containers/InfluxDBContainerV2Test.java | 4 +-- .../InfluxDBContainerV2WithUserTest.java | 2 +- .../containers/InfluxDBV2TestHelper.java | 21 +++++++++++++++ 5 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 3b6868f7ebe..4f641331aa3 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -6,6 +6,5 @@ dependencies { implementation "org.influxdb:influxdb-java:2.21" testImplementation "org.influxdb:influxdb-java:2.21" - implementation "com.influxdb:influxdb-client-java:3.1.0" testImplementation "com.influxdb:influxdb-client-java:3.1.0" } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index 89761bc99fd..9c439a9c442 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -1,9 +1,7 @@ package org.testcontainers.containers; -import com.influxdb.client.InfluxDBClient; -import com.influxdb.client.InfluxDBClientFactory; -import com.influxdb.client.InfluxDBClientOptions; import java.util.Optional; +import lombok.Getter; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitAllStrategy; import org.testcontainers.utility.DockerImageName; @@ -20,9 +18,13 @@ public class InfluxDBContainerV2 extends GenericContainer { private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); private static final int NO_CONTENT_STATUS_CODE = 204; + @Getter private String username = "test-user"; + @Getter private String password = "test-password"; + @Getter private String bucket = "test-bucket"; + @Getter private String organization = "test-org"; private Optional retention = Optional.empty(); private Optional adminToken = Optional.empty(); @@ -140,24 +142,6 @@ public InfluxDBContainerV2 withAdminToken(final String adminToken) { return this; } - - /** - * This method uses the InfluxDBClientOptions builder to build a InfluxDB client - * - * @return a InfluxDB client - * @see InfluxDBClientOptions - * @see InfluxDBClient - */ - public InfluxDBClient getInfluxDBClient() { - final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() - .url(this.getUrl()) - .authenticate(this.username, this.password.toCharArray()) - .bucket(this.bucket) - .org(this.organization) - .build(); - return InfluxDBClientFactory.create(influxDBClientOptions); - } - /** * @return a url to influxDb */ diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java index 7b69289bc25..2d8c066c2bb 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java @@ -23,7 +23,7 @@ public class InfluxDBContainerV2Test { @Before public void setUp() { - this.client = influxDBContainerV2.getInfluxDBClient(); + this.client = InfluxDBV2TestHelper.getInfluxDBClient(influxDBContainerV2); } @After @@ -40,7 +40,7 @@ public void getUrl() { @Test public void getNewInfluxDB() { - final InfluxDBClient actual = influxDBContainerV2.getInfluxDBClient(); + final InfluxDBClient actual = InfluxDBV2TestHelper.getInfluxDBClient(influxDBContainerV2); assertThat(actual, notNullValue()); assertThat(actual.health().getStatus(), is(StatusEnum.PASS)); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java index 0b10ccf7f4f..7cc94aef475 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java @@ -43,7 +43,7 @@ public class InfluxDBContainerV2WithUserTest { @Before public void setUp() { - this.client = influxDBContainerV2.getInfluxDBClient(); + this.client = InfluxDBV2TestHelper.getInfluxDBClient(influxDBContainerV2); } @After diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java new file mode 100644 index 00000000000..267619eb1fb --- /dev/null +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java @@ -0,0 +1,21 @@ +package org.testcontainers.containers; + +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientFactory; +import com.influxdb.client.InfluxDBClientOptions; + +public final class InfluxDBV2TestHelper { + + private InfluxDBV2TestHelper() { + } + + public static InfluxDBClient getInfluxDBClient(final InfluxDBContainerV2 influxDBContainerV2) { + final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() + .url(influxDBContainerV2.getUrl()) + .authenticate(influxDBContainerV2.getUsername(), influxDBContainerV2.getPassword().toCharArray()) + .bucket(influxDBContainerV2.getBucket()) + .org(influxDBContainerV2.getOrganization()) + .build(); + return InfluxDBClientFactory.create(influxDBClientOptions); + } +} From 9526fb30ce7e994ed226558b1d24fb7611308a41 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 17:55:46 +0200 Subject: [PATCH 22/51] add getter to properties --- .../java/org/testcontainers/containers/InfluxDBContainerV2.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index 9c439a9c442..73ae13eb754 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -26,7 +26,9 @@ public class InfluxDBContainerV2 extends GenericContainer { private String bucket = "test-bucket"; @Getter private String organization = "test-org"; + @Getter private Optional retention = Optional.empty(); + @Getter private Optional adminToken = Optional.empty(); From b1a84a192f652a722a2ff0233cab0caf18a5da69 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 18:02:12 +0200 Subject: [PATCH 23/51] Update modules/influxdb/build.gradle Co-authored-by: Sergei Egorov --- modules/influxdb/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 4f641331aa3..d214a5f60f0 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -3,8 +3,8 @@ description = "Testcontainers :: InfluxDB" dependencies { api project(':testcontainers') - implementation "org.influxdb:influxdb-java:2.21" - testImplementation "org.influxdb:influxdb-java:2.21" + compileOnly 'org.influxdb:influxdb-java:2.21' + testImplementation 'org.influxdb:influxdb-java:2.21' testImplementation "com.influxdb:influxdb-client-java:3.1.0" } From 08af8254b37913d988a92b1b67648189b2873034 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 18:02:35 +0200 Subject: [PATCH 24/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java Co-authored-by: Sergei Egorov --- .../java/org/testcontainers/containers/InfluxDBContainer.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index ec1652f8769..bf11741d378 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -3,8 +3,7 @@ import org.testcontainers.utility.DockerImageName; /** - * @deprecated instead use {@link InfluxDBContainerV1#InfluxDBContainerV1(DockerImageName) } for InfluxDB 1.x or {@link - * InfluxDBContainerV2#InfluxDBContainerV2(DockerImageName)} for InfluxDB 2.x instead + * @deprecated instead use {@link InfluxDBContainerV1} for InfluxDB 1.x or {@link InfluxDBContainerV2} for InfluxDB 2.x instead */ @Deprecated public class InfluxDBContainer extends InfluxDBContainerV1 { From 7d5d03d4e00aed81856bfafda1733447bccf0274 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 18:03:21 +0200 Subject: [PATCH 25/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java Co-authored-by: Sergei Egorov --- .../org/testcontainers/containers/InfluxDBContainerV2.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index 73ae13eb754..719a27f8456 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -33,10 +33,7 @@ public class InfluxDBContainerV2 extends GenericContainer { public InfluxDBContainerV2(final String imageName) { - super(DockerImageName.parse(imageName)); - final DockerImageName dockerImageName = DockerImageName.parse(imageName); - dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - this.setWaitStrategy(); + this(DockerImageName.parse(imageName)); } From 548713b925e6f4f9d22a5835ce87517a8651cfe1 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 18:09:40 +0200 Subject: [PATCH 26/51] add suggestions --- .../containers/InfluxDBContainer.java | 15 +++++++++-- .../containers/InfluxDBContainerV1.java | 21 +++------------- .../containers/InfluxDBContainerV2.java | 25 ++++++++----------- .../InfluxDBContainerV1WithUserTest.java | 3 ++- .../InfluxDBContainerV2WithUserTest.java | 5 ++-- 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index bf11741d378..742ba027d3b 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -7,12 +7,23 @@ */ @Deprecated public class InfluxDBContainer extends InfluxDBContainerV1 { + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); + private static final String DEFAULT_TAG = "1.4.3"; + + /** + * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + */ + @Deprecated public InfluxDBContainer() { - super(); + this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); } + /** + * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + */ + @Deprecated public InfluxDBContainer(final String version) { - super(version); + this(DEFAULT_IMAGE_NAME.withTag(version)); } public InfluxDBContainer(final DockerImageName influxdbTestImage) { diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index e97b4a76be6..41970d53860 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -1,13 +1,14 @@ package org.testcontainers.containers; -import java.util.Collections; -import java.util.Set; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitAllStrategy; import org.testcontainers.utility.DockerImageName; +import java.util.Collections; +import java.util.Set; + /** * See https://store.docker.com/images/influxdb */ @@ -31,22 +32,6 @@ public class InfluxDBContainerV1 extends GenericContainer { private String username = "any"; private String password = "any"; - /** - * @deprecated use {@link InfluxDBContainerV1(DockerImageName)} instead - */ - @Deprecated - public InfluxDBContainerV1() { - this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); - } - - /** - * @deprecated use {@link InfluxDBContainerV1(DockerImageName)} instead - */ - @Deprecated - public InfluxDBContainerV1(final String version) { - this(DEFAULT_IMAGE_NAME.withTag(version)); - } - public InfluxDBContainerV1(final DockerImageName dockerImageName) { super(dockerImageName); diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index 719a27f8456..f9d639361ba 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -1,11 +1,12 @@ package org.testcontainers.containers; -import java.util.Optional; import lombok.Getter; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitAllStrategy; import org.testcontainers.utility.DockerImageName; +import java.util.Optional; + /** * Refer to * the official InfluxDB 2.x container repository @@ -40,7 +41,15 @@ public InfluxDBContainerV2(final String imageName) { public InfluxDBContainerV2(final DockerImageName imageName) { super(imageName); imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - this.setWaitStrategy(); + this.waitStrategy = new WaitAllStrategy() + .withStrategy( + Wait + .forHttp("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE) + ) + .withStrategy(Wait.forListeningPort()); + this.addExposedPort(INFLUXDB_PORT); } /** @@ -147,16 +156,4 @@ public InfluxDBContainerV2 withAdminToken(final String adminToken) { public String getUrl() { return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); } - - private void setWaitStrategy() { - this.waitStrategy = new WaitAllStrategy() - .withStrategy( - Wait - .forHttp("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE) - ) - .withStrategy(Wait.forListeningPort()); - this.addExposedPort(INFLUXDB_PORT); - } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java index 118ecba1408..daf87f6a502 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java @@ -1,6 +1,5 @@ package org.testcontainers.containers; -import java.util.concurrent.TimeUnit; import org.influxdb.InfluxDB; import org.influxdb.dto.Point; import org.influxdb.dto.Query; @@ -8,6 +7,8 @@ import org.junit.Rule; import org.junit.Test; +import java.util.concurrent.TimeUnit; + import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java index 7cc94aef475..80465fa2e46 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java @@ -8,13 +8,14 @@ import com.influxdb.client.write.Point; import com.influxdb.query.FluxRecord; import com.influxdb.query.FluxTable; -import java.time.Instant; -import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; +import java.time.Instant; +import java.util.List; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; From 53414cac04a8ecc5846e3ef37cb7e2c6b3aaec10 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 18:14:42 +0200 Subject: [PATCH 27/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java Co-authored-by: Sergei Egorov --- .../java/org/testcontainers/containers/InfluxDBContainer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 742ba027d3b..c9423e6311a 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -6,7 +6,7 @@ * @deprecated instead use {@link InfluxDBContainerV1} for InfluxDB 1.x or {@link InfluxDBContainerV2} for InfluxDB 2.x instead */ @Deprecated -public class InfluxDBContainer extends InfluxDBContainerV1 { +public class InfluxDBContainer> extends InfluxDBContainerV1 { private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); private static final String DEFAULT_TAG = "1.4.3"; From 22090c782b40792d3643b9731c4ac6e1ad745c59 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 18:15:15 +0200 Subject: [PATCH 28/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java Co-authored-by: Sergei Egorov --- .../java/org/testcontainers/containers/InfluxDBContainerV2.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java index f9d639361ba..1d417ec7252 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java @@ -14,7 +14,7 @@ */ public class InfluxDBContainerV2 extends GenericContainer { - public static final Integer INFLUXDB_PORT = 8086; + private static final Integer INFLUXDB_PORT = 8086; private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); private static final int NO_CONTENT_STATUS_CODE = 204; From afa957fe1a5a548ed3cd1ec485aabdfe7b96253c Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 18:15:30 +0200 Subject: [PATCH 29/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java Co-authored-by: Sergei Egorov --- .../java/org/testcontainers/containers/InfluxDBContainerV1.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index 41970d53860..363ff387045 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -14,7 +14,7 @@ */ public class InfluxDBContainerV1 extends GenericContainer { - public static final Integer INFLUXDB_PORT = 8086; + private static final Integer INFLUXDB_PORT = 8086; private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); private static final String DEFAULT_TAG = "1.4.3"; From 2c03ddaea7879545b83115bf4cc7b65de74a6a4d Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 5 Aug 2021 18:15:44 +0200 Subject: [PATCH 30/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java Co-authored-by: Sergei Egorov --- .../org/testcontainers/containers/InfluxDBContainerV1.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index 363ff387045..1c55e730a5c 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -21,8 +21,7 @@ public class InfluxDBContainerV1 extends GenericContainer { private static final int NO_CONTENT_STATUS_CODE = 204; - @Deprecated - public static final String VERSION = DEFAULT_TAG; + static final String VERSION = DEFAULT_TAG; private boolean authEnabled = true; private String admin = "admin"; From 207de33a3e9992e0666340d1a19b844d0f8ae082 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Fri, 6 Aug 2021 17:08:48 +0200 Subject: [PATCH 31/51] fix gradle check --- .../containers/InfluxDBContainer.java | 77 ++++++++++++++++++- .../containers/InfluxDBContainerV1.java | 47 +++++------ .../InfluxDBContainerV1WithUserTest.java | 2 +- 3 files changed, 101 insertions(+), 25 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index c9423e6311a..d97cbeed99b 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -6,7 +6,7 @@ * @deprecated instead use {@link InfluxDBContainerV1} for InfluxDB 1.x or {@link InfluxDBContainerV2} for InfluxDB 2.x instead */ @Deprecated -public class InfluxDBContainer> extends InfluxDBContainerV1 { +public class InfluxDBContainer> extends InfluxDBContainerV1 { private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); private static final String DEFAULT_TAG = "1.4.3"; @@ -29,4 +29,79 @@ public InfluxDBContainer(final String version) { public InfluxDBContainer(final DockerImageName influxdbTestImage) { super(influxdbTestImage); } + + /** + * Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`. + * + * @param authEnabled Enables authentication. + * @return a reference to this container instance + */ + @Override + public SELF withAuthEnabled(final boolean authEnabled) { + this.authEnabled = authEnabled; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_ADMIN_USER`. + * + * @param admin The name of the admin user to be created. If this is unset, no admin user is created. + * @return a reference to this container instance + */ + @Override + public SELF withAdmin(final String admin) { + this.admin = admin; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_ADMIN_PASSWORD`. + * + * @param adminPassword TThe password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a + * random password is generated and printed to standard out. + * @return a reference to this container instance + */ + @Override + public SELF withAdminPassword(final String adminPassword) { + this.adminPassword = adminPassword; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_DB`. + * + * @param database Automatically initializes a database with the name of this environment variable. + * @return a reference to this container instance + */ + @Override + public SELF withDatabase(final String database) { + this.database = database; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_USER`. + * + * @param username The name of a user to be created with no privileges. If `INFLUXDB_DB` is set, this user will be + * granted read and write permissions for that database. + * @return a reference to this container instance + */ + @Override + public SELF withUsername(final String username) { + this.username = username; + return this.self(); + } + + /** + * Set env variable `INFLUXDB_USER_PASSWORD`. + * + * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is + * generated and printed to standard out. + * @return a reference to this container instance + */ + @Override + public SELF withPassword(final String password) { + this.password = password; + return this.self(); + } } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index 1c55e730a5c..6159e9d2293 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -12,26 +12,27 @@ /** * See https://store.docker.com/images/influxdb */ -public class InfluxDBContainerV1 extends GenericContainer { +class InfluxDBContainerV1> extends GenericContainer { - private static final Integer INFLUXDB_PORT = 8086; + public static final Integer INFLUXDB_PORT = 8086; private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); private static final String DEFAULT_TAG = "1.4.3"; private static final int NO_CONTENT_STATUS_CODE = 204; - static final String VERSION = DEFAULT_TAG; + @Deprecated + public static final String VERSION = DEFAULT_TAG; - private boolean authEnabled = true; - private String admin = "admin"; - private String adminPassword = "password"; + protected boolean authEnabled = true; + protected String admin = "admin"; + protected String adminPassword = "password"; - private String database; - private String username = "any"; - private String password = "any"; + protected String database; + protected String username = "any"; + protected String password = "any"; - public InfluxDBContainerV1(final DockerImageName dockerImageName) { + InfluxDBContainerV1(final DockerImageName dockerImageName) { super(dockerImageName); dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); @@ -61,15 +62,16 @@ public Set getLivenessCheckPortNumbers() { return Collections.singleton(this.getMappedPort(INFLUXDB_PORT)); } + /** * Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`. * * @param authEnabled Enables authentication. * @return a reference to this container instance */ - public InfluxDBContainerV1 withAuthEnabled(final boolean authEnabled) { + public SELF withAuthEnabled(final boolean authEnabled) { this.authEnabled = authEnabled; - return this; + return this.self(); } /** @@ -78,9 +80,9 @@ public InfluxDBContainerV1 withAuthEnabled(final boolean authEnabled) { * @param admin The name of the admin user to be created. If this is unset, no admin user is created. * @return a reference to this container instance */ - public InfluxDBContainerV1 withAdmin(final String admin) { + public SELF withAdmin(final String admin) { this.admin = admin; - return this; + return this.self(); } /** @@ -90,9 +92,9 @@ public InfluxDBContainerV1 withAdmin(final String admin) { * random password is generated and printed to standard out. * @return a reference to this container instance */ - public InfluxDBContainerV1 withAdminPassword(final String adminPassword) { + public SELF withAdminPassword(final String adminPassword) { this.adminPassword = adminPassword; - return this; + return this.self(); } /** @@ -101,9 +103,9 @@ public InfluxDBContainerV1 withAdminPassword(final String adminPassword) { * @param database Automatically initializes a database with the name of this environment variable. * @return a reference to this container instance */ - public InfluxDBContainerV1 withDatabase(final String database) { + public SELF withDatabase(final String database) { this.database = database; - return this; + return this.self(); } /** @@ -113,9 +115,9 @@ public InfluxDBContainerV1 withDatabase(final String database) { * granted read and write permissions for that database. * @return a reference to this container instance */ - public InfluxDBContainerV1 withUsername(final String username) { + public SELF withUsername(final String username) { this.username = username; - return this; + return this.self(); } /** @@ -125,12 +127,11 @@ public InfluxDBContainerV1 withUsername(final String username) { * generated and printed to standard out. * @return a reference to this container instance */ - public InfluxDBContainerV1 withPassword(final String password) { + public SELF withPassword(final String password) { this.password = password; - return this; + return this.self(); } - /** * @return a url to influxDb */ diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java index daf87f6a502..3f9fc967a10 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java @@ -23,7 +23,7 @@ public class InfluxDBContainerV1WithUserTest { private static final String PASSWORD = "test-password"; @Rule - public InfluxDBContainerV1 influxDBContainer = new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE) + public InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD); From 5250407eb0b2f163aaf6a30bc0c7a897ce2947c4 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Fri, 6 Aug 2021 17:18:28 +0200 Subject: [PATCH 32/51] override methods --- .../containers/InfluxDBContainer.java | 18 ++++++------------ .../containers/InfluxDBContainerV1.java | 12 ++++++------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index d97cbeed99b..d93bb0e2af4 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -38,8 +38,7 @@ public InfluxDBContainer(final DockerImageName influxdbTestImage) { */ @Override public SELF withAuthEnabled(final boolean authEnabled) { - this.authEnabled = authEnabled; - return this.self(); + return (SELF) this; } /** @@ -50,8 +49,7 @@ public SELF withAuthEnabled(final boolean authEnabled) { */ @Override public SELF withAdmin(final String admin) { - this.admin = admin; - return this.self(); + return (SELF) this; } /** @@ -63,8 +61,7 @@ public SELF withAdmin(final String admin) { */ @Override public SELF withAdminPassword(final String adminPassword) { - this.adminPassword = adminPassword; - return this.self(); + return (SELF) this; } /** @@ -75,8 +72,7 @@ public SELF withAdminPassword(final String adminPassword) { */ @Override public SELF withDatabase(final String database) { - this.database = database; - return this.self(); + return (SELF) this; } /** @@ -88,8 +84,7 @@ public SELF withDatabase(final String database) { */ @Override public SELF withUsername(final String username) { - this.username = username; - return this.self(); + return (SELF) this; } /** @@ -101,7 +96,6 @@ public SELF withUsername(final String username) { */ @Override public SELF withPassword(final String password) { - this.password = password; - return this.self(); + return (SELF) this; } } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java index 6159e9d2293..936a3016506 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java @@ -24,13 +24,13 @@ class InfluxDBContainerV1> extends Generi @Deprecated public static final String VERSION = DEFAULT_TAG; - protected boolean authEnabled = true; - protected String admin = "admin"; - protected String adminPassword = "password"; + private boolean authEnabled = true; + private String admin = "admin"; + private String adminPassword = "password"; - protected String database; - protected String username = "any"; - protected String password = "any"; + private String database; + private String username = "any"; + private String password = "any"; InfluxDBContainerV1(final DockerImageName dockerImageName) { super(dockerImageName); From bba98d80ac5abe6b05b5cffd8ecb94bc822d44e6 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Sat, 30 Jul 2022 11:38:31 +0200 Subject: [PATCH 33/51] Move InfluxDB code into one class --- docs/modules/databases/influxdb.md | 12 +- modules/influxdb/build.gradle | 4 +- .../containers/InfluxDBContainer.java | 254 +++++++++++++----- .../containers/InfluxDBContainerV1.java | 150 ----------- .../containers/InfluxDBContainerV2.java | 159 ----------- ...V2Test.java => InfluxDBContainerTest.java} | 14 +- .../containers/InfluxDBContainerV1Test.java | 4 +- .../InfluxDBContainerV1WithUserTest.java | 2 +- ...ava => InfluxDBContainerWithUserTest.java} | 8 +- .../containers/InfluxDBV2TestHelper.java | 10 +- 10 files changed, 211 insertions(+), 406 deletions(-) delete mode 100644 modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java delete mode 100644 modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java rename modules/influxdb/src/test/java/org/testcontainers/containers/{InfluxDBContainerV2Test.java => InfluxDBContainerTest.java} (76%) rename modules/influxdb/src/test/java/org/testcontainers/containers/{InfluxDBContainerV2WithUserTest.java => InfluxDBContainerWithUserTest.java} (90%) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index a3dcd54e827..fc2ebfa11f8 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -4,7 +4,7 @@ Testcontainers module for InfluxData [InfluxDB](https://www.influxdata.com/produ ## Important Note -They are breaking changes in InfluxDB v2.x. For more information refer to the +They are breaking changes in InfluxDB 2.x. For more information refer to the main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). InfluxDB official [container registry](https://hub.docker.com/_/influxdb) on docker hub. @@ -13,7 +13,7 @@ official [container registry](https://hub.docker.com/_/influxdb) on docker hub. Running influxDbContainer as a stand-in for InfluxDB in a test: -[InfluxDBContainerV2Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java) +[InfluxDBContainerTest](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) @@ -35,15 +35,15 @@ official [docs](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docke It is possible to override the default values, take a look at these tests: -[InfluxDBContainerV2WithUserTest](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java) +[InfluxDBContainerWithUserTest](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java) -**NOTE**: You can find the latest documentation about the influxdb v2.x java +**NOTE**: You can find the latest documentation about the InfluxDB 2.x java client [here](https://github.com/influxdata/influxdb-client-java). -## InfluxDB V1.x Usage example (Deprecated) +## InfluxDB 1.x Usage example (Deprecated) -Running influxDbContainer as a stand-in for InfluxDB in a test: +Running InfluxDb Container as a stand-in for InfluxDB in a test: [InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 5608075e05b..7d53ebad3d2 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -3,8 +3,8 @@ description = "Testcontainers :: InfluxDB" dependencies { api project(':testcontainers') - compileOnly 'org.influxdb:influxdb-java:2.22' - testImplementation 'org.influxdb:influxdb-java:2.21' + compileOnly 'org.influxdb:influxdb-java:2.23' + testImplementation 'org.influxdb:influxdb-java:2.23' testImplementation "com.influxdb:influxdb-client-java:3.1.0" } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 7dde6a403c6..7fb7b982706 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -1,76 +1,163 @@ package org.testcontainers.containers; +import lombok.Getter; +import org.influxdb.InfluxDB; +import org.influxdb.InfluxDBFactory; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitAllStrategy; +import org.testcontainers.utility.ComparableVersion; import org.testcontainers.utility.DockerImageName; +import java.util.Optional; + /** - * @deprecated instead use {@link InfluxDBContainerV1} for InfluxDB 1.x or {@link InfluxDBContainerV2} for InfluxDB 2.x instead + * Represents an InfluxDB Docker instance. + * Refer to + * the official InfluxDB 1.x and 2.x container repository + * on docker hub for detailed documentation and newest tags. */ -@Deprecated -public class InfluxDBContainer> extends InfluxDBContainerV1 { +public class InfluxDBContainer extends GenericContainer { + + private static final Integer INFLUXDB_PORT = 8086; private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); + private static final int NO_CONTENT_STATUS_CODE = 204; - private static final String DEFAULT_TAG = "1.4.3"; - @Deprecated - public static final String VERSION = DEFAULT_TAG; + @Getter + private String username = "any"; + @Getter + private String password = "any"; + /** + * Properties InfluxDB <= 2 + */ private boolean authEnabled = true; - private String admin = "admin"; - private String adminPassword = "password"; - private String database; - private String username = "any"; - - private String password = "any"; - /** - * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + * InfluxDB >= 2 */ - @Deprecated - public InfluxDBContainer() { - this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); - } + @Getter + private String bucket = "test-bucket"; + @Getter + private String organization = "test-org"; + @Getter + private Optional retention = Optional.empty(); + @Getter + private Optional adminToken = Optional.empty(); - /** - * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead - */ - @Deprecated - public InfluxDBContainer(final String version) { - this(DEFAULT_IMAGE_NAME.withTag(version)); + private final boolean isAtLeastMajorVersion2; + + + public InfluxDBContainer(final String dockerImageName) { + this(DockerImageName.parse(dockerImageName)); } public InfluxDBContainer(final DockerImageName dockerImageName) { super(dockerImageName); dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - waitStrategy = - new WaitAllStrategy() - .withStrategy(Wait.forHttp("/ping").withBasicCredentials(username, password).forStatusCode(204)) - .withStrategy(Wait.forListeningPort()); + logger().info("Starting an InfluxDB container using [{}]", dockerImageName); + this.waitStrategy = new WaitAllStrategy() + .withStrategy( + Wait + .forHttp("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE) + ) + .withStrategy(Wait.forListeningPort()); - addExposedPort(INFLUXDB_PORT); + this.isAtLeastMajorVersion2 = + new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("2.0.0"); + this.addExposedPort(INFLUXDB_PORT); } + /** + * Sets the InfluxDB environment variables based on the version + */ @Override protected void configure() { - addEnv("INFLUXDB_ADMIN_USER", admin); - addEnv("INFLUXDB_ADMIN_PASSWORD", adminPassword); + if (isAtLeastMajorVersion2) { + setInfluxDBV2Envs(); + } else { + setInfluxDBV1Envs(); + } + } - addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(authEnabled)); + /** + *

+ * The InfluxDB 2.x image contains some extra functionality to automatically bootstrap the system. This functionality is + * enabled by setting the DOCKER_INFLUXDB_INIT_MODE environment variable to the value "setup" when running the + * container. Additional environment variables are used to configure the setup logic: + *

    + *
  • DOCKER_INFLUXDB_INIT_USERNAME: The username to set for the system's initial super-user (Required). + *
  • DOCKER_INFLUXDB_INIT_PASSWORD: The password to set for the system's initial super-user (Required). + *
  • DOCKER_INFLUXDB_INIT_ORG: The name to set for the system's initial organization (Required). + *
  • DOCKER_INFLUXDB_INIT_BUCKET: The name to set for the system's initial bucket (Required). + *
  • DOCKER_INFLUXDB_INIT_RETENTION: The duration the system's initial bucket should retain data. If not set, + * the initial bucket will retain data forever. + *
  • DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: The authentication token to associate with the system's initial + * super-user. If not set, a token will be auto-generated by the system. + *
+ *
+ * @see full documentation + */ + private void setInfluxDBV2Envs() { + this.addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); - addEnv("INFLUXDB_DB", database); - addEnv("INFLUXDB_USER", username); - addEnv("INFLUXDB_USER_PASSWORD", password); + this.addEnv("DOCKER_INFLUXDB_INIT_USERNAME", this.username); + this.addEnv("DOCKER_INFLUXDB_INIT_PASSWORD", this.password); + + this.addEnv("DOCKER_INFLUXDB_INIT_ORG", this.organization); + this.addEnv("DOCKER_INFLUXDB_INIT_BUCKET", this.bucket); + + this.retention.ifPresent(ret -> this.addEnv("DOCKER_INFLUXDB_INIT_RETENTION", ret)); + this.adminToken.ifPresent(token -> this.addEnv("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", token)); } - @Override - public Set getLivenessCheckPortNumbers() { - return Collections.singleton(getMappedPort(INFLUXDB_PORT)); - public InfluxDBContainer(final DockerImageName influxdbTestImage) { - super(influxdbTestImage); + /** + * Sets the InfluxDB 1.x environment variables + */ + private void setInfluxDBV1Envs() { + this.addEnv("INFLUXDB_USER", this.username); + this.addEnv("INFLUXDB_USER_PASSWORD", this.password); + + this.addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled)); + + this.addEnv("INFLUXDB_ADMIN_USER", this.admin); + this.addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword); + + this.addEnv("INFLUXDB_DB", this.database); + } + + /** + * Set env variable `INFLUXDB_USER` for InfluxDB <= 2.
+ * + * Set env variable `DOCKER_INFLUXDB_INIT_USERNAME` for InfluxDB >= 2. + * + * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will + * be granted read and write permissions for that database. + * @return a reference to this container instance + */ + public InfluxDBContainer withUsername(final String username) { + this.username = username; + return this; + } + + /** + * Set env variable `INFLUXDB_USER_PASSWORD` for InfluxDB <= 1.
+ * + * Set env variable `DOCKER_INFLUXDB_INIT_PASSWORD` for InfluxDB >= 2. + * + * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is + * generated and printed to standard out. + * @return a reference to this container instance + */ + public InfluxDBContainer withPassword(final String password) { + this.password = password; + return this; } /** @@ -79,9 +166,9 @@ public InfluxDBContainer(final DockerImageName influxdbTestImage) { * @param authEnabled Enables authentication. * @return a reference to this container instance */ - @Override - public SELF withAuthEnabled(final boolean authEnabled) { - return (SELF) this; + public InfluxDBContainer withAuthEnabled(final boolean authEnabled) { + this.authEnabled = authEnabled; + return this.self(); } /** @@ -90,21 +177,21 @@ public SELF withAuthEnabled(final boolean authEnabled) { * @param admin The name of the admin user to be created. If this is unset, no admin user is created. * @return a reference to this container instance */ - @Override - public SELF withAdmin(final String admin) { - return (SELF) this; + public InfluxDBContainer withAdmin(final String admin) { + this.admin = admin; + return this.self(); } /** * Set env variable `INFLUXDB_ADMIN_PASSWORD`. * - * @param adminPassword The password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a - * random password is generated and printed to standard out. + * @param adminPassword TThe password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a + * random password is generated and printed to standard out. * @return a reference to this container instance */ - @Override - public SELF withAdminPassword(final String adminPassword) { - return (SELF) this; + public InfluxDBContainer withAdminPassword(final String adminPassword) { + this.adminPassword = adminPassword; + return this.self(); } /** @@ -113,41 +200,68 @@ public SELF withAdminPassword(final String adminPassword) { * @param database Automatically initializes a database with the name of this environment variable. * @return a reference to this container instance */ - @Override - public SELF withDatabase(final String database) { - return (SELF) this; + public InfluxDBContainer withDatabase(final String database) { + this.database = database; + return this.self(); } /** - * Set env variable `INFLUXDB_USER`. + * Set env variable `DOCKER_INFLUXDB_INIT_ORG`. * - * @param username The name of a user to be created with no privileges. If `INFLUXDB_DB` is set, this user will be - * granted read and write permissions for that database. + * @param organization The organization for the initial setup of influxDB. * @return a reference to this container instance */ - @Override - public SELF withUsername(final String username) { - return (SELF) this; + public InfluxDBContainer withOrganization(final String organization) { + this.organization = organization; + return this; } /** - * Set env variable `INFLUXDB_USER_PASSWORD`. + * Set env variable `DOCKER_INFLUXDB_INIT_BUCKET`. * - * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is - * generated and printed to standard out. + * @param bucket Automatically initializes a bucket with the name of this environment variable. * @return a reference to this container instance */ - @Override - public SELF withPassword(final String password) { - return (SELF) this; - this.password = password; - return self(); + public InfluxDBContainer withBucket(final String bucket) { + this.bucket = bucket; + return this; } /** - * @return a url to influxDb + * Set env variable `DOCKER_INFLUXDB_INIT_RETENTION`. + * + * @param retention Duration bucket will retain data (0 is infinite, default is 0). + * @return a reference to this container instance + */ + public InfluxDBContainer withRetention(final String retention) { + this.retention = Optional.of(retention); + return this; + } + + /** + * Set env variable `DOCKER_INFLUXDB_INIT_ADMIN_TOKEN`. + * + * @param adminToken Authentication token to associate with the admin user. + * @return a reference to this container instance + */ + public InfluxDBContainer withAdminToken(final String adminToken) { + this.adminToken = Optional.of(adminToken); + return this; + } + + /** + * @return a url to InfluxDB */ public String getUrl() { - return "http://" + getHost() + ":" + getLivenessCheckPort(); + return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); + } + + /** + * @return a InfluxDB client for InfluxDB <= 2. + */ + public InfluxDB getNewInfluxDB() { + final InfluxDB influxDB = InfluxDBFactory.connect(this.getUrl(), this.username, this.password); + influxDB.setDatabase(this.database); + return influxDB; } } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java deleted file mode 100644 index 936a3016506..00000000000 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV1.java +++ /dev/null @@ -1,150 +0,0 @@ -package org.testcontainers.containers; - -import org.influxdb.InfluxDB; -import org.influxdb.InfluxDBFactory; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.containers.wait.strategy.WaitAllStrategy; -import org.testcontainers.utility.DockerImageName; - -import java.util.Collections; -import java.util.Set; - -/** - * See https://store.docker.com/images/influxdb - */ -class InfluxDBContainerV1> extends GenericContainer { - - public static final Integer INFLUXDB_PORT = 8086; - - private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); - private static final String DEFAULT_TAG = "1.4.3"; - - private static final int NO_CONTENT_STATUS_CODE = 204; - - @Deprecated - public static final String VERSION = DEFAULT_TAG; - - private boolean authEnabled = true; - private String admin = "admin"; - private String adminPassword = "password"; - - private String database; - private String username = "any"; - private String password = "any"; - - InfluxDBContainerV1(final DockerImageName dockerImageName) { - super(dockerImageName); - - dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - - this.waitStrategy = new WaitAllStrategy() - .withStrategy(Wait.forHttp("/ping").withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE)) - .withStrategy(Wait.forListeningPort()); - - this.addExposedPort(INFLUXDB_PORT); - } - - @Override - protected void configure() { - this.addEnv("INFLUXDB_ADMIN_USER", this.admin); - this.addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword); - - this.addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled)); - - this.addEnv("INFLUXDB_DB", this.database); - this.addEnv("INFLUXDB_USER", this.username); - this.addEnv("INFLUXDB_USER_PASSWORD", this.password); - } - - @Override - public Set getLivenessCheckPortNumbers() { - return Collections.singleton(this.getMappedPort(INFLUXDB_PORT)); - } - - - /** - * Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`. - * - * @param authEnabled Enables authentication. - * @return a reference to this container instance - */ - public SELF withAuthEnabled(final boolean authEnabled) { - this.authEnabled = authEnabled; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_ADMIN_USER`. - * - * @param admin The name of the admin user to be created. If this is unset, no admin user is created. - * @return a reference to this container instance - */ - public SELF withAdmin(final String admin) { - this.admin = admin; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_ADMIN_PASSWORD`. - * - * @param adminPassword TThe password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a - * random password is generated and printed to standard out. - * @return a reference to this container instance - */ - public SELF withAdminPassword(final String adminPassword) { - this.adminPassword = adminPassword; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_DB`. - * - * @param database Automatically initializes a database with the name of this environment variable. - * @return a reference to this container instance - */ - public SELF withDatabase(final String database) { - this.database = database; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_USER`. - * - * @param username The name of a user to be created with no privileges. If `INFLUXDB_DB` is set, this user will be - * granted read and write permissions for that database. - * @return a reference to this container instance - */ - public SELF withUsername(final String username) { - this.username = username; - return this.self(); - } - - /** - * Set env variable `INFLUXDB_USER_PASSWORD`. - * - * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is - * generated and printed to standard out. - * @return a reference to this container instance - */ - public SELF withPassword(final String password) { - this.password = password; - return this.self(); - } - - /** - * @return a url to influxDb - */ - public String getUrl() { - return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); - } - - /** - * @return a influxDb client - */ - public InfluxDB getNewInfluxDB() { - final InfluxDB influxDB = InfluxDBFactory.connect(this.getUrl(), this.username, this.password); - influxDB.setDatabase(this.database); - return influxDB; - } -} diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java deleted file mode 100644 index 1d417ec7252..00000000000 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainerV2.java +++ /dev/null @@ -1,159 +0,0 @@ -package org.testcontainers.containers; - -import lombok.Getter; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.containers.wait.strategy.WaitAllStrategy; -import org.testcontainers.utility.DockerImageName; - -import java.util.Optional; - -/** - * Refer to - * the official InfluxDB 2.x container repository - * on docker hub for detailed documentation and newest tags. - */ -public class InfluxDBContainerV2 extends GenericContainer { - - private static final Integer INFLUXDB_PORT = 8086; - - private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); - private static final int NO_CONTENT_STATUS_CODE = 204; - - @Getter - private String username = "test-user"; - @Getter - private String password = "test-password"; - @Getter - private String bucket = "test-bucket"; - @Getter - private String organization = "test-org"; - @Getter - private Optional retention = Optional.empty(); - @Getter - private Optional adminToken = Optional.empty(); - - - public InfluxDBContainerV2(final String imageName) { - this(DockerImageName.parse(imageName)); - } - - - public InfluxDBContainerV2(final DockerImageName imageName) { - super(imageName); - imageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - this.waitStrategy = new WaitAllStrategy() - .withStrategy( - Wait - .forHttp("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE) - ) - .withStrategy(Wait.forListeningPort()); - this.addExposedPort(INFLUXDB_PORT); - } - - /** - *

- * The InfluxDB image contains some extra functionality to automatically bootstrap the system. This functionality is - * enabled by setting the DOCKER_INFLUXDB_INIT_MODE environment variable to the value "setup" when running the - * container. Additional environment variables are used to configure the setup logic: - *

    - *
  • DOCKER_INFLUXDB_INIT_USERNAME: The username to set for the system's initial super-user (Required). - *
  • DOCKER_INFLUXDB_INIT_PASSWORD: The password to set for the system's initial super-user (Required). - *
  • DOCKER_INFLUXDB_INIT_ORG: The name to set for the system's initial organization (Required). - *
  • DOCKER_INFLUXDB_INIT_BUCKET: The name to set for the system's initial bucket (Required). - *
  • DOCKER_INFLUXDB_INIT_RETENTION: The duration the system's initial bucket should retain data. If not set, - * the initial bucket will retain data forever. - *
  • DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: The authentication token to associate with the system's initial - * super-user. If not set, a token will be auto-generated by the system. - *
- *
- * See - * full documentation - */ - @Override - protected void configure() { - this.addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); - this.addEnv("DOCKER_INFLUXDB_INIT_USERNAME", this.username); - this.addEnv("DOCKER_INFLUXDB_INIT_PASSWORD", this.password); - this.addEnv("DOCKER_INFLUXDB_INIT_ORG", this.organization); - this.addEnv("DOCKER_INFLUXDB_INIT_BUCKET", this.bucket); - this.retention.ifPresent(ret -> this.addEnv("DOCKER_INFLUXDB_INIT_RETENTION", ret)); - this.adminToken.ifPresent(token -> this.addEnv("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", token)); - } - - /** - * Set env variable `DOCKER_INFLUXDB_INIT_USERNAME`. - * - * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will - * be granted read and write permissions for that database. - * @return a reference to this container instance - */ - public InfluxDBContainerV2 withUsername(final String username) { - this.username = username; - return this; - } - - /** - * Set env variable `DOCKER_INFLUXDB_INIT_PASSWORD`. - * - * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is - * generated and printed to standard out. - * @return a reference to this container instance - */ - public InfluxDBContainerV2 withPassword(final String password) { - this.password = password; - return this; - } - - /** - * Set env variable `DOCKER_INFLUXDB_INIT_ORG`. - * - * @param organization The organization for the initial setup of influxDB. - * @return a reference to this container instance - */ - public InfluxDBContainerV2 withOrganization(final String organization) { - this.organization = organization; - return this; - } - - /** - * Set env variable `DOCKER_INFLUXDB_INIT_BUCKET`. - * - * @param bucket Automatically initializes a bucket with the name of this environment variable. - * @return a reference to this container instance - */ - public InfluxDBContainerV2 withBucket(final String bucket) { - this.bucket = bucket; - return this; - } - - /** - * Set env variable `DOCKER_INFLUXDB_INIT_RETENTION`. - * - * @param retention Duration bucket will retain data (0 is infinite, default is 0). - * @return a reference to this container instance - */ - public InfluxDBContainerV2 withRetention(final String retention) { - this.retention = Optional.of(retention); - return this; - } - - /** - * Set env variable `DOCKER_INFLUXDB_INIT_ADMIN_TOKEN`. - * - * @param adminToken Authentication token to associate with the admin user. - * @return a reference to this container instance - */ - public InfluxDBContainerV2 withAdminToken(final String adminToken) { - this.adminToken = Optional.of(adminToken); - return this; - } - - /** - * @return a url to influxDb - */ - public String getUrl() { - return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); - } -} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java similarity index 76% rename from modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java rename to modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index 2d8c066c2bb..afcf2d800f3 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -11,19 +11,19 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; -public class InfluxDBContainerV2Test { +public class InfluxDBContainerTest { private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE.getVersionPart(); @ClassRule - public static final InfluxDBContainerV2 influxDBContainerV2 = - new InfluxDBContainerV2(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE); + public static final InfluxDBContainer INFLUX_DB_CONTAINER = + new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE); private InfluxDBClient client = null; @Before public void setUp() { - this.client = InfluxDBV2TestHelper.getInfluxDBClient(influxDBContainerV2); + this.client = InfluxDBV2TestHelper.getInfluxDBClient(INFLUX_DB_CONTAINER); } @After @@ -33,14 +33,14 @@ public void tearDown() { @Test public void getUrl() { - final String actual = influxDBContainerV2.getUrl(); + final String actual = INFLUX_DB_CONTAINER.getUrl(); assertThat(actual, notNullValue()); } @Test public void getNewInfluxDB() { - final InfluxDBClient actual = InfluxDBV2TestHelper.getInfluxDBClient(influxDBContainerV2); + final InfluxDBClient actual = InfluxDBV2TestHelper.getInfluxDBClient(INFLUX_DB_CONTAINER); assertThat(actual, notNullValue()); assertThat(actual.health().getStatus(), is(StatusEnum.PASS)); @@ -59,7 +59,7 @@ public void checkVersion() { @Test public void isRunning() { - final boolean actual = influxDBContainerV2.isRunning(); + final boolean actual = INFLUX_DB_CONTAINER.isRunning(); assertThat(actual, is(true)); } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 9b3241ee59b..b3a74908762 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -4,14 +4,14 @@ import org.junit.ClassRule; import org.junit.Test; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; public class InfluxDBContainerV1Test { @ClassRule - public static InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + public static InfluxDBContainer influxDBContainer = new InfluxDBContainer( InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE ); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java index 677bb8e043b..89c48df144a 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java @@ -26,7 +26,7 @@ public class InfluxDBContainerV1WithUserTest { private static final String PASSWORD = "test-password"; @Rule - public InfluxDBContainer influxDBContainer = new InfluxDBContainer<>(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE) + public InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java similarity index 90% rename from modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java rename to modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java index 80465fa2e46..7a3ed897c3a 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV2WithUserTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java @@ -20,7 +20,7 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; -public class InfluxDBContainerV2WithUserTest { +public class InfluxDBContainerWithUserTest { private static final String USERNAME = "new-test-user"; private static final String PASSWORD = "new-test-password"; @@ -33,8 +33,8 @@ public class InfluxDBContainerV2WithUserTest { private InfluxDBClient client = null; @ClassRule - public static final InfluxDBContainerV2 influxDBContainerV2 = - new InfluxDBContainerV2(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE) + public static final InfluxDBContainer INFLUX_DB_CONTAINER = + new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) @@ -44,7 +44,7 @@ public class InfluxDBContainerV2WithUserTest { @Before public void setUp() { - this.client = InfluxDBV2TestHelper.getInfluxDBClient(influxDBContainerV2); + this.client = InfluxDBV2TestHelper.getInfluxDBClient(INFLUX_DB_CONTAINER); } @After diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java index 267619eb1fb..4d0cd7120b8 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java @@ -9,12 +9,12 @@ public final class InfluxDBV2TestHelper { private InfluxDBV2TestHelper() { } - public static InfluxDBClient getInfluxDBClient(final InfluxDBContainerV2 influxDBContainerV2) { + public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() - .url(influxDBContainerV2.getUrl()) - .authenticate(influxDBContainerV2.getUsername(), influxDBContainerV2.getPassword().toCharArray()) - .bucket(influxDBContainerV2.getBucket()) - .org(influxDBContainerV2.getOrganization()) + .url(influxDBContainer.getUrl()) + .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) + .bucket(influxDBContainer.getBucket()) + .org(influxDBContainer.getOrganization()) .build(); return InfluxDBClientFactory.create(influxDBClientOptions); } From 0d99d8db24d37255a0fd80e54c0c73b411f77eb4 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Sat, 30 Jul 2022 11:47:18 +0200 Subject: [PATCH 34/51] Update docs --- docs/modules/databases/influxdb.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index fc2ebfa11f8..f59f6d46632 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -8,7 +8,7 @@ They are breaking changes in InfluxDB 2.x. For more information refer to the main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). InfluxDB official [container registry](https://hub.docker.com/_/influxdb) on docker hub. -## InfluxDB V2.x Usage example +## InfluxDB 2.x usage example Running influxDbContainer as a stand-in for InfluxDB in a test: @@ -41,7 +41,7 @@ It is possible to override the default values, take a look at these tests: **NOTE**: You can find the latest documentation about the InfluxDB 2.x java client [here](https://github.com/influxdata/influxdb-client-java). -## InfluxDB 1.x Usage example (Deprecated) +## InfluxDB 1.x usage example Running InfluxDb Container as a stand-in for InfluxDB in a test: From 8f56b21f831fc4df6e7c64413e642f0aa4be3196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez=20Gonzales?= Date: Thu, 4 Aug 2022 15:14:52 -0500 Subject: [PATCH 35/51] Update docs/modules/databases/influxdb.md --- docs/modules/databases/influxdb.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index f59f6d46632..1d7a3f6a113 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -4,7 +4,7 @@ Testcontainers module for InfluxData [InfluxDB](https://www.influxdata.com/produ ## Important Note -They are breaking changes in InfluxDB 2.x. For more information refer to the +There are breaking changes in InfluxDB 2.x. For more information refer to the main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). InfluxDB official [container registry](https://hub.docker.com/_/influxdb) on docker hub. From cc1fc1a0ae2537aa7a194d81d918bdf22b620a02 Mon Sep 17 00:00:00 2001 From: raminqaf Date: Fri, 5 Aug 2022 14:28:55 +0200 Subject: [PATCH 36/51] Add reviews --- docs/modules/databases/influxdb.md | 102 ++++++++---- modules/influxdb/build.gradle | 2 +- .../containers/InfluxDBContainer.java | 33 +++- .../containers/InfluxDBContainerTest.java | 155 +++++++++++++----- .../containers/InfluxDBContainerV1Test.java | 149 +++++++++++++++-- .../InfluxDBContainerV1WithUserTest.java | 75 --------- .../InfluxDBContainerWithUserTest.java | 94 ----------- .../containers/InfluxDBTestImages.java | 8 - ...TestHelper.java => InfluxDBTestUtils.java} | 8 +- 9 files changed, 351 insertions(+), 275 deletions(-) delete mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java delete mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java delete mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java rename modules/influxdb/src/test/java/org/testcontainers/containers/{InfluxDBV2TestHelper.java => InfluxDBTestUtils.java} (69%) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index f59f6d46632..504355bad83 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -2,15 +2,15 @@ Testcontainers module for InfluxData [InfluxDB](https://www.influxdata.com/products/influxdb/). -## Important Note +## Important note -They are breaking changes in InfluxDB 2.x. For more information refer to the +There are breaking changes in InfluxDB 2.x. For more information refer to the main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). InfluxDB official [container registry](https://hub.docker.com/_/influxdb) on docker hub. ## InfluxDB 2.x usage example -Running influxDbContainer as a stand-in for InfluxDB in a test: +Running influxDb container as a stand-in for InfluxDB in a test: [InfluxDBContainerTest](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) @@ -19,53 +19,93 @@ Running influxDbContainer as a stand-in for InfluxDB in a test: The influxDB will be setup with the following data:
-| Property | Default Value | -| ------------- |:-------------:| -| username | test-user | -| password | test-password | -| organization | test-org | -| bucket | test-bucket | -| retention | 0 (infinite) | -| adminToken | - | +| Property | Default Value | +|--------------|:-------------:| +| username | test-user | +| password | test-password | +| organization | test-org | +| bucket | test-bucket | +| retention | 0 (infinite) | +| adminToken | - | For more details about the InfluxDB setup go to the -official [docs](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials) +official [InfluxDB docs](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials) . -It is possible to override the default values, take a look at these tests: - - -[InfluxDBContainerWithUserTest](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java) - +In the following you will find a snippet to create a InfluxDB client using the java client: + +```java +class InfluxDBClient { + public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { + final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() + .url(influxDBContainer.getUrl()) + .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) + .bucket(influxDBContainer.getBucket()) + .org(influxDBContainer.getOrganization()) + .build(); + return InfluxDBClientFactory.create(influxDBClientOptions); + } +} +``` -**NOTE**: You can find the latest documentation about the InfluxDB 2.x java -client [here](https://github.com/influxdata/influxdb-client-java). +!!! hint + You can find the latest documentation about the InfluxDB 2.x java client [here](https://github.com/influxdata/influxdb-client-java). ## InfluxDB 1.x usage example -Running InfluxDb Container as a stand-in for InfluxDB in a test: +Running InfluxDb container as a stand-in for InfluxDB in a test: [InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) +The influxDB will be setup with the following data:
+ +| Property | Default Value | +|---------------|:-------------:| +| username | test-user | +| password | test-password | +| authEnabled | true | +| admin | admin | +| adminPassword | password | +| database | - | + +In the following you will find a snippet to create a InfluxDB client using the java client: + +```java +class InfluxDBClient { + public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { + final InfluxDB influxDB = InfluxDBFactory.connect(influxDBContainer.getUrl(), influxDBContainer.getUsername(), influxDBContainer.getPassword()); + influxDB.setDatabase(influxDBContainer.getDatabase()); + return influxDB; + } +} +``` + +!!! hint + You can find the latest documentation about the InfluxDB 1.x java client [here](https://github.com/influxdata/influxdb-java). + ## Adding this module to your project dependencies Add the following dependency to your `pom.xml`/`build.gradle` file: === "Gradle" - ```groovy - testImplementation "org.testcontainers:influxdb:{{latest_version}}" - ``` + +```groovy +testImplementation "org.testcontainers:influxdb:{{latest_version}}" +``` + === "Maven" - ```xml - - org.testcontainers - influxdb - {{latest_version}} - test - - ``` + +```xml + + + org.testcontainers + influxdb + {{latest_version}} + test + +``` !!! hint Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency. diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 7d53ebad3d2..f3c5c9f4877 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -6,5 +6,5 @@ dependencies { compileOnly 'org.influxdb:influxdb-java:2.23' testImplementation 'org.influxdb:influxdb-java:2.23' - testImplementation "com.influxdb:influxdb-client-java:3.1.0" + testImplementation "com.influxdb:influxdb-client-java:6.4.0" } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 7fb7b982706..bc2bc91f136 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -20,13 +20,15 @@ public class InfluxDBContainer extends GenericContainer { private static final Integer INFLUXDB_PORT = 8086; private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); + private static final String DEFAULT_TAG = "1.4.3"; + @Deprecated + public static final String VERSION = DEFAULT_TAG; private static final int NO_CONTENT_STATUS_CODE = 204; - @Getter - private String username = "any"; + private String username = "test-user"; @Getter - private String password = "any"; + private String password = "test-password"; /** * Properties InfluxDB <= 2 @@ -50,16 +52,27 @@ public class InfluxDBContainer extends GenericContainer { private final boolean isAtLeastMajorVersion2; + /** + * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + */ + @Deprecated + public InfluxDBContainer() { + this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG)); + } - public InfluxDBContainer(final String dockerImageName) { - this(DockerImageName.parse(dockerImageName)); + /** + * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead + */ + @Deprecated + public InfluxDBContainer(final String version) { + this(DEFAULT_IMAGE_NAME.withTag(version)); } public InfluxDBContainer(final DockerImageName dockerImageName) { super(dockerImageName); dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - logger().info("Starting an InfluxDB container using [{}]", dockerImageName); + this.logger().info("Starting an InfluxDB container using [{}]", dockerImageName); this.waitStrategy = new WaitAllStrategy() .withStrategy( Wait @@ -79,10 +92,10 @@ public InfluxDBContainer(final DockerImageName dockerImageName) { */ @Override protected void configure() { - if (isAtLeastMajorVersion2) { - setInfluxDBV2Envs(); + if (this.isAtLeastMajorVersion2) { + this.setInfluxDBV2Envs(); } else { - setInfluxDBV1Envs(); + this.setInfluxDBV1Envs(); } } @@ -258,7 +271,9 @@ public String getUrl() { /** * @return a InfluxDB client for InfluxDB <= 2. + * @deprecated Use the new InfluxDB client library. */ + @Deprecated public InfluxDB getNewInfluxDB() { final InfluxDB influxDB = InfluxDBFactory.connect(this.getUrl(), this.username, this.password); influxDB.setDatabase(this.database); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index afcf2d800f3..90899c28c62 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -1,66 +1,147 @@ package org.testcontainers.containers; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + import com.influxdb.client.InfluxDBClient; -import com.influxdb.client.domain.HealthCheck.StatusEnum; +import com.influxdb.client.QueryApi; +import com.influxdb.client.WriteApi; +import com.influxdb.client.domain.Bucket; +import com.influxdb.client.domain.Run.StatusEnum; +import com.influxdb.client.domain.WritePrecision; +import com.influxdb.client.write.Point; +import com.influxdb.query.FluxRecord; +import com.influxdb.query.FluxTable; +import java.io.IOException; +import java.time.Instant; +import java.util.List; +import org.jetbrains.annotations.Nullable; import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; import org.junit.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - public class InfluxDBContainerTest { + private static final String USERNAME = "new-test-user"; + private static final String PASSWORD = "new-test-password"; + private static final String ORG = "new-test-org"; + private static final String BUCKET = "new-test-bucket"; + private static final String RETENTION = "1w"; + private static final String ADMIN_TOKEN = "super-secret-token"; - private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE.getVersionPart(); - - @ClassRule - public static final InfluxDBContainer INFLUX_DB_CONTAINER = - new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE); - - private InfluxDBClient client = null; - - @Before - public void setUp() { - this.client = InfluxDBV2TestHelper.getInfluxDBClient(INFLUX_DB_CONTAINER); - } + @Nullable + private InfluxDBClient influxDBClient = null; @After - public void tearDown() { - this.client.close(); + public void stopInfluxDBClient() { + if (this.influxDBClient != null) { + this.influxDBClient.close(); + this.influxDBClient = null; + } } @Test public void getUrl() { - final String actual = INFLUX_DB_CONTAINER.getUrl(); + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE)) { - assertThat(actual, notNullValue()); - } + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), is(true)); - @Test - public void getNewInfluxDB() { - final InfluxDBClient actual = InfluxDBV2TestHelper.getInfluxDBClient(INFLUX_DB_CONTAINER); + final String actual = influxDBContainer.getUrl(); - assertThat(actual, notNullValue()); - assertThat(actual.health().getStatus(), is(StatusEnum.PASS)); + assertThat(actual, notNullValue()); + } } @Test - public void checkVersion() { - assertThat(this.client, notNullValue()); + public void getInfluxDBClient() { + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE)) { - assertThat(this.client.health().getStatus(), is(StatusEnum.PASS)); + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), is(true)); - final String actualVersion = String.format("%s", this.client.health().getVersion()); + this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); - assertThat(actualVersion, is(TEST_VERSION)); + assertThat(this.influxDBClient, notNullValue()); + assertThat(this.influxDBClient.ping(), is(true)); + } } @Test - public void isRunning() { - final boolean actual = INFLUX_DB_CONTAINER.isRunning(); + public void getBucket() { + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE)) { + influxDBContainer + .withUsername(USERNAME) + .withPassword(PASSWORD) + .withOrganization(ORG) + .withBucket(BUCKET) + .withRetention(RETENTION) + .withAdminToken(ADMIN_TOKEN); + + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), is(true)); + + this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); + + assertThat(this.influxDBClient, notNullValue()); + + final Bucket bucket = this.influxDBClient.getBucketsApi().findBucketByName(BUCKET); + assertThat(bucket, notNullValue()); + + assertThat(bucket.getName(), is(BUCKET)); + } + } + + @Test + public void queryForWriteAndRead() { + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE)) { + influxDBContainer + .withUsername(USERNAME) + .withPassword(PASSWORD) + .withOrganization(ORG) + .withBucket(BUCKET) + .withRetention(RETENTION) + .withAdminToken(ADMIN_TOKEN); + + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), is(true)); + + this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); + + assertThat(this.influxDBClient, notNullValue()); + + try (final WriteApi writeApi = this.influxDBClient.makeWriteApi()) { + + // + // Write by Data Point + // + final Point point = Point.measurement("temperature") + .addTag("location", "west") + .addField("value", 55.0D) + .time(Instant.now().toEpochMilli(), WritePrecision.MS); + + writeApi.writePoint(point); + + } + + // + // Query data + // + final String flux = String.format("from(bucket:\"%s\") |> range(start: 0)", BUCKET); + + final QueryApi queryApi = this.influxDBClient.getQueryApi(); + + final FluxTable fluxTable = queryApi.query(flux).get(0); + final List records = fluxTable.getRecords(); + assertThat(records.size(), is(1)); - assertThat(actual, is(true)); + } } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index b3a74908762..37d3edde000 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -1,46 +1,159 @@ package org.testcontainers.containers; -import org.influxdb.InfluxDB; -import org.junit.ClassRule; -import org.junit.Test; - import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +import java.util.Set; +import java.util.concurrent.TimeUnit; +import org.hamcrest.CoreMatchers; +import org.influxdb.InfluxDB; +import org.influxdb.dto.Point; +import org.influxdb.dto.Query; +import org.influxdb.dto.QueryResult; +import org.jetbrains.annotations.Nullable; +import org.junit.After; +import org.junit.Test; public class InfluxDBContainerV1Test { - @ClassRule - public static InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE - ); + private static final String TEST_VERSION = InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE.getVersionPart(); + + private static final String DATABASE = "test"; + + private static final String USER = "new-test-user"; + + private static final String PASSWORD = "new-test-password"; + @Nullable + private InfluxDB influxDBClient = null; + + @After + public void stopInfluxDBClient() { + if (this.influxDBClient != null) { + this.influxDBClient.close(); + this.influxDBClient = null; + } + } @Test public void getUrl() { - final String actual = influxDBContainer.getUrl(); + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { + + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); - assertThat(actual, notNullValue()); + final String actual = influxDBContainer.getUrl(); + + assertThat(actual, notNullValue()); + } } @Test public void getNewInfluxDB() { - final InfluxDB actual = influxDBContainer.getNewInfluxDB(); + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { + + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); - assertThat(actual, notNullValue()); - assertThat(actual.ping(), notNullValue()); + final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB(); + + assertThat(influxDBClient, notNullValue()); + assertThat(influxDBClient.ping(), notNullValue()); + } } @Test public void getLivenessCheckPort() { - final Integer actual = influxDBContainer.getLivenessCheckPort(); + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { + + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); - assertThat(actual, notNullValue()); + final Set actual = influxDBContainer.getLivenessCheckPortNumbers(); + + assertThat(actual, notNullValue()); + } } @Test - public void isRunning() { - final boolean actual = influxDBContainer.isRunning(); + public void describeDatabases() { + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { + influxDBContainer.withDatabase(DATABASE) + .withUsername(USER) + .withPassword(PASSWORD); + + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + + this.influxDBClient = influxDBContainer.getNewInfluxDB(); + + assertThat(this.influxDBClient, notNullValue()); + assertThat(this.influxDBClient.describeDatabases(), hasItem(DATABASE)); + } + } + + @Test + public void checkVersion() { + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { + influxDBContainer.withDatabase(DATABASE) + .withUsername(USER) + .withPassword(PASSWORD); + + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + + this.influxDBClient = influxDBContainer.getNewInfluxDB(); + + assertThat(this.influxDBClient, notNullValue()); + + assertThat(this.influxDBClient.ping(), notNullValue()); + + assertThat(this.influxDBClient.version(), is(TEST_VERSION)); + } + } + + @Test + public void queryForWriteAndRead() { + try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { + influxDBContainer.withDatabase(DATABASE) + .withUsername(USER) + .withPassword(PASSWORD); + + // Start the container. This step might take some time... + influxDBContainer.start(); + assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + + this.influxDBClient = influxDBContainer.getNewInfluxDB(); + + final Point point = Point + .measurement("cpu") + .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + .addField("idle", 90L) + .addField("user", 9L) + .addField("system", 1L) + .build(); + this.influxDBClient.write(point); + + final Query query = new Query("SELECT idle FROM cpu", DATABASE); + final QueryResult actual = this.influxDBClient.query(query); - assertThat(actual, is(true)); + assertThat(actual, notNullValue()); + assertThat(actual.getError(), nullValue()); + assertThat(actual.getResults(), notNullValue()); + assertThat(actual.getResults().size(), is(1)); + } } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java deleted file mode 100644 index 89c48df144a..00000000000 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1WithUserTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.testcontainers.containers; - -import org.influxdb.InfluxDB; -import org.influxdb.dto.Point; -import org.influxdb.dto.Query; -import org.influxdb.dto.QueryResult; -import org.junit.Rule; -import org.junit.Test; - -import java.util.concurrent.TimeUnit; - -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; - -public class InfluxDBContainerV1WithUserTest { - - private static final String TEST_VERSION = InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE.getVersionPart(); - - private static final String DATABASE = "test"; - - private static final String USER = "test-user"; - - private static final String PASSWORD = "test-password"; - - @Rule - public InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V1_TEST_IMAGE) - .withDatabase(DATABASE) - .withUsername(USER) - .withPassword(PASSWORD); - - @Test - public void describeDatabases() { - final InfluxDB actual = this.influxDBContainer.getNewInfluxDB(); - - assertThat(actual, notNullValue()); - assertThat(actual.describeDatabases(), hasItem(DATABASE)); - } - - @Test - public void checkVersion() { - final InfluxDB actual = this.influxDBContainer.getNewInfluxDB(); - - assertThat(actual, notNullValue()); - - assertThat(actual.ping(), notNullValue()); - assertThat(actual.ping().getVersion(), is(TEST_VERSION)); - - assertThat(actual.version(), is(TEST_VERSION)); - } - - @Test - public void queryForWriteAndRead() { - final InfluxDB influxDB = this.influxDBContainer.getNewInfluxDB(); - - final Point point = Point - .measurement("cpu") - .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) - .addField("idle", 90L) - .addField("user", 9L) - .addField("system", 1L) - .build(); - influxDB.write(point); - - final Query query = new Query("SELECT idle FROM cpu", DATABASE); - final QueryResult actual = influxDB.query(query); - - assertThat(actual, notNullValue()); - assertThat(actual.getError(), nullValue()); - assertThat(actual.getResults(), notNullValue()); - assertThat(actual.getResults().size(), is(1)); - } -} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java deleted file mode 100644 index 7a3ed897c3a..00000000000 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java +++ /dev/null @@ -1,94 +0,0 @@ -package org.testcontainers.containers; - -import com.influxdb.client.InfluxDBClient; -import com.influxdb.client.QueryApi; -import com.influxdb.client.WriteApi; -import com.influxdb.client.domain.Bucket; -import com.influxdb.client.domain.WritePrecision; -import com.influxdb.client.write.Point; -import com.influxdb.query.FluxRecord; -import com.influxdb.query.FluxTable; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; - -import java.time.Instant; -import java.util.List; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - -public class InfluxDBContainerWithUserTest { - - private static final String USERNAME = "new-test-user"; - private static final String PASSWORD = "new-test-password"; - private static final String ORG = "new-test-org"; - private static final String BUCKET = "new-test-bucket"; - private static final String RETENTION = "1w"; - private static final String ADMIN_TOKEN = "super-secret-token"; - - - private InfluxDBClient client = null; - - @ClassRule - public static final InfluxDBContainer INFLUX_DB_CONTAINER = - new InfluxDBContainer(InfluxDBTestImages.INFLUXDB_V2_TEST_IMAGE) - .withUsername(USERNAME) - .withPassword(PASSWORD) - .withOrganization(ORG) - .withBucket(BUCKET) - .withRetention(RETENTION) - .withAdminToken(ADMIN_TOKEN); - - @Before - public void setUp() { - this.client = InfluxDBV2TestHelper.getInfluxDBClient(INFLUX_DB_CONTAINER); - } - - @After - public void tearDown() { - this.client.close(); - } - - @Test - public void getBucket() { - assertThat(this.client, notNullValue()); - - final Bucket bucket = this.client.getBucketsApi().findBucketByName(BUCKET); - assertThat(bucket, notNullValue()); - - assertThat(bucket.getName(), is(BUCKET)); - } - - @Test - public void queryForWriteAndRead() { - assertThat(this.client, notNullValue()); - - try (final WriteApi writeApi = this.client.getWriteApi()) { - - // - // Write by Data Point - // - final Point point = Point.measurement("temperature") - .addTag("location", "west") - .addField("value", 55.0D) - .time(Instant.now().toEpochMilli(), WritePrecision.MS); - - writeApi.writePoint(point); - - } - - // - // Query data - // - final String flux = String.format("from(bucket:\"%s\") |> range(start: 0)", BUCKET); - - final QueryApi queryApi = this.client.getQueryApi(); - - final FluxTable fluxTable = queryApi.query(flux).get(0); - final List records = fluxTable.getRecords(); - assertThat(records.size(), is(1)); - } -} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java deleted file mode 100644 index 8b12ce92ebd..00000000000 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestImages.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.testcontainers.containers; - -import org.testcontainers.utility.DockerImageName; - -public interface InfluxDBTestImages { - DockerImageName INFLUXDB_V1_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); - DockerImageName INFLUXDB_V2_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); -} diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java similarity index 69% rename from modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java rename to modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java index 4d0cd7120b8..34bc5ccf9d6 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBV2TestHelper.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java @@ -3,10 +3,14 @@ import com.influxdb.client.InfluxDBClient; import com.influxdb.client.InfluxDBClientFactory; import com.influxdb.client.InfluxDBClientOptions; +import org.testcontainers.utility.DockerImageName; -public final class InfluxDBV2TestHelper { +public final class InfluxDBTestUtils { - private InfluxDBV2TestHelper() { + static final DockerImageName INFLUXDB_V1_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); + static final DockerImageName INFLUXDB_V2_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); + + private InfluxDBTestUtils() { } public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { From 3ff5ec26ae92f47fc26cccda9a8eff1d25fb5d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez=20Gonzales?= Date: Fri, 5 Aug 2022 18:52:45 -0500 Subject: [PATCH 37/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java --- .../org/testcontainers/containers/InfluxDBContainer.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index bc2bc91f136..c3a5c8dcb0a 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -11,10 +11,7 @@ import java.util.Optional; /** - * Represents an InfluxDB Docker instance. - * Refer to - * the official InfluxDB 1.x and 2.x container repository - * on docker hub for detailed documentation and newest tags. + * Testcontainers implementation for InfluxDB. */ public class InfluxDBContainer extends GenericContainer { From 0dee6363347b436a2820aea512e181a6a247dbf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 5 Aug 2022 19:15:00 -0500 Subject: [PATCH 38/51] Fix spotless and move to assertj --- modules/influxdb/build.gradle | 3 +- .../containers/InfluxDBContainer.java | 23 ++-- .../containers/InfluxDBContainerTest.java | 81 ++++++-------- .../containers/InfluxDBContainerV1Test.java | 100 ++++++++---------- .../containers/InfluxDBTestUtils.java | 6 +- 5 files changed, 99 insertions(+), 114 deletions(-) diff --git a/modules/influxdb/build.gradle b/modules/influxdb/build.gradle index 81b51ccff08..c412d837d76 100644 --- a/modules/influxdb/build.gradle +++ b/modules/influxdb/build.gradle @@ -4,7 +4,8 @@ dependencies { api project(':testcontainers') compileOnly 'org.influxdb:influxdb-java:2.23' - + + testImplementation 'org.assertj:assertj-core:3.23.1' testImplementation 'org.influxdb:influxdb-java:2.23' testImplementation "com.influxdb:influxdb-client-java:6.4.0" } diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index c3a5c8dcb0a..db773f2553a 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -18,12 +18,15 @@ public class InfluxDBContainer extends GenericContainer { private static final Integer INFLUXDB_PORT = 8086; private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); private static final String DEFAULT_TAG = "1.4.3"; + @Deprecated public static final String VERSION = DEFAULT_TAG; + private static final int NO_CONTENT_STATUS_CODE = 204; @Getter private String username = "test-user"; + @Getter private String password = "test-password"; @@ -40,10 +43,13 @@ public class InfluxDBContainer extends GenericContainer { */ @Getter private String bucket = "test-bucket"; + @Getter private String organization = "test-org"; + @Getter private Optional retention = Optional.empty(); + @Getter private Optional adminToken = Optional.empty(); @@ -70,14 +76,15 @@ public InfluxDBContainer(final DockerImageName dockerImageName) { dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); this.logger().info("Starting an InfluxDB container using [{}]", dockerImageName); - this.waitStrategy = new WaitAllStrategy() - .withStrategy( - Wait - .forHttp("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE) - ) - .withStrategy(Wait.forListeningPort()); + this.waitStrategy = + new WaitAllStrategy() + .withStrategy( + Wait + .forHttp("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE) + ) + .withStrategy(Wait.forListeningPort()); this.isAtLeastMajorVersion2 = new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("2.0.0"); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index 90899c28c62..dd9b7223f18 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -1,26 +1,24 @@ package org.testcontainers.containers; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; - import com.influxdb.client.InfluxDBClient; import com.influxdb.client.QueryApi; import com.influxdb.client.WriteApi; import com.influxdb.client.domain.Bucket; -import com.influxdb.client.domain.Run.StatusEnum; import com.influxdb.client.domain.WritePrecision; import com.influxdb.client.write.Point; import com.influxdb.query.FluxRecord; import com.influxdb.query.FluxTable; -import java.io.IOException; -import java.time.Instant; -import java.util.List; import org.jetbrains.annotations.Nullable; import org.junit.After; import org.junit.Test; +import java.time.Instant; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + public class InfluxDBContainerTest { + private static final String USERNAME = "new-test-user"; private static final String PASSWORD = "new-test-password"; private static final String ORG = "new-test-org"; @@ -41,39 +39,37 @@ public void stopInfluxDBClient() { @Test public void getUrl() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE)) { - - // Start the container. This step might take some time... + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + ) { influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); final String actual = influxDBContainer.getUrl(); - - assertThat(actual, notNullValue()); + assertThat(actual).isNotNull(); } } @Test public void getInfluxDBClient() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE)) { - - // Start the container. This step might take some time... + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + ) { influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); - assertThat(this.influxDBClient, notNullValue()); - assertThat(this.influxDBClient.ping(), is(true)); + assertThat(this.influxDBClient).isNotNull(); + assertThat(this.influxDBClient.ping()).isTrue(); } } @Test public void getBucket() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE)) { + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + ) { influxDBContainer .withUsername(USERNAME) .withPassword(PASSWORD) @@ -82,66 +78,55 @@ public void getBucket() { .withRetention(RETENTION) .withAdminToken(ADMIN_TOKEN); - // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); - assertThat(this.influxDBClient, notNullValue()); + assertThat(this.influxDBClient).isNotNull(); final Bucket bucket = this.influxDBClient.getBucketsApi().findBucketByName(BUCKET); - assertThat(bucket, notNullValue()); + assertThat(bucket).isNotNull(); - assertThat(bucket.getName(), is(BUCKET)); + assertThat(bucket.getName()).isEqualTo(BUCKET); } } @Test public void queryForWriteAndRead() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE)) { - influxDBContainer + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) .withBucket(BUCKET) .withRetention(RETENTION) - .withAdminToken(ADMIN_TOKEN); - - // Start the container. This step might take some time... + .withAdminToken(ADMIN_TOKEN) + ) { influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); - assertThat(this.influxDBClient, notNullValue()); + assertThat(this.influxDBClient).isNotNull(); try (final WriteApi writeApi = this.influxDBClient.makeWriteApi()) { - - // - // Write by Data Point - // - final Point point = Point.measurement("temperature") + final Point point = Point + .measurement("temperature") .addTag("location", "west") .addField("value", 55.0D) .time(Instant.now().toEpochMilli(), WritePrecision.MS); writeApi.writePoint(point); - } - // - // Query data - // final String flux = String.format("from(bucket:\"%s\") |> range(start: 0)", BUCKET); final QueryApi queryApi = this.influxDBClient.getQueryApi(); final FluxTable fluxTable = queryApi.query(flux).get(0); final List records = fluxTable.getRecords(); - assertThat(records.size(), is(1)); - + assertThat(records).hasSize(1); } } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 37d3edde000..392e8fa0721 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -1,14 +1,5 @@ package org.testcontainers.containers; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; - -import java.util.Set; -import java.util.concurrent.TimeUnit; -import org.hamcrest.CoreMatchers; import org.influxdb.InfluxDB; import org.influxdb.dto.Point; import org.influxdb.dto.Query; @@ -17,6 +8,11 @@ import org.junit.After; import org.junit.Test; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + public class InfluxDBContainerV1Test { private static final String TEST_VERSION = InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE.getVersionPart(); @@ -26,6 +22,7 @@ public class InfluxDBContainerV1Test { private static final String USER = "new-test-user"; private static final String PASSWORD = "new-test-password"; + @Nullable private InfluxDB influxDBClient = null; @@ -39,102 +36,97 @@ public void stopInfluxDBClient() { @Test public void getUrl() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { - + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + ) { // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); final String actual = influxDBContainer.getUrl(); - assertThat(actual, notNullValue()); + assertThat(actual).isNotNull(); } } @Test public void getNewInfluxDB() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { - + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + ) { // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB(); - assertThat(influxDBClient, notNullValue()); - assertThat(influxDBClient.ping(), notNullValue()); + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping().isGood()).isTrue(); } } @Test public void getLivenessCheckPort() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { - + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + ) { // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); final Set actual = influxDBContainer.getLivenessCheckPortNumbers(); - assertThat(actual, notNullValue()); + assertThat(actual).isNotNull(); } } @Test public void describeDatabases() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { - influxDBContainer.withDatabase(DATABASE) - .withUsername(USER) - .withPassword(PASSWORD); + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + ) { + influxDBContainer.withDatabase(DATABASE).withUsername(USER).withPassword(PASSWORD); // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); this.influxDBClient = influxDBContainer.getNewInfluxDB(); - assertThat(this.influxDBClient, notNullValue()); - assertThat(this.influxDBClient.describeDatabases(), hasItem(DATABASE)); + assertThat(this.influxDBClient).isNotNull(); + assertThat(this.influxDBClient.describeDatabases()).contains(DATABASE); } } @Test public void checkVersion() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { - influxDBContainer.withDatabase(DATABASE) - .withUsername(USER) - .withPassword(PASSWORD); + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + ) { + influxDBContainer.withDatabase(DATABASE).withUsername(USER).withPassword(PASSWORD); // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); this.influxDBClient = influxDBContainer.getNewInfluxDB(); - assertThat(this.influxDBClient, notNullValue()); - - assertThat(this.influxDBClient.ping(), notNullValue()); - - assertThat(this.influxDBClient.version(), is(TEST_VERSION)); + assertThat(this.influxDBClient).isNotNull(); + assertThat(this.influxDBClient.ping().isGood()).isTrue(); + assertThat(this.influxDBClient.version()).isEqualTo(TEST_VERSION); } } @Test public void queryForWriteAndRead() { - try (final InfluxDBContainer influxDBContainer = new InfluxDBContainer( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE)) { - influxDBContainer.withDatabase(DATABASE) - .withUsername(USER) - .withPassword(PASSWORD); + try ( + final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + ) { + influxDBContainer.withDatabase(DATABASE).withUsername(USER).withPassword(PASSWORD); // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning(), CoreMatchers.is(true)); + assertThat(influxDBContainer.isRunning()).isTrue(); this.influxDBClient = influxDBContainer.getNewInfluxDB(); @@ -150,10 +142,10 @@ public void queryForWriteAndRead() { final Query query = new Query("SELECT idle FROM cpu", DATABASE); final QueryResult actual = this.influxDBClient.query(query); - assertThat(actual, notNullValue()); - assertThat(actual.getError(), nullValue()); - assertThat(actual.getResults(), notNullValue()); - assertThat(actual.getResults().size(), is(1)); + assertThat(actual).isNotNull(); + assertThat(actual.getError()).isNull(); + assertThat(actual.getResults()).isNotNull(); + assertThat(actual.getResults()).hasSize(1); } } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java index 34bc5ccf9d6..65ff0aaab38 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java @@ -10,11 +10,11 @@ public final class InfluxDBTestUtils { static final DockerImageName INFLUXDB_V1_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); static final DockerImageName INFLUXDB_V2_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); - private InfluxDBTestUtils() { - } + private InfluxDBTestUtils() {} public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { - final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() + final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions + .builder() .url(influxDBContainer.getUrl()) .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) .bucket(influxDBContainer.getBucket()) From 2fdd745ab089a2990df2b79ec610923a97ec7961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 5 Aug 2022 19:39:57 -0500 Subject: [PATCH 39/51] Fix checkstyle and japicmp --- .../containers/InfluxDBContainer.java | 22 ++++++++++++++----- .../containers/InfluxDBContainerTest.java | 5 +++++ .../containers/InfluxDBTestUtils.java | 1 + 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index db773f2553a..160b323937c 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -8,15 +8,19 @@ import org.testcontainers.utility.ComparableVersion; import org.testcontainers.utility.DockerImageName; +import java.util.Collections; import java.util.Optional; +import java.util.Set; /** * Testcontainers implementation for InfluxDB. */ public class InfluxDBContainer extends GenericContainer { - private static final Integer INFLUXDB_PORT = 8086; + public static final Integer INFLUXDB_PORT = 8086; + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb"); + private static final String DEFAULT_TAG = "1.4.3"; @Deprecated @@ -34,8 +38,11 @@ public class InfluxDBContainer extends GenericContainer { * Properties InfluxDB <= 2 */ private boolean authEnabled = true; + private String admin = "admin"; + private String adminPassword = "password"; + private String database; /** @@ -149,9 +156,14 @@ private void setInfluxDBV1Envs() { this.addEnv("INFLUXDB_DB", this.database); } + @Override + public Set getLivenessCheckPortNumbers() { + return Collections.singleton(getMappedPort(INFLUXDB_PORT)); + } + /** - * Set env variable `INFLUXDB_USER` for InfluxDB <= 2.
- * + * Set env variable `INFLUXDB_USER` for InfluxDB <= 2. + *

* Set env variable `DOCKER_INFLUXDB_INIT_USERNAME` for InfluxDB >= 2. * * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will @@ -164,8 +176,8 @@ public InfluxDBContainer withUsername(final String username) { } /** - * Set env variable `INFLUXDB_USER_PASSWORD` for InfluxDB <= 1.
- * + * Set env variable `INFLUXDB_USER_PASSWORD` for InfluxDB <= 1. + *

* Set env variable `DOCKER_INFLUXDB_INIT_PASSWORD` for InfluxDB >= 2. * * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index dd9b7223f18..93d9899dc48 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -20,10 +20,15 @@ public class InfluxDBContainerTest { private static final String USERNAME = "new-test-user"; + private static final String PASSWORD = "new-test-password"; + private static final String ORG = "new-test-org"; + private static final String BUCKET = "new-test-bucket"; + private static final String RETENTION = "1w"; + private static final String ADMIN_TOKEN = "super-secret-token"; @Nullable diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java index 65ff0aaab38..852ec4d2036 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java @@ -8,6 +8,7 @@ public final class InfluxDBTestUtils { static final DockerImageName INFLUXDB_V1_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); + static final DockerImageName INFLUXDB_V2_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); private InfluxDBTestUtils() {} From e526c770ce432d9a32e7717a788e0bbe68da0ca3 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Tue, 9 Aug 2022 13:25:09 +0200 Subject: [PATCH 40/51] Fix javadoc --- .../containers/InfluxDBContainer.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 160b323937c..99da0a5a8d3 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -35,7 +35,7 @@ public class InfluxDBContainer extends GenericContainer { private String password = "test-password"; /** - * Properties InfluxDB <= 2 + * Properties of InfluxDB 1.x */ private boolean authEnabled = true; @@ -46,7 +46,7 @@ public class InfluxDBContainer extends GenericContainer { private String database; /** - * InfluxDB >= 2 + * Properties of InfluxDB 2.x */ @Getter private String bucket = "test-bucket"; @@ -162,9 +162,9 @@ public Set getLivenessCheckPortNumbers() { } /** - * Set env variable `INFLUXDB_USER` for InfluxDB <= 2. + * Set env variable `INFLUXDB_USER` for InfluxDB 1.x. *

- * Set env variable `DOCKER_INFLUXDB_INIT_USERNAME` for InfluxDB >= 2. + * Set env variable `DOCKER_INFLUXDB_INIT_USERNAME` for InfluxDB 2.x. * * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will * be granted read and write permissions for that database. @@ -176,9 +176,9 @@ public InfluxDBContainer withUsername(final String username) { } /** - * Set env variable `INFLUXDB_USER_PASSWORD` for InfluxDB <= 1. + * Set env variable `INFLUXDB_USER_PASSWORD` for InfluxDB 1.x. *

- * Set env variable `DOCKER_INFLUXDB_INIT_PASSWORD` for InfluxDB >= 2. + * Set env variable `DOCKER_INFLUXDB_INIT_PASSWORD` for InfluxDB 2.x. * * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is * generated and printed to standard out. @@ -286,7 +286,7 @@ public String getUrl() { } /** - * @return a InfluxDB client for InfluxDB <= 2. + * @return a InfluxDB client for InfluxDB 1.x. * @deprecated Use the new InfluxDB client library. */ @Deprecated From ae27fc3c9b796c089cb568a5319a3a96cc184ad7 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Tue, 16 Aug 2022 17:43:04 +0200 Subject: [PATCH 41/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Eddú Meléndez Gonzales --- .../java/org/testcontainers/containers/InfluxDBContainer.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 99da0a5a8d3..2eb712f1845 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -162,9 +162,7 @@ public Set getLivenessCheckPortNumbers() { } /** - * Set env variable `INFLUXDB_USER` for InfluxDB 1.x. - *

- * Set env variable `DOCKER_INFLUXDB_INIT_USERNAME` for InfluxDB 2.x. + * Set user for InfluxDB. * * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will * be granted read and write permissions for that database. From 8c8ec4db3d21225e08cae9e1ea986c5cd56dca1f Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Tue, 16 Aug 2022 17:43:15 +0200 Subject: [PATCH 42/51] Update modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Eddú Meléndez Gonzales --- .../java/org/testcontainers/containers/InfluxDBContainer.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 2eb712f1845..dfe5d198eed 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -174,9 +174,7 @@ public InfluxDBContainer withUsername(final String username) { } /** - * Set env variable `INFLUXDB_USER_PASSWORD` for InfluxDB 1.x. - *

- * Set env variable `DOCKER_INFLUXDB_INIT_PASSWORD` for InfluxDB 2.x. + * Set password for InfluxDB. * * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is * generated and printed to standard out. From 8e1442da20b4ada54249008bf0cdc13c4fea857a Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Tue, 16 Aug 2022 18:05:30 +0200 Subject: [PATCH 43/51] update readme --- docs/modules/databases/influxdb.md | 31 ++++++++++++++----- .../InfluxDBContainerWithUserTest.java | 0 2 files changed, 23 insertions(+), 8 deletions(-) delete mode 100644 modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index 504355bad83..4c9c24cd3b4 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -35,9 +35,20 @@ official [InfluxDB docs](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to In the following you will find a snippet to create a InfluxDB client using the java client: ```java -class InfluxDBClient { - public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { - final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder() +public class SomeTest { + + @Rule + public InfluxDBContainer influxDbContainer = new InfluxDBContainer(); + + @Test + public void someTestMethod() { + InfluxDBClient influxDB = getInfluxDBClient(this.influxDbContainer); + // ... + } + + private static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { + final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions + .builder() .url(influxDBContainer.getUrl()) .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) .bucket(influxDBContainer.getBucket()) @@ -73,11 +84,15 @@ The influxDB will be setup with the following data:
In the following you will find a snippet to create a InfluxDB client using the java client: ```java -class InfluxDBClient { - public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { - final InfluxDB influxDB = InfluxDBFactory.connect(influxDBContainer.getUrl(), influxDBContainer.getUsername(), influxDBContainer.getPassword()); - influxDB.setDatabase(influxDBContainer.getDatabase()); - return influxDB; +public class SomeTest { + + @Rule + public InfluxDBContainer influxDbContainer = new InfluxDBContainer(); + + @Test + public void someTestMethod() { + InfluxDB influxDB = influxDbContainer.getNewInfluxDB(); + // ... } } ``` diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerWithUserTest.java deleted file mode 100644 index e69de29bb2d..00000000000 From d99342cd7c8c04b2c5dcc1146122560cd02d0d6d Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Wed, 17 Aug 2022 09:15:46 +0200 Subject: [PATCH 44/51] add snippet --- docs/modules/databases/influxdb.md | 43 +++++------------------------- 1 file changed, 6 insertions(+), 37 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index 4c9c24cd3b4..a14de547b6b 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -34,30 +34,9 @@ official [InfluxDB docs](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to In the following you will find a snippet to create a InfluxDB client using the java client: -```java -public class SomeTest { - - @Rule - public InfluxDBContainer influxDbContainer = new InfluxDBContainer(); - - @Test - public void someTestMethod() { - InfluxDBClient influxDB = getInfluxDBClient(this.influxDbContainer); - // ... - } - - private static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { - final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions - .builder() - .url(influxDBContainer.getUrl()) - .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) - .bucket(influxDBContainer.getBucket()) - .org(influxDBContainer.getOrganization()) - .build(); - return InfluxDBClientFactory.create(influxDBClientOptions); - } -} -``` + +[InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) + !!! hint You can find the latest documentation about the InfluxDB 2.x java client [here](https://github.com/influxdata/influxdb-client-java). @@ -83,19 +62,9 @@ The influxDB will be setup with the following data:
In the following you will find a snippet to create a InfluxDB client using the java client: -```java -public class SomeTest { - - @Rule - public InfluxDBContainer influxDbContainer = new InfluxDBContainer(); - - @Test - public void someTestMethod() { - InfluxDB influxDB = influxDbContainer.getNewInfluxDB(); - // ... - } -} -``` + +[InfluxDBContainer.getNewInfluxDB()](../../../modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java) + !!! hint You can find the latest documentation about the InfluxDB 1.x java client [here](https://github.com/influxdata/influxdb-java). From edfc72c7363e61ba3574e53565c96b59b330d734 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Tue, 23 Aug 2022 16:15:10 +0200 Subject: [PATCH 45/51] add reviews --- docs/modules/databases/influxdb.md | 26 ++-- .../containers/InfluxDBContainer.java | 82 +++++------- .../containers/InfluxDBContainerTest.java | 101 +++++++------- .../containers/InfluxDBContainerV1Test.java | 124 ++++++------------ .../containers/InfluxDBTestUtils.java | 12 +- 5 files changed, 136 insertions(+), 209 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index a14de547b6b..c88c1526425 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -4,20 +4,20 @@ Testcontainers module for InfluxData [InfluxDB](https://www.influxdata.com/produ ## Important note -There are breaking changes in InfluxDB 2.x. For more information refer to the -main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). InfluxDB -official [container registry](https://hub.docker.com/_/influxdb) on docker hub. +There are breaking changes in InfluxDB 2.x. +For more information refer to the main [documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/). +You can find more information about the official InfluxDB image on [Docker Hub](https://hub.docker.com/_/influxdb). ## InfluxDB 2.x usage example -Running influxDb container as a stand-in for InfluxDB in a test: +Running a `InfluxDBContainer` as a stand-in for InfluxDB in a test: [InfluxDBContainerTest](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) -The influxDB will be setup with the following data:
+The InfluxDB instance will be setup with the following data:
| Property | Default Value | |--------------|:-------------:| @@ -28,28 +28,26 @@ The influxDB will be setup with the following data:
| retention | 0 (infinite) | | adminToken | - | -For more details about the InfluxDB setup go to the -official [InfluxDB docs](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials) -. +For more details about the InfluxDB setup, please visit the official [InfluxDB documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials). -In the following you will find a snippet to create a InfluxDB client using the java client: +In the following example you will find a snippet to create an InfluxDB client using the official Java client: [InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) !!! hint - You can find the latest documentation about the InfluxDB 2.x java client [here](https://github.com/influxdata/influxdb-client-java). + You can find the latest documentation about the InfluxDB 2.x Java client [here](https://github.com/influxdata/influxdb-client-java). ## InfluxDB 1.x usage example -Running InfluxDb container as a stand-in for InfluxDB in a test: +Running a `InfluxDBContainer` as a stand-in for InfluxDB in a test: [InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) -The influxDB will be setup with the following data:
+The InfluxDB instance will be setup with the following data:
| Property | Default Value | |---------------|:-------------:| @@ -60,14 +58,14 @@ The influxDB will be setup with the following data:
| adminPassword | password | | database | - | -In the following you will find a snippet to create a InfluxDB client using the java client: +In the following example you will find a snippet to create an InfluxDB client using the official Java client: [InfluxDBContainer.getNewInfluxDB()](../../../modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java) !!! hint - You can find the latest documentation about the InfluxDB 1.x java client [here](https://github.com/influxdata/influxdb-java). + You can find the latest documentation about the InfluxDB 1.x Java client [here](https://github.com/influxdata/influxdb-java). ## Adding this module to your project dependencies diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index dfe5d198eed..0926809914e 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -1,17 +1,15 @@ package org.testcontainers.containers; +import java.util.Collections; +import java.util.Optional; +import java.util.Set; import lombok.Getter; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.containers.wait.strategy.WaitAllStrategy; +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; import org.testcontainers.utility.ComparableVersion; import org.testcontainers.utility.DockerImageName; -import java.util.Collections; -import java.util.Optional; -import java.util.Set; - /** * Testcontainers implementation for InfluxDB. */ @@ -83,15 +81,11 @@ public InfluxDBContainer(final DockerImageName dockerImageName) { dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); this.logger().info("Starting an InfluxDB container using [{}]", dockerImageName); - this.waitStrategy = - new WaitAllStrategy() - .withStrategy( - Wait - .forHttp("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE) - ) - .withStrategy(Wait.forListeningPort()); + + this.waitStrategy = new HttpWaitStrategy() + .forPath("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE); this.isAtLeastMajorVersion2 = new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("2.0.0"); @@ -111,22 +105,10 @@ protected void configure() { } /** - *

- * The InfluxDB 2.x image contains some extra functionality to automatically bootstrap the system. This functionality is - * enabled by setting the DOCKER_INFLUXDB_INIT_MODE environment variable to the value "setup" when running the - * container. Additional environment variables are used to configure the setup logic: - *

    - *
  • DOCKER_INFLUXDB_INIT_USERNAME: The username to set for the system's initial super-user (Required). - *
  • DOCKER_INFLUXDB_INIT_PASSWORD: The password to set for the system's initial super-user (Required). - *
  • DOCKER_INFLUXDB_INIT_ORG: The name to set for the system's initial organization (Required). - *
  • DOCKER_INFLUXDB_INIT_BUCKET: The name to set for the system's initial bucket (Required). - *
  • DOCKER_INFLUXDB_INIT_RETENTION: The duration the system's initial bucket should retain data. If not set, - * the initial bucket will retain data forever. - *
  • DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: The authentication token to associate with the system's initial - * super-user. If not set, a token will be auto-generated by the system. - *
- *
- * @see full documentation + * Sets the InfluxDB 2.x environment variables + * + * @see InfluxDB Dockerhub for full documentation on InfluxDB's + * envrinoment variables */ private void setInfluxDBV2Envs() { this.addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); @@ -158,14 +140,13 @@ private void setInfluxDBV1Envs() { @Override public Set getLivenessCheckPortNumbers() { - return Collections.singleton(getMappedPort(INFLUXDB_PORT)); + return Collections.singleton(this.getMappedPort(INFLUXDB_PORT)); } /** - * Set user for InfluxDB. + * Set user for InfluxDB * - * @param username The name of a user to be created with no privileges. If `INFLUXDB_BUCKET` is set, this user will - * be granted read and write permissions for that database. + * @param username The username to set for the system's initial super-user * @return a reference to this container instance */ public InfluxDBContainer withUsername(final String username) { @@ -174,10 +155,9 @@ public InfluxDBContainer withUsername(final String username) { } /** - * Set password for InfluxDB. + * Set password for InfluxDB * - * @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password is - * generated and printed to standard out. + * @param password The password to set for the system's initial super-user * @return a reference to this container instance */ public InfluxDBContainer withPassword(final String password) { @@ -186,7 +166,7 @@ public InfluxDBContainer withPassword(final String password) { } /** - * Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`. + * Determines if authentication should be enabled or not * * @param authEnabled Enables authentication. * @return a reference to this container instance @@ -197,7 +177,7 @@ public InfluxDBContainer withAuthEnabled(final boolean authEnabled) { } /** - * Set env variable `INFLUXDB_ADMIN_USER`. + * Sets the admin user * * @param admin The name of the admin user to be created. If this is unset, no admin user is created. * @return a reference to this container instance @@ -208,10 +188,10 @@ public InfluxDBContainer withAdmin(final String admin) { } /** - * Set env variable `INFLUXDB_ADMIN_PASSWORD`. + * Sets the admin password * - * @param adminPassword TThe password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a - * random password is generated and printed to standard out. + * @param adminPassword The password for the admin user. If this is unset, a random password is generated and + * printed to standard out. * @return a reference to this container instance */ public InfluxDBContainer withAdminPassword(final String adminPassword) { @@ -220,9 +200,9 @@ public InfluxDBContainer withAdminPassword(final String adminPassword) { } /** - * Set env variable `INFLUXDB_DB`. + * Initializes database with given name * - * @param database Automatically initializes a database with the name of this environment variable. + * @param database name of the database. * @return a reference to this container instance */ public InfluxDBContainer withDatabase(final String database) { @@ -231,7 +211,7 @@ public InfluxDBContainer withDatabase(final String database) { } /** - * Set env variable `DOCKER_INFLUXDB_INIT_ORG`. + * Sets the organization name * * @param organization The organization for the initial setup of influxDB. * @return a reference to this container instance @@ -242,9 +222,9 @@ public InfluxDBContainer withOrganization(final String organization) { } /** - * Set env variable `DOCKER_INFLUXDB_INIT_BUCKET`. + * Initializes bucket with given name * - * @param bucket Automatically initializes a bucket with the name of this environment variable. + * @param bucket name of the bucket. * @return a reference to this container instance */ public InfluxDBContainer withBucket(final String bucket) { @@ -253,9 +233,9 @@ public InfluxDBContainer withBucket(final String bucket) { } /** - * Set env variable `DOCKER_INFLUXDB_INIT_RETENTION`. + * Sets the retention in days * - * @param retention Duration bucket will retain data (0 is infinite, default is 0). + * @param retention days bucket will retain data (0 is infinite, default is 0). * @return a reference to this container instance */ public InfluxDBContainer withRetention(final String retention) { @@ -264,7 +244,7 @@ public InfluxDBContainer withRetention(final String retention) { } /** - * Set env variable `DOCKER_INFLUXDB_INIT_ADMIN_TOKEN`. + * Sets the admin token * * @param adminToken Authentication token to associate with the admin user. * @return a reference to this container instance diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index 93d9899dc48..8956cbc3252 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -1,21 +1,19 @@ package org.testcontainers.containers; +import static org.assertj.core.api.Assertions.assertThat; + import com.influxdb.client.InfluxDBClient; import com.influxdb.client.QueryApi; import com.influxdb.client.WriteApi; import com.influxdb.client.domain.Bucket; +import com.influxdb.client.domain.BucketRetentionRules; import com.influxdb.client.domain.WritePrecision; import com.influxdb.client.write.Point; import com.influxdb.query.FluxRecord; import com.influxdb.query.FluxTable; -import org.jetbrains.annotations.Nullable; -import org.junit.After; -import org.junit.Test; - import java.time.Instant; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; public class InfluxDBContainerTest { @@ -30,43 +28,35 @@ public class InfluxDBContainerTest { private static final String RETENTION = "1w"; private static final String ADMIN_TOKEN = "super-secret-token"; - - @Nullable - private InfluxDBClient influxDBClient = null; - - @After - public void stopInfluxDBClient() { - if (this.influxDBClient != null) { - this.influxDBClient.close(); - this.influxDBClient = null; - } - } + private static final int SECONDS_IN_WEEK = 604800; @Test - public void getUrl() { + public void getInfluxDBClient() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) ) { influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - final String actual = influxDBContainer.getUrl(); - assertThat(actual).isNotNull(); + try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping()).isTrue(); + } } } @Test - public void getInfluxDBClient() { + public void getInfluxDBClientWithAdminToken() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) ) { - influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - - this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); + influxDBContainer.withAdminToken(ADMIN_TOKEN).start(); + assertThat(influxDBContainer.getAdminToken()).isNotEmpty(); - assertThat(this.influxDBClient).isNotNull(); - assertThat(this.influxDBClient.ping()).isTrue(); + try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClientWithToken( + influxDBContainer.getUrl(), influxDBContainer.getAdminToken().get())) { + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping()).isTrue(); + } } } @@ -80,20 +70,21 @@ public void getBucket() { .withPassword(PASSWORD) .withOrganization(ORG) .withBucket(BUCKET) - .withRetention(RETENTION) - .withAdminToken(ADMIN_TOKEN); + .withRetention(RETENTION); influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - - this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); - assertThat(this.influxDBClient).isNotNull(); + try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + final Bucket bucket = influxDBClient.getBucketsApi().findBucketByName(BUCKET); + assertThat(bucket).isNotNull(); - final Bucket bucket = this.influxDBClient.getBucketsApi().findBucketByName(BUCKET); - assertThat(bucket).isNotNull(); - - assertThat(bucket.getName()).isEqualTo(BUCKET); + assertThat(bucket.getName()).isEqualTo(BUCKET); + assertThat(bucket.getRetentionRules()). + hasSize(1) + .first() + .extracting(BucketRetentionRules::getEverySeconds) + .isEqualTo(SECONDS_IN_WEEK); + } } } @@ -106,32 +97,28 @@ public void queryForWriteAndRead() { .withOrganization(ORG) .withBucket(BUCKET) .withRetention(RETENTION) - .withAdminToken(ADMIN_TOKEN) ) { influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - - this.influxDBClient = InfluxDBTestUtils.getInfluxDBClient(influxDBContainer); - assertThat(this.influxDBClient).isNotNull(); + try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + try (final WriteApi writeApi = influxDBClient.makeWriteApi()) { + final Point point = Point + .measurement("temperature") + .addTag("location", "west") + .addField("value", 55.0D) + .time(Instant.now().toEpochMilli(), WritePrecision.MS); - try (final WriteApi writeApi = this.influxDBClient.makeWriteApi()) { - final Point point = Point - .measurement("temperature") - .addTag("location", "west") - .addField("value", 55.0D) - .time(Instant.now().toEpochMilli(), WritePrecision.MS); + writeApi.writePoint(point); + } - writeApi.writePoint(point); - } - - final String flux = String.format("from(bucket:\"%s\") |> range(start: 0)", BUCKET); + final String flux = String.format("from(bucket:\"%s\") |> range(start: 0)", BUCKET); - final QueryApi queryApi = this.influxDBClient.getQueryApi(); + final QueryApi queryApi = influxDBClient.getQueryApi(); - final FluxTable fluxTable = queryApi.query(flux).get(0); - final List records = fluxTable.getRecords(); - assertThat(records).hasSize(1); + final FluxTable fluxTable = queryApi.query(flux).get(0); + final List records = fluxTable.getRecords(); + assertThat(records).hasSize(1); + } } } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 392e8fa0721..2448f750ea4 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -1,18 +1,15 @@ package org.testcontainers.containers; +import static org.assertj.core.api.Assertions.assertThat; +import static org.testcontainers.containers.InfluxDBTestUtils.createInfluxDBWithUrl; + +import java.util.concurrent.TimeUnit; import org.influxdb.InfluxDB; import org.influxdb.dto.Point; import org.influxdb.dto.Query; import org.influxdb.dto.QueryResult; -import org.jetbrains.annotations.Nullable; -import org.junit.After; import org.junit.Test; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import static org.assertj.core.api.Assertions.assertThat; - public class InfluxDBContainerV1Test { private static final String TEST_VERSION = InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE.getVersionPart(); @@ -23,60 +20,37 @@ public class InfluxDBContainerV1Test { private static final String PASSWORD = "new-test-password"; - @Nullable - private InfluxDB influxDBClient = null; - - @After - public void stopInfluxDBClient() { - if (this.influxDBClient != null) { - this.influxDBClient.close(); - this.influxDBClient = null; - } - } - @Test - public void getUrl() { + public void createInfluxDBOnlyWithUrlAndCorrectVersion() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) ) { // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); final String actual = influxDBContainer.getUrl(); - assertThat(actual).isNotNull(); - } - } - - @Test - public void getNewInfluxDB() { - try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) - ) { - // Start the container. This step might take some time... - influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - - final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB(); - - assertThat(influxDBClient).isNotNull(); - assertThat(influxDBClient.ping().isGood()).isTrue(); + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(actual)) { + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping().isGood()).isTrue(); + assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); + } } } @Test - public void getLivenessCheckPort() { + public void getNewInfluxDBWithCorrectVersion() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) ) { // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - final Set actual = influxDBContainer.getLivenessCheckPortNumbers(); - - assertThat(actual).isNotNull(); + try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping().isGood()).isTrue(); + assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); + } } } @@ -89,31 +63,10 @@ public void describeDatabases() { // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - - this.influxDBClient = influxDBContainer.getNewInfluxDB(); - assertThat(this.influxDBClient).isNotNull(); - assertThat(this.influxDBClient.describeDatabases()).contains(DATABASE); - } - } - - @Test - public void checkVersion() { - try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) - ) { - influxDBContainer.withDatabase(DATABASE).withUsername(USER).withPassword(PASSWORD); - - // Start the container. This step might take some time... - influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - - this.influxDBClient = influxDBContainer.getNewInfluxDB(); - - assertThat(this.influxDBClient).isNotNull(); - assertThat(this.influxDBClient.ping().isGood()).isTrue(); - assertThat(this.influxDBClient.version()).isEqualTo(TEST_VERSION); + try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { + assertThat(influxDBClient.describeDatabases()).contains(DATABASE); + } } } @@ -126,26 +79,25 @@ public void queryForWriteAndRead() { // Start the container. This step might take some time... influxDBContainer.start(); - assertThat(influxDBContainer.isRunning()).isTrue(); - - this.influxDBClient = influxDBContainer.getNewInfluxDB(); - - final Point point = Point - .measurement("cpu") - .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) - .addField("idle", 90L) - .addField("user", 9L) - .addField("system", 1L) - .build(); - this.influxDBClient.write(point); - - final Query query = new Query("SELECT idle FROM cpu", DATABASE); - final QueryResult actual = this.influxDBClient.query(query); - - assertThat(actual).isNotNull(); - assertThat(actual.getError()).isNull(); - assertThat(actual.getResults()).isNotNull(); - assertThat(actual.getResults()).hasSize(1); + + try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { + final Point point = Point + .measurement("cpu") + .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + .addField("idle", 90L) + .addField("user", 9L) + .addField("system", 1L) + .build(); + influxDBClient.write(point); + + final Query query = new Query("SELECT idle FROM cpu", DATABASE); + final QueryResult actual = influxDBClient.query(query); + + assertThat(actual).isNotNull(); + assertThat(actual.getError()).isNull(); + assertThat(actual.getResults()).isNotNull(); + assertThat(actual.getResults()).hasSize(1); + } } } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java index 852ec4d2036..4ba445713de 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java @@ -3,6 +3,8 @@ import com.influxdb.client.InfluxDBClient; import com.influxdb.client.InfluxDBClientFactory; import com.influxdb.client.InfluxDBClientOptions; +import org.influxdb.InfluxDB; +import org.influxdb.InfluxDBFactory; import org.testcontainers.utility.DockerImageName; public final class InfluxDBTestUtils { @@ -13,7 +15,7 @@ public final class InfluxDBTestUtils { private InfluxDBTestUtils() {} - public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBContainer) { + public static InfluxDBClient createInfluxDBClient(final InfluxDBContainer influxDBContainer) { final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions .builder() .url(influxDBContainer.getUrl()) @@ -23,4 +25,12 @@ public static InfluxDBClient getInfluxDBClient(final InfluxDBContainer influxDBC .build(); return InfluxDBClientFactory.create(influxDBClientOptions); } + + public static InfluxDBClient createInfluxDBClientWithToken(final String url, final String token) { + return InfluxDBClientFactory.create(url, token.toCharArray()); + } + + public static InfluxDB createInfluxDBWithUrl(final String url) { + return InfluxDBFactory.connect(url); + } } From a1d1245ddae95de26bfc6baad7036793f5f7e4b5 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Sat, 15 Oct 2022 14:54:50 +0200 Subject: [PATCH 46/51] Add reviews --- docs/modules/databases/influxdb.md | 23 ++++++++++++--- .../containers/InfluxDBContainer.java | 26 ++++++++--------- .../containers/InfluxDBContainerTest.java | 29 +++++++++++++------ .../containers/InfluxDBContainerV1Test.java | 26 ++++++++++++----- .../containers/InfluxDBTestUtils.java | 2 +- 5 files changed, 71 insertions(+), 35 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index c88c1526425..0999b914ef0 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -30,10 +30,19 @@ The InfluxDB instance will be setup with the following data:
For more details about the InfluxDB setup, please visit the official [InfluxDB documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials). -In the following example you will find a snippet to create an InfluxDB client using the official Java client: +In the following example you will find a snippet to create an InfluxDB client using the official Java client with default values: + +[InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:constructorWithDefaultVariables + + +Creating an instance with InfluxDB admin token; + +[InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:constructorWithAdminToken + +Or creating an instance with custom username, password, bucket, organization, and retention time: -[InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) +[InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:constructorWithCustomVariables !!! hint @@ -41,10 +50,16 @@ In the following example you will find a snippet to create an InfluxDB client us ## InfluxDB 1.x usage example -Running a `InfluxDBContainer` as a stand-in for InfluxDB in a test: +Running a `InfluxDBContainer` as a stand-in for InfluxDB in a test with default env variables: + + +[InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithDefaultVariables + + +or run it with a custom username, password, and database name: -[InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) +[InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithUserPassword The InfluxDB instance will be setup with the following data:
diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 0926809914e..e5823556d04 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -13,7 +13,7 @@ /** * Testcontainers implementation for InfluxDB. */ -public class InfluxDBContainer extends GenericContainer { +public class InfluxDBContainer> extends GenericContainer { public static final Integer INFLUXDB_PORT = 8086; @@ -149,7 +149,7 @@ public Set getLivenessCheckPortNumbers() { * @param username The username to set for the system's initial super-user * @return a reference to this container instance */ - public InfluxDBContainer withUsername(final String username) { + public InfluxDBContainer withUsername(final String username) { this.username = username; return this; } @@ -160,7 +160,7 @@ public InfluxDBContainer withUsername(final String username) { * @param password The password to set for the system's initial super-user * @return a reference to this container instance */ - public InfluxDBContainer withPassword(final String password) { + public InfluxDBContainer withPassword(final String password) { this.password = password; return this; } @@ -171,7 +171,7 @@ public InfluxDBContainer withPassword(final String password) { * @param authEnabled Enables authentication. * @return a reference to this container instance */ - public InfluxDBContainer withAuthEnabled(final boolean authEnabled) { + public InfluxDBContainer withAuthEnabled(final boolean authEnabled) { this.authEnabled = authEnabled; return this.self(); } @@ -182,7 +182,7 @@ public InfluxDBContainer withAuthEnabled(final boolean authEnabled) { * @param admin The name of the admin user to be created. If this is unset, no admin user is created. * @return a reference to this container instance */ - public InfluxDBContainer withAdmin(final String admin) { + public InfluxDBContainer withAdmin(final String admin) { this.admin = admin; return this.self(); } @@ -194,7 +194,7 @@ public InfluxDBContainer withAdmin(final String admin) { * printed to standard out. * @return a reference to this container instance */ - public InfluxDBContainer withAdminPassword(final String adminPassword) { + public InfluxDBContainer withAdminPassword(final String adminPassword) { this.adminPassword = adminPassword; return this.self(); } @@ -205,7 +205,7 @@ public InfluxDBContainer withAdminPassword(final String adminPassword) { * @param database name of the database. * @return a reference to this container instance */ - public InfluxDBContainer withDatabase(final String database) { + public InfluxDBContainer withDatabase(final String database) { this.database = database; return this.self(); } @@ -216,7 +216,7 @@ public InfluxDBContainer withDatabase(final String database) { * @param organization The organization for the initial setup of influxDB. * @return a reference to this container instance */ - public InfluxDBContainer withOrganization(final String organization) { + public InfluxDBContainer withOrganization(final String organization) { this.organization = organization; return this; } @@ -227,7 +227,7 @@ public InfluxDBContainer withOrganization(final String organization) { * @param bucket name of the bucket. * @return a reference to this container instance */ - public InfluxDBContainer withBucket(final String bucket) { + public InfluxDBContainer withBucket(final String bucket) { this.bucket = bucket; return this; } @@ -238,7 +238,7 @@ public InfluxDBContainer withBucket(final String bucket) { * @param retention days bucket will retain data (0 is infinite, default is 0). * @return a reference to this container instance */ - public InfluxDBContainer withRetention(final String retention) { + public InfluxDBContainer withRetention(final String retention) { this.retention = Optional.of(retention); return this; } @@ -249,7 +249,7 @@ public InfluxDBContainer withRetention(final String retention) { * @param adminToken Authentication token to associate with the admin user. * @return a reference to this container instance */ - public InfluxDBContainer withAdminToken(final String adminToken) { + public InfluxDBContainer withAdminToken(final String adminToken) { this.adminToken = Optional.of(adminToken); return this; } @@ -258,7 +258,7 @@ public InfluxDBContainer withAdminToken(final String adminToken) { * @return a url to InfluxDB */ public String getUrl() { - return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT); + return "http://" + getHost() + ":" + getMappedPort(INFLUXDB_PORT); } /** @@ -267,7 +267,7 @@ public String getUrl() { */ @Deprecated public InfluxDB getNewInfluxDB() { - final InfluxDB influxDB = InfluxDBFactory.connect(this.getUrl(), this.username, this.password); + final InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), this.username, this.password); influxDB.setDatabase(this.database); return influxDB; } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index 8956cbc3252..450446a5a87 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -13,6 +13,7 @@ import com.influxdb.query.FluxTable; import java.time.Instant; import java.util.List; +import java.util.Optional; import org.junit.Test; public class InfluxDBContainerTest { @@ -33,7 +34,10 @@ public class InfluxDBContainerTest { @Test public void getInfluxDBClient() { try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + // constructorWithDefaultVariables { + final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + // } ) { influxDBContainer.start(); @@ -47,13 +51,17 @@ public void getInfluxDBClient() { @Test public void getInfluxDBClientWithAdminToken() { try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + // constructorWithAdminToken { + final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE).withAdminToken(ADMIN_TOKEN) + // } ) { - influxDBContainer.withAdminToken(ADMIN_TOKEN).start(); - assertThat(influxDBContainer.getAdminToken()).isNotEmpty(); + influxDBContainer.start(); + final Optional adminToken = influxDBContainer.getAdminToken(); + assertThat(adminToken).isNotEmpty(); try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClientWithToken( - influxDBContainer.getUrl(), influxDBContainer.getAdminToken().get())) { + influxDBContainer.getUrl(), adminToken.get())) { assertThat(influxDBClient).isNotNull(); assertThat(influxDBClient.ping()).isTrue(); } @@ -63,14 +71,16 @@ public void getInfluxDBClientWithAdminToken() { @Test public void getBucket() { try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) - ) { - influxDBContainer + // constructorWithCustomVariables { + final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) .withBucket(BUCKET) .withRetention(RETENTION); + // } + ) { influxDBContainer.start(); @@ -91,7 +101,8 @@ public void getBucket() { @Test public void queryForWriteAndRead() { try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 2448f750ea4..ca7f559d0ae 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -23,7 +23,10 @@ public class InfluxDBContainerV1Test { @Test public void createInfluxDBOnlyWithUrlAndCorrectVersion() { try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + // constructorWithDefaultVariables { + final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + // } ) { // Start the container. This step might take some time... influxDBContainer.start(); @@ -41,7 +44,8 @@ public void createInfluxDBOnlyWithUrlAndCorrectVersion() { @Test public void getNewInfluxDBWithCorrectVersion() { try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) ) { // Start the container. This step might take some time... influxDBContainer.start(); @@ -57,10 +61,14 @@ public void getNewInfluxDBWithCorrectVersion() { @Test public void describeDatabases() { try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + // constructorWithUserPassword { + final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + .withDatabase(DATABASE) + .withUsername(USER) + .withPassword(PASSWORD) + // } ) { - influxDBContainer.withDatabase(DATABASE).withUsername(USER).withPassword(PASSWORD); - // Start the container. This step might take some time... influxDBContainer.start(); @@ -73,10 +81,12 @@ public void describeDatabases() { @Test public void queryForWriteAndRead() { try ( - final InfluxDBContainer influxDBContainer = new InfluxDBContainer(InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + .withDatabase(DATABASE) + .withUsername(USER) + .withPassword(PASSWORD) ) { - influxDBContainer.withDatabase(DATABASE).withUsername(USER).withPassword(PASSWORD); - // Start the container. This step might take some time... influxDBContainer.start(); diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java index 4ba445713de..920bf25f9e3 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java @@ -15,7 +15,7 @@ public final class InfluxDBTestUtils { private InfluxDBTestUtils() {} - public static InfluxDBClient createInfluxDBClient(final InfluxDBContainer influxDBContainer) { + public static InfluxDBClient createInfluxDBClient(final InfluxDBContainer influxDBContainer) { final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions .builder() .url(influxDBContainer.getUrl()) From 8f6f2a5bfd69df85175cef49bb07136352ece6f6 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Tue, 18 Oct 2022 09:00:47 +0200 Subject: [PATCH 47/51] Fix docs --- docs/modules/databases/influxdb.md | 21 ++++++++++--------- .../containers/InfluxDBContainer.java | 2 ++ .../containers/InfluxDBTestUtils.java | 2 ++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index 0999b914ef0..df021bfde62 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -13,7 +13,7 @@ You can find more information about the official InfluxDB image on [Docker Hub]( Running a `InfluxDBContainer` as a stand-in for InfluxDB in a test: -[InfluxDBContainerTest](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) +[Create an InfluxDB container](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) inside_block:constructorWithDefaultVariables @@ -30,19 +30,20 @@ The InfluxDB instance will be setup with the following data:
For more details about the InfluxDB setup, please visit the official [InfluxDB documentation](https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials). -In the following example you will find a snippet to create an InfluxDB client using the official Java client with default values: +It is possible to overwrite the default property values. Create a container with InfluxDB admin token: -[InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:constructorWithDefaultVariables +[Create an InfluxDB container with admin token](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) inside_block:constructorWithAdminToken -Creating an instance with InfluxDB admin token; +Or create a container with custom username, password, bucket, organization, and retention time: -[InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:constructorWithAdminToken +[Create an InfluxDB container with custom settings](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) inside_block:constructorWithCustomVariables -Or creating an instance with custom username, password, bucket, organization, and retention time: +The following code snippet shows how you can create an InfluxDB Java client: + -[InfluxDBTestUtils.getInfluxDBClient()](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:constructorWithCustomVariables +[Create an InfluxDB Java client](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:createInfluxDB2Client !!! hint @@ -53,13 +54,13 @@ Or creating an instance with custom username, password, bucket, organization, an Running a `InfluxDBContainer` as a stand-in for InfluxDB in a test with default env variables: -[InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithDefaultVariables +[Create an InfluxDB container](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithDefaultVariables or run it with a custom username, password, and database name: -[InfluxDBContainerV1Test](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithUserPassword +[Create an InfluxDB container with custom settings](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithUserPassword The InfluxDB instance will be setup with the following data:
@@ -76,7 +77,7 @@ The InfluxDB instance will be setup with the following data:
In the following example you will find a snippet to create an InfluxDB client using the official Java client: -[InfluxDBContainer.getNewInfluxDB()](../../../modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java) +[Create an InfluxDB Java client](../../../modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java) inside_block:createInfluxDBClient !!! hint diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index e5823556d04..94fd694c3f0 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -266,9 +266,11 @@ public String getUrl() { * @deprecated Use the new InfluxDB client library. */ @Deprecated + // createInfluxDBClient { public InfluxDB getNewInfluxDB() { final InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), this.username, this.password); influxDB.setDatabase(this.database); return influxDB; } + // } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java index 920bf25f9e3..0cdec9a95d4 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java @@ -15,6 +15,7 @@ public final class InfluxDBTestUtils { private InfluxDBTestUtils() {} + // createInfluxDB2Client { public static InfluxDBClient createInfluxDBClient(final InfluxDBContainer influxDBContainer) { final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions .builder() @@ -25,6 +26,7 @@ public static InfluxDBClient createInfluxDBClient(final InfluxDBContainer inf .build(); return InfluxDBClientFactory.create(influxDBClientOptions); } + // } public static InfluxDBClient createInfluxDBClientWithToken(final String url, final String token) { return InfluxDBClientFactory.create(url, token.toCharArray()); From aa1e2ca782beada2fe02b4c4978e775de26ee5e1 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Tue, 18 Oct 2022 09:05:48 +0200 Subject: [PATCH 48/51] Fix docs --- docs/modules/databases/influxdb.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index df021bfde62..ad71c07e8d5 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -57,12 +57,6 @@ Running a `InfluxDBContainer` as a stand-in for InfluxDB in a test with default [Create an InfluxDB container](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithDefaultVariables -or run it with a custom username, password, and database name: - - -[Create an InfluxDB container with custom settings](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithUserPassword - - The InfluxDB instance will be setup with the following data:
| Property | Default Value | @@ -74,6 +68,12 @@ The InfluxDB instance will be setup with the following data:
| adminPassword | password | | database | - | +It is possible to overwrite the default values. +For instance, creating an InfluxDB container with a custom username, password, and database name: + +[Create an InfluxDB container with custom settings](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:constructorWithUserPassword + + In the following example you will find a snippet to create an InfluxDB client using the official Java client: From 1d817fe7b7a577a41c7c3c853184b543b2e4e179 Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 20 Oct 2022 15:18:56 +0200 Subject: [PATCH 49/51] add reviews --- .../testcontainers/containers/InfluxDBContainerTest.java | 8 +++++--- .../containers/InfluxDBContainerV1Test.java | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index 450446a5a87..ecaaf9282ac 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Optional; import org.junit.Test; +import org.testcontainers.utility.DockerImageName; public class InfluxDBContainerTest { @@ -29,6 +30,7 @@ public class InfluxDBContainerTest { private static final String RETENTION = "1w"; private static final String ADMIN_TOKEN = "super-secret-token"; + private static final int SECONDS_IN_WEEK = 604800; @Test @@ -36,7 +38,7 @@ public void getInfluxDBClient() { try ( // constructorWithDefaultVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + DockerImageName.parse("influxdb:2.0.7")) // } ) { influxDBContainer.start(); @@ -53,7 +55,7 @@ public void getInfluxDBClientWithAdminToken() { try ( // constructorWithAdminToken { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE).withAdminToken(ADMIN_TOKEN) + DockerImageName.parse("influxdb:2.0.7")).withAdminToken(ADMIN_TOKEN) // } ) { influxDBContainer.start(); @@ -73,7 +75,7 @@ public void getBucket() { try ( // constructorWithCustomVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + DockerImageName.parse("influxdb:2.0.7")) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index ca7f559d0ae..ad004550111 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -9,6 +9,7 @@ import org.influxdb.dto.Query; import org.influxdb.dto.QueryResult; import org.junit.Test; +import org.testcontainers.utility.DockerImageName; public class InfluxDBContainerV1Test { @@ -25,7 +26,7 @@ public void createInfluxDBOnlyWithUrlAndCorrectVersion() { try ( // constructorWithDefaultVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + DockerImageName.parse("influxdb:1.4.3")) // } ) { // Start the container. This step might take some time... @@ -63,7 +64,7 @@ public void describeDatabases() { try ( // constructorWithUserPassword { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + DockerImageName.parse("influxdb:1.4.3")) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD) From adab8110da8689f34362e48e0edc5ebf80afd82e Mon Sep 17 00:00:00 2001 From: Ramin Gharib Date: Thu, 20 Oct 2022 15:21:41 +0200 Subject: [PATCH 50/51] add checkstyle --- .../containers/InfluxDBContainerTest.java | 20 ++++++------- .../containers/InfluxDBContainerV1Test.java | 28 +++++++++---------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index ecaaf9282ac..d72aa15385e 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -1,6 +1,5 @@ package org.testcontainers.containers; -import static org.assertj.core.api.Assertions.assertThat; import com.influxdb.client.InfluxDBClient; import com.influxdb.client.QueryApi; @@ -14,6 +13,7 @@ import java.time.Instant; import java.util.List; import java.util.Optional; +import org.assertj.core.api.Assertions; import org.junit.Test; import org.testcontainers.utility.DockerImageName; @@ -44,8 +44,8 @@ public void getInfluxDBClient() { influxDBContainer.start(); try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { - assertThat(influxDBClient).isNotNull(); - assertThat(influxDBClient.ping()).isTrue(); + Assertions.assertThat(influxDBClient).isNotNull(); + Assertions.assertThat(influxDBClient.ping()).isTrue(); } } } @@ -60,12 +60,12 @@ public void getInfluxDBClientWithAdminToken() { ) { influxDBContainer.start(); final Optional adminToken = influxDBContainer.getAdminToken(); - assertThat(adminToken).isNotEmpty(); + Assertions.assertThat(adminToken).isNotEmpty(); try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClientWithToken( influxDBContainer.getUrl(), adminToken.get())) { - assertThat(influxDBClient).isNotNull(); - assertThat(influxDBClient.ping()).isTrue(); + Assertions.assertThat(influxDBClient).isNotNull(); + Assertions.assertThat(influxDBClient.ping()).isTrue(); } } } @@ -88,10 +88,10 @@ public void getBucket() { try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { final Bucket bucket = influxDBClient.getBucketsApi().findBucketByName(BUCKET); - assertThat(bucket).isNotNull(); + Assertions.assertThat(bucket).isNotNull(); - assertThat(bucket.getName()).isEqualTo(BUCKET); - assertThat(bucket.getRetentionRules()). + Assertions.assertThat(bucket.getName()).isEqualTo(BUCKET); + Assertions.assertThat(bucket.getRetentionRules()). hasSize(1) .first() .extracting(BucketRetentionRules::getEverySeconds) @@ -130,7 +130,7 @@ public void queryForWriteAndRead() { final FluxTable fluxTable = queryApi.query(flux).get(0); final List records = fluxTable.getRecords(); - assertThat(records).hasSize(1); + Assertions.assertThat(records).hasSize(1); } } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index ad004550111..4b981ac99a8 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -1,9 +1,7 @@ package org.testcontainers.containers; -import static org.assertj.core.api.Assertions.assertThat; -import static org.testcontainers.containers.InfluxDBTestUtils.createInfluxDBWithUrl; - import java.util.concurrent.TimeUnit; +import org.assertj.core.api.Assertions; import org.influxdb.InfluxDB; import org.influxdb.dto.Point; import org.influxdb.dto.Query; @@ -34,10 +32,10 @@ public void createInfluxDBOnlyWithUrlAndCorrectVersion() { final String actual = influxDBContainer.getUrl(); - try (final InfluxDB influxDBClient = createInfluxDBWithUrl(actual)) { - assertThat(influxDBClient).isNotNull(); - assertThat(influxDBClient.ping().isGood()).isTrue(); - assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); + try (final InfluxDB influxDBClient = InfluxDBTestUtils.createInfluxDBWithUrl(actual)) { + Assertions.assertThat(influxDBClient).isNotNull(); + Assertions.assertThat(influxDBClient.ping().isGood()).isTrue(); + Assertions.assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); } } } @@ -52,9 +50,9 @@ public void getNewInfluxDBWithCorrectVersion() { influxDBContainer.start(); try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { - assertThat(influxDBClient).isNotNull(); - assertThat(influxDBClient.ping().isGood()).isTrue(); - assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); + Assertions.assertThat(influxDBClient).isNotNull(); + Assertions.assertThat(influxDBClient.ping().isGood()).isTrue(); + Assertions.assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); } } } @@ -74,7 +72,7 @@ public void describeDatabases() { influxDBContainer.start(); try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { - assertThat(influxDBClient.describeDatabases()).contains(DATABASE); + Assertions.assertThat(influxDBClient.describeDatabases()).contains(DATABASE); } } } @@ -104,10 +102,10 @@ public void queryForWriteAndRead() { final Query query = new Query("SELECT idle FROM cpu", DATABASE); final QueryResult actual = influxDBClient.query(query); - assertThat(actual).isNotNull(); - assertThat(actual.getError()).isNull(); - assertThat(actual.getResults()).isNotNull(); - assertThat(actual.getResults()).hasSize(1); + Assertions.assertThat(actual).isNotNull(); + Assertions.assertThat(actual.getError()).isNull(); + Assertions.assertThat(actual.getResults()).isNotNull(); + Assertions.assertThat(actual.getResults()).hasSize(1); } } } From 627a01cbe48988200aae5b0126616a47b5f48af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 15 Nov 2022 09:00:55 -0800 Subject: [PATCH 51/51] Polish --- docs/modules/databases/influxdb.md | 4 +- .../containers/InfluxDBContainer.java | 59 +++++++++-------- .../containers/InfluxDBContainerTest.java | 61 +++++++++++++----- .../containers/InfluxDBContainerV1Test.java | 63 ++++++++++++------- .../containers/InfluxDBTestUtils.java | 28 --------- 5 files changed, 116 insertions(+), 99 deletions(-) diff --git a/docs/modules/databases/influxdb.md b/docs/modules/databases/influxdb.md index ad71c07e8d5..8055f6aed57 100644 --- a/docs/modules/databases/influxdb.md +++ b/docs/modules/databases/influxdb.md @@ -43,7 +43,7 @@ Or create a container with custom username, password, bucket, organization, and The following code snippet shows how you can create an InfluxDB Java client: -[Create an InfluxDB Java client](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java) inside_block:createInfluxDB2Client +[Create an InfluxDB Java client](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java) inside_block:createInfluxDBClient !!! hint @@ -77,7 +77,7 @@ For instance, creating an InfluxDB container with a custom username, password, a In the following example you will find a snippet to create an InfluxDB client using the official Java client: -[Create an InfluxDB Java client](../../../modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java) inside_block:createInfluxDBClient +[Create an InfluxDB Java client](../../../modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java) inside_block:createInfluxDBClient !!! hint diff --git a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java index 94fd694c3f0..2af4ed1254d 100644 --- a/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java +++ b/modules/influxdb/src/main/java/org/testcontainers/containers/InfluxDBContainer.java @@ -1,8 +1,5 @@ package org.testcontainers.containers; -import java.util.Collections; -import java.util.Optional; -import java.util.Set; import lombok.Getter; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; @@ -10,6 +7,10 @@ import org.testcontainers.utility.ComparableVersion; import org.testcontainers.utility.DockerImageName; +import java.util.Collections; +import java.util.Optional; +import java.util.Set; + /** * Testcontainers implementation for InfluxDB. */ @@ -41,6 +42,7 @@ public class InfluxDBContainer> extends Gen private String adminPassword = "password"; + @Getter private String database; /** @@ -80,16 +82,15 @@ public InfluxDBContainer(final DockerImageName dockerImageName) { super(dockerImageName); dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); - this.logger().info("Starting an InfluxDB container using [{}]", dockerImageName); - - this.waitStrategy = new HttpWaitStrategy() - .forPath("/ping") - .withBasicCredentials(this.username, this.password) - .forStatusCode(NO_CONTENT_STATUS_CODE); + this.waitStrategy = + new HttpWaitStrategy() + .forPath("/ping") + .withBasicCredentials(this.username, this.password) + .forStatusCode(NO_CONTENT_STATUS_CODE); this.isAtLeastMajorVersion2 = new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("2.0.0"); - this.addExposedPort(INFLUXDB_PORT); + addExposedPort(INFLUXDB_PORT); } /** @@ -98,9 +99,9 @@ public InfluxDBContainer(final DockerImageName dockerImageName) { @Override protected void configure() { if (this.isAtLeastMajorVersion2) { - this.setInfluxDBV2Envs(); + configureInfluxDBV2(); } else { - this.setInfluxDBV1Envs(); + configureInfluxDBV1(); } } @@ -110,37 +111,37 @@ protected void configure() { * @see InfluxDB Dockerhub for full documentation on InfluxDB's * envrinoment variables */ - private void setInfluxDBV2Envs() { - this.addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); + private void configureInfluxDBV2() { + addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); - this.addEnv("DOCKER_INFLUXDB_INIT_USERNAME", this.username); - this.addEnv("DOCKER_INFLUXDB_INIT_PASSWORD", this.password); + addEnv("DOCKER_INFLUXDB_INIT_USERNAME", this.username); + addEnv("DOCKER_INFLUXDB_INIT_PASSWORD", this.password); - this.addEnv("DOCKER_INFLUXDB_INIT_ORG", this.organization); - this.addEnv("DOCKER_INFLUXDB_INIT_BUCKET", this.bucket); + addEnv("DOCKER_INFLUXDB_INIT_ORG", this.organization); + addEnv("DOCKER_INFLUXDB_INIT_BUCKET", this.bucket); - this.retention.ifPresent(ret -> this.addEnv("DOCKER_INFLUXDB_INIT_RETENTION", ret)); - this.adminToken.ifPresent(token -> this.addEnv("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", token)); + this.retention.ifPresent(ret -> addEnv("DOCKER_INFLUXDB_INIT_RETENTION", ret)); + this.adminToken.ifPresent(token -> addEnv("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", token)); } /** * Sets the InfluxDB 1.x environment variables */ - private void setInfluxDBV1Envs() { - this.addEnv("INFLUXDB_USER", this.username); - this.addEnv("INFLUXDB_USER_PASSWORD", this.password); + private void configureInfluxDBV1() { + addEnv("INFLUXDB_USER", this.username); + addEnv("INFLUXDB_USER_PASSWORD", this.password); - this.addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled)); + addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled)); - this.addEnv("INFLUXDB_ADMIN_USER", this.admin); - this.addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword); + addEnv("INFLUXDB_ADMIN_USER", this.admin); + addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword); - this.addEnv("INFLUXDB_DB", this.database); + addEnv("INFLUXDB_DB", this.database); } @Override public Set getLivenessCheckPortNumbers() { - return Collections.singleton(this.getMappedPort(INFLUXDB_PORT)); + return Collections.singleton(getMappedPort(INFLUXDB_PORT)); } /** @@ -266,11 +267,9 @@ public String getUrl() { * @deprecated Use the new InfluxDB client library. */ @Deprecated - // createInfluxDBClient { public InfluxDB getNewInfluxDB() { final InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), this.username, this.password); influxDB.setDatabase(this.database); return influxDB; } - // } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java index d72aa15385e..fc80e41ff09 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerTest.java @@ -1,7 +1,8 @@ package org.testcontainers.containers; - import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientFactory; +import com.influxdb.client.InfluxDBClientOptions; import com.influxdb.client.QueryApi; import com.influxdb.client.WriteApi; import com.influxdb.client.domain.Bucket; @@ -10,13 +11,14 @@ import com.influxdb.client.write.Point; import com.influxdb.query.FluxRecord; import com.influxdb.query.FluxTable; -import java.time.Instant; -import java.util.List; -import java.util.Optional; import org.assertj.core.api.Assertions; import org.junit.Test; import org.testcontainers.utility.DockerImageName; +import java.time.Instant; +import java.util.List; +import java.util.Optional; + public class InfluxDBContainerTest { private static final String USERNAME = "new-test-user"; @@ -38,12 +40,13 @@ public void getInfluxDBClient() { try ( // constructorWithDefaultVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:2.0.7")) + DockerImageName.parse("influxdb:2.0.7") + ) // } ) { influxDBContainer.start(); - try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + try (final InfluxDBClient influxDBClient = createClient(influxDBContainer)) { Assertions.assertThat(influxDBClient).isNotNull(); Assertions.assertThat(influxDBClient.ping()).isTrue(); } @@ -55,15 +58,21 @@ public void getInfluxDBClientWithAdminToken() { try ( // constructorWithAdminToken { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:2.0.7")).withAdminToken(ADMIN_TOKEN) + DockerImageName.parse("influxdb:2.0.7") + ) + .withAdminToken(ADMIN_TOKEN) // } ) { influxDBContainer.start(); final Optional adminToken = influxDBContainer.getAdminToken(); Assertions.assertThat(adminToken).isNotEmpty(); - try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClientWithToken( - influxDBContainer.getUrl(), adminToken.get())) { + try ( + final InfluxDBClient influxDBClient = createClientWithToken( + influxDBContainer.getUrl(), + adminToken.get() + ) + ) { Assertions.assertThat(influxDBClient).isNotNull(); Assertions.assertThat(influxDBClient.ping()).isTrue(); } @@ -75,7 +84,8 @@ public void getBucket() { try ( // constructorWithCustomVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:2.0.7")) + DockerImageName.parse("influxdb:2.0.7") + ) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) @@ -83,16 +93,16 @@ public void getBucket() { .withRetention(RETENTION); // } ) { - influxDBContainer.start(); - try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + try (final InfluxDBClient influxDBClient = createClient(influxDBContainer)) { final Bucket bucket = influxDBClient.getBucketsApi().findBucketByName(BUCKET); Assertions.assertThat(bucket).isNotNull(); Assertions.assertThat(bucket.getName()).isEqualTo(BUCKET); - Assertions.assertThat(bucket.getRetentionRules()). - hasSize(1) + Assertions + .assertThat(bucket.getRetentionRules()) + .hasSize(1) .first() .extracting(BucketRetentionRules::getEverySeconds) .isEqualTo(SECONDS_IN_WEEK); @@ -104,7 +114,8 @@ public void getBucket() { public void queryForWriteAndRead() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE) + InfluxDBTestUtils.INFLUXDB_V2_TEST_IMAGE + ) .withUsername(USERNAME) .withPassword(PASSWORD) .withOrganization(ORG) @@ -113,7 +124,7 @@ public void queryForWriteAndRead() { ) { influxDBContainer.start(); - try (final InfluxDBClient influxDBClient = InfluxDBTestUtils.createInfluxDBClient(influxDBContainer)) { + try (final InfluxDBClient influxDBClient = createClient(influxDBContainer)) { try (final WriteApi writeApi = influxDBClient.makeWriteApi()) { final Point point = Point .measurement("temperature") @@ -134,4 +145,22 @@ public void queryForWriteAndRead() { } } } + + // createInfluxDBClient { + public static InfluxDBClient createClient(final InfluxDBContainer influxDBContainer) { + final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions + .builder() + .url(influxDBContainer.getUrl()) + .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) + .bucket(influxDBContainer.getBucket()) + .org(influxDBContainer.getOrganization()) + .build(); + return InfluxDBClientFactory.create(influxDBClientOptions); + } + + // } + + public static InfluxDBClient createClientWithToken(final String url, final String token) { + return InfluxDBClientFactory.create(url, token.toCharArray()); + } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java index 4b981ac99a8..c9e292c5675 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBContainerV1Test.java @@ -1,14 +1,17 @@ package org.testcontainers.containers; -import java.util.concurrent.TimeUnit; -import org.assertj.core.api.Assertions; import org.influxdb.InfluxDB; +import org.influxdb.InfluxDBFactory; import org.influxdb.dto.Point; import org.influxdb.dto.Query; import org.influxdb.dto.QueryResult; import org.junit.Test; import org.testcontainers.utility.DockerImageName; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + public class InfluxDBContainerV1Test { private static final String TEST_VERSION = InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE.getVersionPart(); @@ -24,18 +27,17 @@ public void createInfluxDBOnlyWithUrlAndCorrectVersion() { try ( // constructorWithDefaultVariables { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:1.4.3")) + DockerImageName.parse("influxdb:1.4.3") + ) // } ) { // Start the container. This step might take some time... influxDBContainer.start(); - final String actual = influxDBContainer.getUrl(); - - try (final InfluxDB influxDBClient = InfluxDBTestUtils.createInfluxDBWithUrl(actual)) { - Assertions.assertThat(influxDBClient).isNotNull(); - Assertions.assertThat(influxDBClient.ping().isGood()).isTrue(); - Assertions.assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(influxDBContainer)) { + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping().isGood()).isTrue(); + assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); } } } @@ -44,15 +46,16 @@ public void createInfluxDBOnlyWithUrlAndCorrectVersion() { public void getNewInfluxDBWithCorrectVersion() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE + ) ) { // Start the container. This step might take some time... influxDBContainer.start(); - try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { - Assertions.assertThat(influxDBClient).isNotNull(); - Assertions.assertThat(influxDBClient.ping().isGood()).isTrue(); - Assertions.assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(influxDBContainer)) { + assertThat(influxDBClient).isNotNull(); + assertThat(influxDBClient.ping().isGood()).isTrue(); + assertThat(influxDBClient.version()).isEqualTo(TEST_VERSION); } } } @@ -62,7 +65,8 @@ public void describeDatabases() { try ( // constructorWithUserPassword { final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - DockerImageName.parse("influxdb:1.4.3")) + DockerImageName.parse("influxdb:1.4.3") + ) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD) @@ -71,8 +75,8 @@ public void describeDatabases() { // Start the container. This step might take some time... influxDBContainer.start(); - try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { - Assertions.assertThat(influxDBClient.describeDatabases()).contains(DATABASE); + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(influxDBContainer)) { + assertThat(influxDBClient.describeDatabases()).contains(DATABASE); } } } @@ -81,7 +85,8 @@ public void describeDatabases() { public void queryForWriteAndRead() { try ( final InfluxDBContainer influxDBContainer = new InfluxDBContainer<>( - InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE) + InfluxDBTestUtils.INFLUXDB_V1_TEST_IMAGE + ) .withDatabase(DATABASE) .withUsername(USER) .withPassword(PASSWORD) @@ -89,7 +94,7 @@ public void queryForWriteAndRead() { // Start the container. This step might take some time... influxDBContainer.start(); - try (final InfluxDB influxDBClient = influxDBContainer.getNewInfluxDB()) { + try (final InfluxDB influxDBClient = createInfluxDBWithUrl(influxDBContainer)) { final Point point = Point .measurement("cpu") .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) @@ -102,11 +107,23 @@ public void queryForWriteAndRead() { final Query query = new Query("SELECT idle FROM cpu", DATABASE); final QueryResult actual = influxDBClient.query(query); - Assertions.assertThat(actual).isNotNull(); - Assertions.assertThat(actual.getError()).isNull(); - Assertions.assertThat(actual.getResults()).isNotNull(); - Assertions.assertThat(actual.getResults()).hasSize(1); + assertThat(actual).isNotNull(); + assertThat(actual.getError()).isNull(); + assertThat(actual.getResults()).isNotNull(); + assertThat(actual.getResults()).hasSize(1); } } } + + // createInfluxDBClient { + public static InfluxDB createInfluxDBWithUrl(final InfluxDBContainer container) { + InfluxDB influxDB = InfluxDBFactory.connect( + container.getUrl(), + container.getUsername(), + container.getPassword() + ); + influxDB.setDatabase(container.getDatabase()); + return influxDB; + } + // } } diff --git a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java index 0cdec9a95d4..db9ae2bf79f 100644 --- a/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java +++ b/modules/influxdb/src/test/java/org/testcontainers/containers/InfluxDBTestUtils.java @@ -1,10 +1,5 @@ package org.testcontainers.containers; -import com.influxdb.client.InfluxDBClient; -import com.influxdb.client.InfluxDBClientFactory; -import com.influxdb.client.InfluxDBClientOptions; -import org.influxdb.InfluxDB; -import org.influxdb.InfluxDBFactory; import org.testcontainers.utility.DockerImageName; public final class InfluxDBTestUtils { @@ -12,27 +7,4 @@ public final class InfluxDBTestUtils { static final DockerImageName INFLUXDB_V1_TEST_IMAGE = DockerImageName.parse("influxdb:1.4.3"); static final DockerImageName INFLUXDB_V2_TEST_IMAGE = DockerImageName.parse("influxdb:2.0.7"); - - private InfluxDBTestUtils() {} - - // createInfluxDB2Client { - public static InfluxDBClient createInfluxDBClient(final InfluxDBContainer influxDBContainer) { - final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions - .builder() - .url(influxDBContainer.getUrl()) - .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray()) - .bucket(influxDBContainer.getBucket()) - .org(influxDBContainer.getOrganization()) - .build(); - return InfluxDBClientFactory.create(influxDBClientOptions); - } - // } - - public static InfluxDBClient createInfluxDBClientWithToken(final String url, final String token) { - return InfluxDBClientFactory.create(url, token.toCharArray()); - } - - public static InfluxDB createInfluxDBWithUrl(final String url) { - return InfluxDBFactory.connect(url); - } }