shep 0.23.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
//! A load whose target names a sheep the flock already has: the arm that
//! reads no file, so a flag with no template to ride on has to travel as a
//! write of its own.

use super::*;

/// `shep start <name>` is the ordinary way to bring a sheep the flock
/// already has back up, and it takes an early return no Flockfile target
/// takes: the token resolved to a sheep, so no file is read and no `AppConfig`
/// is built for the flag to ride on.
#[tokio::test]
async fn no_log_timestamps_on_a_sheep_named_by_the_token_writes_the_field_first() {
    use shep_client::testing::fake_client_answering;
    use shep_core::status::ProcStatus;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let flock = vec![ProcessInfo::builder(0, "rotating", ProcStatus::Stopped).build()];
    let (client, mut envelopes) =
        fake_client_answering(&sock, move |request: &Request| match request {
            Request::ListFlock => Response::Flock(flock.clone()),
            Request::SetSheepField { name, key, .. } => Response::SheepFieldSet {
                name: name.clone(),
                key: key.clone(),
                pending: true,
                warning: None,
            },
            Request::Restart { .. } => Response::Restarted {
                accepted: vec![ProcessInfo::builder(0, "rotating", ProcStatus::Online).build()],
                refused: Vec::new(),
            },
            _ => Response::Pong,
        })
        .await;

    let mut args = start_args("rotating");
    args.no_log_timestamps = true;
    let (code, _printed, said) = start_against_with_args(&client, &args).await;

    assert_eq!(code, ExitCode::Success, "{said}");
    let mut order = Vec::new();
    while let Ok(envelope) = envelopes.try_recv() {
        match envelope.body {
            Request::SetSheepField { name, key, value } => {
                assert_eq!(name, "rotating", "the token named this sheep");
                assert_eq!(key, "log_timestamps");
                assert_eq!(value, serde_json::json!(false));
                order.push("field".to_string());
            }
            Request::Restart { .. } => order.push("restart".to_string()),
            _ => {}
        }
    }
    assert_eq!(
        order,
        ["field", "restart"],
        "a named sheep is resumed without a merge, so the write is the only \
         route the flag has, and it goes first: {said}"
    );
    assert!(
        said.contains(
            "notice[start]: rotating: log_timestamps waiting on the next spawn \
             (`shep reload rotating` promotes them)"
        ),
        "the write parked, and the verb says so: {said}"
    );
}

/// The same arm without the flag: a named sheep is resumed, and nothing is
/// written that the operator did not ask for.
#[tokio::test]
async fn a_sheep_named_by_the_token_without_the_flag_gets_no_field_write() {
    use shep_client::testing::fake_client_answering;
    use shep_core::status::ProcStatus;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let flock = vec![ProcessInfo::builder(0, "rotating", ProcStatus::Stopped).build()];
    let (client, mut envelopes) =
        fake_client_answering(&sock, move |request: &Request| match request {
            Request::ListFlock => Response::Flock(flock.clone()),
            Request::Restart { .. } => Response::Restarted {
                accepted: vec![ProcessInfo::builder(0, "rotating", ProcStatus::Online).build()],
                refused: Vec::new(),
            },
            _ => Response::Pong,
        })
        .await;

    let (code, _printed, said) = start_against(&client, "rotating").await;

    assert_eq!(code, ExitCode::Success, "{said}");
    assert!(
        !respawns(&mut envelopes).is_empty(),
        "the resume is what this verb was asked for: {said}"
    );
}

/// The refusal case for the named arm: the sheep stays down rather than
/// coming back up with its stamps on.
#[tokio::test]
async fn a_refused_field_write_on_a_named_sheep_stops_before_the_resume() {
    use shep_client::testing::fake_client_answering_refusals;
    use shep_core::protocol::RpcError;
    use shep_core::status::ProcStatus;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let flock = vec![ProcessInfo::builder(0, "rotating", ProcStatus::Stopped).build()];
    let (client, mut envelopes) =
        fake_client_answering_refusals(&sock, move |request: &Request| match request {
            Request::ListFlock => Ok(Response::Flock(flock.clone())),
            Request::SetSheepField { .. } => Err(RpcError {
                code: RpcErrorCode::InvalidConfig,
                message: "no config field named log_timestamps".to_string(),
                daemon_version: None,
            }),
            _ => Ok(Response::Pong),
        })
        .await;

    let mut args = start_args("rotating");
    args.no_log_timestamps = true;
    let (code, printed, said) = start_against_with_args(&client, &args).await;

    assert_eq!(code, ExitCode::InvalidConfig, "{said}");
    assert!(printed.is_empty(), "a failed verb leaves stdout empty");
    assert!(
        said.contains("rotating: log_timestamps"),
        "the failure names the sheep and the value: {said}"
    );
    assert!(
        respawns(&mut envelopes).is_empty(),
        "and the sheep is not brought back up stamped"
    );
}

