Skip to content

Commit 4c9c164

Browse files
committed
influx-db-container
1 parent bf692d8 commit 4c9c164

File tree

8 files changed

+382
-1
lines changed

8 files changed

+382
-1
lines changed

modules/influxdb/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
### Maven
26+
```
27+
<dependency>
28+
<groupId>org.testcontainers</groupId>
29+
<artifactId>influxdb</artifactId>
30+
<version>VERSION</version>
31+
</dependency>
32+
```
33+
34+
### Gradle
35+
36+
```
37+
compile group: 'org.testcontainers', name: 'influxdb', version: 'VERSION'
38+
```

modules/influxdb/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
description = "Testcontainers :: InfluxDB"
2+
3+
ext {
4+
influxdbJavaClientVersion = '2.9'
5+
6+
junitVersion = '4.12'
7+
}
8+
9+
dependencies {
10+
compile project(':testcontainers')
11+
12+
compile "org.influxdb:influxdb-java:${influxdbJavaClientVersion}"
13+
14+
testCompile "junit:junit:$junitVersion"
15+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
import java.util.UUID;
10+
11+
/**
12+
* @link https://store.docker.com/images/influxdb
13+
*/
14+
public class InfluxDBContainer<SELF extends InfluxDBContainer<SELF>> extends GenericContainer<SELF>
15+
implements LinkableContainer {
16+
17+
public static final String VERSION = "latest";
18+
public static final Integer INFLUXDB_PORT = 8086;
19+
20+
private static final String IMAGE_NAME = "influxdb";
21+
22+
private boolean authEnabled = true;
23+
private String admin = "admin";
24+
private String adminPassword = UUID.randomUUID().toString();
25+
26+
private String database;
27+
private String username = "any";
28+
private String password = "any";
29+
30+
31+
public InfluxDBContainer() {
32+
this(VERSION);
33+
}
34+
35+
public InfluxDBContainer(final String version) {
36+
super(IMAGE_NAME + ":" + version);
37+
waitStrategy = new WaitAllStrategy()
38+
.withStrategy(Wait.forHttp("/ping").withBasicCredentials(username, password).forStatusCode(204))
39+
.withStrategy(Wait.forListeningPort());
40+
}
41+
42+
@Override
43+
protected void configure() {
44+
addExposedPort(INFLUXDB_PORT);
45+
46+
addEnv("INFLUXDB_ADMIN_USER", admin);
47+
addEnv("INFLUXDB_ADMIN_PASSWORD", adminPassword);
48+
49+
addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(authEnabled));
50+
51+
addEnv("INFLUXDB_DB", database);
52+
addEnv("INFLUXDB_USER", username);
53+
addEnv("INFLUXDB_USER_PASSWORD", password);
54+
}
55+
56+
@Override
57+
protected Integer getLivenessCheckPort() {
58+
return getMappedPort(INFLUXDB_PORT);
59+
}
60+
61+
/**
62+
* Bind a fixed port on the docker host to a container port
63+
*
64+
* @param hostPort a port on the docker host, which must be available
65+
* @return a reference to this container instance
66+
*/
67+
public SELF withFixedExposedPort(int hostPort) {
68+
super.addFixedExposedPort(hostPort, INFLUXDB_PORT);
69+
return self();
70+
}
71+
72+
/**
73+
* Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`.
74+
*
75+
* @param authEnabled Enables authentication.
76+
* @return a reference to this container instance
77+
*/
78+
public SELF withAuthEnabled(final boolean authEnabled) {
79+
this.authEnabled = authEnabled;
80+
return self();
81+
}
82+
83+
/**
84+
* Set env variable `INFLUXDB_ADMIN_USER`.
85+
*
86+
* @param admin The name of the admin user to be created. If this is unset, no admin user is created.
87+
* @return a reference to this container instance
88+
*/
89+
public SELF withAdmin(final String admin) {
90+
this.admin = admin;
91+
return self();
92+
}
93+
94+
/**
95+
* Set env variable `INFLUXDB_ADMIN_PASSWORD`.
96+
*
97+
* @param adminPassword TThe password for the admin user configured with `INFLUXDB_ADMIN_USER`. If this is unset, a
98+
* random password is generated and printed to standard out.
99+
* @return a reference to this container instance
100+
*/
101+
public SELF withAdminPassword(final String adminPassword) {
102+
this.adminPassword = adminPassword;
103+
return self();
104+
}
105+
106+
/**
107+
* Set env variable `INFLUXDB_DB`.
108+
*
109+
* @param database Automatically initializes a database with the name of this environment variable.
110+
* @return a reference to this container instance
111+
*/
112+
public SELF withDatabase(final String database) {
113+
this.database = database;
114+
return self();
115+
}
116+
117+
/**
118+
* Set env variable `INFLUXDB_USER`.
119+
*
120+
* @param username The name of a user to be created with no privileges. If `INFLUXDB_DB` is set, this user will
121+
* be granted read and write permissions for that database.
122+
* @return a reference to this container instance
123+
*/
124+
public SELF withUsername(final String username) {
125+
this.username = username;
126+
return self();
127+
}
128+
129+
/**
130+
* Set env variable `INFLUXDB_USER_PASSWORD`.
131+
*
132+
* @param password The password for the user configured with `INFLUXDB_USER`. If this is unset, a random password
133+
* is generated and printed to standard out.
134+
* @return a reference to this container instance
135+
*/
136+
public SELF withPassword(final String password) {
137+
this.password = password;
138+
return self();
139+
}
140+
141+
142+
/**
143+
* @return a url to influxDb
144+
*/
145+
public String getUrl() {
146+
return "http://" + getContainerIpAddress() + ":" + getLivenessCheckPort();
147+
}
148+
149+
/**
150+
* @return a influxDb client
151+
*/
152+
public InfluxDB getNewInfluxDB() {
153+
InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), username, password);
154+
influxDB.setDatabase(database);
155+
return influxDB;
156+
}
157+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.hamcrest.CoreMatchers.startsWith;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class InfluxDBContainerTest {
13+
14+
@ClassRule
15+
public static InfluxDBContainer influxDBContainer = new InfluxDBContainer();
16+
17+
@Test
18+
public void getUrl() {
19+
String actual = influxDBContainer.getUrl();
20+
21+
assertThat(actual, startsWith("http://localhost:"));
22+
}
23+
24+
@Test
25+
public void getNewInfluxDB() {
26+
InfluxDB actual = influxDBContainer.getNewInfluxDB();
27+
28+
assertThat(actual, notNullValue());
29+
assertThat(actual.ping(), notNullValue());
30+
}
31+
32+
@Test
33+
public void getLivenessCheckPort() {
34+
Integer actual = influxDBContainer.getLivenessCheckPort();
35+
36+
assertThat(actual, notNullValue());
37+
}
38+
39+
@Test
40+
public void isRunning() {
41+
boolean actual = influxDBContainer.isRunning();
42+
43+
assertThat(actual, is(true));
44+
}
45+
}
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>

