2424import org .testcontainers .containers .InternetProtocol ;
2525import org .testcontainers .containers .wait .strategy .LogMessageWaitStrategy ;
2626
27+ import java .io .File ;
28+ import java .nio .file .Files ;
2729import java .time .Duration ;
2830import java .time .temporal .ChronoUnit ;
31+ import java .util .ArrayList ;
32+ import java .util .List ;
2933
3034import static io .vertx .pgclient .PgConnectOptions .DEFAULT_PORT ;
35+ import static org .junit .Assert .assertTrue ;
3136
3237/**
3338 * Postgresql test database based on https://www.testcontainers.org
@@ -48,6 +53,7 @@ public class ContainerPgRule extends ExternalResource {
4853 private boolean ssl ;
4954 private boolean forceSsl ;
5055 private String user = "postgres" ;
56+ private File domainSocketMount ;
5157
5258 public ContainerPgRule ssl (boolean ssl ) {
5359 this .ssl = ssl ;
@@ -76,6 +82,18 @@ public ContainerPgRule user(String user) {
7682 }
7783
7884 private void initServer (String version ) throws Exception {
85+
86+ // Domain socket on Linux
87+ String osName = System .getProperty ("os.name" );
88+ if (osName != null && (osName .startsWith ("Linux" ) || osName .startsWith ("LINUX" ))) {
89+ // Create temp file, length must be < 107 chars (Linux limitation)
90+ domainSocketMount = Files .createTempDirectory ("postgresql_var_run" ).toFile ();
91+ domainSocketMount .deleteOnExit ();
92+ domainSocketMount .setReadable (true , false );
93+ domainSocketMount .setWritable (true , false );
94+ domainSocketMount .setExecutable (true , false );
95+ }
96+
7997 server = new ServerContainer <>("postgres:" + version )
8098 .withEnv ("POSTGRES_DB" , "postgres" )
8199 .withEnv ("POSTGRES_USER" , user )
@@ -86,6 +104,11 @@ private void initServer(String version) throws Exception {
86104 .withStartupTimeout (Duration .of (60 , ChronoUnit .SECONDS )))
87105 .withCommand ("postgres" , "-c" , "fsync=off" )
88106 .withClasspathResourceMapping ("create-postgres.sql" , "/docker-entrypoint-initdb.d/create-postgres.sql" , BindMode .READ_ONLY );
107+
108+ if (domainSocketMount != null ) {
109+ server = server .withFileSystemBind (domainSocketMount .getAbsolutePath (), "/var/run/postgresql" );
110+ }
111+
89112 if (ssl ) {
90113 server
91114 .withClasspathResourceMapping ("tls/server.crt" , "/server.crt" , BindMode .READ_ONLY )
@@ -104,6 +127,10 @@ private void initServer(String version) throws Exception {
104127 }
105128 }
106129
130+ public String domainSocketPath () {
131+ return domainSocketMount != null ? domainSocketMount .getAbsolutePath () : null ;
132+ }
133+
107134 public static boolean isTestingWithExternalDatabase () {
108135 return isSystemPropertyValid (connectionUri ) || isSystemPropertyValid (tlsConnectionUri ) || isSystemPropertyValid (tlsForceConnectionUri );
109136 }
@@ -124,7 +151,6 @@ public synchronized PgConnectOptions startServer(String databaseVersion) throws
124151 .setPassword ("postgres" );
125152 }
126153
127-
128154 private static String getPostgresVersion () {
129155 String specifiedVersion = System .getProperty ("embedded.postgres.version" );
130156 String version ;
@@ -138,7 +164,7 @@ private static String getPostgresVersion() {
138164 return version ;
139165 }
140166
141- public synchronized void stopServer () throws Exception {
167+ public synchronized void stopServer () {
142168 if (server != null ) {
143169 try {
144170 server .stop ();
0 commit comments