From e78607dbb6deb3d12c626636f419be52f93e0ff2 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 14 Nov 2023 14:51:31 +0000 Subject: [PATCH] fix: [#342] SQLite data file path inside the container for E2E tests The SQLite file path inside the container is not the same as on hte host: In the container: `sqlite:///var/lib/torrust/index/database/sqlite3.db?mode=rwc` It's an absolute path. From the host: `sqlite://./storage/index/lib/database/sqlite3.db?mode=rwc` It's a relative path to where the test are being executed (root project path). TODO: inject as an env var when running the E2E tests isntead of parsing the config file. ``` TORRUST_INDEX_E2E_SHARED=true TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.container.sqlite3.toml" cargo test ``` --- tests/e2e/environment.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/tests/e2e/environment.rs b/tests/e2e/environment.rs index 73652725..1c20ca26 100644 --- a/tests/e2e/environment.rs +++ b/tests/e2e/environment.rs @@ -114,7 +114,7 @@ impl TestEnv { return match maybe_db_driver { Ok(db_driver) => match db_driver { - database::Driver::Sqlite3 => Some(db_path), + database::Driver::Sqlite3 => Some(Self::overwrite_sqlite_path(&db_path, "./storage/index/lib")), database::Driver::Mysql => Some(Self::overwrite_mysql_host(&db_path, "localhost")), }, Err(_) => None, @@ -127,7 +127,26 @@ impl TestEnv { } } - /// It overrides the "Host" in a `SQLx` database connection URL. For example: + /// It overrides the `SQLite` file path in a `SQLx` database connection URL. + /// For example: + /// + /// For: + /// + /// `sqlite:///var/lib/torrust/index/database/sqlite3.db?mode=rwc`. + /// + /// It changes the `mysql` host name to `localhost`: + /// + /// `sqlite://./storage/index/lib/database/sqlite3.db?mode=rwc`. + /// + /// For E2E tests, we use docker compose. Inside the container, the + /// `SQLite` file path is not the same as the host path. + fn overwrite_sqlite_path(db_path: &str, host_path: &str) -> String { + // todo: inject value with env var + db_path.replace("/var/lib/torrust/index", host_path) + } + + /// It overrides the "Host" in a `SQLx` database connection URL. + /// For example: /// /// For: /// @@ -138,10 +157,11 @@ impl TestEnv { /// `mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing`. /// /// For E2E tests, we use docker compose, internally the index connects to - /// the database using the "mysql" host, which is the docker compose service - /// name, but tests connects directly to the localhost since the `MySQL` - /// is exposed to the host. + /// the `MySQL` database using the "mysql" host, which is the docker compose + /// service name, but tests connects directly to the localhost since the + /// `MySQL` is exposed to the host. fn overwrite_mysql_host(db_path: &str, new_host: &str) -> String { + // todo: inject value with env var db_path.replace("@mysql:", &format!("@{new_host}:")) }