Skip to content

Commit c2e3f80

Browse files
banadigabsideup
authored andcommitted
Add InfluxDB support (#686)
* influx-db-container * influx-db-container fixed after code review * Add InfluxDB module
1 parent a36451e commit c2e3f80

File tree

7 files changed

+335
-0
lines changed

7 files changed

+335
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
- Fixed provided but shaded dependencies in modules ([\#693](https://github.com/testcontainers/testcontainers-java/issues/693))
88

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

1112
## [1.7.2] - 2018-04-30
1213

modules/influxdb/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Testcontainers InfluxDB testing module
2+
3+
Testcontainers module for the InfluxData [InfluxDB](https://github.com/influxdata/influxdb).
4+
5+
## Usage example
6+
7+
Running influxDbContainer as a stand-in for InfluxDB in a test:
8+
9+
```java
10+
public class SomeTest {
11+
12+
@Rule
13+
public InfluxDBContainer influxDbContainer = new InfluxDBContainer();
14+
15+
@Test
16+
public void someTestMethod() {
17+
InfluxDB influxDB = influxDbContainer.getNewInfluxDB();
18+
...
19+
```
20+
21+
## Dependency information
22+
23+
Replace `VERSION` with the [latest version available on Maven Central](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.testcontainers%22).
24+
25+
[![](https://api.bintray.com/packages/testcontainers/releases/testcontainers/images/download.svg)](https://bintray.com/testcontainers/releases/testcontainers/_latestVersion)
26+
27+
28+
### Maven
29+
```
30+
<dependency>
31+
<groupId>org.testcontainers</groupId>
32+
<artifactId>influxdb</artifactId>
33+
<version>VERSION</version>
34+
</dependency>
35+
```
36+
37+
### Gradle
38+
39+
```
40+
compile group: 'org.testcontainers', name: 'influxdb', version: 'VERSION'
41+
```

modules/influxdb/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
description = "Testcontainers :: InfluxDB"
2+
3+
dependencies {
4+
compile project(':testcontainers')
5+
6+
compileOnly 'org.influxdb:influxdb-java:2.10'
7+
testCompile 'org.influxdb:influxdb-java:2.10'
8+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package org.testcontainers.containers;
2+
3+
import org.influxdb.InfluxDB;
4+
import org.influxdb.InfluxDBFactory;
5+
import org.testcontainers.containers.traits.LinkableContainer;
6+
import org.testcontainers.containers.wait.strategy.Wait;
7+
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
8+
9+
/**
10+
* @link https://store.docker.com/images/influxdb
11+
*/
12+
public class InfluxDBContainer<SELF extends InfluxDBContainer<SELF>> extends GenericContainer<SELF>
13+
implements LinkableContainer {
14+
15+
public static final String VERSION = "1.4.3";
16+
public static final Integer INFLUXDB_PORT = 8086;
17+
18+
private static final String IMAGE_NAME = "influxdb";
19+
20+
private boolean authEnabled = true;
21+
private String admin = "admin";
22+
private String adminPassword = "password";
23+
24+
private String database;
25+
private String username = "any";
26+
private String password = "any";
27+
28+
29+
public InfluxDBContainer() {
30+
this(VERSION);
31+
}
32+
33+
public InfluxDBContainer(final String version) {
34+
super(IMAGE_NAME + ":" + version);
35+
waitStrategy = new WaitAllStrategy()
36+
.withStrategy(Wait.forHttp("/ping").withBasicCredentials(username, password).forStatusCode(204))
37+
.withStrategy(Wait.forListeningPort());
38+
}
39+
40+
@Override
41+
protected void configure() {
42+
addExposedPort(INFLUXDB_PORT);
43+
44+
addEnv("INFLUXDB_ADMIN_USER", admin);
45+
addEnv("INFLUXDB_ADMIN_PASSWORD", adminPassword);
46+
47+
addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(authEnabled));
48+
49+
addEnv("INFLUXDB_DB", database);
50+
addEnv("INFLUXDB_USER", username);
51+
addEnv("INFLUXDB_USER_PASSWORD", password);
52+
}
53+
54+
@Override
55+
protected Integer getLivenessCheckPort() {
56+
return getMappedPort(INFLUXDB_PORT);
57+
}
58+
59+
60+
/**
61+
* Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`.
62+
*
63+
* @param authEnabled Enables authentication.
64+
* @return a reference to this container instance
65+
*/
66+
public SELF withAuthEnabled(final boolean authEnabled) {
67+
this.authEnabled = authEnabled;
68+
return self();
69+
}
70+
71+
/**
72+
* Set env variable `INFLUXDB_ADMIN_USER`.
73+
*
74+
* @param admin The name of the admin user to be created. If this is unset, no admin user is created.
75+
* @return a reference to this container instance
76+
*/
77+
public SELF withAdmin(final String admin) {
78+
this.admin = admin;
79+
return self();
80+
}
81+
82+
/**
83+
* Set env variable `INFLUXDB_ADMIN_PASSWORD`.
84+
*
85+
* @param adminPassword TThe password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a
86+
* random password is generated and printed to standard out.
87+
* @return a reference to this container instance
88+
*/
89+
public SELF withAdminPassword(final String adminPassword) {
90+
this.adminPassword = adminPassword;
91+
return self();
92+
}
93+
94+
/**
95+
* Set env variable `INFLUXDB_DB`.
96+
*
97+
* @param database Automatically initializes a database with the name of this environment variable.
98+
* @return a reference to this container instance
99+
*/
100+
public SELF withDatabase(final String database) {
101+
this.database = database;
102+
return self();
103+
}
104+
105+
/**
106+
* Set env variable `INFLUXDB_USER`.
107+
*
108+
* @param username The name of a user to be created with no privileges. If `INFLUXDB_DB` is set, this user will
109+
* be granted read and write permissions for that database.
110+
* @return a reference to this container instance
111+
*/
112+
public SELF withUsername(final String username) {
113+
this.username = username;
114+
return self();
115+
}
116+
117+
/**
118+
* Set env variable `INFLUXDB_USER_PASSWORD`.
119+
*
120+
* @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password
121+
* is generated and printed to standard out.
122+
* @return a reference to this container instance
123+
*/
124+
public SELF withPassword(final String password) {
125+
this.password = password;
126+
return self();
127+
}
128+
129+
130+
/**
131+
* @return a url to influxDb
132+
*/
133+
public String getUrl() {
134+
return "http://" + getContainerIpAddress() + ":" + getLivenessCheckPort();
135+
}
136+
137+
/**
138+
* @return a influxDb client
139+
*/
140+
public InfluxDB getNewInfluxDB() {
141+
InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), username, password);
142+
influxDB.setDatabase(database);
143+
return influxDB;
144+
}
145+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.testcontainers.containers;
2+
3+
import org.influxdb.InfluxDB;
4+
import org.junit.ClassRule;
5+
import org.junit.Test;
6+
7+
import static org.hamcrest.CoreMatchers.is;
8+
import static org.hamcrest.CoreMatchers.notNullValue;
9+
import static org.junit.Assert.assertThat;
10+
11+
public class InfluxDBContainerTest {
12+
13+
@ClassRule
14+
public static InfluxDBContainer influxDBContainer = new InfluxDBContainer();
15+
16+
@Test
17+
public void getUrl() {
18+
String actual = influxDBContainer.getUrl();
19+
20+
assertThat(actual, notNullValue());
21+
}
22+
23+
@Test
24+
public void getNewInfluxDB() {
25+
InfluxDB actual = influxDBContainer.getNewInfluxDB();
26+
27+
assertThat(actual, notNullValue());
28+
assertThat(actual.ping(), notNullValue());
29+
}
30+
31+
@Test
32+
public void getLivenessCheckPort() {
33+
Integer actual = influxDBContainer.getLivenessCheckPort();
34+
35+
assertThat(actual, notNullValue());
36+
}
37+
38+
@Test
39+
public void isRunning() {
40+
boolean actual = influxDBContainer.isRunning();
41+
42+
assertThat(actual, is(true));
43+
}
44+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.testcontainers.containers;
2+
3+
import org.influxdb.InfluxDB;
4+
import org.influxdb.dto.Point;
5+
import org.influxdb.dto.Query;
6+
import org.influxdb.dto.QueryResult;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
import static org.hamcrest.CoreMatchers.hasItem;
13+
import static org.hamcrest.CoreMatchers.is;
14+
import static org.hamcrest.CoreMatchers.notNullValue;
15+
import static org.hamcrest.CoreMatchers.nullValue;
16+
import static org.junit.Assert.assertThat;
17+
18+
public class InfluxDBContainerWithUserTest {
19+
20+
private static final String TEST_VERSION = "1.4.3";
21+
private static final String DATABASE = "test";
22+
private static final String USER = "test-user";
23+
private static final String PASSWORD = "test-password";
24+
25+
@Rule
26+
public InfluxDBContainer influxDBContainer = new InfluxDBContainer(TEST_VERSION)
27+
.withDatabase(DATABASE)
28+
.withUsername(USER)
29+
.withPassword(PASSWORD);
30+
31+
@Test
32+
public void describeDatabases() {
33+
InfluxDB actual = influxDBContainer.getNewInfluxDB();
34+
35+
assertThat(actual, notNullValue());
36+
assertThat(actual.describeDatabases(), hasItem(DATABASE));
37+
}
38+
39+
@Test
40+
public void checkVersion() {
41+
InfluxDB actual = influxDBContainer.getNewInfluxDB();
42+
43+
assertThat(actual, notNullValue());
44+
45+
assertThat(actual.ping(), notNullValue());
46+
assertThat(actual.ping().getVersion(), is(TEST_VERSION));
47+
48+
assertThat(actual.version(), is(TEST_VERSION));
49+
}
50+
51+
@Test
52+
public void queryForWriteAndRead() {
53+
InfluxDB influxDB = influxDBContainer.getNewInfluxDB();
54+
55+
Point point = Point.measurement("cpu")
56+
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
57+
.addField("idle", 90L)
58+
.addField("user", 9L)
59+
.addField("system", 1L)
60+
.build();
61+
influxDB.write(point);
62+
63+
Query query = new Query("SELECT idle FROM cpu", DATABASE);
64+
QueryResult actual = influxDB.query(query);
65+
66+
assertThat(actual, notNullValue());
67+
assertThat(actual.getError(), nullValue());
68+
assertThat(actual.getResults(), notNullValue());
69+
assertThat(actual.getResults().size(), is(1));
70+
71+
}
72+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<!-- encoders are assigned the type
5+
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6+
<encoder>
7+
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="debug">
12+
<appender-ref ref="STDOUT"/>
13+
</root>
14+
15+
<logger name="org.apache.http" level="WARN"/>
16+
<logger name="com.github.dockerjava" level="WARN"/>
17+
<logger name="org.zeroturnaround.exec" level="WARN"/>
18+
<logger name="com.zaxxer.hikari" level="INFO"/>
19+
<logger name="org.rnorth.tcpunixsocketproxy" level="INFO"/>
20+
<logger name="io.netty" level="WARN"/>
21+
<logger name="org.mongodb" level="INFO"/>
22+
<logger name="org.testcontainers.shaded" level="WARN"/>
23+
<logger name="com.zaxxer.hikari" level="INFO"/>
24+
</configuration>

0 commit comments

Comments
 (0)