1pub mod v2;
41
42use std::fmt;
43use std::time::Duration;
44
45use crate::freshness::{self, Horizon, Observation};
46
47pub const NAME: &str = "health";
50
51pub const PROFILE: &str = "health.v1";
53
54pub const IFACE: &str = "health.v1";
56
57pub const STATUS: &str = "status";
59
60pub const CHECKS: &str = "checks/{check}";
62
63pub const FAULTS: &str = "faults";
65
66pub const STATUS_TTL: Duration = Duration::from_secs(60);
68
69pub const STATUS_REFRESH: Duration = Duration::from_secs(30);
72
73pub const MAX_CHECKS: u64 = 64;
75
76pub const CLOCK_AHEAD: &str = "clock_ahead";
78
79pub const CODES: &[&str] = &[CLOCK_AHEAD];
81
82#[must_use]
84pub fn status_horizon() -> Horizon {
85 Horizon::Within(STATUS_TTL)
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
92pub enum Major {
93 V1,
95 V2,
97}
98
99impl Major {
100 #[must_use]
102 pub const fn iface(self) -> &'static str {
103 match self {
104 Major::V1 => IFACE,
105 Major::V2 => v2::IFACE,
106 }
107 }
108
109 #[must_use]
113 pub fn named(iface: &str) -> Option<Major> {
114 [Major::V2, Major::V1]
115 .into_iter()
116 .find(|m| m.iface() == iface)
117 }
118
119 #[must_use]
125 pub fn of<'a>(interfaces: impl IntoIterator<Item = &'a str>) -> Option<Major> {
126 let mut found = None;
127 for iface in interfaces {
128 match Major::named(iface) {
129 Some(Major::V2) => return Some(Major::V2),
130 Some(Major::V1) => found = Some(Major::V1),
131 None => {}
132 }
133 }
134 found
135 }
136}
137
138#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
141pub enum Level {
142 Ok,
143 Degraded,
144 Failed,
145}
146
147impl Level {
148 #[must_use]
150 pub fn wire(self) -> i32 {
151 match self {
152 Self::Ok => 1,
153 Self::Degraded => 2,
154 Self::Failed => 3,
155 }
156 }
157
158 #[must_use]
160 pub fn as_str(self) -> &'static str {
161 match self {
162 Self::Ok => "ok",
163 Self::Degraded => "degraded",
164 Self::Failed => "failed",
165 }
166 }
167}
168
169impl fmt::Display for Level {
170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171 f.write_str(match self {
172 Self::Ok => "OK",
173 Self::Degraded => "DEGRADED",
174 Self::Failed => "FAILED",
175 })
176 }
177}
178
179#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
181pub enum Read {
182 Level(Level),
184 Unspecified,
187 Unlisted(i32),
190 Undecodable,
193}
194
195impl Read {
196 #[must_use]
198 pub fn from_wire(n: i32) -> Self {
199 match n {
200 0 => Self::Unspecified,
201 1 => Self::Level(Level::Ok),
202 2 => Self::Level(Level::Degraded),
203 3 => Self::Level(Level::Failed),
204 n => Self::Unlisted(n),
205 }
206 }
207
208 #[must_use]
210 pub fn level(self) -> Option<Level> {
211 match self {
212 Self::Level(l) => Some(l),
213 _ => None,
214 }
215 }
216}
217
218#[must_use]
221pub fn worst_check(checks: &[Read]) -> Option<(usize, Level)> {
222 checks
223 .iter()
224 .enumerate()
225 .filter_map(|(i, r)| r.level().map(|l| (i, l)))
226 .fold(None, |best, (i, l)| match best {
227 Some((_, b)) if b >= l => best,
228 _ => Some((i, l)),
229 })
230}
231
232#[must_use]
235pub fn consistent(status: Level, checks: &[Read]) -> bool {
236 worst_check(checks).is_none_or(|(_, w)| w <= status)
237}
238
239#[derive(Debug, Clone, Copy, PartialEq, Eq)]
241pub enum Listing {
242 Listed { token: bool },
245 NotListed,
247 Unread,
249}
250
251#[derive(Debug, Clone, Copy, PartialEq, Eq)]
253pub enum Presence {
254 Present(Listing),
256 Absent,
258 Incomplete,
260 AcrossFace { status_crosses: bool },
264}
265
266#[derive(Debug, Clone, Copy)]
268pub struct Reading<'a> {
269 pub presence: Presence,
270 pub status: &'a [Observation],
273 pub level: Option<Read>,
276 pub checks: Option<&'a [Read]>,
279}
280
281#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
284pub enum Verdict {
285 Healthy,
286 Unhealthy,
287 Stale,
290 Unobservable,
292 NotAsked,
295}
296
297impl Verdict {
298 #[must_use]
300 pub fn as_str(self) -> &'static str {
301 match self {
302 Self::Healthy => "healthy",
303 Self::Unhealthy => "unhealthy",
304 Self::Stale => "stale",
305 Self::Unobservable => "unobservable",
306 Self::NotAsked => "not_asked",
307 }
308 }
309
310 #[must_use]
312 pub fn is_finding(self) -> bool {
313 matches!(self, Self::Unhealthy | Self::Stale)
314 }
315}
316
317impl fmt::Display for Verdict {
318 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
319 f.write_str(self.as_str())
320 }
321}
322
323#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
325pub enum Reason {
326 Ok,
328 Degraded,
330 Failed,
332 Inconsistent,
335 Check,
338 UnknownLevel,
340 Undecodable,
342 Deleted,
344 PresenceIncomplete,
346 NoDescriptor,
348 FaceClosed,
351 NothingCrossed,
355 Absent,
357 NotListed,
359 LastKnown,
361 ChecksUnread,
364 Freshness(freshness::Reason),
367}
368
369impl Reason {
370 #[must_use]
372 pub fn as_str(self) -> &'static str {
373 match self {
374 Self::Ok => "ok",
375 Self::Degraded => "degraded",
376 Self::Failed => "failed",
377 Self::Inconsistent => "inconsistent",
378 Self::Check => "check",
379 Self::UnknownLevel => "unknown_level",
380 Self::Undecodable => "undecodable",
381 Self::Deleted => "deleted",
382 Self::PresenceIncomplete => "presence_incomplete",
383 Self::NoDescriptor => "no_descriptor",
384 Self::FaceClosed => "face_closed",
385 Self::NothingCrossed => "nothing_crossed",
386 Self::Absent => "absent",
387 Self::NotListed => "not_listed",
388 Self::LastKnown => "last_known",
389 Self::ChecksUnread => "checks_unread",
390 Self::Freshness(r) => r.as_str(),
391 }
392 }
393
394 #[must_use]
396 pub fn says(self) -> &'static str {
397 match self {
398 Self::Ok => "its status is OK and confirmed, and no check is worse",
399 Self::Degraded => "its status is DEGRADED",
400 Self::Failed => "its status is FAILED",
401 Self::Inconsistent => "a check is worse than its status, which health.v1 §2.2 forbids",
402 Self::Check => "its status's level is unknown, and a check is DEGRADED or FAILED",
403 Self::UnknownLevel => "its status's level is unknown",
404 Self::Undecodable => "its status does not decode",
405 Self::Deleted => "its owner deleted its status",
406 Self::PresenceIncomplete => {
407 "its token was not seen, and the presence read may be incomplete or refused"
408 }
409 Self::NoDescriptor => "its descriptor could not be read",
410 Self::FaceClosed => "the constrained face does not let its status cross",
411 Self::NothingCrossed => {
412 "nothing of its status has crossed the constrained face, so none is known to exist"
413 }
414 Self::Absent => "it is absent: presence's word, not a level",
415 Self::NotListed => "its descriptor does not list health.v1",
416 Self::LastKnown => "only an archive's last-known status was read, never current",
417 Self::ChecksUnread => "its checks were not read",
418 Self::Freshness(r) => r.says(),
419 }
420 }
421}
422
423impl fmt::Display for Reason {
424 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
425 f.write_str(self.as_str())
426 }
427}
428
429#[derive(Debug, Clone, Copy, PartialEq, Eq)]
432pub struct Judged {
433 pub verdict: Verdict,
434 pub reason: Reason,
435 pub level: Option<Level>,
436}
437
438impl Judged {
439 const fn new(verdict: Verdict, reason: Reason, level: Option<Level>) -> Self {
440 Self {
441 verdict,
442 reason,
443 level,
444 }
445 }
446
447 const fn unobservable(reason: Reason) -> Self {
448 Self::new(Verdict::Unobservable, reason, None)
449 }
450
451 const fn not_asked(reason: Reason) -> Self {
452 Self::new(Verdict::NotAsked, reason, None)
453 }
454
455 const fn unhealthy(reason: Reason, level: Level) -> Self {
456 Self::new(Verdict::Unhealthy, reason, Some(level))
457 }
458}
459
460#[must_use]
464pub fn judge(r: &Reading<'_>) -> Judged {
465 let status = freshness::judge_all(&status_horizon(), r.status);
466 judge_status(r.presence, status, r.level, r.checks)
467}
468
469#[must_use]
482pub fn judge_status(
483 presence: Presence,
484 status: freshness::Judged,
485 level: Option<Read>,
486 checks: Option<&[Read]>,
487) -> Judged {
488 use Reason as R;
489 match presence {
491 Presence::Absent => return Judged::not_asked(R::Absent),
492 Presence::Incomplete => return Judged::unobservable(R::PresenceIncomplete),
493 Presence::AcrossFace {
494 status_crosses: false,
495 } => return Judged::unobservable(R::FaceClosed),
496 Presence::Present(Listing::NotListed) => return Judged::not_asked(R::NotListed),
497 Presence::Present(Listing::Unread) => return Judged::unobservable(R::NoDescriptor),
498 Presence::AcrossFace {
499 status_crosses: true,
500 }
501 | Presence::Present(Listing::Listed { .. }) => {}
502 }
503 let f = status;
505 match f.verdict {
506 freshness::Verdict::Fresh => {}
507 freshness::Verdict::Stale
511 if f.reason == freshness::Reason::NoDelivery
512 && matches!(presence, Presence::AcrossFace { .. }) =>
513 {
514 return Judged::unobservable(R::NothingCrossed);
515 }
516 freshness::Verdict::Stale => {
517 return Judged::new(Verdict::Stale, R::Freshness(f.reason), None);
518 }
519 freshness::Verdict::Unobservable => return Judged::unobservable(R::Freshness(f.reason)),
520 freshness::Verdict::NotAsked => {
521 return match f.reason {
522 freshness::Reason::Deleted => Judged::unobservable(R::Deleted),
523 freshness::Reason::LastKnown => Judged::not_asked(R::LastKnown),
524 other => Judged::not_asked(R::Freshness(other)),
527 };
528 }
529 }
530 let worst = worst_check(checks.unwrap_or(&[])).map(|(_, l)| l);
532 match level.and_then(Read::level) {
533 Some(status) => match worst {
534 Some(w) if w > status => Judged::unhealthy(R::Inconsistent, w),
535 _ => match status {
536 Level::Ok => Judged::new(Verdict::Healthy, R::Ok, Some(Level::Ok)),
537 Level::Degraded => Judged::unhealthy(R::Degraded, Level::Degraded),
538 Level::Failed => Judged::unhealthy(R::Failed, Level::Failed),
539 },
540 },
541 None => match worst {
542 Some(w) if w > Level::Ok => Judged::unhealthy(R::Check, w),
543 _ => Judged::unobservable(match level {
544 Some(Read::Unspecified | Read::Unlisted(_)) => R::UnknownLevel,
545 _ => R::Undecodable,
548 }),
549 },
550 }
551}
552
553#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
555pub struct Rollup {
556 pub worst: Option<Level>,
559 pub healthy: usize,
560 pub unhealthy: usize,
561 pub stale: usize,
562 pub unobservable: usize,
563 pub not_asked: usize,
564}
565
566#[must_use]
570pub fn rollup(judged: impl IntoIterator<Item = Judged>) -> Rollup {
571 let mut r = Rollup::default();
572 for j in judged {
573 match j.verdict {
574 Verdict::Healthy => r.healthy += 1,
575 Verdict::Unhealthy => r.unhealthy += 1,
576 Verdict::Stale => r.stale += 1,
577 Verdict::Unobservable => r.unobservable += 1,
578 Verdict::NotAsked => r.not_asked += 1,
579 }
580 if matches!(j.verdict, Verdict::Healthy | Verdict::Unhealthy) {
581 r.worst = r.worst.max(j.level);
582 }
583 }
584 r
585}
586
587#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
590pub enum Answer {
591 Yes,
592 No,
593 Unobservable,
595 NotAsked,
597}
598
599impl Answer {
600 #[must_use]
602 pub fn as_str(self) -> &'static str {
603 match self {
604 Self::Yes => "yes",
605 Self::No => "no",
606 Self::Unobservable => "unobservable",
607 Self::NotAsked => "not_asked",
608 }
609 }
610}
611
612impl fmt::Display for Answer {
613 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
614 f.write_str(self.as_str())
615 }
616}
617
618#[derive(Debug, Clone, Copy, PartialEq, Eq)]
621pub struct Agreement {
622 pub answer: Answer,
623 pub reason: Reason,
624}
625
626#[must_use]
634pub fn agrees(judged: Judged, checks_read: bool) -> Agreement {
635 let a = |answer, reason| Agreement { answer, reason };
636 match (judged.verdict, judged.reason) {
637 (Verdict::NotAsked, r) => a(Answer::NotAsked, r),
638 (_, Reason::Inconsistent) => a(Answer::No, Reason::Inconsistent),
639 (
640 Verdict::Healthy | Verdict::Unhealthy,
641 r @ (Reason::Ok | Reason::Degraded | Reason::Failed),
642 ) => {
643 if checks_read {
644 a(Answer::Yes, r)
645 } else {
646 a(Answer::Unobservable, Reason::ChecksUnread)
647 }
648 }
649 (Verdict::Unhealthy, Reason::Check) => a(Answer::Unobservable, Reason::UnknownLevel),
652 (_, r) => a(Answer::Unobservable, r),
653 }
654}
655
656#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
664pub enum Heard {
665 Heartbeat,
670 HeartbeatHeld,
675 StatusPut,
678 StatusDeleted,
681 ClockAhead,
683 Fault,
686}
687
688impl Heard {
689 #[must_use]
695 pub fn heartbeat(time: crate::heartbeat::Time) -> Heard {
696 if time.is_instant() {
697 Heard::Heartbeat
698 } else {
699 Heard::HeartbeatHeld
700 }
701 }
702}
703
704#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
706pub enum ClockReason {
707 Ahead,
711 ClockAhead,
714 Confirmed,
718 TimeUnknown,
722 NothingHeard,
725 Absent,
727 NotListed,
729}
730
731impl ClockReason {
732 #[must_use]
734 pub fn as_str(self) -> &'static str {
735 match self {
736 Self::Ahead => "ahead",
737 Self::ClockAhead => "clock_ahead",
738 Self::Confirmed => "confirmed",
739 Self::TimeUnknown => "time_unknown",
740 Self::NothingHeard => "nothing_heard",
741 Self::Absent => "absent",
742 Self::NotListed => "not_listed",
743 }
744 }
745
746 #[must_use]
748 pub fn says(self) -> &'static str {
749 match self {
750 Self::Ahead => "its last heartbeat's time was ahead: its clock guard held",
751 Self::ClockAhead => {
752 "a clock_ahead fault was heard, and no heartbeat with its time known since"
753 }
754 Self::Confirmed => {
755 "its last heartbeat had its time known, after its last clock_ahead fault or with \
756 none heard"
757 }
758 Self::TimeUnknown => {
759 "its last heartbeat's time was no instant, ahead or unknown, and no clock_ahead \
760 fault was heard since one with its time known"
761 }
762 Self::NothingHeard => "no clock_ahead fault and no heartbeat was heard in the window",
763 Self::Absent => "it is absent: presence's word",
764 Self::NotListed => "its descriptor does not list health.v1",
765 }
766 }
767}
768
769impl fmt::Display for ClockReason {
770 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
771 f.write_str(self.as_str())
772 }
773}
774
775#[derive(Debug, Clone, Copy, PartialEq, Eq)]
778pub struct Clock {
779 pub answer: Answer,
780 pub reason: ClockReason,
781}
782
783#[must_use]
796pub fn clock_ahead(presence: Presence, heard: &[Heard]) -> Clock {
797 let c = |answer, reason| Clock { answer, reason };
798 match presence {
799 Presence::Absent => return c(Answer::NotAsked, ClockReason::Absent),
800 Presence::Present(Listing::NotListed) => {
801 return c(Answer::NotAsked, ClockReason::NotListed);
802 }
803 _ => {}
804 }
805 let last = |h: Heard| heard.iter().rposition(|x| *x == h);
806 let fault = last(Heard::ClockAhead);
807 let known = last(Heard::Heartbeat);
808 let unknown = last(Heard::HeartbeatHeld);
809 match (fault, known, unknown) {
810 (Some(f), k, _) if k.is_none_or(|k| k < f) => c(Answer::Yes, ClockReason::ClockAhead),
811 (_, Some(k), u) if u.is_none_or(|u| u < k) => c(Answer::No, ClockReason::Confirmed),
812 (_, _, Some(_)) => c(Answer::Unobservable, ClockReason::TimeUnknown),
813 _ => c(Answer::Unobservable, ClockReason::NothingHeard),
814 }
815}
816
817#[derive(Debug, Clone, Copy, PartialEq, Eq)]
819pub enum Code<'a> {
820 Profile(&'static str),
822 Application(&'a str),
824 Malformed(&'a str),
826}
827
828impl Code<'_> {
829 #[must_use]
831 pub fn as_str(&self) -> &'static str {
832 match self {
833 Self::Profile(_) => "profile",
834 Self::Application(_) => "application",
835 Self::Malformed(_) => "malformed",
836 }
837 }
838}
839
840#[must_use]
843pub fn is_code_form(s: &str) -> bool {
844 let mut bytes = s.bytes();
845 bytes.next().is_some_and(|b| b.is_ascii_lowercase())
846 && bytes.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_')
847}
848
849#[must_use]
851pub fn code(s: &str) -> Code<'_> {
852 if !is_code_form(s) {
853 return Code::Malformed(s);
854 }
855 match CODES.iter().find(|c| **c == s) {
856 Some(c) => Code::Profile(c),
857 None => Code::Application(s),
858 }
859}
860
861#[cfg(test)]
862mod tests {
863 use super::*;
864 use crate::freshness::Last;
865
866 fn fresh() -> [Observation; 1] {
867 [Observation::Subscribed {
868 last: Some(Last::Put(Duration::from_secs(10))),
869 listened: Duration::from_secs(120),
870 complete: true,
871 }]
872 }
873
874 fn present() -> Presence {
875 Presence::Present(Listing::Listed { token: false })
876 }
877
878 #[test]
879 fn the_levels_are_ordered_and_unspecified_is_none_of_them() {
880 assert!(Level::Ok < Level::Degraded && Level::Degraded < Level::Failed);
881 for l in [Level::Ok, Level::Degraded, Level::Failed] {
882 assert_eq!(Read::from_wire(l.wire()), Read::Level(l));
883 }
884 assert_eq!(Read::from_wire(0), Read::Unspecified);
885 assert_eq!(Read::from_wire(4), Read::Unlisted(4));
886 assert_eq!(Read::Unspecified.level(), None);
887 }
888
889 #[test]
890 fn the_worst_check_is_the_first_at_the_worst_known_level() {
891 let checks = [
892 Read::Level(Level::Degraded),
893 Read::Unlisted(9),
894 Read::Level(Level::Failed),
895 Read::Level(Level::Failed),
896 ];
897 assert_eq!(worst_check(&checks), Some((2, Level::Failed)));
898 assert_eq!(worst_check(&[Read::Unspecified]), None);
899 assert!(consistent(Level::Failed, &checks));
900 assert!(!consistent(Level::Degraded, &checks));
901 assert!(consistent(Level::Ok, &[]));
902 }
903
904 #[test]
905 fn a_fresh_status_vouches_for_its_checks_and_a_stale_one_does_not() {
906 let o = fresh();
907 let checks = [Read::Level(Level::Failed)];
908 let mut r = Reading {
909 presence: present(),
910 status: &o,
911 level: Some(Read::Level(Level::Ok)),
912 checks: Some(&checks),
913 };
914 assert_eq!(
915 judge(&r),
916 Judged::unhealthy(Reason::Inconsistent, Level::Failed)
917 );
918 let stale = [Observation::Subscribed {
919 last: Some(Last::Put(Duration::from_secs(61))),
920 listened: Duration::from_secs(120),
921 complete: true,
922 }];
923 r.status = &stale;
924 assert_eq!(judge(&r).verdict, Verdict::Stale);
925 }
926
927 #[test]
928 fn a_rollup_never_folds_stale_into_a_level() {
929 let stale = Judged::new(
930 Verdict::Stale,
931 Reason::Freshness(freshness::Reason::BeyondHorizon),
932 None,
933 );
934 let r = rollup([
935 stale,
936 Judged::new(Verdict::Healthy, Reason::Ok, Some(Level::Ok)),
937 ]);
938 assert_eq!(r.worst, Some(Level::Ok));
939 assert_eq!((r.healthy, r.stale), (1, 1));
940 assert_eq!(rollup([stale]).worst, None);
941 }
942
943 #[test]
944 fn the_status_agrees_with_its_checks_only_when_fresh_at_a_level_and_read() {
945 let j = |verdict, reason, level| Judged::new(verdict, reason, level);
946 let ok = j(Verdict::Healthy, Reason::Ok, Some(Level::Ok));
947 assert_eq!(agrees(ok, true).answer, Answer::Yes);
948 assert_eq!(
949 agrees(ok, false),
950 Agreement {
951 answer: Answer::Unobservable,
952 reason: Reason::ChecksUnread
953 }
954 );
955 let liar = Judged::unhealthy(Reason::Inconsistent, Level::Failed);
956 assert_eq!(agrees(liar, true).answer, Answer::No);
957 let frank = Judged::unhealthy(Reason::Failed, Level::Failed);
958 assert_eq!(agrees(frank, true).answer, Answer::Yes, "worse is allowed");
959 let unknown = Judged::unhealthy(Reason::Check, Level::Failed);
960 assert_eq!(agrees(unknown, true).reason, Reason::UnknownLevel);
961 let stale = j(
962 Verdict::Stale,
963 Reason::Freshness(freshness::Reason::BeyondHorizon),
964 None,
965 );
966 assert_eq!(agrees(stale, true).answer, Answer::Unobservable);
967 let absent = Judged::not_asked(Reason::Absent);
968 assert_eq!(agrees(absent, true).answer, Answer::NotAsked);
969 }
970
971 #[test]
972 fn the_clock_is_ahead_on_a_fault_no_heartbeat_with_its_time_followed() {
973 use Heard::{ClockAhead, Fault, Heartbeat, HeartbeatHeld, StatusDeleted, StatusPut};
974 let p = present();
975 let a = |heard: &[Heard]| clock_ahead(p, heard).answer;
976 assert_eq!(a(&[ClockAhead]), Answer::Yes);
977 assert_eq!(a(&[Heartbeat, ClockAhead]), Answer::Yes);
978 assert_eq!(a(&[ClockAhead, Heartbeat]), Answer::No);
979 assert_eq!(a(&[ClockAhead, StatusDeleted]), Answer::Yes);
980 assert_eq!(
981 a(&[ClockAhead, StatusPut]),
982 Answer::Yes,
983 "the guard holds no put"
984 );
985 assert_eq!(a(&[ClockAhead, HeartbeatHeld]), Answer::Yes);
986 assert_eq!(a(&[Heartbeat]), Answer::No);
987 assert_eq!(a(&[StatusPut]), Answer::Unobservable);
988 assert_eq!(
989 clock_ahead(p, &[Heartbeat, HeartbeatHeld]).reason,
990 ClockReason::TimeUnknown,
991 "a time unknown since: no longer no, and no fault to say yes"
992 );
993 assert_eq!(a(&[ClockAhead, HeartbeatHeld, Heartbeat]), Answer::No);
994 use crate::heartbeat::Time;
997 assert_eq!(Heard::heartbeat(Time::Ahead), HeartbeatHeld);
998 assert_eq!(Heard::heartbeat(Time::Unknown), HeartbeatHeld);
999 assert_eq!(Heard::heartbeat(Time::At { secs: 7, ms: 0 }), Heartbeat);
1000 assert_eq!(
1001 clock_ahead(p, &[Heartbeat, Heard::heartbeat(Time::Ahead)]).reason,
1002 ClockReason::TimeUnknown,
1003 "the ahead time alone answers nothing under health.v1"
1004 );
1005 assert_eq!(a(&[Fault]), Answer::Unobservable, "never from silence");
1006 assert_eq!(a(&[]), Answer::Unobservable);
1007 assert_eq!(
1008 clock_ahead(Presence::Absent, &[ClockAhead]).answer,
1009 Answer::NotAsked
1010 );
1011 }
1012
1013 #[test]
1016 fn a_descriptor_is_read_by_the_major_it_lists_and_v2_where_it_lists_both() {
1017 assert_eq!(Major::of(["freshness.v2", "health.v1"]), Some(Major::V1));
1018 assert_eq!(Major::of(["health.v2", "freshness.v2"]), Some(Major::V2));
1019 assert_eq!(Major::of(["health.v1", "health.v2"]), Some(Major::V2));
1020 assert_eq!(Major::of(["health.v2", "health.v1"]), Some(Major::V2));
1021 assert_eq!(Major::of(["tc.v1"]), None, "not listed: not asked");
1022 assert_eq!(Major::of([]), None);
1023 assert_eq!(Major::named("health.v2"), Some(Major::V2));
1024 assert_eq!(Major::named("health.v3"), None);
1025 assert_eq!((Major::V1.iface(), Major::V2.iface()), (IFACE, v2::IFACE));
1026 }
1027
1028 #[test]
1031 fn a_reading_is_judged_alike_from_its_observations_and_from_their_verdict() {
1032 let o = fresh();
1033 let status = freshness::judge_all(&status_horizon(), &o);
1034 assert_eq!(status.verdict, freshness::Verdict::Fresh);
1035 let checks = [Read::Level(Level::Degraded)];
1036 for level in [
1037 Some(Read::Level(Level::Ok)),
1038 Some(Read::Unspecified),
1039 Some(Read::Undecodable),
1040 None,
1041 ] {
1042 let r = Reading {
1043 presence: present(),
1044 status: &o,
1045 level,
1046 checks: Some(&checks),
1047 };
1048 assert_eq!(
1049 judge(&r),
1050 judge_status(present(), status, level, Some(&checks))
1051 );
1052 }
1053 let unknown = judge_status(present(), status, Some(Read::Unspecified), None);
1055 assert_eq!(
1056 (unknown.verdict, unknown.reason),
1057 (Verdict::Unobservable, Reason::UnknownLevel)
1058 );
1059 let refused = judge_status(present(), status, Some(Read::Undecodable), None);
1060 assert_eq!(
1061 (refused.verdict, refused.reason),
1062 (Verdict::Unobservable, Reason::Undecodable)
1063 );
1064 }
1065
1066 #[test]
1067 fn codes_are_lowercase_ascii_and_the_table_is_the_profiles() {
1068 assert_eq!(code("clock_ahead"), Code::Profile(CLOCK_AHEAD));
1069 assert_eq!(code("tc_netns_gone"), Code::Application("tc_netns_gone"));
1070 assert_eq!(code(""), Code::Malformed(""));
1071 assert_eq!(code("_x"), Code::Malformed("_x"));
1072 assert_eq!(code("Clock_ahead").as_str(), "malformed");
1073 }
1074}