shep 0.24.0

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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! `shep set` against a live shepherd: a sheep's own field, a key inside a
//! sheep's dog table, and a key of a dog's own section, each written through
//! the request the shepherd already answers.
//!
//! One case drives one flock, because the cost here is the daemon and the
//! sheep, not the writes. Every assertion is on the exact text a `shep set`
//! prints beside the value the flock holds afterwards, since the claim is that
//! an operator sees every key a write touches before it touches them.

use super::*;

/// `dogs.toml` for the dog half: a comment above the header, one key the
/// operator wrote by hand, and nothing else. `bark` is a built-in dog, so it is
/// a dog this shepherd knows even with nothing running configured, and it is a
/// dog that publishes a schema of its own.
const DOGS_TOML: &str = "\
# how often bark re-checks delivery
[bark]
poll = \"30s\"
";

/// The whole of `shep set` over one live flock.
#[test]
fn shep_set_writes_a_sheeps_field_a_dog_table_key_and_a_dogs_own_key() {
    let dir = tempfile::tempdir().unwrap();
    let home = dir.path();
    let script = write_test_script(&dir);
    let flockfile = write_flockfile(
        &dir,
        &format!(
            "[[app]]\nname = \"api\"\nscript = '{}'\nmax_memory = \"1G\"\n\n\
             [app.dogs.kelpie.git]\ncheckout = \"/srv/api\"\nmerging = \"ff\"\n",
            script.display()
        ),
    );
    std::fs::write(home.join("dogs.toml"), DOGS_TOML).unwrap();

    let mut guard = DaemonGuard::default();
    let boot = shep(home).arg("start").arg(&flockfile).output().unwrap();
    guard.adopt_home(home);
    assert_success(&boot);
    poll_flock(home, |info| info["status"] == "online");

    // A sheep's own field: the change list names the key with its old value
    // and its new one, and the flock holds the new one afterwards.
    let field = shep(home)
        .arg("set")
        .arg("api.max_memory")
        .arg("512M")
        .arg("--yes")
        .output()
        .unwrap();
    assert_success(&field);
    let text = String::from_utf8_lossy(&field.stdout);
    assert!(text.contains("max_memory"), "{text}");
    assert!(text.contains("changed"), "{text}");
    assert!(text.contains("1G"), "{text}");
    assert!(text.contains("512M"), "{text}");
    assert!(text.contains("wrote 1 change onto `api`"), "{text}");

    let read = shep(home)
        .arg("get")
        .arg("api.max_memory")
        .output()
        .unwrap();
    assert_success(&read);
    assert_eq!(
        String::from_utf8_lossy(&read.stdout),
        "max_memory = \"512M\"\n"
    );

    // A key inside a sheep's dog table: the one key moves and the key beside
    // it stays, because the write is the merged table.
    let nested = shep(home)
        .arg("set")
        .arg("api.dogs.kelpie.git.merging")
        .arg("rebase")
        .arg("--yes")
        .output()
        .unwrap();
    assert_success(&nested);
    let text = String::from_utf8_lossy(&nested.stdout);
    assert!(text.contains("merging"), "{text}");
    assert!(text.contains("ff"), "{text}");
    assert!(text.contains("rebase"), "{text}");

    let read = shep(home)
        .arg("get")
        .arg("api.dogs.kelpie.git")
        .output()
        .unwrap();
    assert_success(&read);
    assert_eq!(
        String::from_utf8_lossy(&read.stdout),
        "[dogs.kelpie.git]\ncheckout = \"/srv/api\"\nmerging = \"rebase\"\n"
    );

    // A dog's own key, in a section the operator wrote by hand: the comment
    // above the header and the key beside the one written both survive, because
    // the write edits the text the shepherd answered rather than replacing it
    // with a rendered map.
    let dog = shep(home)
        .arg("set")
        .arg("bark.history_bytes")
        .arg("1048576")
        .arg("--yes")
        .output()
        .unwrap();
    assert_success(&dog);
    let text = String::from_utf8_lossy(&dog.stdout);
    assert!(text.contains("history_bytes"), "{text}");
    assert!(text.contains("added"), "{text}");

    let read = shep(home).arg("get").arg("bark").output().unwrap();
    assert_success(&read);
    let text = String::from_utf8_lossy(&read.stdout);
    assert!(text.starts_with("[bark]\n"), "{text}");
    assert!(text.contains("poll = \"30s\""), "{text}");
    assert!(text.contains("history_bytes = 1048576"), "{text}");

    // The file itself is what the claim is about: the comment above the header
    // and the key beside the one written both survive, because the write edits
    // the text the shepherd answered rather than replacing it with a rendered
    // map. `shep get` re-roots a section and clears the header's own decor, so
    // that comment is only visible here.
    let file = std::fs::read_to_string(home.join("dogs.toml")).unwrap();
    assert!(
        file.contains("# how often bark re-checks delivery"),
        "{file}"
    );
    assert!(file.contains("poll = \"30s\""), "{file}");
    assert!(file.contains("history_bytes = 1048576"), "{file}");

    // A value the dog's own schema turns down is refused with the key path it
    // sits at, and the section is left exactly as it was.
    let refused = shep(home)
        .arg("set")
        .arg("bark.history_bytes")
        .arg("\"not-a-number\"")
        .arg("--yes")
        .output()
        .unwrap();
    assert_eq!(refused.status.code(), Some(4), "InvalidConfig; {refused:?}");
    let stderr = String::from_utf8_lossy(&refused.stderr);
    assert!(stderr.contains("nothing was written"), "{stderr}");
    assert!(stderr.contains("`bark.history_bytes`"), "{stderr}");

    let read = shep(home)
        .arg("get")
        .arg("bark.history_bytes")
        .output()
        .unwrap();
    assert_success(&read);
    // `shep get` prints a value under the header of the table that holds it.
    assert_eq!(
        String::from_utf8_lossy(&read.stdout),
        "[bark]\nhistory_bytes = 1048576\n"
    );

    // `--dry-run` prints the same list and writes nothing.
    let dry = shep(home)
        .arg("set")
        .arg("api.max_memory")
        .arg("2G")
        .arg("--dry-run")
        .output()
        .unwrap();
    assert_success(&dry);
    let text = String::from_utf8_lossy(&dry.stdout);
    assert!(text.contains("512M"), "{text}");
    assert!(text.contains("2G"), "{text}");
    assert!(text.contains("nothing was written"), "{text}");

    let read = shep(home)
        .arg("get")
        .arg("api.max_memory")
        .output()
        .unwrap();
    assert_success(&read);
    assert_eq!(
        String::from_utf8_lossy(&read.stdout),
        "max_memory = \"512M\"\n"
    );

    // Writing without asking needs a terminal or `--yes`. `Command::output`
    // gives this process no terminal on stdin, which is exactly the shape a
    // script runs in, and a run that cannot ask refuses instead of guessing.
    let unasked = shep(home)
        .arg("set")
        .arg("api.max_memory")
        .arg("8G")
        .output()
        .unwrap();
    assert_eq!(unasked.status.code(), Some(2), "Usage; {unasked:?}");
    let stderr = String::from_utf8_lossy(&unasked.stderr);
    assert!(stderr.contains("no terminal"), "{stderr}");
    assert!(stderr.contains("--yes"), "{stderr}");

    let read = shep(home)
        .arg("get")
        .arg("api.max_memory")
        .output()
        .unwrap();
    assert_success(&read);
    assert_eq!(
        String::from_utf8_lossy(&read.stdout),
        "max_memory = \"512M\"\n"
    );

    // The keys this verb never writes, refused with `copy`'s reasons: `env`
    // names the door that writes env, `name` and `instances` name the verbs
    // that move a sheep and an instance count, and the whole `dogs` map names a
    // dog.
    for (address, expect) in [
        ("api.env", "shep import env"),
        ("api.name", "a name change makes a different sheep"),
        ("api.instances", "shep stock"),
        ("api.dogs", "name a dog"),
    ] {
        let run = shep(home)
            .arg("set")
            .arg(address)
            .arg("x")
            .arg("--yes")
            .output()
            .unwrap();
        assert_eq!(run.status.code(), Some(4), "{address}: {run:?}");
        let stderr = String::from_utf8_lossy(&run.stderr);
        assert!(stderr.contains(expect), "{address}: {stderr}");
    }

    // The key/value store this verb used to write is not this verb's any more:
    // the name is refused, the sentence names where the store lives, and the
    // store is still empty.
    let store = shep(home)
        .arg("set")
        .arg("feature.flag")
        .arg("on")
        .arg("--yes")
        .output()
        .unwrap();
    assert_eq!(store.status.code(), Some(3), "NotFound; {store:?}");
    let stderr = String::from_utf8_lossy(&store.stderr);
    assert!(
        stderr.contains("no sheep or dog named `feature.flag`"),
        "{stderr}"
    );
    assert!(stderr.contains("shep kv set"), "{stderr}");

    let read = shep(home)
        .arg("kv")
        .arg("get")
        .arg("feature.flag")
        .output()
        .unwrap();
    assert_eq!(read.status.code(), Some(3), "NotFound; {read:?}");

    // `--format json` is one envelope, carrying the change list beside the
    // run's own verdict, and nothing else on either stream.
    let json = shep(home)
        .arg("--format")
        .arg("json")
        .arg("set")
        .arg("api.dogs.kelpie.git.remote")
        .arg("upstream")
        .arg("--yes")
        .output()
        .unwrap();
    assert_success(&json);
    assert!(
        json.stderr.is_empty(),
        "{}",
        String::from_utf8_lossy(&json.stderr)
    );
    let envelope: serde_json::Value = serde_json::from_slice(&json.stdout).unwrap();
    assert_eq!(envelope["command"], "set", "{envelope}");
    assert_eq!(envelope["schema_version"], 1, "{envelope}");
    assert_eq!(envelope["data"]["kind"], "sheep", "{envelope}");
    assert_eq!(envelope["data"]["name"], "api", "{envelope}");
    assert_eq!(
        envelope["data"]["path"], "dogs.kelpie.git.remote",
        "{envelope}"
    );
    assert_eq!(envelope["data"]["written"], true, "{envelope}");
    assert_eq!(envelope["data"]["dry_run"], false, "{envelope}");
    let changes = envelope["data"]["changes"]
        .as_array()
        .unwrap_or_else(|| panic!("changes must be an array: {envelope}"));
    assert_eq!(changes.len(), 1, "{envelope}");
    assert_eq!(changes[0]["key"], "remote", "{envelope}");
    assert_eq!(changes[0]["change"], "added", "{envelope}");
    assert!(changes[0]["old"].is_null(), "{envelope}");
    assert_eq!(changes[0]["new"], "upstream", "{envelope}");

    // A value already held is no write at all: no question, no request, and a
    // change list with nothing in it.
    let same = shep(home)
        .arg("--format")
        .arg("json")
        .arg("set")
        .arg("api.max_memory")
        .arg("512M")
        .arg("--yes")
        .output()
        .unwrap();
    assert_success(&same);
    let envelope: serde_json::Value = serde_json::from_slice(&same.stdout).unwrap();
    assert_eq!(envelope["data"]["written"], false, "{envelope}");
    assert_eq!(
        envelope["data"]["changes"],
        serde_json::json!([]),
        "{envelope}"
    );

    // `shep set` is a verb `--help` lists, in the group `shep get` is in.
    let help = shep(home).arg("--help").output().unwrap();
    assert_success(&help);
    let help_text = String::from_utf8_lossy(&help.stdout);
    assert!(
        help_text
            .lines()
            .any(|line| line.starts_with("The shepherd") && line.contains(" set ")),
        "{help_text}"
    );

    graceful_kill(home);
}

