shep 0.24.1

The shep binary: a process manager that keeps a flock of long-running processes alive on macOS, Linux and Windows, with logs, watch and cron restarts, and webhook alerts
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! `shep disable` and `shep enable` on a sheep: what reaches the wire, what
//! is refused before it, and what the operator is told.

use super::*;
use crate::commands::lifecycle::toggle::{Toggle, toggle, toggle_sheep, toggle_with};

fn stopped_api(disabled: bool) -> ProcessInfo {
    ProcessInfo::builder(0, "api", shep_core::status::ProcStatus::Stopped)
        .disabled(disabled.then_some(true))
        .build()
}

/// Runs `verb name` against `client`, handing back the code and both streams.
async fn toggle_against(
    client: &Client,
    verb: Toggle,
    name: &str,
    fmt: Format,
) -> (ExitCode, String, String) {
    let mut out = Vec::new();
    let mut err = Vec::new();
    let code = {
        let mut streams = Streams::bare(&mut out, &mut err, fmt);
        toggle_sheep(client, &mut streams, verb, name, &[]).await
    };
    (
        code,
        String::from_utf8(out).unwrap(),
        String::from_utf8(err).unwrap(),
    )
}

/// fails if the CLI sends anything but the app's exact name, such as a
/// regex or an id, for `disable api`.
#[tokio::test]
async fn disable_sends_the_app_name() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, mut envelopes) = fake_client_answering(&path, |request| match request {
        Request::Disable { .. } => Response::Disabled(vec![stopped_api(true)]),
        Request::ListFlock => Response::Flock(vec![stopped_api(true)]),
        _ => Response::Pong,
    })
    .await;

    let (code, printed, said) =
        toggle_against(&client, Toggle::Disable, "api", Format::Table).await;

    assert_eq!(code, ExitCode::Success, "{said}");
    assert!(
        printed.contains("disabled"),
        "STATUS shows the mark: {printed}"
    );
    let sent = envelopes.recv().await.unwrap();
    assert_eq!(
        sent.body,
        Request::Disable {
            selector: SelectorSpec::Name("api".to_string())
        }
    );
}

#[tokio::test]
async fn enable_sends_the_app_name_and_prints_the_started_sheep() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let online = ProcessInfo::builder(0, "api", shep_core::status::ProcStatus::Online).build();
    let listed = online.clone();
    let (client, mut envelopes) = fake_client_answering(&path, move |request| match request {
        Request::Enable { .. } => Response::Enabled(vec![online.clone()]),
        Request::ListFlock => Response::Flock(vec![listed.clone()]),
        _ => Response::Pong,
    })
    .await;

    let (code, printed, said) = toggle_against(&client, Toggle::Enable, "api", Format::Table).await;

    assert_eq!(code, ExitCode::Success, "{said}");
    assert!(printed.contains("online"), "{printed}");
    assert_eq!(
        envelopes.recv().await.unwrap().body,
        Request::Enable {
            selector: SelectorSpec::Name("api".to_string())
        }
    );
}

/// fails if enabling a sheep that is not disabled prints a table as though
/// something started, or exits non-zero.
#[tokio::test]
async fn enabling_a_sheep_that_is_not_disabled_says_so_and_succeeds() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, _envelopes) = fake_client_answering(&path, |request| match request {
        Request::Enable { .. } => Response::Enabled(Vec::new()),
        _ => Response::Pong,
    })
    .await;

    let (code, printed, said) = toggle_against(&client, Toggle::Enable, "api", Format::Table).await;

    assert_eq!(code, ExitCode::Success, "{said}");
    assert!(
        printed.contains("api") && printed.contains("not disabled"),
        "the operator is told nothing changed: {printed}"
    );
    assert!(
        !printed.contains("online"),
        "no table for a no-op: {printed}"
    );
}

/// fails if `name:slot` reaches the wire, or is refused without naming the
/// app the operator should have typed.
#[tokio::test]
async fn an_instance_is_refused_with_usage_naming_the_app() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, mut envelopes) = fake_client_capturing_envelopes(&path).await;

    for verb in [Toggle::Disable, Toggle::Enable] {
        let (code, printed, said) = toggle_against(&client, verb, "api:1", Format::Table).await;

        assert_eq!(code, ExitCode::Usage, "{said}");
        assert!(said.contains("`api`"), "names the app: {said}");
        assert!(printed.is_empty(), "{printed}");
    }
    assert!(
        envelopes.try_recv().is_err(),
        "a refused instance sends nothing"
    );
}

/// fails if a bare id is widened by the CLI or sent, rather than refused
/// with the name of the app that id belongs to.
#[tokio::test]
async fn an_id_is_refused_with_usage_naming_its_app() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let three = ProcessInfo::builder(3, "api", shep_core::status::ProcStatus::Online).build();
    let (client, mut envelopes) = fake_client_answering(&path, move |request| match request {
        Request::ListFlock => Response::Flock(vec![three.clone()]),
        _ => Response::Pong,
    })
    .await;

    let (code, _printed, said) = toggle_against(&client, Toggle::Disable, "3", Format::Table).await;

    assert_eq!(code, ExitCode::Usage, "{said}");
    assert!(said.contains("`api`"), "names the app: {said}");
    while let Ok(envelope) = envelopes.try_recv() {
        assert_eq!(envelope.body, Request::ListFlock, "only a lookup is sent");
    }
}

