shep-client 0.18.2

Async client for the shep daemon: typed requests, event subscriptions, and starting a shepherd that is not running yet
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//! Typed shorthands for the requests more than one caller makes.
//!
//! Each sends one [`Request`] and answers the one [`Response`] it expects,
//! so a caller never writes the match or the error for any other answer.
//! [`Client`] and [`ReconnectingClient`] carry the same set.

use core::future::Future;

use shep_core::config::DogTable;
use shep_core::protocol::{
    DogSectionToml, HostUsage, LogStream, LogTail, ProcessInfo, Request, Response, RpcError,
    RpcErrorCode, SelectorSpec,
};
use shep_core::secrets::references_in;

use crate::ReconnectingClient;
use crate::client::{Client, LOG_PLANE_DEADLINE, RequestError};

impl Client {
    /// The dog `name`'s own section of `dogs.toml` with every
    /// `{{secret:...}}` reference resolved, empty when it has none.
    ///
    /// Asks [`Request::DogConfigResolved`]. A shepherd too old to know it
    /// is asked [`Request::DogConfig`] instead, and a section that still
    /// holds a reference is refused rather than handed over unresolved.
    ///
    /// # Errors
    ///
    /// As [`Self::request`], and [`RequestError::UnexpectedReply`] for any
    /// answer but [`Response::DogSection`]. [`RequestError::Rpc`] with
    /// [`RpcErrorCode::Unsupported`] when an older shepherd's section names
    /// a secret reference it cannot resolve.
    pub async fn dog_config(&self, name: &str) -> Result<DogSectionToml, RequestError> {
        resolved_dog_section(name, |body| self.request(body)).await
    }

    /// The sheep `name`'s `[app.dogs.<dog>]` table with every
    /// `{{secret:...}}` reference resolved in that sheep's own environment,
    /// `None` when it carries no such table.
    ///
    /// Asks [`Request::SheepDogSettingsResolved`]. A shepherd too old to
    /// know it is asked [`Request::DogSheepSettings`] instead, and a table
    /// that still holds a reference is refused rather than handed over
    /// unresolved.
    ///
    /// # Errors
    ///
    /// As [`Self::request`], including [`RpcErrorCode::NotFound`] for a name
    /// no sheep holds and [`RpcErrorCode::InvalidConfig`] for a reference
    /// that cannot resolve (an older shepherd is asked `ListFlock` too, to
    /// tell that name from a sheep without the table), and
    /// [`RequestError::UnexpectedReply`] for any
    /// answer but [`Response::SheepDogTable`].
    /// [`RequestError::Rpc`] with [`RpcErrorCode::Unsupported`] when an
    /// older shepherd's table names a secret reference it cannot resolve.
    pub async fn sheep_dog_table(
        &self,
        name: &str,
        dog: &str,
    ) -> Result<Option<DogTable>, RequestError> {
        resolved_sheep_dog_table(name, dog, |body| self.request(body)).await
    }

    /// Every supervised entry, dogs included.
    ///
    /// # Errors
    ///
    /// As [`Self::request`], and [`RequestError::UnexpectedReply`] for any
    /// answer but [`Response::Flock`].
    pub async fn list_flock(&self) -> Result<Vec<ProcessInfo>, RequestError> {
        flock(self.request(Request::ListFlock).await?)
    }

    /// Reopens the log files of every sheep `selector` matches, under
    /// [`LOG_PLANE_DEADLINE`], answering each one it matched.
    ///
    /// # Errors
    ///
    /// As [`Self::request`], and [`RequestError::UnexpectedReply`] for any
    /// answer but [`Response::Reopened`].
    pub async fn reopen(&self, selector: SelectorSpec) -> Result<Vec<ProcessInfo>, RequestError> {
        let body = Request::Reopen { selector };
        reopened(
            self.request_with_deadline(body, Some(LOG_PLANE_DEADLINE))
                .await?,
        )
    }