/// `shep set` writes a dog's own key into a section that does not exist yet,
/// for a dog this home has never been given a section for.
#[test]
fn shep_set_creates_a_dogs_section_when_it_has_none() {
    let dir = tempfile::tempdir().unwrap();
    let home = dir.path();
    let script = write_test_script(&dir);
    let flockfile = write_flockfile(
        &dir,
        &format!("[[app]]\nname = \"api\"\nscript = '{}'\n", script.display()),
    );

    let mut guard = DaemonGuard::default();
    let boot = shep(home).arg("start").arg(&flockfile).output().unwrap();
    guard.adopt_home(home);
    assert_success(&boot);
    poll_flock(home, |info| info["status"] == "online");
    assert!(
        !home.join("dogs.toml").exists(),
        "the case is a home with no `dogs.toml` at all"
    );

    let written = shep(home)
        .arg("set")
        .arg("metrics.otlp_timeout")
        .arg("\"12s\"")
        .arg("--yes")
        .output()
        .unwrap();
    assert_success(&written);
    let text = String::from_utf8_lossy(&written.stdout);
    assert!(text.contains("wrote 1 change onto `metrics`"), "{text}");

    let read = shep(home)
        .arg("get")
        .arg("metrics.otlp_timeout")
        .output()
        .unwrap();
    assert_success(&read);
    assert_eq!(
        String::from_utf8_lossy(&read.stdout),
        "[metrics]\notlp_timeout = \"12s\"\n"
    );

    graceful_kill(home);
}

