Skip to content

Commit 1b33145

Browse files
committed
ci: [#390] inject E2E DB connection info with env var
``` 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.
1 parent c17ebbd commit 1b33145

File tree

4 files changed

+23
-55
lines changed

4 files changed

+23
-55
lines changed

contrib/dev-tools/container/e2e/mysql/run-e2e-tests.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ docker ps
4141
./contrib/dev-tools/container/e2e/mysql/install.sh || exit 1
4242

4343
# Run E2E tests with shared app instance
44-
TORRUST_INDEX_E2E_SHARED=true TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.e2e.container.mysql.toml" cargo test || exit 1
44+
TORRUST_INDEX_E2E_SHARED=true \
45+
TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.e2e.container.mysql.toml" \
46+
TORRUST_INDEX_E2E_DB_CONNECT_URL="mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing" \
47+
cargo test \
48+
|| exit 1
4549

4650
# Stop E2E testing environment
4751
./contrib/dev-tools/container/e2e/mysql/e2e-env-down.sh || exit 1

contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ sleep 20s
3939
docker ps
4040

4141
# Run E2E tests with shared app instance
42-
TORRUST_INDEX_E2E_SHARED=true TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.e2e.container.sqlite3.toml" cargo test || exit 1
42+
TORRUST_INDEX_E2E_SHARED=true \
43+
TORRUST_INDEX_E2E_PATH_CONFIG="./share/default/config/index.e2e.container.sqlite3.toml" \
44+
TORRUST_INDEX_E2E_DB_CONNECT_URL="sqlite://./storage/index/lib/database/e2e_testing_sqlite3.db?mode=rwc" \
45+
cargo test \
46+
|| exit 1
4347

4448
# Stop E2E testing environment
4549
./contrib/dev-tools/container/e2e/sqlite/e2e-env-down.sh || exit 1

tests/e2e/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub const DEFAULT_PATH_CONFIG: &str = "./share/default/config/index.development.
2323
/// If present, E2E tests will run against a shared instance of the server
2424
pub const ENV_VAR_INDEX_SHARED: &str = "TORRUST_INDEX_E2E_SHARED";
2525

26+
/// `SQLx` connection string to connect to the E2E database
27+
pub const ENV_VAR_DB_CONNECT_URL: &str = "TORRUST_INDEX_E2E_DB_CONNECT_URL";
28+
2629
/// It loads the application configuration from the environment.
2730
///
2831
/// There are two methods to inject the configuration:

tests/e2e/environment.rs

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::env;
22

3-
use torrust_index::databases::database;
43
use torrust_index::web::api::Version;
54

6-
use super::config::{initialize_configuration, ENV_VAR_INDEX_SHARED};
5+
use super::config::{initialize_configuration, ENV_VAR_DB_CONNECT_URL, ENV_VAR_INDEX_SHARED};
76
use crate::common::contexts::settings::Settings;
87
use crate::environments::{isolated, shared};
98

@@ -98,9 +97,10 @@ impl TestEnv {
9897

9998
/// Provides a database connect URL to connect to the database. For example:
10099
///
101-
/// `sqlite://storage/database/torrust_index_e2e_testing.db?mode=rwc`.
100+
/// - `sqlite://storage/database/torrust_index_e2e_testing.db?mode=rwc`.
101+
/// - `mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing`.
102102
///
103-
/// It's used to run SQL queries against the database needed for some tests.
103+
/// It's used to run SQL queries against the E2E database needed for some tests.
104104
pub fn database_connect_url(&self) -> Option<String> {
105105
let internal_connect_url = self
106106
.starting_settings
@@ -109,62 +109,19 @@ impl TestEnv {
109109

110110
match self.state() {
111111
State::RunningShared => {
112-
if let Some(db_path) = internal_connect_url {
113-
let maybe_db_driver = database::get_driver(&db_path);
114-
115-
return match maybe_db_driver {
116-
Ok(db_driver) => match db_driver {
117-
database::Driver::Sqlite3 => Some(Self::overwrite_sqlite_path(&db_path, "./storage/index/lib")),
118-
database::Driver::Mysql => Some(Self::overwrite_mysql_host(&db_path, "localhost")),
119-
},
120-
Err(_) => None,
121-
};
112+
let connect_url_env_var = ENV_VAR_DB_CONNECT_URL;
113+
114+
if let Ok(connect_url) = env::var(connect_url_env_var) {
115+
Some(connect_url)
116+
} else {
117+
None
122118
}
123-
None
124119
}
125120
State::RunningIsolated => internal_connect_url,
126121
State::Stopped => None,
127122
}
128123
}
129124

130-
/// It overrides the `SQLite` file path in a `SQLx` database connection URL.
131-
/// For example:
132-
///
133-
/// For:
134-
///
135-
/// `sqlite:///var/lib/torrust/index/database/e2e_testing_sqlite3.db?mode=rwc`.
136-
///
137-
/// It changes the `mysql` host name to `localhost`:
138-
///
139-
/// `sqlite://./storage/index/lib/database/e2e_testing_sqlite3.db?mode=rwc`.
140-
///
141-
/// For E2E tests, we use docker compose. Inside the container, the
142-
/// `SQLite` file path is not the same as the host path.
143-
fn overwrite_sqlite_path(db_path: &str, host_path: &str) -> String {
144-
// todo: inject value with env var
145-
db_path.replace("/var/lib/torrust/index", host_path)
146-
}
147-
148-
/// It overrides the "Host" in a `SQLx` database connection URL.
149-
/// For example:
150-
///
151-
/// For:
152-
///
153-
/// `mysql://root:root_secret_password@mysql:3306/torrust_index_e2e_testing`.
154-
///
155-
/// It changes the `mysql` host name to `localhost`:
156-
///
157-
/// `mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing`.
158-
///
159-
/// For E2E tests, we use docker compose, internally the index connects to
160-
/// the `MySQL` database using the "mysql" host, which is the docker compose
161-
/// service name, but tests connects directly to the localhost since the
162-
/// `MySQL` is exposed to the host.
163-
fn overwrite_mysql_host(db_path: &str, new_host: &str) -> String {
164-
// todo: inject value with env var
165-
db_path.replace("@mysql:", &format!("@{new_host}:"))
166-
}
167-
168125
fn state(&self) -> State {
169126
if self.is_shared() {
170127
return State::RunningShared;

0 commit comments

Comments
 (0)