    /// What the flock's machine is doing, or `None` where the shepherd
    /// cannot read its host at all.
    ///
    /// # Errors
    ///
    /// As [`Self::request`], and [`RequestError::UnexpectedReply`] for any
    /// answer but [`Response::HostUsage`].
    pub async fn host_usage(&self) -> Result<Option<HostUsage>, RequestError> {
        host(self.request(Request::HostUsage).await?)
    }

    /// The last `lines` lines of one sheep's out or err log, read by the
    /// shepherd on its own host.
    ///
    /// A file that is absent, unreadable or not recorded is a [`LogTail`]
    /// variant, not an error.
    ///
    /// # Errors
    ///
    /// As [`Self::request`], including [`RpcErrorCode::NotFound`] for an id
    /// no sheep holds, and [`RequestError::UnexpectedReply`] for any answer
    /// but [`Response::LogTail`].
    pub async fn tail_log(
        &self,
        id: u32,
        stream: LogStream,
        lines: u32,
    ) -> Result<LogTail, RequestError> {
        log_tail(self.request(Request::TailLog { id, stream, lines }).await?)
    }
}

impl ReconnectingClient {
    /// Shorthand for [`Client::dog_config`] on the current generation.
    ///
    /// # Errors
    ///
    /// As [`Client::dog_config`].
    pub async fn dog_config(&self, name: &str) -> Result<DogSectionToml, RequestError> {
        resolved_dog_section(name, |body| self.request(body)).await
    }

    /// Shorthand for [`Client::sheep_dog_table`] on the current generation.
    ///
    /// # Errors
    ///
    /// As [`Client::sheep_dog_table`].
    pub async fn sheep_dog_table(
        &self,
        name: &str,
        dog: &str,
    ) -> Result<Option<DogTable>, RequestError> {
        resolved_sheep_dog_table(name, dog, |body| self.request(body)).await
    }

    /// Shorthand for [`Client::list_flock`] on the current generation.
    ///
    /// # Errors
    ///
    /// As [`Client::list_flock`].
    pub async fn list_flock(&self) -> Result<Vec<ProcessInfo>, RequestError> {
        flock(self.request(Request::ListFlock).await?)
    }

    /// Shorthand for [`Client::reopen`] on the current generation.
    ///
    /// # Errors
    ///
    /// As [`Client::reopen`].
    pub async fn reopen(&self, selector: SelectorSpec) -> Result<Vec<ProcessInfo>, RequestError> {
        let body = Request::Reopen { selector };
        reopened(
            self.request_with_deadline(body, Some(LOG_PLANE_DEADLINE))
                .await?,
        )
    }

    /// Shorthand for [`Client::host_usage`] on the current generation.
    ///
    /// # Errors
    ///
    /// As [`Client::host_usage`].
    pub async fn host_usage(&self) -> Result<Option<HostUsage>, RequestError> {
        host(self.request(Request::HostUsage).await?)
    }

    /// Shorthand for [`Client::tail_log`] on the current generation.
    ///
    /// # Errors
    ///
    /// As [`Client::tail_log`].
    pub async fn tail_log(
        &self,
        id: u32,
        stream: LogStream,
        lines: u32,
    ) -> Result<LogTail, RequestError> {
        log_tail(self.request(Request::TailLog { id, stream, lines }).await?)
    }
}

fn dog_config_request(name: &str) -> Request {
    Request::DogConfigResolved {
        name: name.to_owned(),
    }
}