/// A `dogs.toml` section for a name this shepherd knows no dog by is refused
/// before anything is sent, which is the daemon's own guard in a sentence that
/// names the remedy.
#[test]
fn shep_set_refuses_a_section_of_a_dog_nothing_knows() {
    let dir = tempfile::tempdir().unwrap();
    let home = dir.path();
    let script = write_test_script(&dir);
    let flockfile = write_flockfile(
        &dir,
        &format!("[[app]]\nname = \"api\"\nscript = '{}'\n", script.display()),
    );
    let stale = "\
# left behind by a dog that was removed
[ghost]
timeout = 30
";
    std::fs::write(home.join("dogs.toml"), stale).unwrap();

    let mut guard = DaemonGuard::default();
    let boot = shep(home).arg("start").arg(&flockfile).output().unwrap();
    guard.adopt_home(home);
    assert_success(&boot);
    poll_flock(home, |info| info["status"] == "online");

    let refused = shep(home)
        .arg("set")
        .arg("ghost.timeout")
        .arg("5")
        .arg("--yes")
        .output()
        .unwrap();
    assert_eq!(refused.status.code(), Some(3), "{refused:?}");
    let text = String::from_utf8_lossy(&refused.stderr);
    assert!(text.contains("nothing was written"), "{text}");
    assert!(text.contains("`[ghost]` section"), "{text}");
    assert!(
        text.contains("`shep adopt` or `shep enable`"),
        "{text}: the remedy is the daemon's own"
    );
    assert!(
        !text.contains("shep kv set"),
        "{text}: this name is a dog's, not a key/value key"
    );

    assert_eq!(
        std::fs::read_to_string(home.join("dogs.toml")).unwrap(),
        stale,
        "a refusal writes nothing"
    );

    graceful_kill(home);
}

