system_update_configurator/
health.rs

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
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::bridge::{Bridge, BridgeError, OptOutPreference};
use async_trait::async_trait;
use fuchsia_inspect::health::Reporter;
use std::cell::RefCell;

pub struct HealthStatus {
    node: RefCell<fuchsia_inspect::health::Node>,
}

impl HealthStatus {
    pub fn new(root: &fuchsia_inspect::Node) -> Self {
        let mut node = fuchsia_inspect::health::Node::new(root);
        node.set_ok();
        Self { node: RefCell::new(node) }
    }

    fn set_bridge_error(&self, err: Option<&BridgeError>) {
        let mut node = self.node.borrow_mut();

        match err {
            Some(err) => node.set_unhealthy(&format!("Error interacting with storage: {err:#}")),
            None => node.set_ok(),
        }
    }

    pub fn wrap_bridge<I>(self, bridge: I) -> BridgeWithHealthStatus<I> {
        BridgeWithHealthStatus { status: self, inner: bridge }
    }
}

pub struct BridgeWithHealthStatus<I> {
    status: HealthStatus,
    inner: I,
}

#[async_trait(?Send)]
impl<I> Bridge for BridgeWithHealthStatus<I>
where
    I: Bridge,
{
    async fn get_opt_out(&self) -> Result<OptOutPreference, BridgeError> {
        let res = self.inner.get_opt_out().await;
        self.status.set_bridge_error(res.as_ref().err());
        res
    }

    async fn set_opt_out(&mut self, value: OptOutPreference) -> Result<(), BridgeError> {
        let res = self.inner.set_opt_out(value).await;
        self.status.set_bridge_error(res.as_ref().err());
        res
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bridge;
    use diagnostics_assertions::{assert_data_tree, AnyProperty};
    use fuchsia_inspect::Inspector;

    fn verify_healthy(inspector: &Inspector) {
        assert_data_tree!(
            inspector,
            root: {
                "fuchsia.inspect.Health": {
                    "start_timestamp_nanos": AnyProperty,
                    "status": "OK",
                }
            }
        );
    }

    fn verify_unhealthy(inspector: &Inspector) {
        assert_data_tree!(
            inspector,
            root: {
                "fuchsia.inspect.Health": {
                    "start_timestamp_nanos": AnyProperty,
                    "status": "UNHEALTHY",
                    "message": AnyProperty,
                }
            }
        );
    }

    #[fuchsia::test]
    async fn starts_healthy() {
        let inspector = Inspector::default();

        let storage = bridge::testing::Fake::new(OptOutPreference::AllowAllUpdates);
        let _storage = HealthStatus::new(inspector.root()).wrap_bridge(storage);

        verify_healthy(&inspector);
    }

    #[fuchsia::test]
    async fn updates_health_on_storage_error() {
        let inspector = Inspector::default();

        let (storage, fail_requests) =
            bridge::testing::Fake::new_with_error_toggle(OptOutPreference::AllowAllUpdates);
        let storage = HealthStatus::new(inspector.root()).wrap_bridge(storage);

        verify_healthy(&inspector);

        fail_requests.set(true);
        storage.get_opt_out().await.unwrap_err();

        verify_unhealthy(&inspector);
    }

    #[fuchsia::test]
    async fn updates_health_on_storage_success() {
        let inspector = Inspector::default();

        let (storage, fail_requests) =
            bridge::testing::Fake::new_with_error_toggle(OptOutPreference::AllowAllUpdates);
        let storage = HealthStatus::new(inspector.root()).wrap_bridge(storage);

        fail_requests.set(true);
        storage.get_opt_out().await.unwrap_err();

        fail_requests.set(false);
        storage.get_opt_out().await.unwrap();

        verify_healthy(&inspector);
    }
}