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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use shep_client::testing::fake_client_capturing_envelopes;

use super::*;
use crate::cli::Format;
use crate::commands::dogs::test_support::streams;

/// `adopt`'s own report is the only place an operator learns which
/// refusal mode it was.
#[tokio::test]
async fn adopt_of_a_missing_binary_reports_the_refusal_on_stderr() {
    let dir = tempfile::tempdir().unwrap();
    let paths = ShepPaths::resolve(&|_| None, dir.path());
    let mut out = Vec::new();
    let mut err = Vec::new();
    let args = AdoptArgs {
        yes: true,
        name: Some("otel".to_string()),
        path: dir.path().join("nope"),
    };
    let code = adopt(&mut streams(&mut out, &mut err), &paths, &args).await;

    assert_eq!(code, ExitCode::InvalidConfig);
    let text = String::from_utf8(err).unwrap();
    assert!(
        text.contains("no file exists at that path"),
        "the refusal must reach the operator: {text}"
    );
}

#[tokio::test]
async fn adopt_asks_the_shepherd_to_start_that_dog_with_its_adopted_source() {
    for channel in [false, true] {
        let dir = tempfile::tempdir().unwrap();
        let path = shep_client::testing::control_address(dir.path());
        let (client, mut envelopes) = fake_client_capturing_envelopes(&path).await;
        let mut out = Vec::new();
        let mut err = Vec::new();
        let binary = PathBuf::from("/usr/local/bin/shep-otel");
        let _ = adopt_after_config(
            &mut streams(&mut out, &mut err),
            "adopt",
            "otel",
            DogSource::Adopted {
                grants: None,
                path: binary.display().to_string(),
                channel,
                events: Vec::new(),
            },
            &BTreeMap::new(),
            Some(&client),
            false,
        )
        .await;

        let sent = envelopes.recv().await.unwrap();
        assert_eq!(
            sent.body,
            Request::EnableDog {
                name: "otel".to_string(),
                env: BTreeMap::default(),
                source: DogSource::Adopted {
                    grants: None,
                    path: "/usr/local/bin/shep-otel".to_string(),
                    channel,
                    events: Vec::new(),
                },
            }
        );
    }
}

/// Writes an executable `/bin/sh` dog that answers `--version` with
/// `answer` and exits 0 on any other run.
fn write_dog(dir: &Path, answer: &str) -> PathBuf {
    let binary = dir.join("shep-otel");
    let script =
        format!("#!/bin/sh\nif [ \"$1\" = --version ]; then printf '{answer}'; fi\nexit 0\n");
    std::fs::write(&binary, script).unwrap();
    let mut mode = std::fs::metadata(&binary).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o755);
    std::fs::set_permissions(&binary, mode).unwrap();
    binary
}

/// Writes an executable `/bin/sh` dog that answers `--shep-manifest` with
/// `manifest` and exits 0 on any other run.
fn write_manifest_dog(dir: &Path, manifest: &str) -> PathBuf {
    let binary = dir.join("shep-paddock");
    let script = format!(
        "#!/bin/sh\nif [ \"$1\" = --shep-manifest ]; then printf '%s' '{manifest}'; fi\nexit 0\n"
    );
    std::fs::write(&binary, script).unwrap();
    let mut mode = std::fs::metadata(&binary).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o755);
    std::fs::set_permissions(&binary, mode).unwrap();
    binary
}

/// A pane is answered over the channel, so a dog that asked for one without a
/// channel has none. `adopt` says so and adopts the dog anyway: unlike
/// `validate` without a channel, this is a screen the operator loses rather
/// than a check shep cannot run.
#[tokio::test]
async fn adopt_says_when_a_dog_asks_for_a_pane_it_cannot_have() {
    for (manifest, said) in [
        (
            r#"{"manifest":3,"name":"paddock","version":"0.1.0","protocol":12,"pane":true}"#,
            true,
        ),
        (
            r#"{"manifest":3,"name":"paddock","version":"0.1.0","protocol":12,"channel":true,"pane":true}"#,
            false,
        ),
    ] {
        let dir = tempfile::tempdir().unwrap();
        let paths = ShepPaths::resolve(&|_| None, dir.path());
        let args = AdoptArgs {
            yes: true,
            name: Some("paddock".to_string()),
            path: write_manifest_dog(dir.path(), manifest),
        };
        let mut out = Vec::new();
        let mut err = Vec::new();

        let code = adopt(&mut streams(&mut out, &mut err), &paths, &args).await;

        assert_eq!(code, ExitCode::Success);
        let notices = String::from_utf8(err).unwrap();
        assert_eq!(
            notices.contains("dog_pane_without_channel"),
            said,
            "the operator hears which pane the dog will not have: {notices}"
        );
    }
}

