-
Notifications
You must be signed in to change notification settings - Fork 30
fix(testnet/homeserver)!: DockerPostgres::shared() to share Postgres instance but isolate databases and remove pubky-test URL query param
#500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
afa14e5
de56296
b2c6a17
305ef21
6756d97
26fa1b6
448d1e7
e7e341b
f93d5c6
e4a85c7
2d92f57
95293f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,9 @@ pub enum AppContextConversionError { | |||||||
| /// Failed to open SQL DB. | ||||||||
| #[error("Failed to open SQL DB: {0}")] | ||||||||
| SqlDb(sqlx::Error), | ||||||||
| /// Failed to resolve the database mode (e.g. missing URL or invalid TEST_PUBKY_CONNECTION_STRING). | ||||||||
| #[error("Failed to resolve database mode: {0}")] | ||||||||
| DatabaseResolution(anyhow::Error), | ||||||||
| /// Failed to run migrations. | ||||||||
| #[error("Failed to run migrations: {0}")] | ||||||||
| Migrations(anyhow::Error), | ||||||||
|
|
@@ -151,7 +154,12 @@ impl AppContext { | |||||||
| .read_or_create_keypair() | ||||||||
| .map_err(AppContextConversionError::Keypair)?; | ||||||||
|
|
||||||||
| let sql_db = Self::connect_to_sql_db(&conf).await?; | ||||||||
| let db_mode = dir | ||||||||
| .resolve_database_mode(&conf) | ||||||||
| .map_err(AppContextConversionError::DatabaseResolution)?; | ||||||||
| let sql_db = SqlDb::connect(db_mode) | ||||||||
| .await | ||||||||
| .map_err(AppContextConversionError::SqlDb)?; | ||||||||
| Migrator::new(&sql_db) | ||||||||
| .run() | ||||||||
| .await | ||||||||
|
|
@@ -203,7 +211,9 @@ impl AppContext { | |||||||
| fn build_pkarr_builder_from_config(config_toml: &ConfigToml) -> pkarr::ClientBuilder { | ||||||||
| let mut builder = pkarr::ClientBuilder::default(); | ||||||||
| #[cfg(any(test, feature = "testing"))] | ||||||||
| 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() { | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This only happens if the 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 pubky-homeserver/pubky-homeserver/README.md Lines 77 to 79 in b4100e7
One solution could be to pass - if config_toml.general.database_url.is_none()
+ if matches!(db_mode, DatabaseMode::EphemeralTest(_))
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is equal in fragility as before, when a provide database_url without |
||||||||
| builder | ||||||||
| .no_default_network() | ||||||||
| // Keep the client buildable without contacting the public DHT. | ||||||||
|
|
@@ -235,50 +245,33 @@ impl AppContext { | |||||||
| } | ||||||||
| builder | ||||||||
| } | ||||||||
|
|
||||||||
| /// Connect to the SQL database. | ||||||||
| /// If we are in a test environment and it's a test db connection string, | ||||||||
| /// we use an empheral test db. | ||||||||
| /// Otherwise, we use the normal db connection. | ||||||||
| async fn connect_to_sql_db( | ||||||||
| config_toml: &ConfigToml, | ||||||||
| ) -> Result<SqlDb, AppContextConversionError> { | ||||||||
| #[cfg(any(test, feature = "testing"))] | ||||||||
| { | ||||||||
| // If we are in a test environment and it's a test db connection string, | ||||||||
| // we use an empheral test db. | ||||||||
| return if config_toml.general.database_url.is_test_db() { | ||||||||
| Ok(SqlDb::test().await) | ||||||||
| } else { | ||||||||
| SqlDb::connect(&config_toml.general.database_url) | ||||||||
| .await | ||||||||
| .map_err(AppContextConversionError::SqlDb) | ||||||||
| }; | ||||||||
| } | ||||||||
|
|
||||||||
| #[cfg(not(any(test, feature = "testing")))] | ||||||||
| { | ||||||||
| // If we are not in a test environment, we use the normal db connection. | ||||||||
| return SqlDb::connect(&config_toml.general.database_url) | ||||||||
| .await | ||||||||
| .map_err(AppContextConversionError::SqlDb); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| #[cfg(test)] | ||||||||
| mod tests { | ||||||||
| use super::*; | ||||||||
|
|
||||||||
| /// Verifies that the test pkarr builder doesn't contact the public DHT | ||||||||
| /// when database_url is None (the default test config). | ||||||||
| #[test] | ||||||||
| fn test_pkarr_builder_does_not_use_default_network() { | ||||||||
| let builder = | ||||||||
| AppContext::build_pkarr_builder_from_config(&ConfigToml::default_test_config()); | ||||||||
| fn pkarr_builder_does_not_use_default_network() { | ||||||||
| let config = ConfigToml::default_test_config(); | ||||||||
| assert!( | ||||||||
| config.general.database_url.is_none(), | ||||||||
| "default_test_config should have database_url = None" | ||||||||
| ); | ||||||||
| let builder = AppContext::build_pkarr_builder_from_config(&config); | ||||||||
| let builder_debug = format!("{builder:?}"); | ||||||||
|
|
||||||||
| assert!(builder_debug.contains("127.0.0.1:9")); | ||||||||
| assert!( | ||||||||
| builder_debug.contains("127.0.0.1:9"), | ||||||||
| "expected sentinel bootstrap node in builder: {builder_debug}" | ||||||||
| ); | ||||||||
| for relay in pkarr::DEFAULT_RELAYS { | ||||||||
| assert!(!builder_debug.contains(relay)); | ||||||||
| assert!( | ||||||||
| !builder_debug.contains(relay), | ||||||||
| "default relay {relay} should not appear in test builder: {builder_debug}" | ||||||||
| ); | ||||||||
| } | ||||||||
| builder.build().expect("isolated pkarr client should build"); | ||||||||
| } | ||||||||
|
|
||||||||
Uh oh!
There was an error while loading. Please reload this page.