Skip to content

Commit

Permalink
ci: [#390] inject E2E DB connection info with env var
Browse files Browse the repository at this point in the history
```
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
```

Th new `TORRUST_INDEX_E2E_DB_CONNECT_URL` is used to connect to the E2E
DB directly. IT's needed for some tests to setthe initial state needed
for the test, when it's not possible do do it otherwise.
  • Loading branch information
josecelano committed Nov 22, 2023
1 parent c17ebbd commit 1b33145
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 55 deletions.
6 changes: 5 additions & 1 deletion contrib/dev-tools/container/e2e/mysql/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions tests/e2e/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
63 changes: 10 additions & 53 deletions tests/e2e/environment.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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<String> {
let internal_connect_url = self
.starting_settings
Expand All @@ -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;
Expand Down

0 comments on commit 1b33145

Please sign in to comment.