Skip to main content

torrust_tracker_deployer_lib/application/steps/validation/
docker_compose.rs

1//! Docker Compose installation validation step
2//!
3//! This module provides the `ValidateDockerComposeInstallationStep` which validates
4//! that Docker Compose is properly installed and operational on remote hosts.
5//! This step ensures the container orchestration tool is ready for deployment.
6//!
7//! ## Key Features
8//!
9//! - Docker Compose installation verification via remote validation
10//! - Version compatibility checking and functionality testing
11//! - Integration with Docker engine validation
12//! - Integration with SSH-based remote actions
13//!
14//! ## Validation Process
15//!
16//! The step uses the `DockerComposeValidator` remote action to perform
17//! comprehensive checks including version verification, basic functionality
18//! testing, and integration with the Docker engine to ensure complete
19//! container orchestration capabilities.
20
21use tracing::{info, instrument};
22
23use crate::adapters::ssh::SshConfig;
24use crate::infrastructure::remote_actions::{
25    DockerComposeValidator, RemoteAction, RemoteActionError,
26};
27
28/// Step that validates Docker Compose installation on a remote host
29pub struct ValidateDockerComposeInstallationStep {
30    ssh_config: SshConfig,
31}
32
33impl ValidateDockerComposeInstallationStep {
34    #[must_use]
35    pub fn new(ssh_config: SshConfig) -> Self {
36        Self { ssh_config }
37    }
38
39    /// Execute the Docker Compose installation validation step
40    ///
41    /// This will validate that Docker Compose is properly installed and accessible
42    /// on the remote host by checking the Docker Compose version.
43    ///
44    /// # Errors
45    ///
46    /// Returns an error if:
47    /// * SSH connection to the remote host fails
48    /// * Docker Compose validation fails
49    /// * The remote action execution fails for any other reason
50    ///
51    /// # Notes
52    ///
53    /// - In CI environments with network limitations, Docker Compose installation
54    ///   validation may be skipped gracefully
55    /// - The validation checks Docker Compose version and availability
56    #[instrument(
57        name = "validate_docker_compose",
58        skip_all,
59        fields(step_type = "validation", component = "docker_compose")
60    )]
61    pub async fn execute(&self) -> Result<(), RemoteActionError> {
62        info!(
63            component = "docker_compose",
64            "Validating Docker Compose installation"
65        );
66
67        let docker_compose_validator = DockerComposeValidator::new(self.ssh_config.clone());
68
69        docker_compose_validator
70            .execute(&self.ssh_config.host_ip())
71            .await?;
72
73        Ok(())
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use std::net::{IpAddr, Ipv4Addr};
80    use std::path::PathBuf;
81
82    use crate::adapters::ssh::SshCredentials;
83    use crate::shared::Username;
84
85    use super::*;
86
87    #[test]
88    fn it_should_create_validate_docker_compose_installation_step() {
89        let ssh_credentials = SshCredentials::new(
90            PathBuf::from("test_key"),
91            PathBuf::from("test_key.pub"),
92            Username::new("test_user").unwrap(),
93        );
94        let host_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
95        let ssh_config = SshConfig::with_default_port(ssh_credentials, host_ip);
96
97        let step = ValidateDockerComposeInstallationStep::new(ssh_config);
98
99        // Test that the step can be created successfully
100        assert_eq!(step.ssh_config.host_ip(), host_ip);
101    }
102}