/// TOML's `nan`, `inf` and `-inf` have no cell in the JSON the shepherd's
/// requests carry, and `serde_json` renders one as a null. The value is refused
/// before anything is sent, rather than arriving as a write of nothing.
#[test]
fn shep_set_refuses_a_value_that_is_not_a_number() {
    let dir = tempfile::tempdir().unwrap();
    let home = dir.path();
    let script = write_test_script(&dir);
    let flockfile = write_flockfile(
        &dir,
        &format!(
            "[[app]]\nname = \"api\"\nscript = '{}'\nmax_memory = \"1G\"\n",
            script.display()
        ),
    );

    let mut guard = DaemonGuard::default();
    let boot = shep(home).arg("start").arg(&flockfile).output().unwrap();
    guard.adopt_home(home);
    assert_success(&boot);
    poll_flock(home, |info| info["status"] == "online");

    // `-inf` is not in the list a live flock is driven with: clap reads an
    // argument that starts with a dash as a flag, which is the verb's argument
    // grammar rather than the value's. `commands::settings_write::tests` is
    // where the value grammar is pinned.
    for raw in ["nan", "inf", "{ git = { merging = -inf } }", "[1, inf]"] {
        let refused = shep(home)
            .arg("set")
            .arg("api.max_memory")
            .arg(raw)
            .arg("--yes")
            .output()
            .unwrap();
        assert_eq!(refused.status.code(), Some(2), "{raw}: {refused:?}");
        let text = String::from_utf8_lossy(&refused.stderr);
        assert!(text.contains("no place in the JSON"), "{text}");
        assert!(
            text.contains("null"),
            "{text}: the write it would have been"
        );
    }

    let read = shep(home)
        .arg("get")
        .arg("api.max_memory")
        .output()
        .unwrap();
    assert_success(&read);
    assert_eq!(
        String::from_utf8_lossy(&read.stdout),
        "max_memory = \"1G\"\n",
        "a refused value writes nothing"
    );

    graceful_kill(home);
}

