diff --git a/contrib/dev-tools/container/e2e/mysql/run-e2e-tests.sh b/contrib/dev-tools/container/e2e/mysql/run-e2e-tests.sh index 74bf602b..0d2057fa 100755 --- a/contrib/dev-tools/container/e2e/mysql/run-e2e-tests.sh +++ b/contrib/dev-tools/container/e2e/mysql/run-e2e-tests.sh @@ -41,7 +41,11 @@ docker ps ./contrib/dev-tools/container/e2e/mysql/install.sh || exit 1 # Run E2E tests with shared app instance -TORRUST_INDEX_E2E_SHARED=true TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.e2e.container.mysql.toml" cargo test || exit 1 +TORRUST_INDEX_E2E_SHARED=true \ + TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.e2e.container.mysql.toml" \ + TORRUST_INDEX_E2E_DB_CONNECT_URL="mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing" \ + cargo test \ + || exit 1 # Stop E2E testing environment ./contrib/dev-tools/container/e2e/mysql/e2e-env-down.sh || exit 1 diff --git a/contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh b/contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh index d6d38f60..4ddc84cb 100755 --- a/contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh +++ b/contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh @@ -39,7 +39,11 @@ sleep 20s docker ps # Run E2E tests with shared app instance -TORRUST_INDEX_E2E_SHARED=true TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.e2e.container.sqlite3.toml" cargo test || exit 1 +TORRUST_INDEX_E2E_SHARED=true \ + TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.e2e.container.sqlite3.toml" \ + TORRUST_INDEX_E2E_DB_CONNECT_URL="sqlite://./storage/index/lib/database/e2e_testing_sqlite3.db?mode=rwc" \ + cargo test \ + || exit 1 # Stop E2E testing environment ./contrib/dev-tools/container/e2e/sqlite/e2e-env-down.sh || exit 1 diff --git a/tests/e2e/config.rs b/tests/e2e/config.rs index 747e0a05..24cb87bf 100644 --- a/tests/e2e/config.rs +++ b/tests/e2e/config.rs @@ -23,6 +23,9 @@ pub const DEFAULT_PATH_CONFIG: &str = "./share/default/config/index.development. /// If present, E2E tests will run against a shared instance of the server pub const ENV_VAR_INDEX_SHARED: &str = "TORRUST_INDEX_E2E_SHARED"; +/// `SQLx` connection string to connect to the E2E database +pub const ENV_VAR_DB_CONNECT_URL: &str = "TORRUST_INDEX_E2E_DB_CONNECT_URL"; + /// It loads the application configuration from the environment. /// /// There are two methods to inject the configuration: diff --git a/tests/e2e/environment.rs b/tests/e2e/environment.rs index a39873a2..0ee8b55b 100644 --- a/tests/e2e/environment.rs +++ b/tests/e2e/environment.rs @@ -1,9 +1,8 @@ use std::env; -use torrust_index::databases::database; use torrust_index::web::api::Version; -use super::config::{initialize_configuration, ENV_VAR_INDEX_SHARED}; +use super::config::{initialize_configuration, ENV_VAR_DB_CONNECT_URL, ENV_VAR_INDEX_SHARED}; use crate::common::contexts::settings::Settings; use crate::environments::{isolated, shared}; @@ -98,9 +97,10 @@ impl TestEnv { /// Provides a database connect URL to connect to the database. For example: /// - /// `sqlite://storage/database/torrust_index_e2e_testing.db?mode=rwc`. + /// - `sqlite://storage/database/torrust_index_e2e_testing.db?mode=rwc`. + /// - `mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing`. /// - /// It's used to run SQL queries against the database needed for some tests. + /// It's used to run SQL queries against the E2E database needed for some tests. pub fn database_connect_url(&self) -> Option { let internal_connect_url = self .starting_settings @@ -109,62 +109,19 @@ impl TestEnv { match self.state() { State::RunningShared => { - if let Some(db_path) = internal_connect_url { - let maybe_db_driver = database::get_driver(&db_path); - - return match maybe_db_driver { - Ok(db_driver) => match db_driver { - 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, - }; + let connect_url_env_var = ENV_VAR_DB_CONNECT_URL; + + if let Ok(connect_url) = env::var(connect_url_env_var) { + Some(connect_url) + } else { + None } - None } State::RunningIsolated => internal_connect_url, State::Stopped => None, } } - /// It overrides the `SQLite` file path in a `SQLx` database connection URL. - /// For example: - /// - /// For: - /// - /// `sqlite:///var/lib/torrust/index/database/e2e_testing_sqlite3.db?mode=rwc`. - /// - /// It changes the `mysql` host name to `localhost`: - /// - /// `sqlite://./storage/index/lib/database/e2e_testing_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: - /// - /// `mysql://root:root_secret_password@mysql:3306/torrust_index_e2e_testing`. - /// - /// It changes the `mysql` host name to `localhost`: - /// - /// `mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing`. - /// - /// For E2E tests, we use docker compose, internally the index connects to - /// 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}:")) - } - fn state(&self) -> State { if self.is_shared() { return State::RunningShared;