Skip to content

Commit

Permalink
1318: Setup backend test database
Browse files Browse the repository at this point in the history
  • Loading branch information
seluianova committed Jul 16, 2024
1 parent 3e55ab3 commit 64ed529
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ jobs:
steps:
- checkout:
path: ~/project
- setup_remote_docker:
docker_layer_caching: true
- run: git submodule sync
- run: git submodule update --init
- install_app_toolbelt
Expand Down
2 changes: 2 additions & 0 deletions .circleci/src/jobs/check_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ working_directory: ~/project/backend
steps:
- checkout:
path: ~/project
- setup_remote_docker:
docker_layer_caching: true
- run: git submodule sync
- run: git submodule update --init
- install_app_toolbelt
Expand Down
6 changes: 6 additions & 0 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ dependencies {
implementation("at.favre.lib:bcrypt:0.10.2")

implementation("com.google.zxing:core:3.5.2") // QR-Codes

testImplementation(platform("org.testcontainers:testcontainers-bom:1.19.8"))
testImplementation("org.testcontainers:testcontainers")
testImplementation("org.testcontainers:postgresql")
// Replace the library version used in testcontainers to avoid vulnerability
testImplementation("org.apache.commons:commons-compress:1.26.2")
}

ktlint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ class Database {

fun setup(config: BackendConfiguration): org.jetbrains.exposed.sql.Database {
val database = setupWithoutMigrationCheck(config)
setupInitialData(config)
return database
}

fun setupInitialData(config: BackendConfiguration) {
transaction {
assertDatabaseIsInSync()
insertOrUpdateProjects(config)
insertOrUpdateRegions()
insertOrUpdateCategories(Companion::executeScript)
createOrReplaceStoreFunctions(Companion::executeScript)
}
return database
}

fun setupWithoutMigrationCheck(config: BackendConfiguration): org.jetbrains.exposed.sql.Database {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package app.ehrenamtskarte.backend

import app.ehrenamtskarte.backend.common.database.Database
import app.ehrenamtskarte.backend.config.BackendConfiguration
import app.ehrenamtskarte.backend.config.PostgresConfig
import app.ehrenamtskarte.backend.migration.MigrationUtils
import app.ehrenamtskarte.backend.projects.database.ProjectEntity
import app.ehrenamtskarte.backend.regions.database.RegionEntity
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.AfterClass
import org.junit.BeforeClass
import org.junit.Test
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.utility.DockerImageName
import kotlin.test.assertEquals

open class IntegrationTest {
companion object {
private var postgisImage = DockerImageName.parse("postgis/postgis:13-3.0-alpine")
private var postgisContainer = PostgreSQLContainer(postgisImage.asCompatibleSubstituteFor("postgres"))

@JvmStatic
@BeforeClass
fun setupDatabase() {
val resource = ClassLoader.getSystemResource("config.test.yml")
?: throw Exception("Configuration file 'config.test.yml' not found")

postgisContainer.start()

val config = BackendConfiguration.load(resource)
.copy(postgres = PostgresConfig(postgisContainer.jdbcUrl, postgisContainer.username, postgisContainer.password))

val database = Database.setupWithoutMigrationCheck(config)
MigrationUtils.applyRequiredMigrations(database)
Database.setupInitialData(config)
}

@JvmStatic
@AfterClass
fun tearDownDatabase() {
postgisContainer.stop()
}
}

/**
* Temporary tests to verify that the database is properly configured
*/
@Test
fun verifyProjectCount() {
var projectCount = 0L
transaction {
projectCount = ProjectEntity.count()
}
assertEquals(2L, projectCount)
}

@Test
fun verifyRegionCount() {
var regionCount = 0L
transaction {
regionCount = RegionEntity.count()
}
assertEquals(94L, regionCount)
}
}
39 changes: 39 additions & 0 deletions backend/src/test/resources/config.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
production: false
postgres:
url: dummy
user: test
password: test
map:
baseUrl: dummy
geocoding:
enabled: false
host: dummy
csvWriter:
enabled: false
server:
host: 0.0.0.0
port: 8000
dataDirectory: ./data
projects:
- id: bayern.ehrenamtskarte.app
importUrl: dummy
pipelineName: dummy
administrationBaseUrl: dummy
administrationName: dummy
timezone: "Europe/Berlin"
smtp:
host: dummy
port: 587
username: OVERRIDE_IN_LOCAL_CONFIG
password: OVERRIDE_IN_LOCAL_CONFIG
- id: nuernberg.sozialpass.app
importUrl: dummy
pipelineName: dummy
administrationBaseUrl: dummy
administrationName: dummy
timezone: "Europe/Berlin"
smtp:
host: dummy
port: 587
username: OVERRIDE_IN_LOCAL_CONFIG
password: OVERRIDE_IN_LOCAL_CONFIG
14 changes: 14 additions & 0 deletions docs/testcontainers-troubleshooting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Testcontainers troubleshooting

Testcontainers is the framework we use for integration tests in the backend.
It allows to manage application dependencies (such as database, etc) in Docker containers in test code.

Running tests based on Testcontainers requires a valid Docker environment.

If you get the following error, make sure your current user has permission to execute `/var/run/docker.sock` and `/var/run/docker.pid`

2024-07-15T19:27:48.460+0000 [DEBUG] [TestEventLogger] [Test worker] INFO org.testcontainers.dockerclient.DockerMachineClientProviderStrategy - docker-machine executable was not found on PATH ([/home/circleci/.local/bin, /home/circleci/.npm-global/bin, /opt/sbt/bin, /opt/gradle/bin, /opt/apache-maven/bin, /home/circleci/bin, /home/circleci/.local/bin, /usr/local/sbin, /usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin])
2024-07-15T19:27:48.460+0000 [DEBUG] [TestEventLogger] [Test worker] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2024-07-15T19:27:48.460+0000 [DEBUG] [TestEventLogger] UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)
2024-07-15T19:27:48.460+0000 [DEBUG] [TestEventLogger] DockerDesktopClientProviderStrategy: failed with exception NullPointerException (Cannot invoke "java.nio.file.Path.toString()" because the return value of "org.testcontainers.dockerclient.DockerDesktopClientProviderStrategy.getSocketPath()" is null)As no valid configuration was found, execution cannot continue.
2024-07-15T19:27:48.460+0000 [DEBUG] [TestEventLogger] See https://java.testcontainers.org/on_failure.html for more details.

0 comments on commit 64ed529

Please sign in to comment.