/// Asks `ask` for the resolved section, and for the plain one when the
/// shepherd does not know the request.
///
/// A shepherd built before the request refuses it with `Unsupported`, which
/// `Request::Unrecognized` earns on the daemon side. `Unrecognized` covers a
/// code this build was not taught.
async fn resolved_dog_section<F, Fut>(name: &str, ask: F) -> Result<DogSectionToml, RequestError>
where
    F: Fn(Request) -> Fut,
    Fut: Future<Output = Result<Response, RequestError>>,
{
    match ask(dog_config_request(name)).await {
        Err(RequestError::Rpc(refusal)) if is_unknown_request(&refusal) => {
            let plain = Request::DogConfig {
                name: name.to_owned(),
            };
            let section = dog_section(ask(plain).await?)?;
            if !references_in(section.as_str()).is_empty() {
                return Err(RequestError::Rpc(RpcError {
                    code: RpcErrorCode::Unsupported,
                    message: format!(
                        "the shepherd is too old to resolve secret references in the \
                         `{name}` dog's section"
                    ),
                    daemon_version: refusal.daemon_version,
                }));
            }
            Ok(section)
        }
        answer => dog_section(answer?),
    }
}

/// Whether `refusal` is a shepherd saying it does not know the request.
fn is_unknown_request(refusal: &RpcError) -> bool {
    matches!(
        refusal.code,
        RpcErrorCode::Unsupported | RpcErrorCode::Unrecognized
    )
}

/// Asks `ask` for the sheep's resolved table, and for every sheep's plain
/// one when the shepherd does not know the request, as
/// [`resolved_dog_section`] does for a section.
async fn resolved_sheep_dog_table<F, Fut>(
    name: &str,
    dog: &str,
    ask: F,
) -> Result<Option<DogTable>, RequestError>
where
    F: Fn(Request) -> Fut,
    Fut: Future<Output = Result<Response, RequestError>>,
{
    let resolved = Request::SheepDogSettingsResolved {
        name: name.to_owned(),
        dog: dog.to_owned(),
    };
    match ask(resolved).await {
        Err(RequestError::Rpc(refusal)) if is_unknown_request(&refusal) => {
            let plain = Request::DogSheepSettings {
                dog: dog.to_owned(),
            };
            let table = match ask(plain).await? {
                Response::DogSheepSettings { mut tables } => tables.remove(name),
                other => return Err(unexpected("DogSheepSettings", &other)),
            };
            if table.is_none() {
                ensure_a_sheep(name, &ask).await?;
            }
            if table
                .as_ref()
                .is_some_and(|table| !table.references().is_empty())
            {
                return Err(RequestError::Rpc(RpcError {
                    code: RpcErrorCode::Unsupported,
                    message: format!(
                        "the shepherd is too old to resolve secret references in the \
                         `{name}` sheep's `{dog}` table"
                    ),
                    daemon_version: refusal.daemon_version,
                }));
            }
            Ok(table)
        }
        answer => match answer? {
            Response::SheepDogTable { table } => Ok(table),
            other => Err(unexpected("SheepDogSettingsResolved", &other)),
        },
    }
}

/// Succeeds when the flock holds a sheep named `name`, and is `NotFound`
/// otherwise, a dog's own name included.
///
/// `DogSheepSettings` lists only the sheep that carry a table, so on its
/// own it answers an unknown name and a sheep without the table alike.
/// The request this stands in for tells them apart.
async fn ensure_a_sheep<F, Fut>(name: &str, ask: &F) -> Result<(), RequestError>
where
    F: Fn(Request) -> Fut,
    Fut: Future<Output = Result<Response, RequestError>>,
{
    let sheep = flock(ask(Request::ListFlock).await?)?;
    if sheep
        .iter()
        .any(|entry| entry.name == name && entry.dog.is_none())
    {
        return Ok(());
    }
    Err(RequestError::Rpc(RpcError {
        code: RpcErrorCode::NotFound,
        message: format!("no sheep named {name}"),
        daemon_version: None,
    }))
}

fn dog_section(answer: Response) -> Result<DogSectionToml, RequestError> {
    match answer {
        Response::DogSection { toml } => Ok(toml),
        other => Err(unexpected("DogConfigResolved", &other)),
    }
}

fn flock(answer: Response) -> Result<Vec<ProcessInfo>, RequestError> {
    match answer {
        Response::Flock(flock) => Ok(flock),
        other => Err(unexpected("ListFlock", &other)),
    }
}

