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
use std::collections::BTreeMap;

use testcontainers::{core::WaitFor, Image, ImageArgs};

const DEFAULT_IMAGE_NAME: &str = "cockroachdb/cockroach";
const DEFAULT_IMAGE_TAG: &str = "v23.2.3";

/// Module to work with [`Cockroach DB`] inside of tests.
///
/// This module is based on the official [`Cockroach docker image`].
///
/// # Example
/// ```
/// use testcontainers_modules::{cockroach_db, testcontainers::runners::SyncRunner};
///
/// let cockroach = cockroach_db::CockroachDb::default().start();
/// let http_port = cockroach.get_host_port_ipv4(26257);
///
/// // do something with the started cockroach instance..
/// ```
///
/// [`Cockroach`]: https://www.cockroachlabs.com/
/// [`Cockroach docker image`]: https://hub.docker.com/r/cockroachdb/cockroach
/// [`Cockroach commands`]: https://www.cockroachlabs.com/docs/stable/cockroach-commands
#[derive(Debug)]
pub struct CockroachDb {
    name: String,
    tag: String,
    env_vars: BTreeMap<String, String>,
}

impl Default for CockroachDb {
    fn default() -> Self {
        CockroachDb::new(
            DEFAULT_IMAGE_NAME.to_string(),
            DEFAULT_IMAGE_TAG.to_string(),
        )
    }
}

impl CockroachDb {
    fn new(name: String, tag: String) -> Self {
        CockroachDb {
            name,
            tag,
            env_vars: Default::default(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct CockroachDbArgs {
    command: String,
    args: Vec<String>,
}

impl CockroachDbArgs {
    pub fn new(command: String, args: Vec<String>) -> Self {
        Self { command, args }
    }
}

impl Default for CockroachDbArgs {
    fn default() -> Self {
        Self {
            command: "start-single-node".to_string(),
            args: vec!["--insecure".to_string()],
        }
    }
}

impl ImageArgs for CockroachDbArgs {
    fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
        let mut command_and_args = self.args.clone();
        command_and_args.insert(0, self.command.clone());
        Box::new(command_and_args.into_iter())
    }
}

impl Image for CockroachDb {
    type Args = CockroachDbArgs;

    fn name(&self) -> String {
        self.name.clone()
    }

    fn tag(&self) -> String {
        self.tag.clone()
    }

    fn ready_conditions(&self) -> Vec<WaitFor> {
        vec![WaitFor::message_on_stdout("CockroachDB node starting at")]
    }

    fn env_vars(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
        Box::new(self.env_vars.iter())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testcontainers::runners::SyncRunner;

    #[test]
    fn cockroach_db_one_plus_one() {
        let cockroach = CockroachDb::default();
        let node = cockroach.start();

        let connection_string = &format!(
            "postgresql://root@127.0.0.1:{}/defaultdb?sslmode=disable",
            node.get_host_port_ipv4(26257)
        );
        let mut conn = postgres::Client::connect(connection_string, postgres::NoTls).unwrap();

        let rows = conn.query("SELECT 1 + 1", &[]).unwrap();
        assert_eq!(rows.len(), 1);

        let first_row = &rows[0];
        let first_column: i64 = first_row.get(0);
        assert_eq!(first_column, 2);
    }
}