forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerPgRule.java
More file actions
193 lines (163 loc) · 5.88 KB
/
ContainerPgRule.java
File metadata and controls
193 lines (163 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.pgclient.junit;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.PoolOptions;
import org.junit.rules.ExternalResource;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.InternetProtocol;
import org.testcontainers.containers.PostgreSQLContainer;
import static org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT;
/**
* Postgresql test database based on https://www.testcontainers.org
* Require Docker
* Testing can also be done on an external database by setting the system properties :
* - connection.uri
* - tls.connection.uri
*/
public class ContainerPgRule extends ExternalResource {
private static final String connectionUri = System.getProperty("connection.uri");
private static final String tlsConnectionUri = System.getProperty("tls.connection.uri");
private static final String tlsForceConnectionUri = System.getProperty("tls.force.connection.uri");
public static final ContainerPgRule SHARED_INSTANCE = new ContainerPgRule();
private ServerContainer<?> server;
private PgConnectOptions options;
private String databaseVersion;
private boolean ssl;
private boolean forceSsl;
private String user = "postgres";
public ContainerPgRule ssl(boolean ssl) {
this.ssl = ssl;
return this;
}
public ContainerPgRule forceSsl(boolean forceSsl) {
this.forceSsl = forceSsl;
return this;
}
public PgConnectOptions options() {
return new PgConnectOptions(options);
}
public PoolOptions poolOptions() {
return new PoolOptions();
}
public ContainerPgRule user(String user) {
if (user == null) {
throw new NullPointerException();
}
this.user = user;
return this;
}
private void initServer(String version) throws Exception {
server = new ServerContainer<>("postgres:" + version)
.withDatabaseName("postgres")
.withUsername(user)
.withPassword("postgres")
.withClasspathResourceMapping("create-postgres.sql", "/docker-entrypoint-initdb.d/create-postgres.sql", BindMode.READ_ONLY);
if (ssl) {
server
.withClasspathResourceMapping("tls/server.crt", "/server.crt", BindMode.READ_ONLY)
.withClasspathResourceMapping("tls/server.key", "/server.key", BindMode.READ_ONLY)
.withClasspathResourceMapping("tls/ssl.sh", "/docker-entrypoint-initdb.d/ssl.sh", BindMode.READ_ONLY);
if (forceSsl) {
server
.withClasspathResourceMapping("tls/pg_hba.conf", "/tmp/pg_hba.conf", BindMode.READ_ONLY)
.withClasspathResourceMapping("tls/force_ssl.sh", "/docker-entrypoint-initdb.d/force_ssl.sh", BindMode.READ_ONLY);
}
}
if (System.getProperties().containsKey("containerFixedPort")) {
server.withFixedExposedPort(POSTGRESQL_PORT, POSTGRESQL_PORT);
} else {
server.withExposedPorts(POSTGRESQL_PORT);
}
}
public static boolean isTestingWithExternalDatabase() {
return isSystemPropertyValid(connectionUri) || isSystemPropertyValid(tlsConnectionUri) || isSystemPropertyValid(tlsForceConnectionUri);
}
private static boolean isSystemPropertyValid(String systemProperty) {
return systemProperty != null && !systemProperty.isEmpty();
}
public synchronized PgConnectOptions startServer(String databaseVersion) throws Exception {
initServer(databaseVersion);
server.start();
return new PgConnectOptions()
.setPort(server.getMappedPort(POSTGRESQL_PORT))
.setHost(server.getContainerIpAddress())
.setDatabase("postgres")
.setUser(user)
.setPassword("postgres");
}
private static String getPostgresVersion() {
String specifiedVersion = System.getProperty("embedded.postgres.version");
String version;
if (specifiedVersion == null || specifiedVersion.isEmpty()) {
// if version is not specified then V10.10 will be used by default
version = "10.10";
} else {
version = specifiedVersion;
}
return version;
}
public synchronized void stopServer() throws Exception {
if (server != null) {
try {
server.stop();
} finally {
server = null;
}
}
}
@Override
protected void before() throws Throwable {
// use an external database for testing
if (isTestingWithExternalDatabase()) {
if (ssl) {
if (forceSsl) {
options = PgConnectOptions.fromUri(tlsForceConnectionUri);
} else {
options = PgConnectOptions.fromUri(tlsConnectionUri);
}
}
else {
options = PgConnectOptions.fromUri(connectionUri);
}
return;
}
// We do not need to launch another server if it's a shared instance
if (this.server != null) {
return;
}
this.databaseVersion = getPostgresVersion();
options = startServer(databaseVersion);
}
public static boolean isAtLeastPg10() {
// hackish ;-)
return !getPostgresVersion().startsWith("9.");
}
@Override
protected void after() {
if (!isTestingWithExternalDatabase() && this != SHARED_INSTANCE) {
try {
stopServer();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static class ServerContainer<SELF extends ServerContainer<SELF>> extends PostgreSQLContainer<SELF> {
public ServerContainer(String dockerImageName) {
super(dockerImageName);
}
public SELF withFixedExposedPort(int hostPort, int containerPort) {
super.addFixedExposedPort(hostPort, containerPort, InternetProtocol.TCP);
return self();
}
}
}