24
24
import org .testcontainers .containers .InternetProtocol ;
25
25
import org .testcontainers .containers .wait .strategy .LogMessageWaitStrategy ;
26
26
27
+ import java .io .File ;
28
+ import java .nio .file .Files ;
27
29
import java .time .Duration ;
28
30
import java .time .temporal .ChronoUnit ;
31
+ import java .util .ArrayList ;
32
+ import java .util .List ;
29
33
30
34
import static io .vertx .pgclient .PgConnectOptions .DEFAULT_PORT ;
35
+ import static org .junit .Assert .assertTrue ;
31
36
32
37
/**
33
38
* Postgresql test database based on https://www.testcontainers.org
@@ -48,6 +53,7 @@ public class ContainerPgRule extends ExternalResource {
48
53
private boolean ssl ;
49
54
private boolean forceSsl ;
50
55
private String user = "postgres" ;
56
+ private File domainSocketMount ;
51
57
52
58
public ContainerPgRule ssl (boolean ssl ) {
53
59
this .ssl = ssl ;
@@ -76,6 +82,18 @@ public ContainerPgRule user(String user) {
76
82
}
77
83
78
84
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
+
79
97
server = new ServerContainer <>("postgres:" + version )
80
98
.withEnv ("POSTGRES_DB" , "postgres" )
81
99
.withEnv ("POSTGRES_USER" , user )
@@ -86,6 +104,11 @@ private void initServer(String version) throws Exception {
86
104
.withStartupTimeout (Duration .of (60 , ChronoUnit .SECONDS )))
87
105
.withCommand ("postgres" , "-c" , "fsync=off" )
88
106
.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
+
89
112
if (ssl ) {
90
113
server
91
114
.withClasspathResourceMapping ("tls/server.crt" , "/server.crt" , BindMode .READ_ONLY )
@@ -104,6 +127,10 @@ private void initServer(String version) throws Exception {
104
127
}
105
128
}
106
129
130
+ public String domainSocketPath () {
131
+ return domainSocketMount != null ? domainSocketMount .getAbsolutePath () : null ;
132
+ }
133
+
107
134
public static boolean isTestingWithExternalDatabase () {
108
135
return isSystemPropertyValid (connectionUri ) || isSystemPropertyValid (tlsConnectionUri ) || isSystemPropertyValid (tlsForceConnectionUri );
109
136
}
@@ -124,7 +151,6 @@ public synchronized PgConnectOptions startServer(String databaseVersion) throws
124
151
.setPassword ("postgres" );
125
152
}
126
153
127
-
128
154
private static String getPostgresVersion () {
129
155
String specifiedVersion = System .getProperty ("embedded.postgres.version" );
130
156
String version ;
@@ -138,7 +164,7 @@ private static String getPostgresVersion() {
138
164
return version ;
139
165
}
140
166
141
- public synchronized void stopServer () throws Exception {
167
+ public synchronized void stopServer () {
142
168
if (server != null ) {
143
169
try {
144
170
server .stop ();
0 commit comments