/// A whole-section write re-reads the section before it sends, to catch a holder
/// that moved while the operator was answering. The re-read must not read the
/// blank line a section ends with as a change.
#[test]
fn shep_set_writes_a_section_that_ends_in_a_blank_line() {
    let dir = tempfile::tempdir().unwrap();
    let home = dir.path();
    let script = write_test_script(&dir);
    let flockfile = write_flockfile(
        &dir,
        &format!("[[app]]\nname = \"api\"\nscript = '{}'\n", script.display()),
    );
    std::fs::write(home.join("dogs.toml"), "[bark]\npoll = \"30s\"\n\n").unwrap();

    let mut guard = DaemonGuard::default();
    let boot = shep(home).arg("start").arg(&flockfile).output().unwrap();
    guard.adopt_home(home);
    assert_success(&boot);
    poll_flock(home, |info| info["status"] == "online");

    let written = shep(home)
        .arg("set")
        .arg("bark.history_bytes")
        .arg("2097152")
        .arg("--yes")
        .output()
        .unwrap();
    assert_success(&written);
    let text = String::from_utf8_lossy(&written.stdout);
    assert!(text.contains("wrote 1 change onto `bark`"), "{text}");

    let file = std::fs::read_to_string(home.join("dogs.toml")).unwrap();
    assert!(file.contains("poll = \"30s\""), "{file}");
    assert!(file.contains("history_bytes = 2097152"), "{file}");

    graceful_kill(home);
}

/// `<dog>.defaults` is retired: every verb that takes an address refuses it,
/// and names the dog's own section.
#[test]
fn a_retired_defaults_address_is_refused_and_names_the_dogs_section() {
    let dir = tempfile::tempdir().unwrap();
    let home = dir.path();
    let script = write_test_script(&dir);
    let flockfile = write_flockfile(
        &dir,
        &format!(
            "[[app]]\nname = \"api\"\nscript = '{0}'\n\n[app.dogs.metrics]\notlp_timeout = \"3s\"\n",
            script.display()
        ),
    );

    let mut guard = DaemonGuard::default();
    let boot = shep(home).arg("start").arg(&flockfile).output().unwrap();
    guard.adopt_home(home);
    assert_success(&boot);
    poll_flock(home, |info| info["status"] == "online");

    let refused = |args: &[&str]| {
        let out = shep(home).args(args).output().unwrap();
        assert_eq!(out.status.code(), Some(4), "InvalidConfig; {out:?}");
        String::from_utf8_lossy(&out.stderr).to_string()
    };
    for (verb, args) in [
        (
            "set",
            &["set", "metrics.defaults.otlp_timeout", "\"12s\"", "--yes"][..],
        ),
        (
            "unset",
            &["unset", "metrics.defaults.otlp_timeout", "--yes"][..],
        ),
        ("get", &["get", "metrics.defaults.otlp_timeout"][..]),
        (
            "copy",
            &[
                "copy",
                "metrics.defaults.otlp_timeout",
                "api.dogs.metrics.otlp_timeout",
                "--yes",
            ][..],
        ),
    ] {
        let stderr = refused(args);
        assert!(
            stderr.contains("`metrics.otlp_timeout`"),
            "{verb}: {stderr}"
        );
    }
    assert!(refused(&["get", "metrics.defaults"]).contains("`metrics.<key>`"));

    let unknown = shep(home)
        .arg("set")
        .arg("nosuchdog.defaults.x")
        .arg("1")
        .arg("--yes")
        .output()
        .unwrap();
    assert_eq!(unknown.status.code(), Some(3), "NotFound; {unknown:?}");

    graceful_kill(home);
}