/// Runs `start` over a host link named `amp2`, handing back code, stdout and
/// stderr.
async fn start_over_host_with_args(
    client: &Client,
    args: &StartArgs,
) -> (ExitCode, String, String) {
    let mut out = Vec::new();
    let mut err = Vec::new();
    let code = {
        let mut streams = Streams::bare(&mut out, &mut err, Format::Table);
        start_on_host(client, &mut streams, args, "amp2").await
    };
    (
        code,
        String::from_utf8(out).unwrap(),
        String::from_utf8(err).unwrap(),
    )
}

/// A name the host's flock has is resumed the way a local one is, and
/// whatever Flockfile sits in this directory is not read.
#[tokio::test]
async fn a_known_name_over_a_host_is_resumed_with_no_local_file_read() {
    use shep_client::testing::fake_client_answering;
    use shep_core::status::ProcStatus;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let flock = vec![ProcessInfo::builder(0, "api", ProcStatus::Stopped).build()];
    let (client, mut envelopes) =
        fake_client_answering(&sock, move |request: &Request| match request {
            Request::ListFlock => Response::Flock(flock.clone()),
            Request::Restart { .. } => Response::Restarted {
                accepted: vec![ProcessInfo::builder(0, "api", ProcStatus::Online).build()],
                refused: Vec::new(),
            },
            _ => Response::Pong,
        })
        .await;

    let (code, _printed, said) = start_over_host_with_args(&client, &start_args("api")).await;

    assert_eq!(code, ExitCode::Success, "{said}");
    assert_eq!(respawns(&mut envelopes).len(), 1, "{said}");
}

/// Counts the requests of any kind that reached the wire.
fn requests_seen(
    envelopes: &mut tokio::sync::mpsc::UnboundedReceiver<shep_core::protocol::Envelope>,
) -> usize {
    let mut seen = 0;
    while envelopes.try_recv().is_ok() {
        seen += 1;
    }
    seen
}

/// A path would be read on this machine, so it is refused with the command
/// that reads it on the host, and the host is not asked anything.
#[tokio::test]
async fn a_path_target_over_a_host_refuses_with_the_exact_ssh_command() {
    use shep_client::testing::fake_client_answering;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let (client, mut envelopes) =
        fake_client_answering(&sock, |_: &Request| Response::Flock(Vec::new())).await;

    let (code, printed, said) = start_over_host_with_args(&client, &start_args("./api")).await;

    assert_eq!(code, ExitCode::Usage, "{said}");
    assert!(printed.is_empty(), "a failed verb leaves stdout empty");
    assert!(
        said.contains(
            "`shep --host amp2 start ./api` would read ./api on this machine. \
             To start it there, run: ssh amp2 shep start ./api"
        ),
        "{said}"
    );
    assert_eq!(requests_seen(&mut envelopes), 0, "{said}");
}

/// `-` and `--flockfile` read this machine too, whatever the token looks like.
#[tokio::test]
async fn stdin_and_flockfile_targets_over_a_host_refuse_the_same_way() {
    use shep_client::testing::fake_client_answering;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let (client, mut envelopes) =
        fake_client_answering(&sock, |_: &Request| Response::Flock(Vec::new())).await;

    let (code, _printed, said) = start_over_host_with_args(&client, &start_args("-")).await;
    assert_eq!(code, ExitCode::Usage, "{said}");
    assert!(
        said.contains("`shep --host amp2 start -` would read stdin on this machine."),
        "{said}"
    );
    assert!(said.contains("ssh amp2 shep start -"), "{said}");

    let mut args = start_args("web");
    args.flockfile = true;
    let (code, _printed, said) = start_over_host_with_args(&client, &args).await;
    assert_eq!(code, ExitCode::Usage, "{said}");
    assert!(
        said.contains("`shep --host amp2 start --flockfile web` would read web on this machine."),
        "{said}"
    );
    assert!(
        said.contains("ssh amp2 shep start --flockfile web"),
        "{said}"
    );
    assert_eq!(requests_seen(&mut envelopes), 0, "{said}");
}

/// A name the host does not know is a miss on the host, with the ssh command
/// in case it was meant as a script.
#[tokio::test]
async fn an_unknown_name_over_a_host_is_not_found_and_hints_at_ssh() {
    use shep_client::testing::fake_client_answering;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let (client, mut envelopes) =
        fake_client_answering(&sock, |_: &Request| Response::Flock(Vec::new())).await;

    let (code, printed, said) = start_over_host_with_args(&client, &start_args("ghost")).await;

    assert_eq!(code, ExitCode::NotFound, "{said}");
    assert!(printed.is_empty());
    assert!(
        said.contains("ghost is not a sheep or a fold on amp2"),
        "{said}"
    );
    assert!(said.contains("ssh amp2 shep start ghost"), "{said}");
    assert!(
        respawns(&mut envelopes).is_empty(),
        "nothing is started for a name the host lacks"
    );
}

