Skip to content

Commit ddad4a4

Browse files
committed
ci: [#647] E2E tests. Make sure there are not panics in logs
1 parent 670927c commit ddad4a4

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

src/e2e/docker.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ impl Drop for RunningContainer {
1818
/// Ensures that the temporary container is stopped and removed when the
1919
/// struct goes out of scope.
2020
fn drop(&mut self) {
21-
let _unused = Docker::stop(self);
22-
let _unused = Docker::remove(&self.name);
21+
if Docker::is_container_running(&self.name) {
22+
let _unused = Docker::stop(self);
23+
}
24+
if Docker::container_exist(&self.name) {
25+
let _unused = Docker::remove(&self.name);
26+
}
2327
}
2428
}
2529

@@ -180,4 +184,50 @@ impl Docker {
180184

181185
false
182186
}
187+
188+
/// Checks if a Docker container is running.
189+
///
190+
/// # Arguments
191+
///
192+
/// * `container` - The name of the Docker container.
193+
///
194+
/// # Returns
195+
///
196+
/// `true` if the container is running, `false` otherwise.
197+
#[must_use]
198+
pub fn is_container_running(container: &str) -> bool {
199+
match Command::new("docker")
200+
.args(["ps", "-f", &format!("name={container}"), "--format", "{{.Names}}"])
201+
.output()
202+
{
203+
Ok(output) => {
204+
let output_str = String::from_utf8_lossy(&output.stdout);
205+
output_str.contains(container)
206+
}
207+
Err(_) => false,
208+
}
209+
}
210+
211+
/// Checks if a Docker container exists.
212+
///
213+
/// # Arguments
214+
///
215+
/// * `container` - The name of the Docker container.
216+
///
217+
/// # Returns
218+
///
219+
/// `true` if the container exists, `false` otherwise.
220+
#[must_use]
221+
pub fn container_exist(container: &str) -> bool {
222+
match Command::new("docker")
223+
.args(["ps", "-a", "-f", &format!("name={container}"), "--format", "{{.Names}}"])
224+
.output()
225+
{
226+
Ok(output) => {
227+
let output_str = String::from_utf8_lossy(&output.stdout);
228+
output_str.contains(container)
229+
}
230+
Err(_) => false,
231+
}
232+
}
183233
}

src/e2e/runner.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub fn run() {
5757

5858
let running_services = parse_running_services_from_logs(&container);
5959

60+
assert_there_are_no_panics_in_logs(&container);
61+
6062
assert_there_is_at_least_one_service_per_type(&running_services);
6163

6264
let tracker_checker_config =
@@ -69,9 +71,12 @@ pub fn run() {
6971

7072
run_tracker_checker(&tracker_checker_config_path).expect("Tracker checker should check running services");
7173

72-
// More E2E tests could be executed here in the future. For example: `cargo test ...`.
74+
// More E2E tests could be added here in the future.
75+
// For example: `cargo test ...` for only E2E tests, using this shared test env.
76+
77+
stop_tracker_container(&container);
7378

74-
info!("Running container `{}` will be automatically removed", container.name);
79+
remove_tracker_container(&container_name);
7580
}
7681

7782
fn setup_runner_logging(level: LevelFilter) {
@@ -164,6 +169,29 @@ fn run_tracker_container(image: &str, container_name: &str, options: &RunOptions
164169
container
165170
}
166171

172+
fn stop_tracker_container(container: &RunningContainer) {
173+
info!("Stopping docker tracker image: {} ...", container.name);
174+
Docker::stop(container).expect("Container should be stopped");
175+
assert_there_are_no_panics_in_logs(container);
176+
}
177+
178+
fn remove_tracker_container(container_name: &str) {
179+
info!("Removing docker tracker image: {container_name} ...");
180+
Docker::remove(container_name).expect("Container should be removed");
181+
}
182+
183+
fn assert_there_are_no_panics_in_logs(container: &RunningContainer) -> RunningServices {
184+
let logs = Docker::logs(&container.name).expect("Logs should be captured from running container");
185+
186+
assert!(
187+
!(logs.contains(" panicked at ") || logs.contains("RUST_BACKTRACE=1")),
188+
"{}",
189+
format!("Panics found is logs:\n{logs}")
190+
);
191+
192+
RunningServices::parse_from_logs(&logs)
193+
}
194+
167195
fn parse_running_services_from_logs(container: &RunningContainer) -> RunningServices {
168196
let logs = Docker::logs(&container.name).expect("Logs should be captured from running container");
169197

0 commit comments

Comments
 (0)