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
//! `shep upgrade` end to end with a stub `cargo`: what an upgrade writes, what
//! it keeps for `--rollback`, and what it leaves alone.

use std::collections::BTreeMap;

use super::dog_upgrade::{Goal, upgrade, upgrade_all, upgrade_dog};
use super::install_depends::Catalog;
use super::install_tests::{
    COMMIT, Fixture, NEWER_COMMIT, args, config, from_the_row, row, run, typing,
};
use super::receipt;
use super::test_support::{streams, this_builds_ack};
use crate::cli::{AdoptArgs, Format, UpgradeArgs};
use crate::commands::shep_toml::ShepToml;
use crate::dog_index::{AvailableDog, DogSourceKind};
use crate::exit::ExitCode;
use shep_client::testing::{fake_daemon_answering_with_ack, sample_info};
use shep_core::protocol::{DogSource, Envelope, Request, Response};
use tokio::sync::mpsc::UnboundedReceiver;

const URL: &str = "https://shep-pm.com/dogs.json";

/// The manifest of the build an upgrade fetches, asking for one permission more.
const WIDER: &str = r#"{"manifest":1,"name":"otel","version":"0.3.0","protocol":12,"permissions":[{"name":"flock.read","required":true,"reason":"reads the flock to report on it"},{"name":"logs.read","required":false,"reason":"reads logs"}]}"#;

fn at(version: &str) -> AvailableDog {
    row(DogSourceKind::Cargo {
        version: Some(version.to_owned()),
        sha256: BTreeMap::new(),
    })
}

fn otel(fixture: &Fixture) -> std::path::PathBuf {
    fixture.paths.dogs_dir.join("otel")
}

fn held(fixture: &Fixture) -> serde_json::Value {
    serde_json::from_slice(&std::fs::read(otel(fixture).join("receipt.json")).unwrap()).unwrap()
}

/// `shep install otel` of `version`, granting what it declares.
async fn installed_at(fixture: &Fixture, version: &str) {
    let reach = fixture.reach(from_the_row());
    let (code, _out, err) = run(
        fixture,
        &at(version),
        &args(true, false),
        &reach,
        typing(None),
    )
    .await;
    assert_eq!(code, ExitCode::Success, "{err}");
}

/// `shep install otel --ref <git_ref>`, granting what it declares.
async fn installed_from(fixture: &Fixture, git_ref: &str) {
    let reach = fixture.reach(from_the_row());
    let mut ask = args(true, false);
    ask.git_ref = Some(git_ref.to_owned());
    let (code, _out, err) = run(fixture, &at("0.2.4"), &ask, &reach, typing(None)).await;
    assert_eq!(code, ExitCode::Success, "{err}");
}

/// `shep upgrade otel` towards `goal`, against an index holding `rows`.
async fn upgraded(
    fixture: &Fixture,
    rows: &[AvailableDog],
    goal: &Goal,
) -> (ExitCode, String, String) {
    let reach = fixture.reach(from_the_row());
    let (mut out, mut err) = (Vec::new(), Vec::new());
    let mut streams = streams(&mut out, &mut err);
    streams.fmt = Format::Json;
    let code = upgrade_dog(
        &mut streams,
        &fixture.paths,
        &reach,
        &mut typing(None),
        &Catalog::new(URL, rows),
        "otel",
        goal,
    )
    .await;
    (
        code,
        String::from_utf8(out).unwrap(),
        String::from_utf8(err).unwrap(),
    )
}

