diff --git a/.claude/skills/naksha-test/SKILL.md b/.claude/skills/naksha-test/SKILL.md index 4ba73af23..df86a94eb 100644 --- a/.claude/skills/naksha-test/SKILL.md +++ b/.claude/skills/naksha-test/SKILL.md @@ -4,7 +4,7 @@ description: Use ONLY when asked to run tests for the Naksha project. Do NOT use --- # General -Most tests require a database. If the tests are executed without environment variables, they will start docker containers. When unclear, ask the user if they want to run the tests using automatically created Docker containers, or if they prefer to run the tests against their own, possibly local, PostgresQL test database. +Most tests require Postgres. `here-naksha-lib-psql` starts a Docker container if no database URL is configured. The app-service tests default to a standalone Postgres at localhost; use `TEST_CONTAINERS` to have them start a managed container. When unclear, ask the user which mode they want. # Environment Variables All environment variables contain some placeholders that need to be replaced: @@ -16,24 +16,29 @@ All environment variables contain some placeholders that need to be replaced: You can test the connection to the database. If you detect that the connection to the database fails due to wrong credentials or hostname, ask the user for host, port, user, and password _(whatever is needed)_. Use defaults for any value not provided. Tell the user the defaults. -## Library tests (here-naksha-lib-psql) -Only needs one variable. If not set, Docker auto-starts: +## Shared Postgres URL +Set one shared URL when the tests should all use the same standalone Postgres instance: ```bash -export NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false" +export NAKSHA_TEST_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false" ``` -## Server tests (here-naksha-app-service) -Needs all variables. These tests require a running Naksha server and will fail without one. Skip unless explicitly asked: +`NAKSHA_TEST_DB_URL` is used by `here-naksha-lib-psql` and as the fallback for admin and data +storage in `here-naksha-app-service`. The specific variables `NAKSHA_TEST_PSQL_DB_URL`, +`NAKSHA_TEST_ADMIN_DB_URL`, and `NAKSHA_TEST_DATA_DB_URL` override it for their respective test +storage when separate databases are needed. `NAKSHA_TEST_STORAGE_ID` defaults to +`local_psql_test_storage`, and the Gradle test tasks default `HUB_ADMIN_STORAGE_ID` to the same value, +so both suites agree on the admin catalog ID without setting them. -```bash -export NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE -export NAKSHA_TEST_STORAGE_ID=local_psql_test_storage -export HUB_ADMIN_STORAGE_ID=local_psql_test_storage -export NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false" -export NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false" -export NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false" -``` +Use a dedicated test database: test setup may drop Naksha schemas. +For app-service tests, per-storage URLs in `~/.config/naksha/test_admin_db.url` and +`~/.config/naksha/test_data_db.url` take precedence over environment variables. + +## App-service tests (`here-naksha-app-service`) +These tests start a Naksha app instance as part of the test suite; no separately running server is +required. By default, they expect standalone Postgres on localhost, so the context variable is +optional. Set `NAKSHA_APP_SERVICE_TEST_CONTEXT=TEST_CONTAINERS` to use the app-service managed +container instead. # Commands @@ -44,8 +49,8 @@ Docker auto-starts if no env vars are set: ./gradlew :here-naksha-lib-model:jvmTest :here-naksha-lib-psql:jvmTest :here-naksha-lib-jbon:jvmTest :here-naksha-lib-geo:jvmTest ``` -## All JVM tests (includes server tests that will fail without a running server): -Docker auto-starts if no env vars are set. This includes `here-naksha-app-service:jvmTest` which requires a running Naksha server and will fail with `ConnectException` if no server is available: +## All JVM tests: +App-service tests start Naksha as part of the suite. They default to standalone Postgres at localhost; set `NAKSHA_TEST_DB_URL` if it is running elsewhere, or explicitly select `TEST_CONTAINERS` for the app-service managed container. `here-naksha-lib-psql` starts its own container when neither URL variable is set. ```bash ./gradlew jvmTest @@ -56,8 +61,8 @@ Docker auto-starts if no env vars are set. This includes `here-naksha-app-servic ./gradlew :here-naksha-lib-model:jsTest :here-naksha-lib-jbon:jsTest :here-naksha-lib-geo:jsTest ``` -## Server tests -Only run if user explicitly asks. Requires a running Naksha server: +## App-service tests +The test suite starts its own Naksha app instance and requires standalone Postgres or the `TEST_CONTAINERS` context: ```bash ./gradlew :here-naksha-app-service:jvmTest @@ -65,6 +70,6 @@ Only run if user explicitly asks. Requires a running Naksha server: # Common Issues - Kotlintest discovery errors: If `here-naksha-lib-psql:jvmTest` fails with test discovery errors, try `./gradlew clean` first -- Docker not available: The psql tests require Docker. If Docker isn't running, set `NAKSHA_TEST_PSQL_DB_URL` to an external Postgres instance -- Port conflicts: The Docker container uses host port 15432. If this port is in use, the container will fail to start -- Server tests fail with ConnectException: This is expected when no Naksha server is running. Skip these tests unless the server is available +- Connection refused: verify that the standalone Postgres host and port in `NAKSHA_TEST_DB_URL` are reachable from the Gradle process +- Port conflicts: The `here-naksha-lib-psql` Docker container uses host port 15432. If this port is in use, it will fail to start +- App-service tests fail with ConnectException: verify that Postgres is available and that the app-service test context is configured as intended diff --git a/README.md b/README.md index d6d46ef62..16914a436 100644 --- a/README.md +++ b/README.md @@ -248,15 +248,29 @@ Naksha tests can currently run in two modes: 1. Against a Postgres database that you start yourself (for example the containerized Postgres described above). 2. Against a Postgres container started automatically by the build via [Testcontainers](https://github.com/testcontainers). -Before running Gradle, set the environment variables for the mode you want. +For a standalone Postgres instance, set `NAKSHA_TEST_DB_URL` to its JDBC URL. This shared URL is used by +`here-naksha-lib-psql` tests and by both admin and data storage tests in `here-naksha-app-service`. +The app-service test context defaults to standalone Postgres when `NAKSHA_APP_SERVICE_TEST_CONTEXT` is +unset. ### Option 1: Run tests against a local standalone Postgres ```bash -export NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE -export NAKSHA_TEST_STORAGE_ID=local_psql_test_storage -export HUB_ADMIN_STORAGE_ID=local_psql_test_storage +export NAKSHA_TEST_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password&ssl=false" +./gradlew cleanAndTest +``` + +`NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE` is optional because standalone is the default. +All test suites sharing one database must agree on the storage ID stored in the admin catalog. The test +storage ID (`NAKSHA_TEST_STORAGE_ID`) defaults to `local_psql_test_storage`, and the Gradle test tasks set +`HUB_ADMIN_STORAGE_ID` to the same value unless it is set explicitly, so neither needs to be exported. + +Use a dedicated test database: test setup may drop Naksha schemas. If tests need different databases, +override the shared URL with one or more specific URLs. A specific URL takes precedence over +`NAKSHA_TEST_DB_URL` for its test storage: + +```bash # For `here-naksha-lib-psql` tests. export NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password" @@ -264,20 +278,20 @@ export NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=p export NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password" # For `here-naksha-app-service` data storage tests. -# Kept separate from the admin DB URL to allow deployment-like test setups, -# where administrative entities are stored in a different database/storage than data entities. -export NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password" - -./gradlew cleanAndTest +export NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/other_db?user=postgres&password=password" ``` +For app-service tests, a URL in `~/.config/naksha/test_admin_db.url` or +`~/.config/naksha/test_data_db.url` takes precedence over the corresponding environment variables. + ### Option 2: Run tests with Testcontainers-managed Postgres +The `here-naksha-lib-psql` tests start their own Postgres container when neither +`NAKSHA_TEST_PSQL_DB_URL` nor `NAKSHA_TEST_DB_URL` is set. The app-service tests also need their +context set to `TEST_CONTAINERS` to start their managed container. + ```bash export NAKSHA_APP_SERVICE_TEST_CONTEXT=TEST_CONTAINERS -export HUB_ADMIN_STORAGE_ID=local_psql_test_storage -export NAKSHA_TEST_STORAGE_ID=local_psql_test_storage - ./gradlew cleanAndTest ``` diff --git a/ai/test.md b/ai/test.md index adb144a04..79d9e808d 100644 --- a/ai/test.md +++ b/ai/test.md @@ -1,23 +1,17 @@ # Tests -Always first clear the local storage _(please replave `PGPASSWORD` with you own local password)_: +Use a dedicated test Postgres database. Test setup can drop Naksha schemas, so do not point these commands at a database with data you need to keep. Replace the example password with your test database password. ## Clear Storage ```bash -# Clear local storage storage -PGPASSWORD='XprclPDUTMsWpHvvtusuVCNDWsJZPVTP' \ -NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ -NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ -NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ +# Clear schemas from the dedicated local test database only. +PGPASSWORD='password' \ psql -h localhost -p 5432 -U postgres -c 'DROP SCHEMA IF EXISTS "naksha-hub-admin" CASCADE; DROP SCHEMA IF EXISTS naksha_data_schema CASCADE; DROP SCHEMA IF EXISTS "naksha~admin" CASCADE;' ``` ## Run all tests ```bash -# Run app-service tests -PGPASSWORD='XprclPDUTMsWpHvvtusuVCNDWsJZPVTP' \ -NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ -NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ -NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ +# Run all JVM tests against the dedicated local test database. +NAKSHA_TEST_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password&ssl=false" \ NAKSHA_SUPPRESS_PERF_TEST=true \ NAKSHA_DEBUG=true \ ./gradlew jvmTest @@ -27,14 +21,19 @@ The environment variable `NAKSHA_SUPPRESS_PERF_TEST=true` will disable performan ## Run a specific test ```bash -# Run app-service tests -PGPASSWORD='XprclPDUTMsWpHvvtusuVCNDWsJZPVTP' \ -NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ -NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ -NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \ +# Run the PostgreSQL library tests. +NAKSHA_TEST_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password&ssl=false" \ NAKSHA_SUPPRESS_PERF_TEST=true \ NAKSHA_DEBUG=true \ ./gradlew :here-naksha-lib-psql:jvmTest ``` +For only app-service tests: + +```bash +NAKSHA_TEST_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password&ssl=false" \ +NAKSHA_SUPPRESS_PERF_TEST=true \ +./gradlew :here-naksha-app-service:jvmTest +``` + If a specific individual test is wished, just append `--tests "{full qualified classname}"` to the above `gradlew` call. diff --git a/build.gradle.kts b/build.gradle.kts index ee37c0472..5000d76c2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -262,6 +262,13 @@ allprojects { from(rootProject.file("LICENSE")) into("") } + tasks.withType().configureEach { + // Align the hub admin storage ID with the default test storage ID, so that all test suites + // sharing one database agree on the admin catalog ID (otherwise: STORAGE_ID_MISMATCH). + if (System.getenv("HUB_ADMIN_STORAGE_ID") == null) { + environment("HUB_ADMIN_STORAGE_ID", System.getenv("NAKSHA_TEST_STORAGE_ID") ?: "local_psql_test_storage") + } + } val jsNoduleConfig = jsModules[_name] pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { extensions.configure { @@ -311,8 +318,6 @@ allprojects { getByName("jvmTest") { useJUnitPlatform() maxHeapSize = "8g" - val dbUrl = System.getenv("NAKSHA_TEST_PSQL_DB_URL") - if (dbUrl != null) environment("NAKSHA_TEST_PSQL_DB_URL", dbUrl) } } diff --git a/here-naksha-app-service/src/jvmTest/POSTGRES_SETUP.md b/here-naksha-app-service/src/jvmTest/POSTGRES_SETUP.md index 43fd99c0c..a9916917c 100644 --- a/here-naksha-app-service/src/jvmTest/POSTGRES_SETUP.md +++ b/here-naksha-app-service/src/jvmTest/POSTGRES_SETUP.md @@ -6,38 +6,50 @@ Currently, there are two options to provision Postgres in test environment - utilizing TestContainers library that initializes container with Postgres image ([described in detail below](#test-containers)) -To choose the desired option, one should define `NAKSHA_LOCAL_TEST_CONTEXT` environment variable which is checked upon test start to decide how to act. +The app-service tests use `NAKSHA_APP_SERVICE_TEST_CONTEXT` to choose how Postgres is provided. If the variable is unset, tests use a standalone Postgres instance. -| NAKSHA_LOCAL_TEST_CONTEXT | Chosen option | +| `NAKSHA_APP_SERVICE_TEST_CONTEXT` | Chosen option | | - | - | | undefined / empty | Local standalone Postgres instance | -| 'LOCAL_STANDALONE' | Local standalone Postgres instance | -| 'TEST_CONTAINERS' | Container spawned by TestContainers | -| 'unknown value' | Local standalone Postgres instance | +| `LOCAL_STANDALONE` | Local standalone Postgres instance | +| `TEST_CONTAINERS` | Container spawned by Testcontainers | +| Unknown value | Local standalone Postgres instance | + +For tests that use a standalone database, the shared URL variable `NAKSHA_TEST_DB_URL` can configure +the connection for `here-naksha-lib-psql` as well as both admin and data storage tests in +`here-naksha-app-service`. The specific variables `NAKSHA_TEST_PSQL_DB_URL`, +`NAKSHA_TEST_ADMIN_DB_URL`, and `NAKSHA_TEST_DATA_DB_URL` can override the shared URL when a test +needs a separate database. When running both suites against the same database, their admin catalog +IDs must match: `NAKSHA_TEST_STORAGE_ID` defaults to `local_psql_test_storage`, and the Gradle test +tasks default `HUB_ADMIN_STORAGE_ID` to the same value, so neither has to be set. Ensure the URL points to a dedicated test database: the tests may drop Naksha schemas during +setup. + +For app-service tests, per-storage URLs in `~/.config/naksha/test_admin_db.url` and +`~/.config/naksha/test_data_db.url` take precedence over environment variables. Running tests (or any other `gradle` task) that triggers JUnit pipeline "happens as usual": | Command | Behavior | | - | - | | `gradle test` | will run gradle tests expecting local standalone Postgres instance | -| `NAKSHA_LOCAL_TEST_CONTEXT=TEST_CONTAINERS gradle test` | will run gradle test in TestContainers mode | -| `NAKSHA_LOCAL_TEST_CONTEXT=LOCAL_STANDALONE gradle test` | will run gradle tests expecting local standalone Postgres instance | +| `NAKSHA_APP_SERVICE_TEST_CONTEXT=TEST_CONTAINERS gradle test` | will run gradle test in Testcontainers mode | +| `NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE gradle test` | will run gradle tests expecting local standalone Postgres instance | The same applies to other gradle tasks one might want to use that depend on `test` task (like `shadowJar`). See [the main project README](../../../README.md) to get familiar with all options. ### Local standalone Postgres instance \[default\] This is the default and most basic approach. It assumes that there's a Postgres DB running in our local environment.\ -That is also the way our tests are executed on GH pipeline. +The CI pipeline may use either this option or Testcontainers; see the workflow configuration for the selected mode. #### Prerequisites: - Postgres with PostGIS extension running on localhost -- Postgres instance must be publishing **5432** port -- Environment variable `NAKSHA_LOCAL_TEST_CONTEXT` must be null, empty or set to `LOCAL_STANDALONE` +- By default, Postgres is expected on localhost port **5432**; use `NAKSHA_TEST_DB_URL` to specify another host or port +- Environment variable `NAKSHA_APP_SERVICE_TEST_CONTEXT` can be unset or set to `LOCAL_STANDALONE` #### Notes: - The schema used by tests: `naksha_data_schema` -- This schema will dropped before test suite starts +- This schema will be dropped before the test suite starts - When running tests with local standalone instance, tests don't expect any extensions enabled (see [test-config.json](../jvmMain/resources/test-config.json) - Related class: `com.here.naksha.app.init.context.LocalTestContext` diff --git a/here-naksha-app-service/src/jvmTest/java/com/here/naksha/app/init/TestStorageConfig.java b/here-naksha-app-service/src/jvmTest/java/com/here/naksha/app/init/TestStorageConfig.java index 0290b05e6..fc4b68e8b 100644 --- a/here-naksha-app-service/src/jvmTest/java/com/here/naksha/app/init/TestStorageConfig.java +++ b/here-naksha-app-service/src/jvmTest/java/com/here/naksha/app/init/TestStorageConfig.java @@ -17,8 +17,9 @@ public TestStorageConfig(String mapId, PgConfig pgConfig) { } /** - * Reads the configuration from a configuration file from user home directory ({@code ~/.config/naksha/filename}) or from the environment - * variable, if none is possible, a default localhost configuration is used. + * Reads the configuration from a configuration file from user home directory ({@code ~/.config/naksha/filename}), or from the + * {@code envName} environment variable, or from the shared {@code NAKSHA_TEST_DB_URL} environment variable; if none is set, a + * default localhost configuration is used. * * @param filename The filename to search for in {@code ~/.config/naksha/}. * @param envName The environment variable to check. @@ -43,7 +44,7 @@ public TestStorageConfig(String mapId, PgConfig pgConfig) { PgConfig pgConfig = new PgConfig(NakshaHubAdminStorageIdentifiers.getHubAdminStorageId()).withMasterUri(url); return new TestStorageConfig(mapId, pgConfig); } - url = System.getenv("TEST_NAKSHA_PSQL_URL"); + url = System.getenv("NAKSHA_TEST_DB_URL"); if (url != null && url.startsWith("jdbc:postgresql://")) { PgConfig pgConfig = new PgConfig(NakshaHubAdminStorageIdentifiers.getHubAdminStorageId()).withMasterUri(url); return new TestStorageConfig(mapId, pgConfig); diff --git a/here-naksha-lib-psql/src/jvmMain/kotlin/naksha/psql/PsqlTestStorage.kt b/here-naksha-lib-psql/src/jvmMain/kotlin/naksha/psql/PsqlTestStorage.kt index cd82dd540..25e64b219 100644 --- a/here-naksha-lib-psql/src/jvmMain/kotlin/naksha/psql/PsqlTestStorage.kt +++ b/here-naksha-lib-psql/src/jvmMain/kotlin/naksha/psql/PsqlTestStorage.kt @@ -13,7 +13,8 @@ import java.util.concurrent.atomic.AtomicReference * The class to be used as `className` when running local tests. * * If the given configuration contains a `master` configuration, it behaves exactly as [PsqlStorage]. However, if this is not the case, it will perform the following steps in order: - * - If there is an environment variables `NAKSHA_TEST_PSQL_DB_URL` set, it reads the master-URI from this. + * - If there is an environment variable `NAKSHA_TEST_PSQL_DB_URL` set, it reads the master-URI from this. + * - Otherwise, if there is an environment variable `NAKSHA_TEST_DB_URL` set, it reads the master-URI from this. * - If there is a system property `naksha.test.psql.db.url` set _(`-Dnaksha.test.psql.db.url=...`)_, then it reads the master-URI from this. * - If neither is available, it will start a docker container, and use this with database `postgres`, user `postgres`, and password `password` _(which is automatically shutdown when the JVM goes down via shutdown hook)_. * @@ -97,7 +98,7 @@ class PsqlTestStorage : PsqlStorage() { var master = config.getMasterOrNull() if (master == null) { // - read from environment - var uri = System.getenv("NAKSHA_TEST_PSQL_DB_URL") + var uri = System.getenv("NAKSHA_TEST_PSQL_DB_URL") ?: System.getenv("NAKSHA_TEST_DB_URL") // - read from system (for Storage-API support) if (uri == null || uri.isEmpty()) { uri = System.getProperty("naksha.test.psql.db.url") @@ -113,6 +114,7 @@ class PsqlTestStorage : PsqlStorage() { // Use the original URI string (from env var or docker) to avoid ssl=true being appended by masterUri. try { val jdbcUrl = System.getenv("NAKSHA_TEST_PSQL_DB_URL") + ?: System.getenv("NAKSHA_TEST_DB_URL") ?: System.getProperty("naksha.test.psql.db.url") ?: config.masterUri // Only perform the destructive schema drop once per JVM lifetime.