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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use crate::{NodeSpec, RedisConnKey, RedisConnection};
use anyhow::Context;
use std::process::Stdio;
use std::time::Duration;
use tempfile::TempDir;
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader};
use tokio::process::{Child, Command};
use tokio::time::timeout;

/// A local redis server for executing tests against
pub struct RedisServer {
    _daemon: Child,
    port: u16,
    _dir: TempDir,
}

/// Ask the kernel to assign a free port.
/// We pass this to sshd and tell it to listen on that port.
/// This is racy, as releasing the socket technically makes
/// that port available to others using the same technique.
fn allocate_port() -> u16 {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind 127.0.0.1:0 failed");
    listener.local_addr().unwrap().port()
}

impl RedisServer {
    pub fn is_available() -> bool {
        which::which("redis-server").is_ok()
    }

    pub async fn spawn(extra_config: &str) -> anyhow::Result<Self> {
        let mut errors = vec![];

        for _ in 0..2 {
            let port = allocate_port();
            match timeout(
                Duration::from_secs(5),
                Self::spawn_with_port(port, extra_config),
            )
            .await?
            {
                Ok(me) => return Ok(me),
                Err(err) => {
                    errors.push(format!("{err:#}"));
                }
            }
        }
        anyhow::bail!("failed to spawn redis-server: {}", errors.join(". "));
    }

    async fn spawn_with_port(port: u16, extra_config: &str) -> anyhow::Result<Self> {
        let dir = tempfile::tempdir().context("make temp dir")?;
        let mut daemon = Command::new("redis-server")
            .args(["-"])
            .stdin(Stdio::piped())
            .stderr(Stdio::piped())
            .stdout(Stdio::piped())
            .kill_on_drop(true)
            .spawn()
            .context("spawning redis-server")?;

        let mut stdout = BufReader::new(daemon.stdout.take().unwrap());
        let mut stderr = daemon.stderr.take().unwrap();

        tokio::spawn(async move {
            copy_stream_with_line_prefix("redis stderr", &mut stderr, &mut tokio::io::stderr())
                .await
        });

        // Generate configuration
        if let Some(mut stdin) = daemon.stdin.take() {
            stdin
                .write_all(b"bind 127.0.0.1\nlogfile /dev/stdout\n")
                .await?;
            stdin.write_all(format!("port {port}\n").as_bytes()).await?;
            stdin
                .write_all(format!("dir {}\n", dir.path().display()).as_bytes())
                .await?;
            stdin
                .write_all(format!("{extra_config}\n").as_bytes())
                .await?;
            drop(stdin);
        }

        // Wait until the server initializes successfully
        loop {
            let mut line = String::new();
            stdout.read_line(&mut line).await?;
            if line.is_empty() {
                anyhow::bail!("Unexpected EOF while reading output from redis-server");
            }
            eprintln!("{}", line.trim());

            if line.contains("Server initialized")
                || line.contains("The server is now ready to accept connections on port")
            {
                break;
            }
        }

        // Now just pipe the output through to the test harness
        tokio::spawn(async move {
            copy_stream_with_line_prefix("redis stdout", &mut stdout, &mut tokio::io::stderr())
                .await
        });

        Ok(Self {
            _daemon: daemon,
            port,
            _dir: dir,
        })
    }

    pub async fn connection(&self) -> anyhow::Result<RedisConnection> {
        let key = RedisConnKey {
            node: NodeSpec::Single(format!("redis://127.0.0.1:{}", self.port)),
            read_from_replicas: false,
            username: None,
            password: None,
            cluster: None,
            pool_size: None,
            connect_timeout: None,
            recycle_timeout: None,
            wait_timeout: None,
            response_timeout: None,
        };
        key.open()
    }
}

pub struct RedisCluster {
    primary: RedisServer,
    secondary: RedisServer,
    tertiary: RedisServer,
}

