Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions applications/credhub-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dependencies {
// DB
implementation("org.postgresql:postgresql")
implementation("org.mariadb.jdbc:mariadb-java-client:${mariadbJdbcVersion}")
runtimeOnly("software.amazon.jdbc:aws-advanced-jdbc-wrapper:${awsAdvancedJdbcVersion}")
implementation("org.flywaydb:flyway-core:${flywayVersion}")
implementation("org.flywaydb:flyway-mysql:${flywayVersion}")
implementation("org.flywaydb:flyway-database-postgresql:${flywayVersion}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
spring:
datasource:
username: ${AURORA_DB_USERNAME:root}
password: ${AURORA_DB_PASSWORD:}
url: jdbc:aws-wrapper:mariadb://${AURORA_DB_HOST_PORT:localhost:3306}/credhub_dev

hikari:
# TODO: Cursor-generated. Review.
# Connection pool settings optimized for Aurora
maximum-pool-size: 20
minimum-idle: 5
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
leak-detection-threshold: 60000
flyway:
locations: classpath:/db/migration/common,classpath:/db/migration/mysql
jpa:
properties:
hibernate:
# Needed for hibernate to work with aws-wrapper driver
dialect: org.hibernate.dialect.MariaDBDialect
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spring:
datasource:
# Override these with environment variables for testing:
# AURORA_TEST_DB_URL, AURORA_TEST_DB_USERNAME, AURORA_TEST_DB_PASSWORD
username: ${AURORA_TEST_DB_USERNAME:root}
password: ${AURORA_TEST_DB_PASSWORD:}
url: jdbc:aws-wrapper:mariadb://${AURORA_DB_HOST:localhost}:${AURORA_DB_PORT:3306}/credhub_test
flyway:
locations: classpath:/db/migration/common,classpath:/db/migration/mysql
jpa:
properties:
hibernate:
# Needed for hibernate to work with aws-wrapper driver
dialect: org.hibernate.dialect.MariaDBDialect
1 change: 1 addition & 0 deletions backends/credhub/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dependencies {
implementation("org.flywaydb:flyway-mysql:${flywayVersion}")
implementation("org.flywaydb:flyway-database-postgresql:${flywayVersion}")
implementation("org.mariadb.jdbc:mariadb-java-client:${mariadbJdbcVersion}")
runtimeOnly("software.amazon.jdbc:aws-advanced-jdbc-wrapper:${awsAdvancedJdbcVersion}")
implementation("com.h2database:h2")

// Other
Expand Down
1 change: 1 addition & 0 deletions backends/shared-backend-configuration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation("org.postgresql:postgresql")
implementation("com.h2database:h2")
implementation("org.mariadb.jdbc:mariadb-java-client:${mariadbJdbcVersion}")
runtimeOnly("software.amazon.jdbc:aws-advanced-jdbc-wrapper:${awsAdvancedJdbcVersion}")


// Other
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ buildscript {
ktlintVersion = '1.5.0'
passayVersion = '1.6.6'
springBootVersion = '3.5.8'
mariadbJdbcVersion = '2.7.12' // Bumping to v3 breaks some pipeline jobs, so pinning to v2 for now. v2 (current version) is stable and will be supported until about September 2025 (https://mariadb.com/kb/en/about-mariadb-connector-j/).
mariadbJdbcVersion = '3.5.6'
awsAdvancedJdbcVersion = '2.6.7'
grpcVersion = '1.77.0'
// We need to stay with protobuf-java & protoc 3.25.x as the latest
// grpc still depends on protobuf-java & protoc 3.25.x. Once we
Expand Down
1 change: 1 addition & 0 deletions components/auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
testImplementation("org.flywaydb:flyway-database-postgresql:${flywayVersion}")
implementation("org.postgresql:postgresql")
implementation("org.mariadb.jdbc:mariadb-java-client:${mariadbJdbcVersion}")
runtimeOnly("software.amazon.jdbc:aws-advanced-jdbc-wrapper:${awsAdvancedJdbcVersion}")
implementation("com.h2database:h2")

// Other
Expand Down
1 change: 1 addition & 0 deletions components/credentials/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dependencies {
implementation("org.flywaydb:flyway-database-postgresql:${flywayVersion}")
implementation("org.postgresql:postgresql")
implementation("org.mariadb.jdbc:mariadb-java-client:${mariadbJdbcVersion}")
runtimeOnly("software.amazon.jdbc:aws-advanced-jdbc-wrapper:${awsAdvancedJdbcVersion}")
implementation("com.h2database:h2")

// proto
Expand Down
1 change: 1 addition & 0 deletions components/encryption/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
testImplementation("org.flywaydb:flyway-database-postgresql:${flywayVersion}")
implementation("org.postgresql:postgresql")
implementation("org.mariadb.jdbc:mariadb-java-client:${mariadbJdbcVersion}")
runtimeOnly("software.amazon.jdbc:aws-advanced-jdbc-wrapper:${awsAdvancedJdbcVersion}")
implementation("com.h2database:h2")

// Other
Expand Down
1 change: 1 addition & 0 deletions components/permissions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation("org.postgresql:postgresql")
implementation("com.h2database:h2")
implementation("org.mariadb.jdbc:mariadb-java-client:${mariadbJdbcVersion}")
runtimeOnly("software.amazon.jdbc:aws-advanced-jdbc-wrapper:${awsAdvancedJdbcVersion}")


// Other
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.cloudfoundry.credhub.config

import org.springframework.boot.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.context.annotation.Profile
import org.springframework.core.env.Environment
import org.springframework.jdbc.core.JdbcTemplate
import java.sql.ResultSet
import javax.sql.DataSource

@Profile("unit-test-aurora")
@Configuration
class ParallelAuroraTestDataSourceConfiguration(
private val environment: Environment,
) {
private fun getGradleWorkerId(): String = System.getProperty("org.gradle.test.worker")

private fun createTestDatabaseForWorker(workerId: String) {
val workerDatabaseName = "credhub_test_$workerId"
val baseUrl = environment.getProperty("spring.datasource.url", "jdbc:aws-wrapper:mariadb://localhost:3306/credhub_test")
val username = environment.getProperty("spring.datasource.username", "root")
val password = environment.getProperty("spring.datasource.password", "")

// Extract base URL without database name
val urlWithoutDb = baseUrl.substringBeforeLast("/")

val tempDataSource =
DataSourceBuilder
.create()
.url(urlWithoutDb)
.username(username)
.password(password)
.build()

val jdbcTemplate = JdbcTemplate(tempDataSource)
val noDb =
jdbcTemplate
.query(
"SELECT 1 from INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?;",
{ rs: ResultSet, _: Int -> rs.getBoolean(1) },
workerDatabaseName,
).isEmpty()

if (noDb) {
jdbcTemplate.execute("CREATE DATABASE $workerDatabaseName")
}

tempDataSource.connection.close()
}

@Primary
@Bean(name = ["dataSource"])
fun getParallelTestDataSource(): DataSource {
val workerId = getGradleWorkerId()

createTestDatabaseForWorker(workerId)

val baseUrl = environment.getProperty("spring.datasource.url", "jdbc:aws-wrapper:mariadb://localhost:3306/credhub_test")
val username = environment.getProperty("spring.datasource.username", "root")
val password = environment.getProperty("spring.datasource.password", "")

// Extract base URL without database name and append worker-specific database
val urlWithoutDb = baseUrl.substringBeforeLast("/")
val urlWithWorkerDb = "$urlWithoutDb/credhub_test_$workerId"

val dataSource =
DataSourceBuilder
.create()
.url(urlWithWorkerDb)
.username(username)
.password(password)
.build()

return dataSource
}
}
58 changes: 58 additions & 0 deletions scripts/run_tests_aurora.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash

function set_bash_error_handling() {
set -euo pipefail
}

function go_to_project_root_directory() {
local -r script_dir=$( dirname "${BASH_SOURCE[0]}")

cd "$script_dir/.."
}

function clean_test_databases_mysql() {
echo "🛁 Cleaning mysql databases"
echo ""

local -r credhub_test_databases=$(mysql --user=root --protocol=tcp --execute="SHOW DATABASES LIKE 'credhub_test%';" -sN)

for credhub_test_database in $credhub_test_databases; do
echo "Removing test database: $credhub_test_database"
# Use environment variables for remote connection, fallback to local defaults
# AURORA_DB_HOST: hostname of the Aurora/MySQL server (omit for localhost via TCP)
# AURORA_DB_PORT: port number (omit for default 3306)
# AURORA_DB_USERNAME: database username (defaults to 'root')
# AURORA_DB_PASSWORD: database password (omit if empty)
mysql --protocol=tcp \
${AURORA_DB_HOST:+--host=$AURORA_DB_HOST} \
${AURORA_DB_PORT:+--port=$AURORA_DB_PORT} \
--user=${AURORA_DB_USERNAME:-root} \
${AURORA_DB_PASSWORD:+--password=$AURORA_DB_PASSWORD} \
--execute "DROP DATABASE IF EXISTS $credhub_test_database;"
echo ""
done

echo "🏗️ Creating mysql test database: credhub_test"
mysql --user=root --protocol=tcp --execute "CREATE DATABASE credhub_test;"
echo ""
}

function run_tests_aurora() {
local gradle_test_command="test"
echo "✨ Parallel test mode enabled"
echo "🚀 Running aurora tests"
echo ""

mysql --protocol=tcp --user=root --protocol=tcp --execute "SET GLOBAL max_connections = 1000;"
./gradlew clean $gradle_test_command -Dspring.profiles.active=unit-test-aurora
}

function main() {
set_bash_error_handling
go_to_project_root_directory

clean_test_databases_mysql
run_tests_aurora
}

main