/// A bare `start` would discover a Flockfile in this directory, and would
/// boot a shepherd, so over a host it is refused before either.
#[cfg(unix)]
#[tokio::test]
async fn a_bare_start_over_a_host_refuses_before_reading_the_working_directory() {
    let dir = tempfile::tempdir().unwrap();
    let link = crate::remote::Link::Host {
        dest: "amp2",
        options: shep_client::SshOptions::default().program(dir.path().join("no-such-ssh")),
    };
    let mut args = start_args("unused");
    args.targets.clear();
    let mut out = Vec::new();
    let mut err = Vec::new();

    let code = {
        let mut streams = Streams::bare(&mut out, &mut err, Format::Table);
        crate::client::load_command(
            &mut streams,
            &link,
            crate::version_guard::VersionGuard::Enforce,
            &args,
            Load::Start,
        )
        .await
    };

    let said = String::from_utf8(err).unwrap();
    assert_eq!(code, ExitCode::Usage, "{said}");
    assert!(
        said.contains("`shep --host amp2 start` would read the Flockfile in this directory"),
        "{said}"
    );
    assert!(said.contains("ssh amp2 shep start"), "{said}");
    assert!(out.is_empty());
}

/// The refusal for a target read on this machine needs no host, so an
/// unreachable one is not what the operator is told.
#[cfg(unix)]
#[tokio::test]
async fn a_path_target_over_an_unreachable_host_is_refused_not_unreachable() {
    let dir = tempfile::tempdir().unwrap();
    let link = crate::remote::Link::Host {
        dest: "amp2",
        options: shep_client::SshOptions::default().program(dir.path().join("no-such-ssh")),
    };
    for (targets, flockfile) in [(&["./api"][..], false), (&["-"], false), (&["web"], true)] {
        let mut args = start_args("unused");
        args.targets = targets.iter().map(ToString::to_string).collect();
        args.flockfile = flockfile;
        let mut out = Vec::new();
        let mut err = Vec::new();

        let code = {
            let mut streams = Streams::bare(&mut out, &mut err, Format::Table);
            crate::client::load_command(
                &mut streams,
                &link,
                crate::version_guard::VersionGuard::Enforce,
                &args,
                Load::Start,
            )
            .await
        };

        let said = String::from_utf8(err).unwrap();
        assert_eq!(code, ExitCode::Usage, "{targets:?}: {said}");
        assert!(said.contains("ssh amp2 shep start"), "{targets:?}: {said}");
    }
}

/// The hint starts the same thing on the host: every flag and assignment the
/// operator typed rides along.
#[tokio::test]
async fn the_ssh_hint_carries_every_flag_and_assignment() {
    use shep_client::testing::fake_client_answering;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let (client, _envelopes) =
        fake_client_answering(&sock, |_: &Request| Response::Flock(Vec::new())).await;
    let mut args = start_args("FOO=1");
    args.targets.push("./api".to_string());
    args.name = Some("api2".to_string());
    args.fold = Some("web".to_string());
    args.no_log_timestamps = true;

    let (code, _printed, said) = start_over_host_with_args(&client, &args).await;

    assert_eq!(code, ExitCode::Usage, "{said}");
    assert!(
        said.contains("ssh amp2 shep start --name api2 --fold web --no-log-timestamps FOO=1 ./api"),
        "{said}"
    );
}

/// The hint is pasted into this machine's shell and then read by the host's,
/// so a token with a space is quoted for both.
#[tokio::test]
async fn the_ssh_hint_survives_two_shells_for_a_token_with_a_space() {
    use shep_client::testing::fake_client_answering;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let (client, _envelopes) =
        fake_client_answering(&sock, |_: &Request| Response::Flock(Vec::new())).await;

    let (code, _printed, said) =
        start_over_host_with_args(&client, &start_args("./my api.js")).await;

    assert_eq!(code, ExitCode::Usage, "{said}");
    assert!(
        said.contains("`shep --host amp2 start './my api.js'` would read ./my api.js"),
        "{said}"
    );
    assert!(
        said.contains(r"ssh amp2 'shep start '\''./my api.js'\'''"),
        "{said}"
    );
}

/// A listing that failed says nothing about the name, so the failure is what
/// the operator hears, not a miss.
#[tokio::test]
async fn a_failed_listing_over_a_host_is_not_a_not_found() {
    use shep_client::testing::fake_client_answering_refusals;
    use shep_core::protocol::RpcError;

    let dir = tempfile::tempdir().unwrap();
    let sock = shep_client::testing::control_address(dir.path());
    let (client, _envelopes) =
        fake_client_answering_refusals(&sock, |_: &Request| -> Result<Response, RpcError> {
            Err(RpcError {
                code: RpcErrorCode::Internal,
                message: "the roll is unreadable".to_string(),
                daemon_version: None,
            })
        })
        .await;

    let (code, printed, said) = start_over_host_with_args(&client, &start_args("web")).await;

    assert_eq!(code, ExitCode::Internal, "{said}");
    assert!(printed.is_empty());
    assert!(said.contains("the roll is unreadable"), "{said}");
    assert!(!said.contains("is not a sheep or a fold"), "{said}");
}