Skip to content

Commit

Permalink
Fetch aerie/postgres db user usernames from envvars
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythicaeda committed Apr 17, 2024
1 parent c9ad1c2 commit b1257be
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;

import java.io.File;
Expand Down Expand Up @@ -34,22 +35,28 @@ public DatabaseTestHelper(String dbName, String appName) throws SQLException, IO
* Sets up the test database
*/
private HikariDataSource startDatabase() throws IOException, InterruptedException {
// Load database admin credentials from the environment
final var aerieUsername = getEnv("AERIE_USERNAME");
final var aeriePassword = getEnv("AERIE_PASSWORD");

final var postgresUsername = getEnv("POSTGRES_USER");
final var postgresPassword = getEnv("POSTGRES_PASSWORD");

// Create test database and grant privileges
{
final var pb = new ProcessBuilder("psql",
"postgresql://postgres:postgres@localhost:5432",
"postgresql://"+postgresUsername+":"+postgresPassword+"@localhost:5432/postgres",
"-v", "ON_ERROR_STOP=1",
"-c", "CREATE DATABASE " + dbName + ";",
"-c", "GRANT ALL PRIVILEGES ON DATABASE " + dbName + " TO aerie;"
"-c", "GRANT ALL PRIVILEGES ON DATABASE " + dbName + " TO "+aerieUsername+";"
);

final var proc = pb.start();

// Handle the case where we cannot connect to postgres by skipping the tests
final var errors = new String(proc.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
Assumptions.assumeFalse(
( errors.contains("Connection refused")
|| errors.contains("role \"postgres\" does not exist")));
|| errors.contains("role \""+postgresUsername+"\" does not exist")));
proc.waitFor();
proc.destroy();
}
Expand All @@ -58,9 +65,9 @@ private HikariDataSource startDatabase() throws IOException, InterruptedExceptio
// Apparently, the previous privileges are insufficient on their own
{
final var pb = new ProcessBuilder("psql",
"postgresql://aerie:aerie@localhost:5432/" + dbName,
"postgresql://"+aerieUsername+":"+aeriePassword+"@localhost:5432/" + dbName,
"-v", "ON_ERROR_STOP=1",
"-c", "ALTER DEFAULT PRIVILEGES GRANT ALL ON TABLES TO aerie;",
"-c", "ALTER DEFAULT PRIVILEGES GRANT ALL ON TABLES TO "+aerieUsername+";",
"-c", "\\ir %s".formatted(initSqlScriptFile.getAbsolutePath())
);

Expand All @@ -77,8 +84,9 @@ private HikariDataSource startDatabase() throws IOException, InterruptedExceptio
hikariConfig.addDataSourceProperty("portNumber", "5432");
hikariConfig.addDataSourceProperty("databaseName", dbName);
hikariConfig.addDataSourceProperty("applicationName", appName);
hikariConfig.setUsername("aerie");
hikariConfig.setPassword("aerie");

hikariConfig.setUsername(aerieUsername);
hikariConfig.setPassword(aeriePassword);

hikariConfig.setConnectionInitSql("set time zone 'UTC'");

Expand All @@ -92,12 +100,16 @@ public void close() throws SQLException, IOException, InterruptedException {
Assumptions.assumeTrue(connection != null);
connection.close();

// Grab postgres credentials from environment
final var postgresUsername = getEnv("POSTGRES_USER");
final var postgresPassword = getEnv("POSTGRES_PASSWORD");

// Clear out all data from the database on test conclusion
// This is done WITH (FORCE) so there aren't issues with trying
// to drop a database while there are connected sessions from
// dev tools
final var pb = new ProcessBuilder("psql",
"postgresql://postgres:postgres@localhost:5432",
"postgresql://"+postgresUsername+":"+postgresPassword+"@localhost:5432/postgres",
"-v", "ON_ERROR_STOP=1",
"-c", "DROP DATABASE IF EXISTS " + dbName + " WITH (FORCE);"
);
Expand All @@ -114,6 +126,11 @@ public Connection connection() {
return connection;
}

private static String getEnv(final String key) {
final var env = System.getenv(key);
return env == null ? Assertions.fail("Could not find envvar: "+key) : env;
}

public void clearTable(@Language(value="SQL", prefix="SELECT * FROM ") String table) throws SQLException {
try (final var statement = connection.createStatement()) {
statement.executeUpdate("TRUNCATE " + table + " CASCADE;");
Expand Down

0 comments on commit b1257be

Please sign in to comment.