Skip to content

Commit cf5c0e8

Browse files
committed
Use dev services instead of docker-maven-plugin for Postgresql
1 parent 9e380b4 commit cf5c0e8

File tree

15 files changed

+118
-447
lines changed

15 files changed

+118
-447
lines changed

integration-tests/jpa-postgresql-withxml/pom.xml

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
<name>Quarkus - Integration Tests - JPA - PostgreSQL with XML</name>
1414
<description>Module that contains JPA related tests running with the PostgreSQL database</description>
1515

16-
<properties>
17-
<postgres.url>jdbc:postgresql://localhost:5431/hibernate_orm_test</postgres.url>
18-
</properties>
19-
2016
<dependencies>
2117
<dependency>
2218
<groupId>io.quarkus</groupId>
@@ -161,86 +157,6 @@
161157
</plugins>
162158
</build>
163159
</profile>
164-
165-
<profile>
166-
<id>docker-postgresql</id>
167-
<activation>
168-
<property>
169-
<name>start-containers</name>
170-
</property>
171-
</activation>
172-
<build>
173-
<plugins>
174-
<plugin>
175-
<groupId>io.fabric8</groupId>
176-
<artifactId>docker-maven-plugin</artifactId>
177-
<configuration>
178-
<images>
179-
<image>
180-
<name>${postgres.image}</name>
181-
<alias>postgresql</alias>
182-
<run>
183-
<env>
184-
<POSTGRES_USER>hibernate_orm_test</POSTGRES_USER>
185-
<POSTGRES_PASSWORD>hibernate_orm_test</POSTGRES_PASSWORD>
186-
<POSTGRES_DB>hibernate_orm_test</POSTGRES_DB>
187-
</env>
188-
<ports>
189-
<port>5431:5432</port>
190-
</ports>
191-
<wait>
192-
<!-- For some reason, Postgres will tell us it's ready *twice*,
193-
and that's the truth the second time only.
194-
See https://github.com/fabric8io/docker-maven-plugin/issues/628
195-
See also how the condition is configured in testcontainers:
196-
https://github.com/testcontainers/testcontainers-java/blob/c64aab9fd5e3a452ee0faf793560327eb4da9841/modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLContainer.java#L52-L55 -->
197-
<time>20000</time>
198-
<log>(?s)ready to accept connections.*ready to accept connections</log>
199-
</wait>
200-
</run>
201-
</image>
202-
</images>
203-
<!--Stops all postgres images currently running, not just those we just started.
204-
Useful to stop processes still running from a previously failed integration test run -->
205-
<allContainers>true</allContainers>
206-
</configuration>
207-
<executions>
208-
<execution>
209-
<id>docker-start</id>
210-
<phase>compile</phase>
211-
<goals>
212-
<goal>stop</goal>
213-
<goal>start</goal>
214-
</goals>
215-
</execution>
216-
<execution>
217-
<id>docker-stop</id>
218-
<phase>post-integration-test</phase>
219-
<goals>
220-
<goal>stop</goal>
221-
</goals>
222-
</execution>
223-
</executions>
224-
</plugin>
225-
<plugin>
226-
<groupId>org.codehaus.mojo</groupId>
227-
<artifactId>exec-maven-plugin</artifactId>
228-
<executions>
229-
<execution>
230-
<id>docker-prune</id>
231-
<phase>generate-resources</phase>
232-
<goals>
233-
<goal>exec</goal>
234-
</goals>
235-
<configuration>
236-
<executable>${docker-prune.location}</executable>
237-
</configuration>
238-
</execution>
239-
</executions>
240-
</plugin>
241-
</plugins>
242-
</build>
243-
</profile>
244160
</profiles>
245161

246162

integration-tests/jpa-postgresql-withxml/src/main/resources/application.properties

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
quarkus.datasource.username=hibernate_orm_test
2-
quarkus.datasource.password=hibernate_orm_test
3-
quarkus.datasource.jdbc.url=${postgres.url}
41
quarkus.datasource.jdbc.max-size=8
52

63
quarkus.hibernate-orm.packages=io.quarkus.it.jpa.postgresql.defaultpu

integration-tests/jpa-postgresql/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
By default, the tests of this module are disabled.
66

7-
To run the tests in a standard JVM with PostgreSQL started as a Docker container, you can run the following command:
7+
To run the tests in a standard JVM with PostgreSQL started as a Dev Service, you can run the following command:
88

99
```
1010
mvn clean install -Dtest-containers -Dstart-containers
@@ -16,12 +16,12 @@ Additionally, you can generate a native image and run the tests for this native
1616
mvn clean install -Dtest-containers -Dstart-containers -Dnative
1717
```
1818

19-
If you don't want to run PostgreSQL as a Docker container, you can start your own PostgreSQL server. It needs to listen on the default port and have a database called `hibernate_orm_test` accessible to the user `hibernate_orm_test` with the password `hibernate_orm_test`.
19+
If you don't want to run PostgreSQL as a Dev Service, you can start your own PostgreSQL server. It needs to listen on the default port and have a database called `hibernate_orm_test` accessible to the user `hibernate_orm_test` with the password `hibernate_orm_test`.
2020

