fix(testnet/homeserver)!: DockerPostgres::shared() to share Postgres instance but isolate databases and remove pubky-test URL query param - #500
Conversation
68748c7 to
995e5fc
Compare
995e5fc to
1d77cc7
Compare
pubky-test URL query param
9a19656 to
18a8833
Compare
18a8833 to
457cbed
Compare
pubky-test URL query parampubky-test URL query param
pubky-test URL query parampubky-test URL query param
457cbed to
20b30f7
Compare
|
Thanks both for review and finding that issue. My bad - I must have made the removal of I have removed the "ephemeral db" decision from the testing cfg and instead drive it off of Im going to look into refactoring the whole |
20b30f7 to
b4100e7
Compare
| if config_toml.general.database_url.is_test_db() { | ||
| // In test builds, no explicit database_url means we're in a test environment | ||
| // where we must avoid contacting the public DHT. | ||
| if config_toml.general.database_url.is_none() { |
There was a problem hiding this comment.
we must avoid contacting the public DHT
This only happens if the database_url is unset. So if a MockDataDir is created with an explicit database_url:
let mut config = ConfigToml::minimal_test_config();
config.general.database_url = Some(postgres_url);
let dir = MockDataDir::new(config, None)?;
HomeserverApp::start_with_mock_data_dir(dir).await?;then the test can publish test records to the public DHT, and / or fail the test if the environment is sandboxed and outbound connections are denied.
I guess in our test suites we wouldn't do that, but the README documents how library users might write their own tests, so others could inadvertently trigger this edge-case:
pubky-homeserver/pubky-homeserver/README.md
Lines 77 to 79 in b4100e7
One solution could be to pass db_mode: &DatabaseMode as an extra arg to build_pkarr_builder_from_config then internally check it directly:
- if config_toml.general.database_url.is_none()
+ if matches!(db_mode, DatabaseMode::EphemeralTest(_))There was a problem hiding this comment.
This is equal in fragility as before, when a provide database_url without ?pubky-test=true would not isolate pkarr. I'll fix this properly when i refactor the whole data dir/db/file system config situation, if that okay with you.
| temp_dirs: vec![], | ||
| postgres_connection_string: Self::extract_postgres_connection_string_from_env_variable( | ||
| ), | ||
| postgres_connection_string: None, |
There was a problem hiding this comment.
This change means the persistent command from pubky-testnet/README.md
TEST_PUBKY_CONNECTION_STRING='postgres://postgres:postgres@localhost:5432/postgres' \
cargo run -p pubky-testnet -- persist ./my-testnet-datanow ignores TEST_PUBKY_CONNECTION_STRING and instead uses the generated config default
database_url = "postgres://localhost:5432/pubky_homeserver"from config.sample.toml, which doesn't have the user/pass or the right DB name for the Docker DB setup.
There was a problem hiding this comment.
this also is pre-existing and also will be addressed in the follow-up refactor
| /// Create an ephemeral `pubky_test_{uuid}` database on the server | ||
| /// identified by the URL, then connect to it. The database is dropped | ||
| /// when the [`SqlDb`](super::SqlDb) is dropped. |
There was a problem hiding this comment.
The database is dropped when the
SqlDbis dropped.
Dropping SqlDb only queues cleanup; deletion requires #[pubky_testnet::test] or an explicit drop_test_databases() call. Callers without either leak databases on external PostgreSQL instances.
Simplest fix is probably to change these doc lines to say that dropping registers the DB for cleanup and that callers must use the #[pubky_testnet::test] macro or call drop_test_databases(). Ideally the README examples1 should include or mention that too.
Footnotes
There was a problem hiding this comment.
Updated doc here and also added missing [crate::test]!
There was a problem hiding this comment.
I'll also look into making it clearer where/when this is needed, since it was missed on some tests already.
…new tables per-test
…ON_STRING + pubky-test flag
…ml, env var or test default
… DBs. Instead, drive ephemeral-ness from the testing feature.
…ST_PUBKY_CONNECTION_STRING"
…dir is persisted, then so is the DB.
… signup methods in tests
b4100e7 to
f6bb478
Compare
… up various docs, add missing [crate::test] macros
f6bb478 to
95293f6
Compare
The idea behind
DockerPostgres::shared()is that the work of downloading, building and starting up Postgres shouldn't need to be done for each testnet which needs to use the Postgres instance. For example, in a test suite in which each test requires its own testnet.The existing behaviour was that the Postgres instance and database was being shared. The fix here is to share the Postgres instance but give each test a fresh, ephemeral database.
These changes motivated a re-write of how databse urls are set, as the logic was spread across different places and behaviour not well-defined.
Previously we configured ephemeral database by a
?pubky-test=truequery parameter in the connection URL, a fragile convention that is easy to forget and adds confusion. We replaced it with aDatabaseModeenum that is resolved at startup based on compile-time cfg flags. In test builds you always getEphemeralTest, in production you always getDirect. The URL is now just a server address, it no longer carries behavioral intent.We also change
database_urlfromConnectionStringtoOption<ConnectionString>. Test configs set it toNone, deferring resolution to the env var or built-in default. Explicit urls (e.g. Docker Postgres) areSomeand used as-is. Production builds throw and error ifNone.The priority for database URL resolution in test builds is: