carte 0.4.0

Runs your apps' processes on demand behind *.localhost hostnames, one stack per git worktree
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use std::io::{Read, Write};
use std::net::TcpStream;
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use std::sync::OnceLock;
use std::time::{Duration, Instant};

use carte::process::{IoMode, Output, ProcessHandle, SpawnSpec, Spawner};
use nix::sys::signal::{Signal, kill};
use nix::unistd::Pid;
use tokio::io::AsyncReadExt;

fn fixture() -> &'static Path {
    static PATH: OnceLock<PathBuf> = OnceLock::new();
    PATH.get_or_init(|| {
        let status = std::process::Command::new(env!("CARGO"))
            .args(["build", "--quiet", "-p", "fixture-app"])
            .status()
            .expect("cargo build fixture-app");
        assert!(status.success());
        let exe = std::env::current_exe().unwrap();
        exe.parent().unwrap().parent().unwrap().join("fixture-app")
    })
}

fn spawner() -> Spawner {
    Spawner::new(env!("CARGO_BIN_EXE_carte"))
}

fn spec() -> SpawnSpec {
    SpawnSpec::new(fixture().display().to_string(), std::env::temp_dir())
}

async fn read_until(output: &mut Output, seen: &mut Vec<u8>, needle: &str) {
    let found = tokio::time::timeout(Duration::from_secs(10), async {
        let mut buf = [0u8; 4096];
        while !String::from_utf8_lossy(seen).contains(needle) {
            let n = output.read(&mut buf).await.unwrap();
            assert!(n > 0, "EOF before {needle:?}");
            seen.extend_from_slice(&buf[..n]);
        }
    })
    .await;
    assert!(
        found.is_ok(),
        "timed out waiting for {needle:?}, got {:?}",
        String::from_utf8_lossy(seen)
    );
}

async fn read_to_end(output: &mut Output) -> String {
    let mut seen = Vec::new();
    tokio::time::timeout(Duration::from_secs(10), output.read_to_end(&mut seen))
        .await
        .expect("output EOF")
        .unwrap();
    String::from_utf8_lossy(&seen).into_owned()
}

async fn run_to_end(handle: &mut ProcessHandle) -> (ExitStatus, String) {
    let mut output = handle.take_output().unwrap();
    let (status, text) = tokio::join!(handle.wait(), read_to_end(&mut output));
    (status.unwrap(), text)
}

fn drain(mut output: Output) {
    tokio::spawn(async move { while output.read(&mut [0u8; 4096]).await.is_ok_and(|n| n > 0) {} });
}

fn alive(pid: u32) -> bool {
    kill(Pid::from_raw(pid as i32), None).is_ok()
}

