Skip to content

Commit d4f497d

Browse files
committed
Polish "Fix handling of env vars in Bitnami's Postgres image"
See gh-43783
1 parent c8f2fb0 commit d4f497d

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed

spring-boot-project/spring-boot-docker-compose/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ dependencies {
2020
dockerTestRuntimeOnly("com.microsoft.sqlserver:mssql-jdbc")
2121
dockerTestRuntimeOnly("com.oracle.database.r2dbc:oracle-r2dbc")
2222
dockerTestRuntimeOnly("io.r2dbc:r2dbc-mssql")
23+
dockerTestRuntimeOnly("org.postgresql:postgresql")
24+
dockerTestRuntimeOnly("org.postgresql:r2dbc-postgresql")
2325

2426
implementation("com.fasterxml.jackson.core:jackson-databind")
2527
implementation("com.fasterxml.jackson.module:jackson-module-parameter-names")

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,15 @@
1616

1717
package org.springframework.boot.docker.compose.service.connection.postgres;
1818

19+
import java.sql.Driver;
20+
1921
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
2022
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
23+
import org.springframework.boot.jdbc.DatabaseDriver;
2124
import org.springframework.boot.testsupport.container.TestImage;
25+
import org.springframework.jdbc.core.JdbcTemplate;
26+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
27+
import org.springframework.util.ClassUtils;
2228

2329
import static org.assertj.core.api.Assertions.assertThat;
2430

@@ -51,4 +57,21 @@ private void assertConnectionDetails(JdbcConnectionDetails connectionDetails) {
5157
assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:postgresql://").endsWith("/mydatabase");
5258
}
5359

60+
private void checkDatabaseAccess(JdbcConnectionDetails connectionDetails) throws ClassNotFoundException {
61+
assertThat(executeQuery(connectionDetails, DatabaseDriver.POSTGRESQL.getValidationQuery(), Integer.class))
62+
.isEqualTo(1);
63+
}
64+
65+
@SuppressWarnings("unchecked")
66+
private <T> T executeQuery(JdbcConnectionDetails connectionDetails, String sql, Class<T> result)
67+
throws ClassNotFoundException {
68+
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
69+
dataSource.setUrl(connectionDetails.getJdbcUrl());
70+
dataSource.setUsername(connectionDetails.getUsername());
71+
dataSource.setPassword(connectionDetails.getPassword());
72+
dataSource.setDriverClass((Class<? extends Driver>) ClassUtils.forName(connectionDetails.getDriverClassName(),
73+
getClass().getClassLoader()));
74+
return new JdbcTemplate(dataSource).queryForObject(sql, result);
75+
}
76+
5477
}

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,16 @@
1616

1717
package org.springframework.boot.docker.compose.service.connection.postgres;
1818

19+
import java.time.Duration;
20+
21+
import io.r2dbc.spi.ConnectionFactories;
1922
import io.r2dbc.spi.ConnectionFactoryOptions;
2023

2124
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
2225
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
26+
import org.springframework.boot.jdbc.DatabaseDriver;
2327
import org.springframework.boot.testsupport.container.TestImage;
28+
import org.springframework.r2dbc.core.DatabaseClient;
2429

2530
import static org.assertj.core.api.Assertions.assertThat;
2631

@@ -53,4 +58,18 @@ private void assertConnectionDetails(R2dbcConnectionDetails connectionDetails) {
5358
assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.PASSWORD)).isEqualTo("secret");
5459
}
5560

61+
private void checkDatabaseAccess(R2dbcConnectionDetails connectionDetails) {
62+
assertThat(executeQuery(connectionDetails, DatabaseDriver.POSTGRESQL.getValidationQuery(), Integer.class))
63+
.isEqualTo(1);
64+
}
65+
66+
private <T> T executeQuery(R2dbcConnectionDetails connectionDetails, String sql, Class<T> result) {
67+
ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions();
68+
return DatabaseClient.create(ConnectionFactories.get(connectionFactoryOptions))
69+
.sql(sql)
70+
.mapValue(result)
71+
.first()
72+
.block(Duration.ofSeconds(30));
73+
}
74+
5675
}

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironment.java

+8-17
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
*/
3333
class PostgresEnvironment {
3434

35-
private static final String[] USERNAME_KEYS = new String[] { "POSTGRES_USER", "POSTGRES_USERNAME",
36-
"POSTGRESQL_USER", "POSTGRESQL_USERNAME" };
35+
private static final String[] USERNAME_KEYS = new String[] { "POSTGRES_USER", "POSTGRESQL_USER",
36+
"POSTGRESQL_USERNAME" };
3737

3838
private static final String DEFAULT_USERNAME = "postgres";
3939

40-
private static final String[] DATABASE_KEYS = new String[] { "POSTGRES_DB", "POSTGRES_DATABASE",
40+
private static final String[] DATABASE_KEYS = new String[] { "POSTGRES_DB", "POSTGRESQL_DB",
4141
"POSTGRESQL_DATABASE" };
4242

4343
private final String username;
@@ -47,18 +47,18 @@ class PostgresEnvironment {
4747
private final String database;
4848

4949
PostgresEnvironment(Map<String, String> env) {
50-
this.username = extractUsername(env);
50+
this.username = extract(env, USERNAME_KEYS, DEFAULT_USERNAME);
5151
this.password = extractPassword(env);
52-
this.database = extractDatabase(env);
52+
this.database = extract(env, DATABASE_KEYS, this.username);
5353
}
5454

55-
private String extractUsername(Map<String, String> env) {
56-
for (String key : USERNAME_KEYS) {
55+
private String extract(Map<String, String> env, String[] keys, String defaultValue) {
56+
for (String key : keys) {
5757
if (env.containsKey(key)) {
5858
return env.get(key);
5959
}
6060
}
61-
return DEFAULT_USERNAME;
61+
return defaultValue;
6262
}
6363

6464
private String extractPassword(Map<String, String> env) {
@@ -67,15 +67,6 @@ private String extractPassword(Map<String, String> env) {
6767
return password;
6868
}
6969

70-
private String extractDatabase(Map<String, String> env) {
71-
for (String key : DATABASE_KEYS) {
72-
if (env.containsKey(key)) {
73-
return env.get(key);
74-
}
75-
}
76-
return this.username;
77-
}
78-
7970
String getUsername() {
8071
return this.username;
8172
}

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironmentTests.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@ void getUsernameWhenHasPostgresqlUser() {
6767
assertThat(environment.getUsername()).isEqualTo("me");
6868
}
6969

70-
@Test
71-
void getUsernameWhenHasPostgresUsername() {
72-
PostgresEnvironment environment = new PostgresEnvironment(
73-
Map.of("POSTGRES_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
74-
assertThat(environment.getUsername()).isEqualTo("me");
75-
}
76-
7770
@Test
7871
void getUsernameWhenHasPostgresqlUsername() {
7972
PostgresEnvironment environment = new PostgresEnvironment(
@@ -134,9 +127,9 @@ void getDatabaseWhenHasPostgresDb() {
134127
}
135128

136129
@Test
137-
void getDatabaseWhenHasPostgresDatabase() {
130+
void getDatabaseWhenHasPostgresqlDb() {
138131
PostgresEnvironment environment = new PostgresEnvironment(
139-
Map.of("POSTGRES_DATABASE", "db", "POSTGRESQL_PASSWORD", "secret"));
132+
Map.of("POSTGRESQL_DB", "db", "POSTGRESQL_PASSWORD", "secret"));
140133
assertThat(environment.getDatabase()).isEqualTo("db");
141134
}
142135

0 commit comments

Comments
 (0)