Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Nov 15, 2022
1 parent 8a190d0 commit 627a01c
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 99 deletions.
4 changes: 2 additions & 2 deletions docs/modules/databases/influxdb.md
Expand Up @@ -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:

<!--codeinclude-->
[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
<!--/codeinclude-->

!!! hint
Expand Down Expand Up @@ -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:

<!--codeinclude-->
[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
<!--/codeinclude-->

!!! hint
Expand Down
@@ -1,15 +1,16 @@
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.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.
*/
Expand Down Expand Up @@ -41,6 +42,7 @@ public class InfluxDBContainer<SELF extends InfluxDBContainer<SELF>> extends Gen

private String adminPassword = "password";

@Getter
private String database;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -98,9 +99,9 @@ public InfluxDBContainer(final DockerImageName dockerImageName) {
@Override
protected void configure() {
if (this.isAtLeastMajorVersion2) {
this.setInfluxDBV2Envs();
configureInfluxDBV2();
} else {
this.setInfluxDBV1Envs();
configureInfluxDBV1();
}
}

Expand All @@ -110,37 +111,37 @@ protected void configure() {
* @see <a href="https://hub.docker.com/_/influxdb"> InfluxDB Dockerhub </a> for full documentation on InfluxDB's
* envrinoment variables</a>
*/
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<Integer> getLivenessCheckPortNumbers() {
return Collections.singleton(this.getMappedPort(INFLUXDB_PORT));
return Collections.singleton(getMappedPort(INFLUXDB_PORT));
}

/**
Expand Down Expand Up @@ -266,11 +267,9 @@ public String getUrl() {
* @deprecated Use the new <a href="https://github.com/influxdata/influxdb-client-java">InfluxDB client library.</a>
*/
@Deprecated
// createInfluxDBClient {
public InfluxDB getNewInfluxDB() {
final InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), this.username, this.password);
influxDB.setDatabase(this.database);
return influxDB;
}
// }
}
@@ -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;
Expand All @@ -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";
Expand All @@ -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();
}
Expand All @@ -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<String> 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();
}
Expand All @@ -75,24 +84,25 @@ 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)
.withBucket(BUCKET)
.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);
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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());
}
}

0 comments on commit 627a01c

Please sign in to comment.