2121
You can then run the tests as follows (either with `-Dnative` or not):
2222

2323
```
24-
mvn clean install -Dtest-containers
24+
mvn clean install -Dtest-containers -Dpostgres.url=jdbc:postgresql://...
2525
```
2626

27-
If you have specific requirements, you can define a specific connection URL with `-Dpostgres.url=jdbc:postgresql://...`.
27+
If you have specific requirements, you can also define a specific connection URL with `-Dpostgres.url=jdbc:postgresql://...`.

integration-tests/jpa-postgresql/pom.xml

Lines changed: 22 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
<name>Quarkus - Integration Tests - JPA - PostgreSQL</name>
1414
<description>Module that contains JPA related tests running with the PostgreSQL database</description>
1515

16-
<properties>
17-
<postgres.url>jdbc:postgresql://localhost:5431/hibernate_orm_test</postgres.url>
18-
</properties>
19-
2016
<dependencies>
2117
<dependency>
2218
<groupId>io.quarkus</groupId>
@@ -46,6 +42,28 @@
4642
<artifactId>quarkus-junit5-internal</artifactId>
4743
<scope>test</scope>
4844
</dependency>
45+
<!-- <dependency>-->
46+
<!-- <groupId>org.junit.platform</groupId>-->
47+
<!-- <artifactId>junit-platform-suite</artifactId>-->
48+
<!-- <scope>test</scope>-->
49+
<!-- </dependency>-->
50+
<dependency>
51+
<groupId>org.testcontainers</groupId>
52+
<artifactId>postgresql</artifactId>
53+
<exclusions>
54+
<exclusion>
55+
<groupId>junit</groupId>
56+
<artifactId>junit</artifactId>
57+
</exclusion>
58+
</exclusions>
59+
<scope>test</scope>
60+
</dependency>
61+
<!-- Until we have testcontainers 2, which has https://github.com/testcontainers/testcontainers-java/issues/970, add back JUnit 4 so testcontainers doesn't break everything :( -->
62+
<dependency>
63+
<groupId>junit</groupId>
64+
<artifactId>junit</artifactId>
65+
<version>${junit4.version}</version>
66+
</dependency>
4967
<dependency>
5068
<groupId>io.rest-assured</groupId>
5169
<artifactId>rest-assured</artifactId>
@@ -171,86 +189,6 @@
171189
</plugins>
172190
</build>
173191
</profile>
174-
175-
<profile>
176-
<id>docker-postgresql</id>
177-
<activation>
178-
<property>
179-
<name>start-containers</name>
180-
</property>
181-
</activation>
182-
<build>
183-
<plugins>
184-
<plugin>
185-
<groupId>io.fabric8</groupId>
186-
<artifactId>docker-maven-plugin</artifactId>
187-
<configuration>
188-
<images>
189-
<image>
190-
<name>${postgres.image}</name>
191-
<alias>postgresql</alias>
192-
<run>
193-
<env>
194-
<POSTGRES_USER>hibernate_orm_test</POSTGRES_USER>
195-
<POSTGRES_PASSWORD>hibernate_orm_test</POSTGRES_PASSWORD>
196-
<POSTGRES_DB>hibernate_orm_test</POSTGRES_DB>
197-
</env>
198-
<ports>
199-
<port>5431:5432</port>
200-
</ports>
201-
<wait>
202-
<!-- For some reason, Postgres will tell us it's ready *twice*,
203-
and that's the truth the second time only.
204-
See https://github.com/fabric8io/docker-maven-plugin/issues/628
205-
See also how the condition is configured in testcontainers:
206-
https://github.com/testcontainers/testcontainers-java/blob/c64aab9fd5e3a452ee0faf793560327eb4da9841/modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLContainer.java#L52-L55 -->
207-
<time>20000</time>
208-
<log>(?s)ready to accept connections.*ready to accept connections</log>
209-
</wait>
210-
</run>
211-
</image>
212-
</images>
213-
<!--Stops all postgres images currently running, not just those we just started.
214-
Useful to stop processes still running from a previously failed integration test run -->
215-
<allContainers>true</allContainers>
216-
</configuration>
217-
<executions>
218-
<execution>
219-
<id>docker-start</id>
220-
<phase>compile</phase>
221-
<goals>
222-
<goal>stop</goal>
223-
<goal>start</goal>
224-
</goals>
225-
</execution>
226-
<execution>
227-
<id>docker-stop</id>
228-
<phase>post-integration-test</phase>
229-
<goals>
230-
<goal>stop</goal>
231-
</goals>
232-
</execution>
233-
</executions>
234-
</plugin>
235-
<plugin>
236-
<groupId>org.codehaus.mojo</groupId>
237-
<artifactId>exec-maven-plugin</artifactId>
238-
<executions>
239-
<execution>
240-
<id>docker-prune</id>
241-
<phase>generate-resources</phase>
242-
<goals>
243-
<goal>exec</goal>
244-
</goals>
245-
<configuration>
246-
<executable>${docker-prune.location}</executable>
247-
</configuration>
248-
</execution>
249-
</executions>
250-
</plugin>
251-
</plugins>
252-
</build>
253-
</profile>
254192
</profiles>
255193