/// Through `adopt` itself, so the ask travels from a real probe answer
/// to `shep.toml`. A dog that does not ask is the control.
#[tokio::test]
async fn adopt_records_a_channel_ask_only_when_the_dog_makes_one() {
    for (answer, asked) in [
        (r"shep-otel 0.1.0\nshep-channel: true\n", true),
        (r"shep-otel 0.1.0\n", false),
    ] {
        let dir = tempfile::tempdir().unwrap();
        let paths = ShepPaths::resolve(&|_| None, dir.path());
        let args = AdoptArgs {
            yes: true,
            name: Some("otel".to_string()),
            path: write_dog(dir.path(), answer),
        };
        let mut out = Vec::new();
        let mut err = Vec::new();

        let code = adopt(&mut streams(&mut out, &mut err), &paths, &args).await;

        assert_eq!(code, ExitCode::Success);
        let written = std::fs::read_to_string(&paths.daemon_config).unwrap();
        let cfg = shep_core::config::DaemonConfig::load(Some(&written), &|_| None).unwrap();
        assert_eq!(
            cfg.daemon.channel_dogs.contains(&"otel".to_string()),
            asked,
            "{written}"
        );
        let notices = String::from_utf8(err).unwrap();
        assert_eq!(
            notices.contains("asked for the shepherd channel"),
            asked,
            "the operator hears what the dog asked for: {notices}"
        );
    }
}

/// `shepherd_acted` and `status` together separate "only the config
/// changed" from "a shepherd acted", and this verb's two branches differ
/// in little else. The pair is the guard: either field alone passes while
/// the other branch carries the wrong one.
#[tokio::test]
async fn adopt_reports_whether_a_shepherd_acted() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, _envelopes) = shep_client::testing::fake_client_answering(&path, |_| {
        Response::DogStarted(shep_client::testing::sample_info())
    })
    .await;
    let binary = PathBuf::from("/usr/local/bin/shep-otel");

    for (client, expected) in [
        (Some(&client), DogActionStatus::Acted("online")),
        (None, NO_SHEPHERD_ENABLE_STATUS),
    ] {
        let mut out = Vec::new();
        let mut err = Vec::new();
        let mut streams = streams(&mut out, &mut err);
        streams.fmt = Format::Json;

        let code = adopt_after_config(
            &mut streams,
            "adopt",
            "otel",
            DogSource::Adopted {
                grants: None,
                path: binary.display().to_string(),
                channel: false,
                events: Vec::new(),
            },
            &BTreeMap::new(),
            client,
            false,
        )
        .await;

        assert_eq!(code, ExitCode::Success);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(
            json["data"]["shepherd_acted"],
            serde_json::json!(expected.shepherd_acted()),
            "client present: {}, payload: {}",
            client.is_some(),
            String::from_utf8_lossy(&out)
        );
        assert_eq!(
            json["data"]["status"],
            expected.text(),
            "payload: {}",
            String::from_utf8_lossy(&out)
        );
    }
}

#[tokio::test]
async fn adopt_with_no_shepherd_writes_the_config_and_exits_zero() {
    let dir = tempfile::tempdir().unwrap();
    let paths = ShepPaths::resolve(&|_| None, dir.path());
    let binary = dir.path().join("shep-otel");
    std::fs::write(&binary, "#!/bin/sh\nexit 0\n").unwrap();
    let mut mode = std::fs::metadata(&binary).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o755);
    std::fs::set_permissions(&binary, mode).unwrap();

    let mut out = Vec::new();
    let mut err = Vec::new();
    let args = AdoptArgs {
        yes: true,
        name: Some("otel".to_string()),
        path: binary,
    };
    let code = adopt(&mut streams(&mut out, &mut err), &paths, &args).await;

    assert_eq!(code, ExitCode::Success);
    let written = std::fs::read_to_string(&paths.daemon_config).unwrap();
    assert!(
        written.contains("otel"),
        "the config edit must still land: {written}"
    );
    let text = String::from_utf8(out).unwrap();
    assert!(
        text.contains("next shepherd"),
        "the operator needs to know the dog is not running yet: {text}"
    );
}

