Skip to main content

atlas_local/client/
pull_image.rs

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