forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfluxDBContainer.java
145 lines (121 loc) · 4.44 KB
/
InfluxDBContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package org.testcontainers.containers;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import java.util.Collections;
import java.util.Set;
/**
* See <a href="https://store.docker.com/images/influxdb">https://store.docker.com/images/influxdb</a>
*/
public class InfluxDBContainer<SELF extends InfluxDBContainer<SELF>> extends GenericContainer<SELF> {
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());
addExposedPort(INFLUXDB_PORT);
}
@Override
protected void configure() {
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
public Set<Integer> getLivenessCheckPortNumbers() {
return Collections.singleton(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;
}
}