#[tokio::test]
async fn adopt_with_no_name_flag_defaults_from_the_stripped_stem() {
    let dir = tempfile::tempdir().unwrap();
    let paths = ShepPaths::resolve(&|_| None, dir.path());
    let binary = dir.path().join("shep-otel");
    std::fs::write(&binary, "#!/bin/sh\nexit 0\n").unwrap();
    let mut mode = std::fs::metadata(&binary).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o755);
    std::fs::set_permissions(&binary, mode).unwrap();

    let mut out = Vec::new();
    let mut err = Vec::new();
    let args = AdoptArgs {
        yes: true,
        path: binary,
        name: None,
    };
    let code = adopt(&mut streams(&mut out, &mut err), &paths, &args).await;

    assert_eq!(code, ExitCode::Success);
    let written = std::fs::read_to_string(&paths.daemon_config).unwrap();
    let cfg = shep_core::config::DaemonConfig::load(Some(&written), &|_| None).unwrap();
    assert!(
        cfg.daemon.adopted_dogs.contains_key("otel"),
        "the defaulted name must be `otel`, not `shep-otel`: {written}"
    );
}

/// `dispatch_adopted_dog` (`lib.rs`) runs only once clap has failed to
/// match the name against a real subcommand, so such a dog is
/// unreachable. The refusal must precede any `shep.toml` write.
#[tokio::test]
async fn adopt_refuses_a_name_that_collides_with_a_built_in_verb() {
    let dir = tempfile::tempdir().unwrap();
    let paths = ShepPaths::resolve(&|_| None, dir.path());
    let binary = dir.path().join("watchdog");
    std::fs::write(&binary, "#!/bin/sh\nexit 0\n").unwrap();
    let mut mode = std::fs::metadata(&binary).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o755);
    std::fs::set_permissions(&binary, mode).unwrap();

    let mut out = Vec::new();
    let mut err = Vec::new();
    // "stop" is a real verb; "ls" is `flock`'s own visible alias.
    for reserved in ["stop", "ls"] {
        let args = AdoptArgs {
            yes: true,
            path: binary.clone(),
            name: Some(reserved.to_string()),
        };
        let code = adopt(&mut streams(&mut out, &mut err), &paths, &args).await;
        assert_eq!(
            code,
            ExitCode::InvalidConfig,
            "`{reserved}` must be refused"
        );
    }
    assert!(
        !paths.daemon_config.exists(),
        "a name collision must never touch shep.toml: {}",
        paths.daemon_config.display()
    );
}

/// The outcome is the same whichever order runs, so the two are told
/// apart by the refusal: `args.path` names nothing on disk, so vet-first
/// would give "no file exists at that path".
#[tokio::test]
async fn a_name_collision_is_refused_before_vet_binary_ever_runs() {
    let dir = tempfile::tempdir().unwrap();
    let paths = ShepPaths::resolve(&|_| None, dir.path());

    let mut out = Vec::new();
    let mut err = Vec::new();
    let args = AdoptArgs {
        yes: true,
        path: dir.path().join("nope"),
        name: Some("stop".to_string()),
    };
    let code = adopt(&mut streams(&mut out, &mut err), &paths, &args).await;

    assert_eq!(code, ExitCode::InvalidConfig);
    let text = String::from_utf8(err).unwrap();
    assert!(
        text.contains("already a shep verb or alias"),
        "the collision must be the reported reason, not vet_binary's own refusal: {text}"
    );
    assert!(
        !text.contains("no file exists at that path"),
        "vet_binary must never run on a name that was always going to be refused: {text}"
    );
}

#[test]
fn resolve_adopt_path_prefers_a_literal_path_that_exists() {
    let dir = tempfile::tempdir().unwrap();
    let binary = dir.path().join("thing");
    std::fs::write(&binary, "").unwrap();

    let resolved = resolve_adopt_path(&binary, None, None);
    assert_eq!(resolved, binary);
}

#[test]
fn resolve_adopt_path_expands_a_leading_tilde_against_the_given_home() {
    let home = tempfile::tempdir().unwrap();
    let binary_dir = home.path().join(".cargo/bin");
    std::fs::create_dir_all(&binary_dir).unwrap();
    let binary = binary_dir.join("shep-log-rotate");
    std::fs::write(&binary, "").unwrap();

    let raw = Path::new("~/.cargo/bin/shep-log-rotate");
    let resolved = resolve_adopt_path(raw, Some(home.path()), None);
    assert_eq!(resolved, binary);
}

/// `exists` answers `false` for a path it cannot search, which would hand
/// `vet_binary` the unexpanded `~/…` and a "no file exists" for a file
/// that is there.
#[test]
fn resolve_adopt_path_expands_a_tilde_path_it_cannot_search() {
    let home = tempfile::tempdir().unwrap();
    let sealed = home.path().join("sealed");
    std::fs::create_dir(&sealed).unwrap();
    std::fs::write(sealed.join("dog"), "").unwrap();
    let mut mode = std::fs::metadata(&sealed).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o000);
    std::fs::set_permissions(&sealed, mode.clone()).unwrap();

    // A process that bypasses mode bits (root) can still search the
    // directory, so the lookup never fails and nothing is under test.
    let hidden = std::fs::metadata(sealed.join("dog")).is_err();
    let raw = Path::new("~/sealed/dog");
    let resolved = resolve_adopt_path(raw, Some(home.path()), None);

    // Restored before any assert so the tempdir can be removed.
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o700);
    std::fs::set_permissions(&sealed, mode).unwrap();
    if !hidden {
        return;
    }
    assert_eq!(resolved, sealed.join("dog"));
}