/// fails if a wildcard is refused client-side, or is widened or narrowed
/// before it reaches the shepherd, which owns what a sweep matches.
#[tokio::test]
async fn a_wildcard_is_sent_to_the_shepherd_as_typed() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, mut envelopes) = fake_client_answering(&path, |request| match request {
        Request::Disable { .. } => Response::Disabled(vec![stopped_api(true)]),
        Request::Enable { .. } => Response::Enabled(vec![stopped_api(false)]),
        Request::ListFlock => Response::Flock(vec![stopped_api(true)]),
        _ => Response::Pong,
    })
    .await;

    let targets = [
        ("all", SelectorSpec::All),
        ("/api/", SelectorSpec::Regex("api".to_string())),
        ("fold:backed", SelectorSpec::Fold("backed".to_string())),
    ];
    for (target, selector) in targets {
        for verb in [Toggle::Disable, Toggle::Enable] {
            let (code, _printed, said) = toggle_against(&client, verb, target, Format::Table).await;
            assert_eq!(code, ExitCode::Success, "{target}: {said}");
            let sent = loop {
                let body = envelopes.recv().await.unwrap().body;
                if body != Request::ListFlock {
                    break body;
                }
            };
            let expected = match verb {
                Toggle::Disable => Request::Disable {
                    selector: selector.clone(),
                },
                Toggle::Enable => Request::Enable {
                    selector: selector.clone(),
                },
            };
            assert_eq!(sent, expected, "{target}");
        }
    }
}

/// fails if `enable all` with nothing disabled prints a table as though
/// something started.
#[tokio::test]
async fn enable_all_with_nothing_disabled_says_so_and_succeeds() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, _envelopes) = fake_client_answering(&path, |request| match request {
        Request::Enable { .. } => Response::Enabled(Vec::new()),
        _ => Response::Pong,
    })
    .await;

    let (code, printed, said) = toggle_against(&client, Toggle::Enable, "all", Format::Table).await;

    assert_eq!(code, ExitCode::Success, "{said}");
    assert!(
        printed.contains("matching `all`") && printed.contains("nothing changed"),
        "{printed}"
    );
}

/// fails if `disable all` with every sheep already disabled prints a table or
/// exits non-zero for work that is already done.
#[tokio::test]
async fn disable_all_with_everything_disabled_says_so_and_succeeds() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, _envelopes) = fake_client_answering(&path, |request| match request {
        Request::Disable { .. } => Response::Disabled(Vec::new()),
        _ => Response::Pong,
    })
    .await;

    let (code, printed, said) =
        toggle_against(&client, Toggle::Disable, "all", Format::Table).await;

    assert_eq!(code, ExitCode::Success, "{said}");
    assert!(
        printed.contains("already disabled") && printed.contains("nothing changed"),
        "{printed}"
    );
}

/// fails if an older shepherd's `Unsupported` reaches the operator as a bare
/// protocol error instead of telling them to upgrade it.
#[tokio::test]
async fn an_older_shepherd_is_told_to_upgrade() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, _daemon) =
        fake_client_replying_err(&path, RpcErrorCode::Unsupported, "unknown variant").await;

    let (code, _printed, said) =
        toggle_against(&client, Toggle::Disable, "api", Format::Table).await;

    assert_eq!(code, ExitCode::Unsupported, "{said}");
    assert!(
        said.contains("upgrad") && said.contains("shepherd"),
        "{said}"
    );
}

/// fails if the shepherd's `Disabled` refusal stops exiting 2 or loses the
/// `shep enable` it names.
#[tokio::test]
async fn a_disabled_refusal_exits_usage_and_names_shep_enable() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, _daemon) = fake_client_replying_err(
        &path,
        RpcErrorCode::Disabled,
        "api is disabled; run `shep enable api`",
    )
    .await;

    let (code, _printed, said) =
        toggle_against(&client, Toggle::Disable, "api", Format::Table).await;

    assert_eq!(code, ExitCode::Usage, "{said}");
    assert!(said.contains("shep enable api"), "{said}");
}

/// fails if an unknown name loses its own exit code and the shepherd's words.
#[tokio::test]
async fn an_unknown_name_keeps_its_exit_code_and_message() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, _daemon) =
        fake_client_replying_err(&path, RpcErrorCode::NotFound, "no sheep named api").await;

    let (code, _printed, said) =
        toggle_against(&client, Toggle::Enable, "api", Format::Table).await;

    assert_eq!(code, ExitCode::NotFound, "{said}");
    assert!(said.contains("no sheep named api"), "{said}");
}

