1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use testcontainers::{
    core::{CmdWaitFor, ExecCommand, WaitFor},
    Image,
};

const NAME: &str = "mongo";
const TAG: &str = "5.0.6";

#[derive(Debug, Clone)]
enum InstanceKind {
    Standalone,
    ReplSet,
}

impl Default for InstanceKind {
    fn default() -> Self {
        Self::Standalone
    }
}

#[derive(Default, Debug, Clone)]
pub struct Mongo {
    kind: InstanceKind,
}

impl Mongo {
    pub fn new() -> Self {
        Self {
            kind: InstanceKind::Standalone,
        }
    }
    pub fn repl_set() -> Self {
        Self {
            kind: InstanceKind::ReplSet,
        }
    }
}

impl Image for Mongo {
    fn name(&self) -> &str {
        NAME
    }

    fn tag(&self) -> &str {
        TAG
    }

    fn ready_conditions(&self) -> Vec<WaitFor> {
        vec![WaitFor::message_on_stdout("Waiting for connections")]
    }

    fn cmd(&self) -> impl IntoIterator<Item = impl Into<std::borrow::Cow<'_, str>>> {
        match self.kind {
            InstanceKind::Standalone => Vec::<String>::new(),
            InstanceKind::ReplSet => vec!["--replSet".to_string(), "rs".to_string()],
        }
    }

    fn exec_after_start(
        &self,
        _: testcontainers::core::ContainerState,
    ) -> Result<Vec<ExecCommand>, testcontainers::TestcontainersError> {
        match self.kind {
            InstanceKind::Standalone => Ok(Default::default()),
            InstanceKind::ReplSet => Ok(vec![ExecCommand::new(vec![
                "mongosh".to_string(),
                "--quiet".to_string(),
                "--eval".to_string(),
                "'rs.initiate()'".to_string(),
            ])
            .with_cmd_ready_condition(CmdWaitFor::message_on_stdout(
                "Using a default configuration for the set",
            ))
            .with_container_ready_conditions(vec![WaitFor::message_on_stdout(
                "Rebuilding PrimaryOnlyService due to stepUp",
            )])]),
        }
    }
}

#[cfg(test)]
mod tests {
    use mongodb::*;
    use testcontainers::{core::IntoContainerPort, runners::AsyncRunner};

    use crate::mongo;

    #[tokio::test]
    async fn mongo_fetch_document() -> Result<(), Box<dyn std::error::Error + 'static>> {
        let _ = pretty_env_logger::try_init();
        let node = mongo::Mongo::default().start().await?;
        let host_ip = node.get_host().await?;
        let host_port = node.get_host_port_ipv4(27017.tcp()).await?;
        let url = format!("mongodb://{host_ip}:{host_port}/");

        let client: Client = Client::with_uri_str(&url).await.unwrap();
        let db = client.database("some_db");
        let coll = db.collection("some_coll");

        let insert_one_result = coll.insert_one(bson::doc! { "x": 42 }).await.unwrap();
        assert!(!insert_one_result
            .inserted_id
            .as_object_id()
            .unwrap()
            .to_hex()
            .is_empty());

        let find_one_result: bson::Document = coll
            .find_one(bson::doc! { "x": 42 })
            .await
            .unwrap()
            .unwrap();
        assert_eq!(42, find_one_result.get_i32("x").unwrap());

        Ok(())
    }

    #[tokio::test]
    async fn mongo_repl_set_fetch_document() -> Result<(), Box<dyn std::error::Error + 'static>> {
        let _ = pretty_env_logger::try_init();
        let node = mongo::Mongo::repl_set().start().await?;
        let host_ip = node.get_host().await?;
        let host_port = node.get_host_port_ipv4(27017).await?;
        let url = format!("mongodb://{host_ip}:{host_port}/?directConnection=true",);

        let client: Client = Client::with_uri_str(url).await?;
        let db = client.database("some_db");
        let coll = db.collection("some-coll");

        let mut session = client.start_session().await?;
        session.start_transaction().await?;

        let insert_one_result = coll
            .insert_one(bson::doc! { "x": 42 })
            .session(&mut session)
            .await?;
        assert!(!insert_one_result
            .inserted_id
            .as_object_id()
            .unwrap()
            .to_hex()
            .is_empty());
        session.commit_transaction().await?;

        let find_one_result: bson::Document = coll
            .find_one(bson::doc! { "x": 42 })
            .await
            .unwrap()
            .unwrap();

        assert_eq!(42, find_one_result.get_i32("x").unwrap());
        Ok(())
    }
}