Skip to content

Commit e78607d

Browse files
committed
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 ```
1 parent 1cce823 commit e78607d

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

tests/e2e/environment.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl TestEnv {
114114

115115
return match maybe_db_driver {
116116
Ok(db_driver) => match db_driver {
117-
database::Driver::Sqlite3 => Some(db_path),
117+
database::Driver::Sqlite3 => Some(Self::overwrite_sqlite_path(&db_path, "./storage/index/lib")),
118118
database::Driver::Mysql => Some(Self::overwrite_mysql_host(&db_path, "localhost")),
119119
},
120120
Err(_) => None,
@@ -127,7 +127,26 @@ impl TestEnv {
127127
}
128128
}
129129

130-
/// It overrides the "Host" in a `SQLx` database connection URL. For example:
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/sqlite3.db?mode=rwc`.
136+
///
137+
/// It changes the `mysql` host name to `localhost`:
138+
///
139+
/// `sqlite://./storage/index/lib/database/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:
131150
///
132151
/// For:
133152
///
@@ -138,10 +157,11 @@ impl TestEnv {
138157
/// `mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing`.
139158
///
140159
/// For E2E tests, we use docker compose, internally the index connects to
141-
/// the database using the "mysql" host, which is the docker compose service
142-
/// name, but tests connects directly to the localhost since the `MySQL`
143-
/// is exposed to the host.
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.
144163
fn overwrite_mysql_host(db_path: &str, new_host: &str) -> String {
164+
// todo: inject value with env var
145165
db_path.replace("@mysql:", &format!("@{new_host}:"))
146166
}
147167

0 commit comments

Comments
 (0)