Browse Source

Fix handling of env vars in Bitnami's Postgres image

See gh-43783

Signed-off-by: He Zean <realhezean@gmail.com>
pull/43904/head
He Zean 11 months ago committed by Andy Wilkinson
parent
commit
c8f2fb0d94
  1. 7
      spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java
  2. 2
      spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests.java
  3. 4
      spring-boot-project/spring-boot-docker-compose/src/dockerTest/resources/org/springframework/boot/docker/compose/service/connection/postgres/postgres-bitnami-compose.yaml
  4. 33
      spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironment.java
  5. 35
      spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresEnvironmentTests.java

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

@ -33,13 +33,16 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -33,13 +33,16 @@ import static org.assertj.core.api.Assertions.assertThat;
class PostgresJdbcDockerComposeConnectionDetailsFactoryIntegrationTests {
@DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL)
void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) {
void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) throws ClassNotFoundException {
assertConnectionDetails(connectionDetails);
checkDatabaseAccess(connectionDetails);
}
@DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL)
void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) {
void runWithBitnamiImageCreatesConnectionDetails(JdbcConnectionDetails connectionDetails)
throws ClassNotFoundException {
assertConnectionDetails(connectionDetails);
checkDatabaseAccess(connectionDetails);
}
private void assertConnectionDetails(JdbcConnectionDetails connectionDetails) {

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

@ -37,11 +37,13 @@ class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests { @@ -37,11 +37,13 @@ class PostgresR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests {
@DockerComposeTest(composeFile = "postgres-compose.yaml", image = TestImage.POSTGRESQL)
void runCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) {
assertConnectionDetails(connectionDetails);
checkDatabaseAccess(connectionDetails);
}
@DockerComposeTest(composeFile = "postgres-bitnami-compose.yaml", image = TestImage.BITNAMI_POSTGRESQL)
void runWithBitnamiImageCreatesConnectionDetails(R2dbcConnectionDetails connectionDetails) {
assertConnectionDetails(connectionDetails);
checkDatabaseAccess(connectionDetails);
}
private void assertConnectionDetails(R2dbcConnectionDetails connectionDetails) {

4
spring-boot-project/spring-boot-docker-compose/src/dockerTest/resources/org/springframework/boot/docker/compose/service/connection/postgres/postgres-bitnami-compose.yaml

@ -4,6 +4,6 @@ services: @@ -4,6 +4,6 @@ services:
ports:
- '5432'
environment:
- 'POSTGRESQL_USER=myuser'
- 'POSTGRESQL_DB=mydatabase'
- 'POSTGRESQL_USERNAME=myuser'
- 'POSTGRESQL_DATABASE=mydatabase'
- 'POSTGRESQL_PASSWORD=secret'

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,9 +28,18 @@ import org.springframework.util.StringUtils; @@ -28,9 +28,18 @@ import org.springframework.util.StringUtils;
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author He Zean
*/
class PostgresEnvironment {
private static final String[] USERNAME_KEYS = new String[] { "POSTGRES_USER", "POSTGRES_USERNAME",
"POSTGRESQL_USER", "POSTGRESQL_USERNAME" };
private static final String DEFAULT_USERNAME = "postgres";
private static final String[] DATABASE_KEYS = new String[] { "POSTGRES_DB", "POSTGRES_DATABASE",
"POSTGRESQL_DATABASE" };
private final String username;
private final String password;
@ -38,9 +47,18 @@ class PostgresEnvironment { @@ -38,9 +47,18 @@ class PostgresEnvironment {
private final String database;
PostgresEnvironment(Map<String, String> env) {
this.username = env.getOrDefault("POSTGRES_USER", env.getOrDefault("POSTGRESQL_USER", "postgres"));
this.username = extractUsername(env);
this.password = extractPassword(env);
this.database = env.getOrDefault("POSTGRES_DB", env.getOrDefault("POSTGRESQL_DB", this.username));
this.database = extractDatabase(env);
}
private String extractUsername(Map<String, String> env) {
for (String key : USERNAME_KEYS) {
if (env.containsKey(key)) {
return env.get(key);
}
}
return DEFAULT_USERNAME;
}
private String extractPassword(Map<String, String> env) {
@ -49,6 +67,15 @@ class PostgresEnvironment { @@ -49,6 +67,15 @@ class PostgresEnvironment {
return password;
}
private String extractDatabase(Map<String, String> env) {
for (String key : DATABASE_KEYS) {
if (env.containsKey(key)) {
return env.get(key);
}
}
return this.username;
}
String getUsername() {
return this.username;
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException; @@ -31,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author He Zean
*/
class PostgresEnvironmentTests {
@ -66,6 +67,20 @@ class PostgresEnvironmentTests { @@ -66,6 +67,20 @@ class PostgresEnvironmentTests {
assertThat(environment.getUsername()).isEqualTo("me");
}
@Test
void getUsernameWhenHasPostgresUsername() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRES_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("me");
}
@Test
void getUsernameWhenHasPostgresqlUsername() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("me");
}
@Test
void getPasswordWhenHasPostgresPassword() {
PostgresEnvironment environment = new PostgresEnvironment(Map.of("POSTGRES_PASSWORD", "secret"));
@ -104,6 +119,13 @@ class PostgresEnvironmentTests { @@ -104,6 +119,13 @@ class PostgresEnvironmentTests {
assertThat(environment.getDatabase()).isEqualTo("me");
}
@Test
void getDatabaseWhenNoPostgresqlDatabaseAndPostgresqlUsername() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_USERNAME", "me", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("me");
}
@Test
void getDatabaseWhenHasPostgresDb() {
PostgresEnvironment environment = new PostgresEnvironment(
@ -112,9 +134,16 @@ class PostgresEnvironmentTests { @@ -112,9 +134,16 @@ class PostgresEnvironmentTests {
}
@Test
void getDatabaseWhenHasPostgresqlDb() {
void getDatabaseWhenHasPostgresDatabase() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRES_DATABASE", "db", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
@Test
void getDatabaseWhenHasPostgresqlDatabase() {
PostgresEnvironment environment = new PostgresEnvironment(
Map.of("POSTGRESQL_DB", "db", "POSTGRESQL_PASSWORD", "secret"));
Map.of("POSTGRESQL_DATABASE", "db", "POSTGRESQL_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("db");
}

Loading…
Cancel
Save