1- //! Container cleanup task for E2E testing
1+ //! Container management task for E2E testing
22//!
3- //! This module provides the E2E testing task for cleaning up Docker containers
4- //! after test completion. It handles the transition from running to stopped
5- //! container state and ensures proper resource cleanup.
3+ //! This module provides container management functionality for E2E testing,
4+ //! including stopping containers and handling automatic cleanup through testcontainers.
5+ //! It distinguishes between stopping containers (immediate) and cleanup (automatic) .
66//!
77//! ## Key Operations
88//!
9- //! - Stops running Docker containers
10- //! - Cleans up container resources
11- //! - Provides logging for cleanup operations
9+ //! - **Stop**: Immediately stops running Docker containers
10+ //! - **Cleanup**: Automatic deletion handled by testcontainers library
11+ //! - Provides logging for container operations
12+ //!
13+ //! ## Container vs VM Difference
14+ //!
15+ //! Unlike VMs that require explicit infrastructure destruction, Docker containers
16+ //! managed by testcontainers are automatically deleted when they go out of scope.
17+ //! This module provides both stop and cleanup functions for API symmetry.
1218//!
1319//! ## Integration
1420//!
15- //! This task is specifically designed for container-based E2E testing workflows
16- //! and should be used as the final step in container test scenarios.
21+ //! This task is specifically designed for container-based E2E testing workflows.
1722
1823use tracing:: info;
1924
2025use crate :: e2e:: containers:: RunningProvisionedContainer ;
2126
22- /// Cleanup a running Docker container
27+ /// Stop a running Docker container
2328///
24- /// This function stops a running Docker container and performs cleanup operations.
25- /// It transitions the container from running to stopped state and ensures proper
26- /// resource cleanup .
29+ /// This function stops a running Docker container, transitioning it from running
30+ /// to stopped state. The actual container deletion is handled automatically by
31+ /// the testcontainers library when the container goes out of scope .
2732///
2833/// # Arguments
2934///
30- /// * `running_container` - The running container to be cleaned up
35+ /// * `running_container` - The running container to be stopped
3136///
3237/// # Returns
3338///
3439/// This function consumes the running container and returns nothing, ensuring
35- /// the container cannot be used after cleanup .
40+ /// the container cannot be used after stopping .
3641///
3742/// # Example
3843///
3944/// ```rust,no_run
40- /// use torrust_tracker_deploy::e2e::tasks::container::cleanup_infrastructure::cleanup_infrastructure ;
45+ /// use torrust_tracker_deploy::e2e::tasks::container::cleanup_infrastructure::stop_test_infrastructure ;
4146/// use torrust_tracker_deploy::e2e::containers::StoppedProvisionedContainer;
4247///
4348/// #[tokio::main]
@@ -47,17 +52,17 @@ use crate::e2e::containers::RunningProvisionedContainer;
4752///
4853/// // ... perform tests ...
4954///
50- /// cleanup_infrastructure (running_container);
51- /// println!("Container cleanup completed ");
55+ /// stop_test_infrastructure (running_container);
56+ /// println!("Container stopped successfully ");
5257/// Ok(())
5358/// }
5459/// ```
55- pub fn cleanup_infrastructure ( running_container : RunningProvisionedContainer ) {
60+ pub fn stop_test_infrastructure ( running_container : RunningProvisionedContainer ) {
5661 let container_id = running_container. container_id ( ) . to_string ( ) ;
5762
5863 info ! (
5964 container_id = %container_id,
60- "Starting container cleanup "
65+ "Stopping test container "
6166 ) ;
6267
6368 // Transition container from running to stopped state
@@ -66,6 +71,23 @@ pub fn cleanup_infrastructure(running_container: RunningProvisionedContainer) {
6671 info ! (
6772 container_id = %container_id,
6873 status = "success" ,
69- "Container cleanup completed successfully"
74+ "Container stopped successfully - deletion handled automatically by testcontainers"
75+ ) ;
76+ }
77+
78+ /// Cleanup test infrastructure for containers (no-op)
79+ ///
80+ /// For containers managed by testcontainers, cleanup (deletion) is handled automatically
81+ /// when containers go out of scope. This function is provided for API symmetry with
82+ /// VM-based workflows that require explicit infrastructure destruction.
83+ ///
84+ /// # Note
85+ ///
86+ /// This function does nothing for containers. The actual cleanup is automatic.
87+ /// Use `stop_test_infrastructure()` to stop running containers.
88+ pub fn cleanup_test_infrastructure ( ) {
89+ info ! (
90+ operation = "container_cleanup" ,
91+ "Container cleanup is automatic via testcontainers - no action needed"
7092 ) ;
7193}
0 commit comments