Skip to main content

atlas_local/client/
pull_image.rs

1use crate::{client::Client, docker::DockerPullImage};
2
3#[derive(Debug, thiserror::Error)]
4#[error("Failed to pull image: {0}")]
5pub struct PullImageError(#[from] bollard::errors::Error);
6
7impl<D: DockerPullImage> Client<D> {
8    /// Pulls the Atlas Local image.
9    ///
10    /// # Arguments
11    ///
12    /// * `image` - The image to pull.
13    /// * `tag` - The tag to pull.
14    pub async fn pull_image(&self, image: &str, tag: &str) -> Result<(), PullImageError> {
15        self.docker.pull_image(image, tag).await?;
16        Ok(())
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    use bollard::errors::Error as BollardError;
24    use mockall::mock;
25
26    mock! {
27        Docker {}
28
29        impl DockerPullImage for Docker {
30            async fn pull_image(&self, image: &str, tag: &str) -> Result<(), BollardError>;
31        }
32    }
33
34    #[tokio::test]
35    async fn test_pull_image() {
36        // Arrange
37        let mut mock_docker = MockDocker::new();
38
39        // Set up expectations
40        mock_docker
41            .expect_pull_image()
42            .with(
43                mockall::predicate::eq("mongodb/mongodb-atlas-local"),
44                mockall::predicate::eq("8.0.0"),
45            )
46            .times(1)
47            .returning(|_, _| Ok(()));
48
49        let client = Client::new(mock_docker);
50
51        // Act
52        let result = client
53            .pull_image("mongodb/mongodb-atlas-local", "8.0.0")
54            .await;
55
56        // Assert
57        assert!(result.is_ok());
58    }
59
60    #[tokio::test]
61    async fn test_pull_image_docker_error() {
62        // Arrange
63        let mut mock_docker = MockDocker::new();
64
65        // Set up expectations
66        mock_docker
67            .expect_pull_image()
68            .with(
69                mockall::predicate::eq("mongodb/mongodb-atlas-local"),
70                mockall::predicate::eq("invalid-tag"),
71            )
72            .times(1)
73            .returning(|_, _| {
74                Err(BollardError::DockerResponseServerError {
75                    status_code: 404,
76                    message: "Tag not found".to_string(),
77                })
78            });
79
80        let client = Client::new(mock_docker);
81
82        // Act
83        let result = client
84            .pull_image("mongodb/mongodb-atlas-local", "invalid-tag")
85            .await;
86
87        // Assert
88        assert!(result.is_err());
89        assert!(matches!(result.unwrap_err(), PullImageError(_)));
90    }
91}