/// fails if `disable api` with no shepherd falls through to a silent
/// success, or autostarts one.
#[tokio::test]
async fn with_no_shepherd_a_sheep_is_refused_and_the_remedy_is_named() {
    let home = tempfile::tempdir().unwrap();
    let paths = ShepPaths::resolve(&|_| None, home.path());

    for verb in [Toggle::Disable, Toggle::Enable] {
        let mut out = Vec::new();
        let mut err = Vec::new();
        let code = {
            let mut streams = Streams::bare(&mut out, &mut err, Format::Table);
            toggle(&mut streams, &paths, verb, "api", true).await
        };
        let said = String::from_utf8(err).unwrap();

        assert_eq!(code, ExitCode::DaemonUnreachable, "{said}");
        assert!(
            said.contains("no shepherd") && said.contains("shep start"),
            "{said}"
        );
        assert!(out.is_empty());
    }
}

/// fails if a dog's name stops going the way it always has: through the
/// config, with no shepherd required.
#[tokio::test]
async fn a_dogs_name_still_edits_the_config_with_no_shepherd() {
    let home = tempfile::tempdir().unwrap();
    let paths = ShepPaths::resolve(&|_| None, home.path());
    let mut out = Vec::new();
    let mut err = Vec::new();
    let code = {
        let mut streams = Streams::bare(&mut out, &mut err, Format::Table);
        toggle(&mut streams, &paths, Toggle::Enable, "metrics", true).await
    };

    assert_eq!(code, ExitCode::Success, "{}", String::from_utf8_lossy(&err));
    let config = std::fs::read_to_string(&paths.daemon_config).unwrap();
    assert!(config.contains("metrics"), "{config}");
}

/// A dog's name over a host is a selector like any other: the dog half reads
/// and edits this machine's `shep.toml`, and the host's dogs are not in it.
/// `mydog` is adopted here, so a local read would send it down the dog path and
/// never to the wire.
#[tokio::test]
async fn enable_over_a_host_treats_a_dog_name_as_a_sheep_selector_and_reads_no_local_shep_toml() {
    let dir = tempfile::tempdir().unwrap();
    let paths = ShepPaths::resolve(&|_| None, dir.path());
    std::fs::create_dir_all(&paths.run).unwrap();
    crate::commands::shep_toml::ShepToml::edit(&paths.daemon_config, |cfg| {
        cfg.adopt_dog("mydog", std::path::Path::new("/usr/local/bin/mydog"), false)
            .unwrap();
    })
    .unwrap();
    let config_before = std::fs::read(&paths.daemon_config).unwrap();
    let (client, mut envelopes) = fake_client_answering(&paths.socket, |request| match request {
        Request::Enable { .. } => Response::Enabled(Vec::new()),
        _ => Response::Pong,
    })
    .await;
    let mut out = Vec::new();
    let mut err = Vec::new();

    let code = {
        let mut streams = Streams::bare(&mut out, &mut err, Format::Table);
        toggle_with(
            &mut streams,
            None,
            async |_| Ok(client),
            Toggle::Enable,
            "mydog",
            true,
        )
        .await
    };

    let said = String::from_utf8(err).unwrap();
    assert_eq!(code, ExitCode::Success, "{said}");
    assert_eq!(
        envelopes.recv().await.unwrap().body,
        Request::Enable {
            selector: SelectorSpec::Name("mydog".to_string())
        },
        "the host's shepherd is the one that knows whether this name is a dog"
    );
    assert_eq!(std::fs::read(&paths.daemon_config).unwrap(), config_before);
    assert!(!paths.dogs_config.exists(), "no dog verb ran here");
}

/// fails if a missing remote shepherd sends the operator to `shep start` on
/// this machine, which would start the wrong one.
#[cfg(unix)]
#[tokio::test]
async fn disable_over_a_host_with_no_remote_shepherd_names_the_host_not_shep_start() {
    use std::os::unix::fs::PermissionsExt as _;

    use crate::commands::lifecycle::toggle::toggle_on_host;

    let dir = tempfile::tempdir().unwrap();
    let program = dir.path().join("ssh");
    std::fs::write(
        &program,
        "#!/bin/sh\necho 'no shepherd is running' >&2\nexit 5\n",
    )
    .unwrap();
    std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap();
    let link = crate::remote::Link::Host {
        dest: "amp2",
        options: shep_client::SshOptions::default().program(program),
    };
    let mut out = Vec::new();
    let mut err = Vec::new();

    let code = {
        let mut streams = Streams::bare(&mut out, &mut err, Format::Table);
        toggle_on_host(
            &mut streams,
            &link,
            crate::version_guard::VersionGuard::Enforce,
            Toggle::Disable,
            "api",
        )
        .await
    };

    let said = String::from_utf8(err).unwrap();
    assert_eq!(code, ExitCode::DaemonUnreachable, "{said}");
    assert!(said.contains("no shepherd is running on `amp2`"), "{said}");
    assert!(!said.contains("shep start"), "{said}");
    assert!(out.is_empty());
}