fn reopened(answer: Response) -> Result<Vec<ProcessInfo>, RequestError> {
    match answer {
        Response::Reopened(sheep) => Ok(sheep),
        other => Err(unexpected("Reopen", &other)),
    }
}

fn host(answer: Response) -> Result<Option<HostUsage>, RequestError> {
    match answer {
        Response::HostUsage(usage) => Ok(usage),
        other => Err(unexpected("HostUsage", &other)),
    }
}

fn log_tail(answer: Response) -> Result<LogTail, RequestError> {
    match answer {
        Response::LogTail(tail) => Ok(tail),
        other => Err(unexpected("TailLog", &other)),
    }
}

fn unexpected(asked: &'static str, answer: &Response) -> RequestError {
    RequestError::UnexpectedReply {
        asked,
        answered: answer.name(),
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::time::Duration;

    use shep_core::protocol::{DogSource, Envelope, HostUsage, RpcError, RpcErrorCode};
    use shep_core::status::ProcStatus;
    use tokio::sync::mpsc::UnboundedReceiver;

    use super::*;
    use crate::testing::{
        Handovers, Handshake, control_address, fake_client_answering, fake_daemon_across_handovers,
        sample_ack, sample_info, serve_one_request,
    };

    /// The sentinel stands for a secret's value: it must reach no error text.
    const SENTINEL: &str = "sentinel-value-1f3a";

    /// What a shepherd that has never heard of the request answers.
    fn does_not_know_the_request() -> RpcError {
        RpcError {
            code: RpcErrorCode::Unsupported,
            message: "this shepherd does not implement the request".to_owned(),
            daemon_version: None,
        }
    }

    /// A shepherd built before `DogConfigResolved`, whose section is `section`.
    fn older_shepherd(path: &std::path::Path, section: &str) -> Handovers {
        let shepherd = fake_daemon_across_handovers(path, vec![Handshake::Accept(sample_ack())]);
        shepherd.reply_to_dog_config(section);
        shepherd.refuse_dog_config_resolved(does_not_know_the_request());
        shepherd
    }

    /// The request bodies the shepherd received, in order.
    fn asked(shepherd: &Handovers) -> Vec<Request> {
        shepherd
            .envelopes()
            .into_iter()
            .map(|(_, envelope)| envelope.body)
            .collect()
    }

    fn named(name: &str) -> (Request, Request) {
        (
            Request::DogConfigResolved {
                name: name.to_owned(),
            },
            Request::DogConfig {
                name: name.to_owned(),
            },
        )
    }

    fn too_old_refusal(name: &str) -> RequestError {
        RequestError::Rpc(RpcError {
            code: RpcErrorCode::Unsupported,
            message: format!(
                "the shepherd is too old to resolve secret references in the `{name}` dog's section"
            ),
            daemon_version: None,
        })
    }

    /// The one envelope the client under test sent.
    async fn sent(envelopes: &mut UnboundedReceiver<Envelope>) -> Envelope {
        tokio::time::timeout(Duration::from_secs(5), envelopes.recv())
            .await
            .expect("the request reached the fake daemon")
            .expect("the fake daemon is still up")
    }

    #[tokio::test]
    async fn dog_config_asks_by_name_and_answers_the_section() {
        let dir = tempfile::tempdir().unwrap();
        let (client, mut envelopes) =
            fake_client_answering(&control_address(dir.path()), |_| Response::DogSection {
                toml: "poll = \"1m\"\n".to_owned().into(),
            })
            .await;

        let section = client.dog_config("bark").await.unwrap();

        assert_eq!(section.as_str(), "poll = \"1m\"\n");
        assert_eq!(
            sent(&mut envelopes).await.body,
            Request::DogConfigResolved {
                name: "bark".to_owned()
            }
        );
    }

    #[tokio::test]
    async fn list_flock_answers_the_listing() {
        let dir = tempfile::tempdir().unwrap();
        let (client, _envelopes) = fake_client_answering(&control_address(dir.path()), |_| {
            Response::Flock(vec![sample_info()])
        })
        .await;

        assert_eq!(client.list_flock().await.unwrap(), vec![sample_info()]);
    }

    /// The log plane walks the flock file by file, so the default budget
    /// can run out on a slow disk.
    #[tokio::test]
    async fn reopen_names_the_selector_and_asks_for_the_log_plane_budget() {
        let dir = tempfile::tempdir().unwrap();
        let (client, mut envelopes) = fake_client_answering(&control_address(dir.path()), |_| {
            Response::Reopened(vec![sample_info()])
        })
        .await;

        let reopened = client
            .reopen(SelectorSpec::Name("web".to_owned()))
            .await
            .unwrap();

        assert_eq!(reopened, vec![sample_info()]);
        let envelope = sent(&mut envelopes).await;
        assert_eq!(
            envelope.body,
            Request::Reopen {
                selector: SelectorSpec::Name("web".to_owned())
            }
        );
        let budget = u64::try_from(LOG_PLANE_DEADLINE.as_millis()).unwrap();
        assert_eq!(envelope.deadline_ms, Some(budget));
    }

    #[tokio::test]
    async fn host_usage_answers_the_reading() {
        let dir = tempfile::tempdir().unwrap();
        let reading = HostUsage {
            cpu_percent: None,
            memory_used_bytes: 1,
            memory_total_bytes: 2,
            disk_bytes_per_second: None,
            network_bytes_per_second: None,
        };
        let (client, _envelopes) = fake_client_answering(&control_address(dir.path()), move |_| {
            Response::HostUsage(Some(reading))
        })
        .await;

        assert_eq!(client.host_usage().await.unwrap(), Some(reading));
    }

    fn two_lines() -> LogTail {
        LogTail::Lines {
            lines: vec!["listening".to_owned(), "ready".to_owned()],
            truncated: true,
        }
    }

    #[tokio::test]
    async fn tail_log_sends_the_request_and_returns_the_tail() {
        let dir = tempfile::tempdir().unwrap();
        let (client, mut envelopes) = fake_client_answering(&control_address(dir.path()), |_| {
            Response::LogTail(two_lines())
        })
        .await;

        let tail = client.tail_log(3, LogStream::Err, 20).await.unwrap();

        assert_eq!(tail, two_lines());
        let envelope = sent(&mut envelopes).await;
        assert_eq!(
            envelope.body,
            Request::TailLog {
                id: 3,
                stream: LogStream::Err,
                lines: 20
            }
        );
        let budget = u64::try_from(crate::client::DEFAULT_DEADLINE.as_millis()).unwrap();
        assert_eq!(envelope.deadline_ms, Some(budget), "the default deadline");
    }

    #[tokio::test]
    async fn tail_log_refuses_any_other_answer() {
        let dir = tempfile::tempdir().unwrap();
        let (client, _envelopes) =
            fake_client_answering(&control_address(dir.path()), |_| Response::Pong).await;

        let err = client.tail_log(3, LogStream::Out, 20).await.unwrap_err();

        assert_eq!(
            err,
            RequestError::UnexpectedReply {
                asked: "TailLog",
                answered: "Pong",
            }
        );
    }

    #[tokio::test]
    async fn a_reconnecting_client_tail_log_sends_the_request_and_returns_the_tail() {
        let dir = tempfile::tempdir().unwrap();
        let socket = control_address(dir.path());
        let served = serve_one_request(&socket, sample_ack(), Response::LogTail(two_lines())).await;
        let client = ReconnectingClient::connect(&socket).await.unwrap();

        let tail = client.tail_log(3, LogStream::Out, 5).await.unwrap();

        assert_eq!(tail, two_lines());
        let envelope = tokio::time::timeout(Duration::from_secs(5), served)
            .await
            .expect("the request reached the fake daemon")
            .unwrap();
        assert_eq!(
            envelope.body,
            Request::TailLog {
                id: 3,
                stream: LogStream::Out,
                lines: 5
            }
        );
    }

    #[tokio::test]
    async fn a_reconnecting_client_tail_log_refuses_any_other_answer() {
        let dir = tempfile::tempdir().unwrap();
        let socket = control_address(dir.path());
        let _served = serve_one_request(&socket, sample_ack(), Response::Pong).await;
        let client = ReconnectingClient::connect(&socket).await.unwrap();

        let err = client.tail_log(3, LogStream::Out, 5).await.unwrap_err();

        assert_eq!(
            err,
            RequestError::UnexpectedReply {
                asked: "TailLog",
                answered: "Pong",
            }
        );
    }

    /// The error names both variants and never the section's body.
    #[tokio::test]
    async fn another_answer_is_named_by_variant_alone() {
        let dir = tempfile::tempdir().unwrap();
        let secret = "https://example.invalid/hook/super-secret-token";
        let (client, _envelopes) = fake_client_answering(&control_address(dir.path()), move |_| {
            Response::DogSection {
                toml: format!("webhook = \"{secret}\"\n").into(),
            }
        })
        .await;

        let err = client.list_flock().await.unwrap_err();

        assert_eq!(
            err,
            RequestError::UnexpectedReply {
                asked: "ListFlock",
                answered: "DogSection",
            }
        );
        assert_eq!(
            err.to_string(),
            "the daemon answered ListFlock with DogSection"
        );
    }

    #[tokio::test]
    async fn a_reconnecting_client_asks_the_same_way() {
        let dir = tempfile::tempdir().unwrap();
        let socket = control_address(dir.path());
        let served = serve_one_request(
            &socket,
            sample_ack(),
            Response::DogSection {
                toml: String::new().into(),
            },
        )
        .await;
        let client = ReconnectingClient::connect(&socket).await.unwrap();

        let section = client.dog_config("metrics").await.unwrap();

        assert_eq!(section.as_str(), "");
        let envelope = tokio::time::timeout(Duration::from_secs(5), served)
            .await
            .expect("the request reached the fake daemon")
            .unwrap();
        assert_eq!(
            envelope.body,
            Request::DogConfigResolved {
                name: "metrics".to_owned()
            }
        );
    }

    #[tokio::test]
    async fn an_older_shepherd_that_does_not_know_the_request_is_asked_the_plain_one_when_the_section_names_no_reference()
     {
        let dir = tempfile::tempdir().unwrap();
        let path = control_address(dir.path());
        let shepherd = older_shepherd(&path, "poll = \"1m\"\n");
        let client = Client::connect(&path).await.unwrap();

        let section = client.dog_config("bark").await.unwrap();

        assert_eq!(section.as_str(), "poll = \"1m\"\n");
        let (resolved, plain) = named("bark");
        assert_eq!(asked(&shepherd), vec![resolved, plain]);
    }

    #[tokio::test]
    async fn an_older_shepherd_with_a_reference_in_the_section_is_refused_without_the_section_text()
    {
        let dir = tempfile::tempdir().unwrap();
        let path = control_address(dir.path());
        let _shepherd = older_shepherd(&path, &format!("token = \"{{{{secret:{SENTINEL}}}}}\"\n"));
        let client = Client::connect(&path).await.unwrap();

        let err = client.dog_config("bark").await.unwrap_err();

        assert_eq!(err, too_old_refusal("bark"));
        let text = format!("{err} {err:?}");
        assert!(!text.contains(SENTINEL), "{text}");
        assert!(!text.contains("{{secret:"), "{text}");
    }

    /// An escaped `{{{{secret:` and a malformed token name no reference, so
    /// the plain section is safe to hand over.
    #[tokio::test]
    async fn an_older_shepherd_with_only_text_that_looks_like_a_reference_is_read_as_before() {
        for text in [
            "note = \"{{{{secret:X}}}}\"\n",
            "note = \"{{secret:bad key}}\"\n",
        ] {
            let dir = tempfile::tempdir().unwrap();
            let path = control_address(dir.path());
            let _shepherd = older_shepherd(&path, text);
            let client = Client::connect(&path).await.unwrap();

            let section = client.dog_config("bark").await.unwrap();

            assert_eq!(section.as_str(), text);
        }
    }

    #[tokio::test]
    async fn a_reconnecting_client_on_an_older_shepherd_asks_the_plain_one_when_the_section_names_no_reference()
     {
        let dir = tempfile::tempdir().unwrap();
        let path = control_address(dir.path());
        let shepherd = older_shepherd(&path, "poll = \"1m\"\n");
        let client = ReconnectingClient::connect(&path).await.unwrap();

        let section = client.dog_config("metrics").await.unwrap();

        assert_eq!(section.as_str(), "poll = \"1m\"\n");
        let (resolved, plain) = named("metrics");
        assert_eq!(asked(&shepherd), vec![resolved, plain]);
    }

    #[tokio::test]
    async fn a_reconnecting_client_on_an_older_shepherd_with_a_reference_is_refused_without_the_section_text()
     {
        let dir = tempfile::tempdir().unwrap();
        let path = control_address(dir.path());
        let _shepherd = older_shepherd(&path, &format!("token = \"{{{{secret:{SENTINEL}}}}}\"\n"));
        let client = ReconnectingClient::connect(&path).await.unwrap();

        let err = client.dog_config("metrics").await.unwrap_err();

        assert_eq!(err, too_old_refusal("metrics"));
        let text = format!("{err} {err:?}");
        assert!(!text.contains(SENTINEL), "{text}");
        assert!(!text.contains("{{secret:"), "{text}");
    }

    fn deploy_table(token: &str) -> DogTable {
        DogTable::from(serde_json::Map::from_iter([(
            "env".to_owned(),
            serde_json::json!({ "TOKEN": token }),
        )]))
    }

    /// A shepherd built before `SheepDogSettingsResolved`, whose `deploy`
    /// table on `web` is `table`.
    fn older_shepherd_with_table(path: &std::path::Path, table: DogTable) -> Handovers {
        let shepherd = fake_daemon_across_handovers(path, vec![Handshake::Accept(sample_ack())]);
        shepherd.reply_to_dog_sheep_settings(BTreeMap::from([("web".to_owned(), table)]));
        shepherd.reply_to_list(vec![
            sheep_named(1, "web"),
            sheep_named(2, "api"),
            // A dog's own name is not a sheep's.
            ProcessInfo::builder(3, "bark", ProcStatus::Online)
                .dog(Some(DogSource::BuiltIn { grants: None }))
                .build(),
        ]);
        shepherd.refuse_dog_config_resolved(does_not_know_the_request());
        shepherd
    }

    fn sheep_named(id: u32, name: &str) -> ProcessInfo {
        ProcessInfo::builder(id, name, ProcStatus::Online).build()
    }

    fn resolved_and_plain_table_reads() -> Vec<Request> {
        vec![
            Request::SheepDogSettingsResolved {
                name: "web".to_owned(),
                dog: "deploy".to_owned(),
            },
            Request::DogSheepSettings {
                dog: "deploy".to_owned(),
            },
        ]
    }

    #[tokio::test]
    async fn sheep_dog_table_asks_by_name_and_dog() {
        let dir = tempfile::tempdir().unwrap();
        let (client, mut envelopes) =
            fake_client_answering(&control_address(dir.path()), |_| Response::SheepDogTable {
                table: Some(deploy_table("resolved")),
            })
            .await;

        let table = client.sheep_dog_table("web", "deploy").await.unwrap();

        assert_eq!(table, Some(deploy_table("resolved")));
        assert_eq!(
            sent(&mut envelopes).await.body,
            Request::SheepDogSettingsResolved {
                name: "web".to_owned(),
                dog: "deploy".to_owned(),
            }
        );
    }

    #[tokio::test]
    async fn a_reconnecting_client_sheep_dog_table_asks_the_same_way() {
        let dir = tempfile::tempdir().unwrap();
        let socket = control_address(dir.path());
        let served = serve_one_request(
            &socket,
            sample_ack(),
            Response::SheepDogTable { table: None },
        )
        .await;
        let client = ReconnectingClient::connect(&socket).await.unwrap();

        assert_eq!(client.sheep_dog_table("web", "deploy").await.unwrap(), None);

        let envelope = tokio::time::timeout(Duration::from_secs(5), served)
            .await
            .expect("the request reached the fake daemon")
            .unwrap();
        assert_eq!(envelope.body, resolved_and_plain_table_reads()[0]);
    }

    #[tokio::test]
    async fn sheep_dog_table_refuses_any_other_answer() {
        let dir = tempfile::tempdir().unwrap();
        let (client, _envelopes) =
            fake_client_answering(&control_address(dir.path()), |_| Response::Pong).await;

        let err = client.sheep_dog_table("web", "deploy").await.unwrap_err();

        assert_eq!(
            err,
            RequestError::UnexpectedReply {
                asked: "SheepDogSettingsResolved",
                answered: "Pong",
            }
        );
    }

    #[tokio::test]
    async fn sheep_dog_table_falls_back_to_dog_sheep_settings_on_an_older_shepherd() {
        let dir = tempfile::tempdir().unwrap();
        let path = control_address(dir.path());
        let plain = DogTable::from(serde_json::Map::from_iter([(
            "retries".to_owned(),
            serde_json::json!(3),
        )]));
        let shepherd = older_shepherd_with_table(&path, plain.clone());
        let client = Client::connect(&path).await.unwrap();

        assert_eq!(
            client.sheep_dog_table("web", "deploy").await.unwrap(),
            Some(plain)
        );
        assert_eq!(
            client.sheep_dog_table("api", "deploy").await.unwrap(),
            None,
            "a sheep the older shepherd lists no table for"
        );
        assert_eq!(asked(&shepherd)[..2], resolved_and_plain_table_reads());
    }

    #[tokio::test]
    async fn sheep_dog_table_on_an_older_shepherd_is_not_found_for_a_name_no_sheep_holds() {
        let dir = tempfile::tempdir().unwrap();
        let path = control_address(dir.path());
        let _shepherd = older_shepherd_with_table(&path, deploy_table("plain"));
        let client = Client::connect(&path).await.unwrap();

        for name in ["ghost", "bark"] {
            let err = client.sheep_dog_table(name, "deploy").await.unwrap_err();

            assert_eq!(
                err,
                RequestError::Rpc(RpcError {
                    code: RpcErrorCode::NotFound,
                    message: format!("no sheep named {name}"),
                    daemon_version: None,
                })
            );
        }
    }

    #[tokio::test]
    async fn sheep_dog_table_refuses_an_older_shepherds_table_holding_a_reference() {
        let dir = tempfile::tempdir().unwrap();
        let path = control_address(dir.path());
        let _shepherd =
            older_shepherd_with_table(&path, deploy_table(&format!("{{{{secret:{SENTINEL}}}}}")));
        let client = Client::connect(&path).await.unwrap();

        let err = client.sheep_dog_table("web", "deploy").await.unwrap_err();

        assert_eq!(
            err,
            RequestError::Rpc(RpcError {
                code: RpcErrorCode::Unsupported,
                message: "the shepherd is too old to resolve secret references in the `web` \
                          sheep's `deploy` table"
                    .to_owned(),
                daemon_version: None,
            })
        );
        let text = format!("{err} {err:?}");
        assert!(!text.contains(SENTINEL), "{text}");
        assert!(!text.contains("{{secret:"), "{text}");
    }
}