impl RedisCluster {
    /// Check whether redis is available to run as a cluster.
    /// We look for redis 7.x and later, because we rely on
    /// the --cluster-yes option actually working as part of
    /// our cluster initialization. It doesn't work on redis 5.x
    /// which is present on rocky8 for example.
    pub async fn is_available() -> bool {
        if !RedisServer::is_available() {
            return false;
        }

        match Command::new("redis-cli").arg("-v").output().await {
            Ok(output) => {
                let stdout = String::from_utf8_lossy(&output.stdout);
                match stdout.lines().next() {
                    Some(line) => {
                        let Some((redis, version)) = line.split_once(" ") else {
                            return false;
                        };
                        if redis == "redis-cli" {
                            let Some((major, _rest)) = version.split_once(".") else {
                                return false;
                            };
                            let Ok(major) = major.parse::<u32>() else {
                                return false;
                            };
                            major >= 7
                        } else {
                            false
                        }
                    }
                    None => false,
                }
            }
            Err(_) => false,
        }
    }

    pub async fn spawn() -> anyhow::Result<Self> {
        let extra_config = "cluster-enabled yes\n";
        let primary = RedisServer::spawn(extra_config).await?;
        let secondary = RedisServer::spawn(extra_config).await?;
        let tertiary = RedisServer::spawn(extra_config).await?;

        let cluster_setup = Command::new("redis-cli")
            .args([
                "--cluster",
                "create",
                &format!("127.0.0.1:{}", primary.port),
                &format!("127.0.0.1:{}", secondary.port),
                &format!("127.0.0.1:{}", tertiary.port),
                "--cluster-yes",
            ])
            .kill_on_drop(true)
            .output()
            .await
            .context("create redis cluster")?;

        if !cluster_setup.stdout.is_empty() {
            eprintln!(
                "cluster_setup stdout: {}",
                String::from_utf8_lossy(&cluster_setup.stdout)
            );
        }
        if !cluster_setup.stderr.is_empty() {
            eprintln!(
                "cluster_setup stderr: {}",
                String::from_utf8_lossy(&cluster_setup.stderr)
            );
        }

        Ok(Self {
            primary,
            secondary,
            tertiary,
        })
    }

    pub async fn connection(&self) -> anyhow::Result<RedisConnection> {
        let key = RedisConnKey {
            node: NodeSpec::Cluster(vec![
                format!("redis://127.0.0.1:{}", self.primary.port),
                format!("redis://127.0.0.1:{}", self.secondary.port),
                format!("redis://127.0.0.1:{}", self.tertiary.port),
            ]),
            read_from_replicas: false,
            username: None,
            password: None,
            cluster: None,
            pool_size: None,
            connect_timeout: None,
            recycle_timeout: None,
            wait_timeout: None,
            response_timeout: None,
        };
        key.open()
    }
}

async fn copy_stream_with_line_prefix<SRC, DEST>(
    prefix: &str,
    src: SRC,
    mut dest: DEST,
) -> std::io::Result<()>
where
    SRC: AsyncRead + Unpin,
    DEST: AsyncWrite + Unpin,
{
    let mut src = tokio::io::BufReader::new(src);
    loop {
        let mut line = String::new();
        src.read_line(&mut line).await?;
        if !line.is_empty() {
            dest.write_all(format!("{prefix}: {line}").as_bytes())
                .await?;
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[tokio::test]
    async fn test_basic_operation() -> anyhow::Result<()> {
        if !RedisServer::is_available() {
            return Ok(());
        }
        let daemon = RedisServer::spawn("").await?;
        let connection = daemon.connection().await?;

        let mut cmd = redis::cmd("SET");
        cmd.arg("my_key").arg(42);
        connection.query(cmd).await?;

        let mut cmd = redis::cmd("GET");
        cmd.arg("my_key");
        let value = connection.query(cmd).await?;

        assert_eq!(value, redis::Value::BulkString(b"42".to_vec()));

        Ok(())
    }

    #[tokio::test]
    async fn test_basic_operation_cluster() -> anyhow::Result<()> {
        if !RedisCluster::is_available().await {
            return Ok(());
        }
        let daemon = RedisCluster::spawn().await?;
        let connection = daemon.connection().await?;

        let mut cmd = redis::cmd("SET");
        cmd.arg("my_key").arg(42);
        connection.query(cmd).await?;

        let mut cmd = redis::cmd("GET");
        cmd.arg("my_key");
        let value = connection.query(cmd).await?;

        assert_eq!(value, redis::Value::BulkString(b"42".to_vec()));

        Ok(())
    }
}