Skip to content

Commit

Permalink
Fix Testcontainers regression
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneLimburg committed Mar 12, 2022
1 parent 6ea86f7 commit a3f28a8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<maven.compiler.target>11</maven.compiler.target>

<jupiter.version>5.6.2</jupiter.version>
<testcontainers.version>1.16.2</testcontainers.version>
<testcontainers.version>1.16.3</testcontainers.version>
<flyway.version>8.4.3</flyway.version>

<sonar.projectKey>ArneLimburg_testflight</sonar.projectKey>
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/space/testflight/InContainerDataMySqlContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
*/
package space.testflight;

import java.util.ArrayList;
import java.util.List;

import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.InternetProtocol;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.images.builder.ImageFromDockerfile;

import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.Ports;

public class InContainerDataMySqlContainer extends MySQLContainer<InContainerDataMySqlContainer>
implements DefaultTaggableContainer<InContainerDataMySqlContainer> {

Expand All @@ -30,11 +36,8 @@ public InContainerDataMySqlContainer(String image) {
.from(image)
.run("mkdir -p /var/lib/mysql-data && chown -R mysql:mysql /var/lib/mysql-data")
.build()));
initCommand();
}

private void initCommand() {
withClasspathResourceMapping("/space/testflight/mysql/mysql.cnf", "/etc/mysql/conf.d/mysql.cnf", BindMode.READ_ONLY);
exposeContainerPort();
}

@Override
Expand All @@ -51,4 +54,19 @@ public String getDefaultImageName() {
public void addFixedPort(int hostPort, int containerPort) {
super.addFixedExposedPort(hostPort, containerPort, InternetProtocol.TCP);
}

private void exposeContainerPort() {
withCreateContainerCmdModifier(cmd -> {
List<ExposedPort> exposedPorts = new ArrayList<>();
for (ExposedPort p : cmd.getExposedPorts()) {
exposedPorts.add(p);
}
exposedPorts.add(ExposedPort.tcp(getContainerPort()));
cmd.withExposedPorts(exposedPorts);

Ports ports = cmd.getHostConfig().getPortBindings();
ports.bind(ExposedPort.tcp(getContainerPort()), Ports.Binding.empty());
cmd.getHostConfig().withPortBindings(ports);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@
*/
package space.testflight;

import java.util.ArrayList;
import java.util.List;

import org.testcontainers.containers.InternetProtocol;
import org.testcontainers.containers.PostgreSQLContainer;

import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.Ports;

public class InContainerDataPostgreSqlContainer extends PostgreSQLContainer<InContainerDataPostgreSqlContainer>
implements DefaultTaggableContainer<InContainerDataPostgreSqlContainer> {

public InContainerDataPostgreSqlContainer(String dockerImage) {
super(dockerImage);
withEnv("PGDATA", "/var/lib/postgresql/data-local");
exposeContainerPort();
}

@Override
Expand All @@ -40,4 +47,19 @@ public int getContainerPort() {
public void addFixedPort(int hostPort, int containerPort) {
super.addFixedExposedPort(hostPort, containerPort, InternetProtocol.TCP);
}

private void exposeContainerPort() {
withCreateContainerCmdModifier(cmd -> {
List<ExposedPort> exposedPorts = new ArrayList<>();
for (ExposedPort p : cmd.getExposedPorts()) {
exposedPorts.add(p);
}
exposedPorts.add(ExposedPort.tcp(getContainerPort()));
cmd.withExposedPorts(exposedPorts);

Ports ports = cmd.getHostConfig().getPortBindings();
ports.bind(ExposedPort.tcp(getContainerPort()), Ports.Binding.empty());
cmd.getHostConfig().withPortBindings(ports);
});
}
}

0 comments on commit a3f28a8

Please sign in to comment.