/// `cargo install shep-log-rotate` puts the binary on `$PATH` under its
/// own name, and nowhere else.
#[test]
fn resolve_adopt_path_falls_back_to_a_path_lookup_for_a_bare_name() {
    let dir = tempfile::tempdir().unwrap();
    let binary = dir.path().join("shep-log-rotate");
    std::fs::write(&binary, "").unwrap();
    let mut mode = std::fs::metadata(&binary).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o755);
    std::fs::set_permissions(&binary, mode).unwrap();
    let path_var = std::ffi::OsString::from(dir.path());

    let raw = Path::new("shep-log-rotate");
    let resolved = resolve_adopt_path(raw, None, Some(&path_var));
    assert_eq!(resolved, binary);
}

/// A same-named file elsewhere on `$PATH` would silently adopt the
/// wrong binary.
#[test]
fn resolve_adopt_path_does_not_path_search_a_name_with_a_directory_component() {
    let path_dir = tempfile::tempdir().unwrap();
    // A file that would match if `$PATH` were searched, so the guard is
    // what this proves rather than an absent file.
    let decoy = path_dir.path().join("thing");
    std::fs::write(&decoy, "").unwrap();
    let mut mode = std::fs::metadata(&decoy).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o755);
    std::fs::set_permissions(&decoy, mode).unwrap();
    let path_var = std::ffi::OsString::from(path_dir.path());

    let raw = Path::new("./thing");
    let resolved = resolve_adopt_path(raw, None, Some(&path_var));
    assert_eq!(
        resolved, raw,
        "a name with its own directory must never be searched on $PATH"
    );
}

/// Keeps a plain missing path reporting [`AdoptRefusal::Missing`]
/// rather than a resolution-specific error.
#[test]
fn resolve_adopt_path_returns_raw_unchanged_when_nothing_resolves() {
    let raw = Path::new("/nonexistent/shep-nothing");
    assert_eq!(resolve_adopt_path(raw, None, None), raw);
}

#[test]
fn default_dog_name_strips_one_leading_shep_prefix_and_no_further() {
    assert_eq!(
        default_dog_name(Path::new("/opt/bin/shep-log-rotate")),
        "log-rotate"
    );
    assert_eq!(default_dog_name(Path::new("/opt/bin/otel")), "otel");
    // Stripping the prefix here would leave "", an unreachable name,
    // so the whole stem is kept.
    assert_eq!(default_dog_name(Path::new("/opt/bin/shep-")), "shep-");
}

#[test]
fn collides_with_a_verb_covers_names_and_visible_aliases() {
    assert!(collides_with_a_verb("stop"), "a real verb must collide");
    assert!(collides_with_a_verb("ls"), "flock's own alias must collide");
    // clap generates the `help` subcommand when it builds a command, so
    // it is missing until `Cli::command()` is built. `shep help` prints
    // shep's own help and never reaches a dog.
    assert!(
        collides_with_a_verb("help"),
        "clap's generated help subcommand is a name a dog can never take"
    );
    assert!(
        !collides_with_a_verb("watchdog"),
        "an arbitrary name must not collide"
    );
}

/// `EnableDog` leaves a dog that is running as it is, so an upgrade that
/// records a new binary must stop the old process first.
#[tokio::test]
async fn a_restart_stops_the_dog_before_it_asks_for_it_to_start_again() {
    let dir = tempfile::tempdir().unwrap();
    let path = shep_client::testing::control_address(dir.path());
    let (client, mut envelopes) = fake_client_capturing_envelopes(&path).await;
    let mut out = Vec::new();
    let mut err = Vec::new();

    let _ = adopt_after_config(
        &mut streams(&mut out, &mut err),
        "upgrade",
        "otel",
        DogSource::Adopted {
            grants: None,
            path: "/usr/local/bin/shep-otel".to_string(),
            channel: false,
            events: Vec::new(),
        },
        &BTreeMap::new(),
        Some(&client),
        true,
    )
    .await;

    let first = envelopes.recv().await.unwrap();
    assert_eq!(
        first.body,
        Request::DisableDog {
            name: "otel".to_string()
        }
    );
    let second = envelopes.recv().await.unwrap();
    assert!(
        matches!(second.body, Request::EnableDog { ref name, .. } if name == "otel"),
        "{:?}",
        second.body
    );
}