testcontainers_modules/cockroach_db/
mod.rs1use std::borrow::Cow;
2
3use testcontainers::{core::WaitFor, Image};
4
5const DEFAULT_IMAGE_NAME: &str = "cockroachdb/cockroach";
6const DEFAULT_IMAGE_TAG: &str = "v23.2.3";
7
8#[derive(Debug, Default, Clone)]
26pub struct CockroachDb {
27 cmd: CockroachDbCmd,
28}
29
30impl CockroachDb {
31 pub fn new(cmd: CockroachDbCmd) -> Self {
33 CockroachDb { cmd }
34 }
35}
36
37#[derive(Debug, Clone, Copy)]
39pub enum CockroachDbCmd {
40 StartSingleNode {
42 insecure: bool,
57 },
58}
59
60impl Default for CockroachDbCmd {
61 fn default() -> Self {
62 Self::StartSingleNode { insecure: true }
63 }
64}
65
66impl Image for CockroachDb {
67 fn name(&self) -> &str {
68 DEFAULT_IMAGE_NAME
69 }
70
71 fn tag(&self) -> &str {
72 DEFAULT_IMAGE_TAG
73 }
74
75 fn ready_conditions(&self) -> Vec<WaitFor> {
76 vec![WaitFor::message_on_stdout("CockroachDB node starting at")]
77 }
78
79 fn cmd(&self) -> impl IntoIterator<Item = impl Into<Cow<'_, str>>> {
80 self.cmd
81 }
82}
83
84impl IntoIterator for CockroachDbCmd {
85 type Item = String;
86 type IntoIter = <Vec<String> as IntoIterator>::IntoIter;
87
88 fn into_iter(self) -> Self::IntoIter {
89 match self {
90 CockroachDbCmd::StartSingleNode { insecure } => {
91 let mut cmd = vec!["start-single-node".to_string()];
92 if insecure {
93 cmd.push("--insecure".to_string());
94 }
95 cmd.into_iter()
96 }
97 }
98 }
99}
100
101#[cfg(test)]
102mod tests {
103 use testcontainers::core::IntoContainerPort;
104
105 use super::*;
106 use crate::testcontainers::runners::SyncRunner;
107
108 #[test]
109 fn cockroach_db_one_plus_one() -> Result<(), Box<dyn std::error::Error + 'static>> {
110 let cockroach = CockroachDb::default();
111 let node = cockroach.start()?;
112
113 let connection_string = &format!(
114 "postgresql://root@127.0.0.1:{}/defaultdb?sslmode=disable",
115 node.get_host_port_ipv4(26257.tcp())?
116 );
117 let mut conn = postgres::Client::connect(connection_string, postgres::NoTls).unwrap();
118
119 let rows = conn.query("SELECT 1 + 1", &[]).unwrap();
120 assert_eq!(rows.len(), 1);
121
122 let first_row = &rows[0];
123 let first_column: i64 = first_row.get(0);
124 assert_eq!(first_column, 2);
125 Ok(())
126 }
127}