|
| 1 | +package org.testcontainers.db2; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.model.Capability; |
| 4 | +import org.testcontainers.containers.JdbcDatabaseContainer; |
| 5 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 6 | +import org.testcontainers.utility.DockerImageName; |
| 7 | +import org.testcontainers.utility.LicenseAcceptance; |
| 8 | + |
| 9 | +import java.time.Duration; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +/** |
| 13 | + * Testcontainers implementation for IBM DB2. |
| 14 | + * <p> |
| 15 | + * Supported images: {@code icr.io/db2_community/db2}, {@code ibmcom/db2} |
| 16 | + * <p> |
| 17 | + * Exposed ports: |
| 18 | + * <ul> |
| 19 | + * <li>Database: 50000</li> |
| 20 | + * </ul> |
| 21 | + */ |
| 22 | +public class Db2Container extends JdbcDatabaseContainer<Db2Container> { |
| 23 | + |
| 24 | + public static final String NAME = "db2"; |
| 25 | + |
| 26 | + private static final DockerImageName DEFAULT_NEW_IMAGE_NAME = DockerImageName.parse("icr.io/db2_community/db2"); |
| 27 | + |
| 28 | + public static final int DB2_PORT = 50000; |
| 29 | + |
| 30 | + private String databaseName = "test"; |
| 31 | + |
| 32 | + private String username = "db2inst1"; |
| 33 | + |
| 34 | + private String password = "foobar1234"; |
| 35 | + |
| 36 | + public Db2Container(String dockerImageName) { |
| 37 | + this(DockerImageName.parse(dockerImageName)); |
| 38 | + } |
| 39 | + |
| 40 | + public Db2Container(final DockerImageName dockerImageName) { |
| 41 | + super(dockerImageName); |
| 42 | + dockerImageName.assertCompatibleWith(DEFAULT_NEW_IMAGE_NAME); |
| 43 | + |
| 44 | + withCreateContainerCmdModifier(cmd -> cmd.withCapAdd(Capability.IPC_LOCK).withCapAdd(Capability.IPC_OWNER)); |
| 45 | + waitingFor(Wait.forLogMessage(".*Setup has completed\\..*", 1).withStartupTimeout(Duration.ofMinutes(10))); |
| 46 | + |
| 47 | + addExposedPort(DB2_PORT); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return the ports on which to check if the container is ready |
| 52 | + * @deprecated use {@link #getLivenessCheckPortNumbers()} instead |
| 53 | + */ |
| 54 | + @Override |
| 55 | + @Deprecated |
| 56 | + protected Set<Integer> getLivenessCheckPorts() { |
| 57 | + return super.getLivenessCheckPorts(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected void configure() { |
| 62 | + // If license was not accepted programmatically, check if it was accepted via resource file |
| 63 | + if (!getEnvMap().containsKey("LICENSE")) { |
| 64 | + LicenseAcceptance.assertLicenseAccepted(this.getDockerImageName()); |
| 65 | + acceptLicense(); |
| 66 | + } |
| 67 | + |
| 68 | + addEnv("DBNAME", databaseName); |
| 69 | + addEnv("DB2INSTANCE", username); |
| 70 | + addEnv("DB2INST1_PASSWORD", password); |
| 71 | + |
| 72 | + // These settings help the DB2 container start faster |
| 73 | + if (!getEnvMap().containsKey("AUTOCONFIG")) { |
| 74 | + addEnv("AUTOCONFIG", "false"); |
| 75 | + } |
| 76 | + if (!getEnvMap().containsKey("ARCHIVE_LOGS")) { |
| 77 | + addEnv("ARCHIVE_LOGS", "false"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Accepts the license for the DB2 container by setting the LICENSE=accept |
| 83 | + * variable as described at <a href="https://hub.docker.com/r/ibmcom/db2">https://hub.docker.com/r/ibmcom/db2</a> |
| 84 | + */ |
| 85 | + public Db2Container acceptLicense() { |
| 86 | + addEnv("LICENSE", "accept"); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String getDriverClassName() { |
| 92 | + return "com.ibm.db2.jcc.DB2Driver"; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String getJdbcUrl() { |
| 97 | + String additionalUrlParams = constructUrlParameters(":", ";", ";"); |
| 98 | + return "jdbc:db2://" + getHost() + ":" + getMappedPort(DB2_PORT) + "/" + databaseName + additionalUrlParams; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String getUsername() { |
| 103 | + return username; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getPassword() { |
| 108 | + return password; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getDatabaseName() { |
| 113 | + return databaseName; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Db2Container withUsername(String username) { |
| 118 | + this.username = username; |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Db2Container withPassword(String password) { |
| 124 | + this.password = password; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Db2Container withDatabaseName(String dbName) { |
| 130 | + this.databaseName = dbName; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + protected void waitUntilContainerStarted() { |
| 136 | + getWaitStrategy().waitUntilReady(this); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + protected String getTestQueryString() { |
| 141 | + return "SELECT 1 FROM SYSIBM.SYSDUMMY1"; |
| 142 | + } |
| 143 | +} |
0 commit comments