Skip to content

Commit

Permalink
Add InfluxDB support (#686)
Browse files Browse the repository at this point in the history
* influx-db-container

* influx-db-container fixed after code review

* Add InfluxDB module
  • Loading branch information
banadiga authored and bsideup committed May 16, 2018
1 parent a36451e commit c2e3f80
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
- Fixed provided but shaded dependencies in modules ([\#693](https://github.com/testcontainers/testcontainers-java/issues/693))

### Changed
- Added InfluxDB module ([\#686](https://github.com/testcontainers/testcontainers-java/pull/686))

## [1.7.2] - 2018-04-30

Expand Down
41 changes: 41 additions & 0 deletions modules/influxdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Testcontainers InfluxDB testing module

Testcontainers module for the InfluxData [InfluxDB](https://github.com/influxdata/influxdb).

## Usage example

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() {
InfluxDB influxDB = influxDbContainer.getNewInfluxDB();
...
```

## Dependency information

Replace `VERSION` with the [latest version available on Maven Central](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.testcontainers%22).

[![](https://api.bintray.com/packages/testcontainers/releases/testcontainers/images/download.svg)](https://bintray.com/testcontainers/releases/testcontainers/_latestVersion)


### Maven
```
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>influxdb</artifactId>
<version>VERSION</version>
</dependency>
```

### Gradle

```
compile group: 'org.testcontainers', name: 'influxdb', version: 'VERSION'
```
8 changes: 8 additions & 0 deletions modules/influxdb/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
description = "Testcontainers :: InfluxDB"

dependencies {
compile project(':testcontainers')

compileOnly 'org.influxdb:influxdb-java:2.10'
testCompile 'org.influxdb:influxdb-java:2.10'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package org.testcontainers.containers;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.testcontainers.containers.traits.LinkableContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;

/**
* @link https://store.docker.com/images/influxdb
*/
public class InfluxDBContainer<SELF extends InfluxDBContainer<SELF>> extends GenericContainer<SELF>
implements LinkableContainer {

public static final String VERSION = "1.4.3";
public static final Integer INFLUXDB_PORT = 8086;

private static final String IMAGE_NAME = "influxdb";

private boolean authEnabled = true;
private String admin = "admin";
private String adminPassword = "password";

private String database;
private String username = "any";
private String password = "any";


public InfluxDBContainer() {
this(VERSION);
}

public InfluxDBContainer(final String version) {
super(IMAGE_NAME + ":" + version);
waitStrategy = new WaitAllStrategy()
.withStrategy(Wait.forHttp("/ping").withBasicCredentials(username, password).forStatusCode(204))
.withStrategy(Wait.forListeningPort());
}

@Override
protected void configure() {
addExposedPort(INFLUXDB_PORT);

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);
}

@Override
protected Integer getLivenessCheckPort() {
return 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 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 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 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 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 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 self();
}


/**
* @return a url to influxDb
*/
public String getUrl() {
return "http://" + getContainerIpAddress() + ":" + getLivenessCheckPort();
}

/**
* @return a influxDb client
*/
public InfluxDB getNewInfluxDB() {
InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), username, password);
influxDB.setDatabase(database);
return influxDB;
}
}
Original file line number Diff line number Diff line change
@@ -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 InfluxDBContainerTest {

@ClassRule
public static InfluxDBContainer influxDBContainer = new InfluxDBContainer();

@Test
public void getUrl() {
String actual = influxDBContainer.getUrl();

assertThat(actual, notNullValue());
}

@Test
public void getNewInfluxDB() {
InfluxDB actual = influxDBContainer.getNewInfluxDB();

assertThat(actual, notNullValue());
assertThat(actual.ping(), notNullValue());
}

@Test
public void getLivenessCheckPort() {
Integer actual = influxDBContainer.getLivenessCheckPort();

assertThat(actual, notNullValue());
}

@Test
public void isRunning() {
boolean actual = influxDBContainer.isRunning();

assertThat(actual, is(true));
}
}
Original file line number Diff line number Diff line change
@@ -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 InfluxDBContainerWithUserTest {

private static final String TEST_VERSION = "1.4.3";
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(TEST_VERSION)
.withDatabase(DATABASE)
.withUsername(USER)
.withPassword(PASSWORD);

@Test
public void describeDatabases() {
InfluxDB actual = influxDBContainer.getNewInfluxDB();

assertThat(actual, notNullValue());
assertThat(actual.describeDatabases(), hasItem(DATABASE));
}

@Test
public void checkVersion() {
InfluxDB actual = 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() {
InfluxDB influxDB = influxDBContainer.getNewInfluxDB();

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);

assertThat(actual, notNullValue());
assertThat(actual.getError(), nullValue());
assertThat(actual.getResults(), notNullValue());
assertThat(actual.getResults().size(), is(1));

}
}
24 changes: 24 additions & 0 deletions modules/influxdb/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="debug">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.apache.http" level="WARN"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="org.zeroturnaround.exec" level="WARN"/>
<logger name="com.zaxxer.hikari" level="INFO"/>
<logger name="org.rnorth.tcpunixsocketproxy" level="INFO"/>
<logger name="io.netty" level="WARN"/>
<logger name="org.mongodb" level="INFO"/>
<logger name="org.testcontainers.shaded" level="WARN"/>
<logger name="com.zaxxer.hikari" level="INFO"/>
</configuration>

0 comments on commit c2e3f80

Please sign in to comment.