Skip to content

Commit 943c280

Browse files
committed
Add PostgreSQL unix domain socket test based on test containers (previously was ru.yandex.qatools.embed/postgresql-embedded)
1 parent 1ea3a2d commit 943c280

File tree

3 files changed

+140
-10
lines changed

3 files changed

+140
-10
lines changed

vertx-pg-client/pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@
5858
</dependency>
5959

6060
<!-- Testing purposes -->
61-
<!--
62-
<dependency>
63-
<groupId>ru.yandex.qatools.embed</groupId>
64-
<artifactId>postgresql-embedded</artifactId>
65-
<version>2.10</version>
66-
<scope>test</scope>
67-
</dependency>
68-
-->
6961
<dependency>
7062
<groupId>net.java.dev.jna</groupId>
7163
<artifactId>jna</artifactId>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (C) 2018 Julien Viet
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package io.vertx.tests.pgclient;
18+
19+
import io.vertx.pgclient.PgBuilder;
20+
import io.vertx.pgclient.PgConnectOptions;
21+
import io.vertx.pgclient.SslMode;
22+
import io.vertx.core.Vertx;
23+
import io.vertx.core.VertxOptions;
24+
import io.vertx.ext.unit.TestContext;
25+
import io.vertx.ext.unit.junit.VertxUnitRunner;
26+
import io.vertx.sqlclient.Pool;
27+
import io.vertx.sqlclient.SqlClient;
28+
import io.vertx.sqlclient.SqlConnectOptions;
29+
import io.vertx.sqlclient.impl.ConnectionFactoryBase;
30+
import io.vertx.tests.pgclient.junit.ContainerPgRule;
31+
import org.junit.*;
32+
import org.junit.runner.RunWith;
33+
34+
import java.io.File;
35+
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertFalse;
38+
import static org.junit.Assume.assumeTrue;
39+
40+
@RunWith(VertxUnitRunner.class)
41+
public class UnixDomainSocketTest {
42+
43+
@ClassRule
44+
public static ContainerPgRule rule = new ContainerPgRule();
45+
private Pool client;
46+
private PgConnectOptions options;
47+
private Vertx vertx;
48+
49+
@Before
50+
public void before() {
51+
Assume.assumeNotNull(rule.domainSocketPath());
52+
vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
53+
options = rule.options().setHost(rule.domainSocketPath()).setPort(5432);
54+
}
55+
56+
@After
57+
public void after(TestContext ctx) {
58+
if (vertx != null) {
59+
vertx.close().onComplete(ctx.asyncAssertSuccess());
60+
}
61+
if (client != null) {
62+
client.close().onComplete(ctx.asyncAssertSuccess());
63+
}
64+
}
65+
66+
@Test
67+
public void uriTest(TestContext context) {
68+
String uri = "postgresql://postgres:postgres@/postgres?host=" + options.getHost() + "&port=" + options.getPort();
69+
client = PgBuilder.pool().connectingTo(uri).using(vertx).build();
70+
client
71+
.getConnection()
72+
.onComplete(context.asyncAssertSuccess(SqlClient::close));
73+
}
74+
75+
@Test
76+
public void simpleConnect(TestContext context) {
77+
client = PgBuilder.pool().connectingTo(new PgConnectOptions(options)).using(vertx).build();
78+
client
79+
.getConnection()
80+
.onComplete(context.asyncAssertSuccess(pgConnection -> pgConnection.close().onComplete(context.asyncAssertSuccess())));
81+
}
82+
83+
@Test
84+
public void connectWithVertxInstance(TestContext context) {
85+
client = PgBuilder.pool().connectingTo(new PgConnectOptions(options)).using(vertx).build();
86+
client
87+
.getConnection()
88+
.onComplete(context.asyncAssertSuccess(pgConnection -> {
89+
pgConnection.close();
90+
}));
91+
}
92+
93+
@Ignore
94+
@Test
95+
public void testIgnoreSslMode(TestContext context) {
96+
client = PgBuilder.pool().connectingTo(new PgConnectOptions(options).setSslMode(SslMode.REQUIRE)).using(vertx).build();
97+
client
98+
.getConnection()
99+
.onComplete(context.asyncAssertSuccess(pgConnection -> {
100+
assertFalse(pgConnection.isSSL());
101+
pgConnection.close();
102+
}));
103+
}
104+
105+
@Test
106+
public void testNativeTransportMustBeEnabled(TestContext ctx) {
107+
Pool pool = PgBuilder.pool().connectingTo(SqlConnectOptions.fromUri("postgresql:///dbname?host=/var/lib/postgresql")).build();
108+
pool.getConnection().onComplete(ctx.asyncAssertFailure(err -> {
109+
assertEquals(ConnectionFactoryBase.NATIVE_TRANSPORT_REQUIRED, err.getMessage());
110+
}));
111+
}
112+
}

vertx-pg-client/src/test/java/io/vertx/tests/pgclient/junit/ContainerPgRule.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@
2424
import org.testcontainers.containers.InternetProtocol;
2525
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
2626

27+
import java.io.File;
28+
import java.nio.file.Files;
2729
import java.time.Duration;
2830
import java.time.temporal.ChronoUnit;
31+
import java.util.ArrayList;
32+
import java.util.List;
2933

3034
import 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

Comments
 (0)