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 .ArrayList ;
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 ;
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