Skip to content

Commit

Permalink
influx-db-container
Browse files Browse the repository at this point in the history
  • Loading branch information
banadiga committed May 14, 2018
1 parent bf692d8 commit 4c9c164
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 1 deletion.
38 changes: 38 additions & 0 deletions modules/influxdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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).

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

### Gradle

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

ext {
influxdbJavaClientVersion = '2.9'

junitVersion = '4.12'
}

dependencies {
compile project(':testcontainers')

compile "org.influxdb:influxdb-java:${influxdbJavaClientVersion}"

testCompile "junit:junit:$junitVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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;

import java.util.UUID;

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

public static final String VERSION = "latest";
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 = UUID.randomUUID().toString();

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

/**
* Bind a fixed port on the docker host to a container port
*
* @param hostPort a port on the docker host, which must be available
* @return a reference to this container instance
*/
public SELF withFixedExposedPort(int hostPort) {
super.addFixedExposedPort(hostPort, INFLUXDB_PORT);
return self();
}

/**
* 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,45 @@
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.hamcrest.CoreMatchers.startsWith;
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, startsWith("http://localhost:"));
}

@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>
3 changes: 2 additions & 1 deletion modules/spock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ dependencies {
compile 'org.spockframework:spock-core:1.0-groovy-2.4'

testCompile project(':mysql')
testCompile project(':influxdb')
testCompile project(':postgresql')
testCompile 'com.zaxxer:HikariCP:2.6.1'

testRuntime 'org.postgresql:postgresql:42.0.0'
testRuntime 'mysql:mysql-connector-java:6.0.6'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.testcontainers.spock

import org.testcontainers.containers.InfluxDBContainer
import spock.lang.Shared
import spock.lang.Specification

import static org.testcontainers.containers.InfluxDBContainer.VERSION

@Testcontainers
class InfluxDBContainerIT extends Specification {

private static final String DATABASE = "terst"
private static final String USER = "testuser"
private static final String PASSWORD = "testpassword"

@Shared
public InfluxDBContainer influxDBContainer = new InfluxDBContainer(VERSION)
.withDatabase(DATABASE)
.withUsername(USER)
.withPassword(PASSWORD)

def "dummy test"() {
expect:
influxDBContainer.isRunning()
influxDBContainer.url
influxDBContainer.newInfluxDB
influxDBContainer.newInfluxDB.ping()
}
}

0 comments on commit 4c9c164

Please sign in to comment.