Skip to content

Commit 16cd105

Browse files
committed
Start adding domain socket support to ContainerPgRule
1 parent 1ea3a2d commit 16cd105

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

vertx-pg-client/src/test/java/io/vertx/tests/pgclient/junit/ContainerPgRule.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@
2424
import org.testcontainers.containers.InternetProtocol;
2525
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
2626

27+
import java.io.File;
28+
import java.nio.file.Files;
29+
import java.nio.file.attribute.PosixFilePermission;
2730
import java.time.Duration;
2831
import java.time.temporal.ChronoUnit;
32+
import java.util.ArrayList;
33+
import java.util.HashSet;
34+
import java.util.List;
2935

3036
import static io.vertx.pgclient.PgConnectOptions.DEFAULT_PORT;
37+
import static org.junit.Assert.assertTrue;
3138

3239
/**
3340
* Postgresql test database based on https://www.testcontainers.org
@@ -48,6 +55,7 @@ public class ContainerPgRule extends ExternalResource {
4855
private boolean ssl;
4956
private boolean forceSsl;
5057
private String user = "postgres";
58+
private File sock;
5159

5260
public ContainerPgRule ssl(boolean ssl) {
5361
this.ssl = ssl;
@@ -76,6 +84,29 @@ public ContainerPgRule user(String user) {
7684
}
7785

7886
private void initServer(String version) throws Exception {
87+
88+
List<String> cmd = new ArrayList<>(List.of("postgres", "-c", "fsync=off"));
89+
90+
// Domain socket on Linux
91+
String osName = System.getProperty("os.name");
92+
if (osName != null && (osName.startsWith("Linux") || osName.startsWith("LINUX"))) {
93+
// Create temp file, length must be < 107 chars (Linux limitation)
94+
sock = Files.createTempFile("pg_", ".sock").toFile();
95+
assertTrue(sock.delete());
96+
assertTrue(sock.mkdir());
97+
sock.deleteOnExit();
98+
Files.setPosixFilePermissions(sock.toPath(), new HashSet<>(List.of(
99+
PosixFilePermission.OWNER_EXECUTE,
100+
PosixFilePermission.OWNER_READ,
101+
PosixFilePermission.OWNER_WRITE,
102+
PosixFilePermission.GROUP_EXECUTE,
103+
PosixFilePermission.GROUP_READ,
104+
PosixFilePermission.GROUP_WRITE
105+
)));
106+
cmd.add("-k");
107+
cmd.add("/var/run/mysqld"); // Todo : change this path
108+
}
109+
79110
server = new ServerContainer<>("postgres:" + version)
80111
.withEnv("POSTGRES_DB", "postgres")
81112
.withEnv("POSTGRES_USER", user)
@@ -84,8 +115,13 @@ private void initServer(String version) throws Exception {
84115
.withRegEx(".*database system is ready to accept connections.*\\s")
85116
.withTimes(2)
86117
.withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS)))
87-
.withCommand("postgres", "-c", "fsync=off")
118+
.withCommand(cmd.toArray(new String[0]))
88119
.withClasspathResourceMapping("create-postgres.sql", "/docker-entrypoint-initdb.d/create-postgres.sql", BindMode.READ_ONLY);
120+
121+
if (sock != null) {
122+
server = server.withFileSystemBind(sock.getAbsolutePath(), "/var/run/mysqld");
123+
}
124+
89125
if (ssl) {
90126
server
91127
.withClasspathResourceMapping("tls/server.crt", "/server.crt", BindMode.READ_ONLY)

0 commit comments

Comments
 (0)