2424import org .testcontainers .containers .InternetProtocol ;
2525import 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 ;
2730import java .time .Duration ;
2831import java .time .temporal .ChronoUnit ;
32+ import java .util .Arrays ;
33+ import java .util .HashSet ;
34+ import java .util .List ;
2935
3036import static io .vertx .pgclient .PgConnectOptions .DEFAULT_PORT ;
37+ import static org .junit .Assert .assertTrue ;
38+ import static org .junit .Assume .assumeTrue ;
3139
3240/**
3341 * Postgresql test database based on https://www.testcontainers.org
@@ -48,6 +56,7 @@ public class ContainerPgRule extends ExternalResource {
4856 private boolean ssl ;
4957 private boolean forceSsl ;
5058 private String user = "postgres" ;
59+ private File sock ;
5160
5261 public ContainerPgRule ssl (boolean ssl ) {
5362 this .ssl = ssl ;
@@ -76,6 +85,29 @@ public ContainerPgRule user(String user) {
7685 }
7786
7887 private void initServer (String version ) throws Exception {
88+
89+ List <String > cmd = Arrays .asList ("postgres" , "-c" , "fsync=off" );
90+
91+ // Domain socket on Linux
92+ String osName = System .getProperty ("os.name" );
93+ if (osName != null && (osName .startsWith ("Linux" ) || osName .startsWith ("LINUX" ))) {
94+ // Create temp file, length must be < 107 chars (Linux limitation)
95+ sock = Files .createTempFile ("pg_" , ".sock" ).toFile ();
96+ assertTrue (sock .delete ());
97+ assertTrue (sock .mkdir ());
98+ sock .deleteOnExit ();
99+ Files .setPosixFilePermissions (sock .toPath (), new HashSet <>(Arrays .asList (
100+ PosixFilePermission .OWNER_EXECUTE ,
101+ PosixFilePermission .OWNER_READ ,
102+ PosixFilePermission .OWNER_WRITE ,
103+ PosixFilePermission .GROUP_EXECUTE ,
104+ PosixFilePermission .GROUP_READ ,
105+ PosixFilePermission .GROUP_WRITE
106+ )));
107+ cmd .add ("-k" );
108+ cmd .add ("/var/run/mysqld" ); // Todo : change this path
109+ }
110+
79111 server = new ServerContainer <>("postgres:" + version )
80112 .withEnv ("POSTGRES_DB" , "postgres" )
81113 .withEnv ("POSTGRES_USER" , user )
@@ -84,8 +116,13 @@ private void initServer(String version) throws Exception {
84116 .withRegEx (".*database system is ready to accept connections.*\\ s" )
85117 .withTimes (2 )
86118 .withStartupTimeout (Duration .of (60 , ChronoUnit .SECONDS )))
87- .withCommand ("postgres" , "-c" , "fsync=off" )
119+ .withCommand (cmd . toArray ( new String [ 0 ]) )
88120 .withClasspathResourceMapping ("create-postgres.sql" , "/docker-entrypoint-initdb.d/create-postgres.sql" , BindMode .READ_ONLY );
121+
122+ if (sock != null ) {
123+ server = server .withFileSystemBind (sock .getAbsolutePath (), "/var/run/mysqld" );
124+ }
125+
89126 if (ssl ) {
90127 server
91128 .withClasspathResourceMapping ("tls/server.crt" , "/server.crt" , BindMode .READ_ONLY )
0 commit comments