forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfluxDBContainerV1.java
150 lines (124 loc) · 4.83 KB
/
InfluxDBContainerV1.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
146
147
148
149
150
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 org.testcontainers.utility.DockerImageName;
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 InfluxDBContainerV1 extends GenericContainer<InfluxDBContainerV1> {
public static final Integer INFLUXDB_PORT = 8086;
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb");
private static final String DEFAULT_TAG = "1.4.3";
private static final int NO_CONTENT_STATUS_CODE = 204;
@Deprecated
public static final String VERSION = DEFAULT_TAG;
private boolean authEnabled = true;
private String admin = "admin";
private String adminPassword = "password";
private String database;
private String username = "any";
private String password = "any";
public InfluxDBContainerV1(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
this.waitStrategy = new WaitAllStrategy()
.withStrategy(Wait.forHttp("/ping").withBasicCredentials(this.username, this.password)
.forStatusCode(NO_CONTENT_STATUS_CODE))
.withStrategy(Wait.forListeningPort());
this.addExposedPort(INFLUXDB_PORT);
}
@Override
protected void configure() {
this.addEnv("INFLUXDB_ADMIN_USER", this.admin);
this.addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword);
this.addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled));
this.addEnv("INFLUXDB_DB", this.database);
this.addEnv("INFLUXDB_USER", this.username);
this.addEnv("INFLUXDB_USER_PASSWORD", this.password);
}
@Override
public Set<Integer> getLivenessCheckPortNumbers() {
return Collections.singleton(this.getMappedPort(INFLUXDB_PORT));
}
/**
* Set env variable `INFLUXDB_HTTP_AUTH_ENABLED`.
*
* @param authEnabled Enables authentication.
* @return a reference to this container instance
*/
public InfluxDBContainerV1 withAuthEnabled(final boolean authEnabled) {
this.authEnabled = authEnabled;
return this;
}
/**
* 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 InfluxDBContainerV1 withAdmin(final String admin) {
this.admin = admin;
return this;
}
/**
* 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 InfluxDBContainerV1 withAdminPassword(final String adminPassword) {
this.adminPassword = adminPassword;
return this;
}
/**
* 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 InfluxDBContainerV1 withDatabase(final String database) {
this.database = database;
return this;
}
/**
* 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 InfluxDBContainerV1 withUsername(final String username) {
this.username = username;
return this;
}
/**
* 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 InfluxDBContainerV1 withPassword(final String password) {
this.password = password;
return this;
}
/**
* @return a url to influxDb
*/
public String getUrl() {
return "http://" + this.getHost() + ":" + this.getMappedPort(INFLUXDB_PORT);
}
/**
* @return a influxDb client
*/
public InfluxDB getNewInfluxDB() {
final InfluxDB influxDB = InfluxDBFactory.connect(this.getUrl(), this.username, this.password);
influxDB.setDatabase(this.database);
return influxDB;
}
}