modules/spock/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ dependencies {
99
compile 'org.spockframework:spock-core:1.0-groovy-2.4'
1010

1111
testCompile project(':mysql')
12+
testCompile project(':influxdb')
1213
testCompile project(':postgresql')
1314
testCompile 'com.zaxxer:HikariCP:2.6.1'
1415

1516
testRuntime 'org.postgresql:postgresql:42.0.0'
1617
testRuntime 'mysql:mysql-connector-java:6.0.6'
17-
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.testcontainers.spock
2+
3+
import org.testcontainers.containers.InfluxDBContainer
4+
import spock.lang.Shared
5+
import spock.lang.Specification
6+
7+
import static org.testcontainers.containers.InfluxDBContainer.VERSION
8+
9+
@Testcontainers
10+
class InfluxDBContainerIT extends Specification {
11+
12+
private static final String DATABASE = "terst"
13+
private static final String USER = "testuser"
14+
private static final String PASSWORD = "testpassword"
15+
16+
@Shared
17+
public InfluxDBContainer influxDBContainer = new InfluxDBContainer(VERSION)
18+
.withDatabase(DATABASE)
19+
.withUsername(USER)
20+
.withPassword(PASSWORD)
21+
22+
def "dummy test"() {
23+
expect:
24+
influxDBContainer.isRunning()
25+
influxDBContainer.url
26+
influxDBContainer.newInfluxDB
27+
influxDBContainer.newInfluxDB.ping()
28+
}
29+
}

0 commit comments

Comments
 (0)