#[tokio::test]
async fn a_newer_index_version_installs_into_its_own_directory_and_writes_a_new_receipt() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    let old_binary = otel(&fixture).join("0.2.4/bin/shep-otel");

    let (code, out, err) = upgraded(&fixture, &[at("0.3.0")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    let binary = otel(&fixture).join("0.3.0/bin/shep-otel");
    assert!(binary.is_file());
    assert_eq!(
        config(&fixture).adopted_dog_path("otel").unwrap(),
        binary.canonicalize().unwrap()
    );
    let receipt = held(&fixture);
    assert_eq!(receipt["version"], "0.3.0");
    assert_eq!(receipt["binary"], binary.to_str().unwrap());
    assert_eq!(
        receipt["previous"],
        otel(&fixture).join("0.2.4").to_str().unwrap()
    );
    // The replaced directory is kept, with the receipt it had.
    assert!(old_binary.is_file());
    let kept: serde_json::Value =
        serde_json::from_slice(&std::fs::read(otel(&fixture).join("0.2.4/receipt.json")).unwrap())
            .unwrap();
    assert_eq!(kept["version"], "0.2.4");
    assert!(kept.get("previous").is_none());
    // The row is an upgrade's, and the dog is on: with no shepherd the next one starts it.
    let reply: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(reply["command"], "upgrade");
    assert_eq!(reply["data"]["name"], "otel");
    assert!(
        config(&fixture)
            .enabled_dog_names()
            .contains(&"otel".to_owned())
    );
}

#[tokio::test]
async fn a_dog_at_the_index_version_is_left_alone_and_says_so() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    let before = std::fs::read(otel(&fixture).join("receipt.json")).unwrap();
    std::fs::remove_file(fixture.argv()).unwrap();

    let (code, out, err) = upgraded(&fixture, &[at("0.2.4")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    assert!(err.contains("already at 0.2.4"), "{err}");
    assert!(out.contains("already current"), "{out}");
    assert!(!fixture.argv().exists(), "cargo must not have run");
    assert_eq!(
        std::fs::read(otel(&fixture).join("receipt.json")).unwrap(),
        before
    );
}

#[tokio::test]
async fn a_ref_install_rebuilds_its_ref_at_the_newest_commit_into_a_new_ref_directory() {
    let fixture = Fixture::new();
    installed_from(&fixture, "main").await;
    fixture.builds_commit(NEWER_COMMIT);

    let (code, _out, err) = upgraded(&fixture, &[at("0.2.4")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    let binary = otel(&fixture)
        .join(format!("ref-{NEWER_COMMIT}"))
        .join("bin/shep-otel");
    assert!(binary.is_file(), "{}", binary.display());
    let receipt = held(&fixture);
    assert_eq!(receipt["ref"], "main");
    assert_eq!(receipt["commit"], NEWER_COMMIT);
    assert_eq!(
        receipt["previous"],
        otel(&fixture)
            .join(format!("ref-{COMMIT}"))
            .to_str()
            .unwrap()
    );
    assert!(
        otel(&fixture)
            .join(format!("ref-{COMMIT}/bin/shep-otel"))
            .is_file()
    );
    assert_eq!(
        config(&fixture).adopted_dog_path("otel").unwrap(),
        binary.canonicalize().unwrap()
    );
}

#[tokio::test]
async fn a_ref_with_no_new_commit_is_already_current() {
    let fixture = Fixture::new();
    installed_from(&fixture, "main").await;
    let before = std::fs::read(otel(&fixture).join("receipt.json")).unwrap();

    let (code, _out, err) = upgraded(&fixture, &[at("0.2.4")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    assert!(err.contains("already at main at 3f9c2a1b7d4e"), "{err}");
    assert_eq!(
        std::fs::read(otel(&fixture).join("receipt.json")).unwrap(),
        before
    );
}

#[tokio::test]
async fn ref_switches_to_another_ref_and_release_returns_to_the_index_version() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    fixture.builds_commit(NEWER_COMMIT);

    let (code, _out, err) = upgraded(&fixture, &[at("0.2.4")], &Goal::Ref("next".into())).await;
    assert_eq!(code, ExitCode::Success, "{err}");
    assert_eq!(held(&fixture)["ref"], "next");
    assert_eq!(held(&fixture)["commit"], NEWER_COMMIT);

    let (code, _out, err) = upgraded(&fixture, &[at("0.2.4")], &Goal::Release).await;
    assert_eq!(code, ExitCode::Success, "{err}");
    let receipt = held(&fixture);
    assert_eq!(receipt["version"], "0.2.4");
    assert!(receipt.get("ref").is_none());
}

#[tokio::test]
async fn rollback_puts_back_the_replaced_directory_and_its_receipt() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    let first = held(&fixture);
    let (code, _out, err) = upgraded(&fixture, &[at("0.3.0")], &Goal::Follow).await;
    assert_eq!(code, ExitCode::Success, "{err}");

    let (code, _out, err) = upgraded(&fixture, &[], &Goal::Rollback).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    let receipt = held(&fixture);
    assert_eq!(receipt["version"], "0.2.4");
    assert_eq!(receipt["binary"], first["binary"]);
    assert_eq!(receipt["sha256"], first["sha256"]);
    assert_eq!(
        config(&fixture).adopted_dog_path("otel").unwrap(),
        otel(&fixture)
            .join("0.2.4/bin/shep-otel")
            .canonicalize()
            .unwrap()
    );
    // Nothing was thrown away: the build it left is what a second rollback returns to.
    assert_eq!(
        receipt["previous"],
        otel(&fixture).join("0.3.0").to_str().unwrap()
    );
    assert!(otel(&fixture).join("0.3.0/bin/shep-otel").is_file());
    let (code, _out, err) = upgraded(&fixture, &[], &Goal::Rollback).await;
    assert_eq!(code, ExitCode::Success, "{err}");
    assert_eq!(held(&fixture)["version"], "0.3.0");
}

#[tokio::test]
async fn rollback_with_nothing_replaced_refuses_and_changes_nothing() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    let before = std::fs::read(otel(&fixture).join("receipt.json")).unwrap();

    let (code, _out, err) = upgraded(&fixture, &[], &Goal::Rollback).await;

    assert_eq!(code, ExitCode::Failure);
    assert!(err.contains("nothing to roll `otel` back to"), "{err}");
    assert_eq!(
        std::fs::read(otel(&fixture).join("receipt.json")).unwrap(),
        before
    );
}

#[tokio::test]
async fn only_one_replaced_directory_is_kept() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    for version in ["0.3.0", "0.4.0"] {
        let (code, _out, err) = upgraded(&fixture, &[at(version)], &Goal::Follow).await;
        assert_eq!(code, ExitCode::Success, "{err}");
    }

    assert!(!otel(&fixture).join("0.2.4").exists());
    assert!(otel(&fixture).join("0.3.0").is_dir());
    assert!(otel(&fixture).join("0.4.0").is_dir());
    assert_eq!(
        held(&fixture)["previous"],
        otel(&fixture).join("0.3.0").to_str().unwrap()
    );
}

#[tokio::test]
async fn a_permission_the_new_build_declares_waits_while_the_old_grants_stay() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    fixture.builds_manifest(WIDER);

    let (code, _out, err) = upgraded(&fixture, &[at("0.3.0")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    let record = config(&fixture).grant_record("otel").unwrap().unwrap();
    assert_eq!(record.granted.names().collect::<Vec<_>>(), ["flock.read"]);
    assert_eq!(record.waiting.names().collect::<Vec<_>>(), ["logs.read"]);
    assert!(err.contains("logs.read"), "{err}");
}

#[tokio::test]
async fn the_cargo_path_prints_the_diff_rs_link_with_both_versions() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;

    let (code, _out, err) = upgraded(&fixture, &[at("0.3.0")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    assert!(
        err.contains("https://diff.rs/shep-otel/0.2.4/0.3.0"),
        "{err}"
    );
}

#[tokio::test]
async fn a_dog_adopted_from_a_path_has_no_receipt_and_is_refused_naming_shep_adopt() {
    let fixture = Fixture::new();
    let reach = fixture.reach(from_the_row());
    // Built by the stub, then adopted by hand and its receipt removed.
    installed_at(&fixture, "0.2.4").await;
    std::fs::remove_file(otel(&fixture).join("receipt.json")).unwrap();
    let _ = reach;

    let (code, _out, err) = upgraded(&fixture, &[at("0.3.0")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::Failure);
    assert!(err.contains("no receipt"), "{err}");
    assert!(err.contains("shep adopt"), "{err}");
    assert!(
        !fixture.dir.path().join("attempts").exists() || {
            // Only the install's own build ran.
            std::fs::read_to_string(fixture.attempts())
                .unwrap()
                .lines()
                .count()
                == 1
        }
    );
}

#[tokio::test]
async fn a_name_nobody_adopted_is_not_found() {
    let fixture = Fixture::new();

    let (code, _out, err) = upgraded(&fixture, &[at("0.3.0")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::NotFound);
    assert!(err.contains("not an adopted dog"), "{err}");
}

#[tokio::test]
async fn bare_upgrade_is_not_implemented_and_says_so() {
    let fixture = Fixture::new();
    let (mut out, mut err) = (Vec::new(), Vec::new());

    let code = upgrade(
        &mut streams(&mut out, &mut err),
        &fixture.paths,
        &UpgradeArgs {
            dog: None,
            all: false,
            git_ref: None,
            release: false,
            rollback: false,
            refresh: false,
        },
    )
    .await;

    assert_eq!(code, ExitCode::Failure);
    let err = String::from_utf8(err).unwrap();
    assert!(err.contains("not implemented"), "{err}");
}

#[tokio::test]
async fn dogs_upgrades_each_installed_dog_and_skips_one_adopted_from_a_path() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    // A second dog with no receipt.
    let plain = fixture.dir.path().join("shep-plain");
    std::fs::copy(fixture.dir.path().join("built-dog"), &plain).unwrap();
    std::fs::set_permissions(&plain, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    let (mut out, mut err) = (Vec::new(), Vec::new());
    let code = super::adopt::adopt_asking(
        &mut streams(&mut out, &mut err),
        &fixture.paths,
        &AdoptArgs {
            path: plain,
            name: Some("plain".to_owned()),
            yes: true,
        },
        &mut typing(None),
    )
    .await;
    assert_eq!(code, ExitCode::Success, "{}", String::from_utf8_lossy(&err));
    let reach = fixture.reach(from_the_row());
    let rows = [at("0.3.0")];
    let (mut out, mut err) = (Vec::new(), Vec::new());

    let code = upgrade_all(
        &mut streams(&mut out, &mut err),
        &fixture.paths,
        &reach,
        &mut typing(None),
        &Catalog::new(URL, &rows),
    )
    .await;

    let err = String::from_utf8(err).unwrap();
    assert_eq!(code, ExitCode::Success, "{err}");
    assert_eq!(held(&fixture)["version"], "0.3.0");
    assert!(err.contains("skipping plain"), "{err}");
    assert!(receipt::read(&fixture.paths, "plain").unwrap().is_none());
    let _ = ShepToml::read_only(&fixture.paths.daemon_config).unwrap();
}

#[tokio::test]
async fn dogs_in_json_answers_one_document_holding_each_dogs_row() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    let reach = fixture.reach(from_the_row());
    let mut second = at("0.2.4");
    second.adopt_as = "second".to_owned();
    let (code, _out, err) = run(&fixture, &second, &args(true, false), &reach, typing(None)).await;
    assert_eq!(code, ExitCode::Success, "{err}");
    let mut newer = second.clone();
    newer.source = at("0.3.0").source;
    let rows = [at("0.3.0"), newer];
    let (mut out, mut err) = (Vec::new(), Vec::new());
    let mut json = streams(&mut out, &mut err);
    json.fmt = Format::Json;

    let code = upgrade_all(
        &mut json,
        &fixture.paths,
        &reach,
        &mut typing(None),
        &Catalog::new(URL, &rows),
    )
    .await;

    let err = String::from_utf8(err).unwrap();
    assert_eq!(code, ExitCode::Success, "{err}");
    let document: serde_json::Value = serde_json::from_slice(&out).unwrap_or_else(|e| {
        panic!(
            "stdout is not one JSON document ({e}): {}",
            String::from_utf8_lossy(&out)
        )
    });
    assert_eq!(document["command"], "upgrade");
    let names: Vec<_> = document["data"]
        .as_array()
        .unwrap()
        .iter()
        .map(|row| row["name"].as_str().unwrap().to_owned())
        .collect();
    assert_eq!(names, ["otel", "second"]);
}

#[tokio::test]
async fn dogs_in_json_with_no_installed_dog_answers_an_empty_list() {
    let fixture = Fixture::new();
    let reach = fixture.reach(from_the_row());
    let (mut out, mut err) = (Vec::new(), Vec::new());
    let mut json = streams(&mut out, &mut err);
    json.fmt = Format::Json;

    let code = upgrade_all(
        &mut json,
        &fixture.paths,
        &reach,
        &mut typing(None),
        &Catalog::new(URL, &[]),
    )
    .await;

    assert_eq!(code, ExitCode::Success);
    let document: serde_json::Value = serde_json::from_slice(&out).unwrap();
    assert_eq!(document["data"], serde_json::json!([]));
}

/// A shepherd listening on the fixture's socket, answering every request as
/// a shepherd that has the dog running would.
async fn shepherd(fixture: &Fixture) -> UnboundedReceiver<Envelope> {
    std::fs::create_dir_all(&fixture.paths.run).unwrap();
    fake_daemon_answering_with_ack(
        &fixture.paths.socket,
        this_builds_ack(),
        |request| match request {
            Request::EnableDog { .. } => Response::DogStarted(sample_info()),
            _ => Response::Pong,
        },
    )
    .await
}

/// What the shepherd was asked, in order, once the verb has returned.
fn asked(requests: &mut UnboundedReceiver<Envelope>) -> Vec<Request> {
    std::iter::from_fn(|| requests.try_recv().ok())
        .map(|envelope| envelope.body)
        .collect()
}

/// The adopted path an `EnableDog` carries.
fn started_from(request: &Request) -> &str {
    match request {
        Request::EnableDog {
            source: DogSource::Adopted { path, .. },
            ..
        } => path,
        other => panic!("not an EnableDog from an adopted path: {other:?}"),
    }
}

/// `EnableDog` leaves a running dog alone, so without the stop the old build
/// would keep running after an upgrade that said it had moved.
#[tokio::test]
async fn an_upgrade_stops_the_running_dog_then_starts_it_on_the_new_build() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    let mut requests = shepherd(&fixture).await;

    let (code, _out, err) = upgraded(&fixture, &[at("0.3.0")], &Goal::Follow).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    let asked = asked(&mut requests);
    assert_eq!(asked.len(), 2, "{asked:?}");
    assert_eq!(
        asked[0],
        Request::DisableDog {
            name: "otel".to_owned()
        }
    );
    let new_binary = otel(&fixture).join("0.3.0/bin/shep-otel");
    assert_eq!(
        started_from(&asked[1]),
        new_binary.canonicalize().unwrap().to_str().unwrap()
    );
}

#[tokio::test]
async fn a_rollback_stops_the_running_dog_then_starts_it_on_the_build_it_put_back() {
    let fixture = Fixture::new();
    installed_at(&fixture, "0.2.4").await;
    let (code, _out, err) = upgraded(&fixture, &[at("0.3.0")], &Goal::Follow).await;
    assert_eq!(code, ExitCode::Success, "{err}");
    let mut requests = shepherd(&fixture).await;

    let (code, _out, err) = upgraded(&fixture, &[], &Goal::Rollback).await;

    assert_eq!(code, ExitCode::Success, "{err}");
    let asked = asked(&mut requests);
    assert_eq!(asked.len(), 2, "{asked:?}");
    assert_eq!(
        asked[0],
        Request::DisableDog {
            name: "otel".to_owned()
        }
    );
    let old_binary = otel(&fixture).join("0.2.4/bin/shep-otel");
    assert_eq!(
        started_from(&asked[1]),
        old_binary.canonicalize().unwrap().to_str().unwrap()
    );
}