async fn wait_dead(pid: u32) {
    let deadline = Instant::now() + Duration::from_secs(10);
    while alive(pid) {
        assert!(Instant::now() < deadline, "pid {pid} still alive");
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

async fn read_pidfile(path: &Path) -> u32 {
    let deadline = Instant::now() + Duration::from_secs(10);
    loop {
        if let Ok(s) = std::fs::read_to_string(path)
            && let Ok(pid) = s.trim().parse()
        {
            return pid;
        }
        assert!(
            Instant::now() < deadline,
            "no pidfile at {}",
            path.display()
        );
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

async fn stop(mut handle: ProcessHandle) {
    handle.stop(Duration::from_secs(2)).await.unwrap();
}

#[tokio::test]
async fn pty_output_is_line_buffered_and_a_tty() {
    let mut handle = spawner()
        .spawn(
            &spec()
                .env("FIXTURE_LINES", "3")
                .env("FIXTURE_PARTIAL", "partial>"),
        )
        .await
        .unwrap();
    assert_ne!(handle.pid(), handle.shim_pid());
    let mut output = handle.take_output().unwrap();
    let mut seen = Vec::new();
    read_until(&mut output, &mut seen, "partial>").await;
    drain(output);
    let text = String::from_utf8_lossy(&seen);
    assert!(
        text.starts_with("tty: true\r\nline 1\r\nline 2\r\nline 3\r\n"),
        "{text:?}"
    );
    assert!(alive(handle.pid()));
    stop(handle).await;
}

#[tokio::test]
async fn pipe_mode_merges_output_without_a_tty() {
    let mut handle = spawner()
        .spawn(
            &spec()
                .io(IoMode::Pipes)
                .env("FIXTURE_LINES", "2")
                .env("FIXTURE_CRASH", "4"),
        )
        .await
        .unwrap();
    let (status, text) = run_to_end(&mut handle).await;
    assert!(text.contains("tty: false\nline 1\nline 2\n"), "{text:?}");
    assert!(text.contains("crashing with 4\n"), "{text:?}");
    assert_eq!(status.code(), Some(4));
}

#[tokio::test]
async fn crash_output_survives_exit_on_a_pty() {
    let mut handle = spawner()
        .spawn(&spec().env("FIXTURE_LINES", "50").env("FIXTURE_CRASH", "3"))
        .await
        .unwrap();
    let (status, text) = run_to_end(&mut handle).await;
    assert_eq!(status.code(), Some(3));
    assert!(text.contains("line 50\r\n"), "{text:?}");
    assert!(text.contains("crashing with 3"), "{text:?}");
}

#[tokio::test]
async fn pty_input_reaches_the_foreground_group() {
    let mut handle = spawner()
        .spawn(&spec().env("FIXTURE_ECHO_STDIN", "1"))
        .await
        .unwrap();
    let mut output = handle.take_output().unwrap();
    let mut seen = Vec::new();
    read_until(&mut output, &mut seen, "ready").await;
    handle.write_input(b"hello\n").await.unwrap();
    read_until(&mut output, &mut seen, "echo: hello").await;
    drain(output);
    stop(handle).await;
}

#[tokio::test]
async fn command_runs_with_given_env_and_cwd() {
    let dir = tempfile::tempdir().unwrap();
    let mut handle = spawner()
        .spawn(
            &SpawnSpec::new("echo \"$GREETING from $(pwd -P)\"", dir.path()).env("GREETING", "hi"),
        )
        .await
        .unwrap();
    let (status, text) = run_to_end(&mut handle).await;
    let cwd = dir.path().canonicalize().unwrap();
    assert_eq!(text, format!("hi from {}\r\n", cwd.display()));
    assert!(status.success());
}

#[tokio::test]
async fn serves_http_on_port() {
    for ipv6 in [false, true] {
        let port = std::net::TcpListener::bind("127.0.0.1:0")
            .unwrap()
            .local_addr()
            .unwrap()
            .port();
        let mut spec = spec()
            .env("PORT", port.to_string())
            .env("FIXTURE_NAME", "web")
            .env("FIXTURE_ENV", "PORT,MISSING");
        if ipv6 {
            spec = spec.env("FIXTURE_BIND", "::1");
        }
        let mut handle = spawner().spawn(&spec).await.unwrap();
        let mut output = handle.take_output().unwrap();
        read_until(&mut output, &mut Vec::new(), "ready").await;
        drain(output);

        let host = if ipv6 {
            format!("[::1]:{port}")
        } else {
            format!("127.0.0.1:{port}")
        };
        assert!(get(&host, "/up").ends_with("\r\n\r\nok"));
        let body = get(&host, "/");
        let expected = format!(
            r#"{{"name":"web","pid":{},"env":{{"PORT":"{port}","MISSING":null}}}}"#,
            handle.pid()
        );
        assert!(body.ends_with(&expected), "{body}");
        stop(handle).await;
    }
}

fn get(host: &str, path: &str) -> String {
    let mut stream = TcpStream::connect(host).unwrap();
    write!(stream, "GET {path} HTTP/1.1\r\nHost: x\r\n\r\n").unwrap();
    let mut response = String::new();
    stream.read_to_string(&mut response).unwrap();
    response
}

#[tokio::test]
async fn stop_terminates_the_group() {
    let mut handle = spawner().spawn(&spec()).await.unwrap();
    drain(handle.take_output().unwrap());
    let pid = handle.pid();
    let started = Instant::now();
    let status = handle.stop(Duration::from_secs(5)).await.unwrap();
    assert_eq!(status.signal(), Some(libc::SIGTERM));
    assert!(started.elapsed() < Duration::from_secs(2));
    assert!(!alive(pid));
    assert!(handle.signal_group(Signal::SIGTERM).is_err());
}

#[tokio::test]
async fn stop_escalates_to_kill() {
    let mut handle = spawner()
        .spawn(&spec().env("FIXTURE_IGNORE_TERM", "1"))
        .await
        .unwrap();
    let mut output = handle.take_output().unwrap();
    read_until(&mut output, &mut Vec::new(), "ready").await;
    drain(output);
    let started = Instant::now();
    let status = handle.stop(Duration::from_millis(300)).await.unwrap();
    assert_eq!(status.signal(), Some(libc::SIGKILL));
    assert!(started.elapsed() >= Duration::from_millis(300));
}

#[tokio::test]
async fn sigint_is_delivered_to_the_group() {
    let mut handle = spawner().spawn(&spec()).await.unwrap();
    let mut output = handle.take_output().unwrap();
    read_until(&mut output, &mut Vec::new(), "ready").await;
    drain(output);
    handle.signal_group(Signal::SIGINT).unwrap();
    assert_eq!(handle.wait().await.unwrap().signal(), Some(libc::SIGINT));
}

#[tokio::test]
async fn shim_forwards_signals_to_the_group() {
    let dir = tempfile::tempdir().unwrap();
    let pidfile = dir.path().join("child.pid");
    let mut handle = spawner()
        .spawn(&spec().env("FIXTURE_CHILD_PIDFILE", &pidfile))
        .await
        .unwrap();
    drain(handle.take_output().unwrap());
    let child = read_pidfile(&pidfile).await;
    kill(Pid::from_raw(handle.shim_pid() as i32), Signal::SIGTERM).unwrap();
    assert_eq!(handle.wait().await.unwrap().signal(), Some(libc::SIGTERM));
    wait_dead(child).await;
}

#[tokio::test]
async fn dropping_the_handle_kills_the_whole_group() {
    let dir = tempfile::tempdir().unwrap();
    let pidfile = dir.path().join("child.pid");
    let handle = spawner()
        .spawn(
            &spec()
                .env("FIXTURE_CHILD_PIDFILE", &pidfile)
                .env("FIXTURE_IGNORE_TERM", "1"),
        )
        .await
        .unwrap();
    let (pid, shim) = (handle.pid(), handle.shim_pid());
    let child = read_pidfile(&pidfile).await;
    assert!(alive(child));
    drop(handle);
    wait_dead(child).await;
    wait_dead(pid).await;
    wait_dead(shim).await;
}

#[tokio::test]
async fn closing_the_lifeline_alone_kills_the_group() {
    let dir = tempfile::tempdir().unwrap();
    let pidfile = dir.path().join("child.pid");
    let mut handle = spawner()
        .spawn(&spec().env("FIXTURE_CHILD_PIDFILE", &pidfile))
        .await
        .unwrap();
    drain(handle.take_output().unwrap());
    let child = read_pidfile(&pidfile).await;
    handle.close_lifeline();
    let status = tokio::time::timeout(Duration::from_secs(10), handle.wait())
        .await
        .unwrap()
        .unwrap();
    assert!(!status.success());
    wait_dead(handle.pid()).await;
    wait_dead(child).await;
}

#[tokio::test]
async fn self_daemonising_child_escapes_the_group() {
    let dir = tempfile::tempdir().unwrap();
    let pidfile = dir.path().join("daemon.pid");
    let handle = spawner()
        .spawn(&spec().env("FIXTURE_DAEMON_PIDFILE", &pidfile))
        .await
        .unwrap();
    let pid = handle.pid();
    let daemon = read_pidfile(&pidfile).await;
    drop(handle);
    wait_dead(pid).await;
    let escaped = alive(daemon);
    let _ = kill(Pid::from_raw(daemon as i32), Signal::SIGKILL);
    assert!(
        escaped,
        "expected the setsid grandchild to survive the group kill"
    );
}

#[tokio::test]
async fn failing_command_reports_its_exit_code() {
    let mut handle = spawner()
        .spawn(&SpawnSpec::new("exit 7", "/"))
        .await
        .unwrap();
    assert_eq!(run_to_end(&mut handle).await.0.code(), Some(7));
    let mut handle = spawner()
        .spawn(&SpawnSpec::new("no-such-command-xyz", "/"))
        .await
        .unwrap();
    let (status, text) = run_to_end(&mut handle).await;
    assert_eq!(status.code(), Some(127));
    assert!(text.contains("no-such-command-xyz"), "{text:?}");
}

/// Plays the daemon for `sigkilled_daemon_takes_its_groups_down`; a no-op otherwise.
#[tokio::test]
async fn fake_daemon() {
    let Some(dir) = std::env::var_os("CARTE_TEST_FAKE_DAEMON") else {
        return;
    };
    let dir = PathBuf::from(dir);
    let mut handles = Vec::new();
    for (i, io) in [IoMode::default(), IoMode::Pipes].into_iter().enumerate() {
        let pidfile = dir.join(format!("child{i}.pid"));
        let mut handle = spawner()
            .spawn(
                &spec()
                    .io(io)
                    .env("FIXTURE_CHILD_PIDFILE", &pidfile)
                    .env("FIXTURE_IGNORE_TERM", "1"),
            )
            .await
            .unwrap();
        drain(handle.take_output().unwrap());
        std::fs::write(dir.join(format!("leader{i}.pid")), handle.pid().to_string()).unwrap();
        handles.push(handle);
    }
    std::future::pending::<()>().await;
}

#[tokio::test]
async fn sigkilled_daemon_takes_its_groups_down() {
    let dir = tempfile::tempdir().unwrap();
    fixture();
    let mut daemon = std::process::Command::new(std::env::current_exe().unwrap())
        .args(["--exact", "fake_daemon", "--nocapture"])
        .env("CARTE_TEST_FAKE_DAEMON", dir.path())
        .stdout(std::process::Stdio::null())
        .spawn()
        .unwrap();
    let mut pids = Vec::new();
    for i in 0..2 {
        pids.push(read_pidfile(&dir.path().join(format!("leader{i}.pid"))).await);
        pids.push(read_pidfile(&dir.path().join(format!("child{i}.pid"))).await);
    }
    assert!(pids.iter().all(|&pid| alive(pid)));
    daemon.kill().unwrap();
    daemon.wait().unwrap();
    for pid in pids {
        wait_dead(pid).await;
    }
}