256194

integration-tests/jpa-postgresql/src/main/resources/application.properties

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
quarkus.datasource.username=hibernate_orm_test
2-
quarkus.datasource.password=hibernate_orm_test
3-
quarkus.datasource.jdbc.url=${postgres.url}
41
quarkus.datasource.jdbc.max-size=8
52

63
quarkus.hibernate-orm.packages=io.quarkus.it.jpa.postgresql.defaultpu
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.quarkus.it.jpa.postgresql;
2+
3+
import static org.hamcrest.Matchers.is;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
import io.quarkus.test.junit.QuarkusTest;
12+
import io.quarkus.test.junit.QuarkusTestProfile;
13+
import io.quarkus.test.junit.TestProfile;
14+
import io.restassured.RestAssured;
15+
16+
/* Exercises OverrideJdbcUrlBuildTimeConfigSource, which cannot coexist with dev services. */
17+
@TestProfile(ConfigOverrideTest.Profile.class)
18+
@QuarkusTest
19+
public class ConfigOverrideTest {
20+
21+
@Test
22+
public void base() {
23+
RestAssured.when().get("/jpa/testfunctionality/base").then().body(is("OK"));
24+
}
25+
26+
public static class Profile implements QuarkusTestProfile {
27+
@Override
28+
public Map<String, String> getConfigOverrides() {
29+
// Disable dev services so we know we're connecting to the right database
30+
return Map.of("quarkus.devservices.enabled", "false");
31+
}
32+
33+
@Override
34+
public String getConfigProfile() {
35+
return "someotherprofile";
36+
}
37+
38+
@Override
39+
public List<TestResourceEntry> testResources() {
40+
return Collections.singletonList(new TestResourceEntry(PostgresTestResourceLifecycleManager.class));
41+
}
42+
}
43+
}

integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/OverrideJdbcUrlBuildTimeConfigSource.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.microprofile.config.ConfigProvider;
88
import org.eclipse.microprofile.config.spi.ConfigSource;
99

10+
import io.quarkus.runtime.configuration.ConfigUtils;
1011
import io.smallrye.config.Priorities;
1112
import io.smallrye.config.common.MapBackedConfigSource;
1213

@@ -23,6 +24,12 @@ public String getValue(final String propertyName) {
2324
return super.getValue(propertyName);
2425
}
2526

27+
boolean someotherprofile = ConfigUtils.isProfileActive("someotherprofile");
28+
// This config source should only kick in when the custom profile is active, or dev services get disabled for all tests
29+
if (!someotherprofile) {
30+
return super.getValue(propertyName);
31+
}
32+
2633
boolean isBuildTime = false;
2734
for (ConfigSource configSource : ConfigProvider.getConfig().getConfigSources()) {
2835
if (configSource.getName().equals("PropertiesConfigSource[source=Build system]")) {
@@ -31,10 +38,13 @@ public String getValue(final String propertyName) {
3138
}
3239
}
3340

41+
// Originally, the JDBC Extension queried the JDBC URL value at build time to either start or skip the DevService. In cases where the URL was an expansion, this would fail if the expansion is not available at build time. In the original issue, the JDBC URL was set as an expansion in Vault, only available at runtime.
42+
// To simulate the original issue, the source sets quarkus.datasource.jdbc.url at build time, and sets it to an 'impossible' expansion.
3443
if (isBuildTime) {
35-
return "${postgres.url}";
44+
return "${arbitrary.unavailable.value}";
3645
}
3746

47+
// To keep things working, we can then either return nothing at runtime so that we default to the normal application config, or return valid config at runtime.
3848
return super.getValue(propertyName);
3949
}
4050
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.quarkus.it.jpa.postgresql;
2+
3+
import java.util.Map;
4+
5+
import org.testcontainers.containers.PostgreSQLContainer;
6+
7+
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
8+
9+
public class PostgresTestResourceLifecycleManager implements QuarkusTestResourceLifecycleManager {
10+
11+
private static PostgreSQLContainer<?> postgres;
12+
13+
@SuppressWarnings("resource")
14+
@Override
15+
public Map<String, String> start() {
16+
postgres = new PostgreSQLContainer<>("postgres:14") // the exact value doesn't really matter here
17+
.withDatabaseName("testdb")
18+
.withUsername("test")
19+
.withPassword("test");
20+
21+
postgres.start();
22+
23+
return Map.of("quarkus.datasource.jdbc.url", postgres.getJdbcUrl(), "quarkus.datasource.username", "test",
24+
"quarkus.datasource.password", "test");
25+
}
26+
27+
@Override
28+
public void stop() {
29+
30+
if (postgres != null) {
31+
postgres.stop();
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)