forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Db2Container.java
129 lines (106 loc) · 3.74 KB
/
Db2Container.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
package org.testcontainers.containers;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.WaitingConsumer;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.utility.LicenseAcceptance;
import org.testcontainers.utility.LogUtils;
import java.io.IOException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate;
public class Db2Container extends JdbcDatabaseContainer<Db2Container> {
public static final String NAME = "db2";
public static final String DEFAULT_DB2_IMAGE_NAME = "ibmcom/db2";
public static final String DEFAULT_TAG = "11.5.0.0a";
public static final int DB2_PORT = 50000;
private String databaseName = "test";
private String username = "db2inst1";
private String password = "foobar1234";
public Db2Container() {
this(DEFAULT_DB2_IMAGE_NAME + ":" + DEFAULT_TAG);
}
public Db2Container(String imageName) {
super(imageName);
withPrivilegedMode(true);
this.waitStrategy = new LogMessageWaitStrategy()
.withRegEx(".*Setup has completed\\..*")
.withStartupTimeout(Duration.of(10, ChronoUnit.MINUTES));
addExposedPort(DB2_PORT);
}
@Override
protected Set<Integer> getLivenessCheckPorts() {
return new HashSet<>(getMappedPort(DB2_PORT));
}
@Override
protected void configure() {
// If license was not accepted programatically, check if it was accepted via resource file
if (!getEnvMap().containsKey("LICENSE")) {
LicenseAcceptance.assertLicenseAccepted(this.getDockerImageName());
acceptLicense();
}
addEnv("DBNAME", databaseName);
addEnv("DB2INSTANCE", username);
addEnv("DB2INST1_PASSWORD", password);
// These settings help the DB2 container start faster
if (!getEnvMap().containsKey("AUTOCONFIG"))
addEnv("AUTOCONFIG", "false");
if (!getEnvMap().containsKey("ARCHIVE_LOGS"))
addEnv("ARCHIVE_LOGS", "false");
}
/**
* Accepts the license for the DB2 container by setting the LICENSE=accept
* variable as described at <a href="https://hub.docker.com/r/ibmcom/db2">https://hub.docker.com/r/ibmcom/db2</a>
*/
public Db2Container acceptLicense() {
addEnv("LICENSE", "accept");
return this;
}
@Override
public String getDriverClassName() {
return "com.ibm.db2.jcc.DB2Driver";
}
@Override
public String getJdbcUrl() {
return "jdbc:db2://" + getContainerIpAddress() + ":" + getMappedPort(DB2_PORT) + "/" + databaseName;
}
@Override
public String getUsername() {
return username;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getDatabaseName() {
return databaseName;
}
@Override
public Db2Container withUsername(String username) {
this.username = username;
return this;
}
@Override
public Db2Container withPassword(String password) {
this.password = password;
return this;
}
@Override
public Db2Container withDatabaseName(String dbName) {
this.databaseName = dbName;
return this;
}
@Override
protected void waitUntilContainerStarted() {
getWaitStrategy().waitUntilReady(this);
}
@Override
protected String getTestQueryString() {
return "SELECT 1 FROM SYSIBM.SYSDUMMY1";
}
}