fuchsia_triage/metrics/
fetch.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use super::{missing, syntax_error, MetricValue};
6use crate::config::{DataFetcher, DiagnosticData, Source};
7use anyhow::{anyhow, bail, Context, Error, Result};
8use diagnostics_hierarchy::{DiagnosticsHierarchy, SelectResult};
9use fidl_fuchsia_diagnostics::Selector;
10use fidl_fuchsia_inspect::DEFAULT_TREE_NAME;
11use moniker::ExtendedMoniker;
12use regex::Regex;
13use selectors::{SelectorExt, VerboseError};
14use serde::Serialize;
15use serde_derive::Deserialize;
16use serde_json::map::Map as JsonMap;
17use serde_json::Value as JsonValue;
18use std::collections::HashMap;
19use std::str::FromStr;
20use std::sync::LazyLock;
21
22/// [Fetcher] is a source of values to feed into the calculations. It may contain data either
23/// from snapshot.zip files (e.g. inspect.json data that can be accessed via "select" entries)
24/// or supplied in the specification of a trial.
25#[derive(Clone, Debug)]
26pub enum Fetcher<'a> {
27    FileData(FileDataFetcher<'a>),
28    TrialData(TrialDataFetcher<'a>),
29}
30
31/// [FileDataFetcher] contains fetchers for data in snapshot.zip files.
32#[derive(Clone, Debug)]
33pub struct FileDataFetcher<'a> {
34    pub inspect: &'a InspectFetcher,
35    pub syslog: &'a TextFetcher,
36    pub klog: &'a TextFetcher,
37    pub bootlog: &'a TextFetcher,
38    pub annotations: &'a KeyValueFetcher,
39}
40
41impl<'a> FileDataFetcher<'a> {
42    pub fn new(data: &'a [DiagnosticData]) -> FileDataFetcher<'a> {
43        let mut fetcher = FileDataFetcher {
44            inspect: InspectFetcher::ref_empty(),
45            syslog: TextFetcher::ref_empty(),
46            klog: TextFetcher::ref_empty(),
47            bootlog: TextFetcher::ref_empty(),
48            annotations: KeyValueFetcher::ref_empty(),
49        };
50        for DiagnosticData { source, data, .. } in data.iter() {
51            match source {
52                Source::Inspect => {
53                    if let DataFetcher::Inspect(data) = data {
54                        fetcher.inspect = data;
55                    }
56                }
57                Source::Syslog => {
58                    if let DataFetcher::Text(data) = data {
59                        fetcher.syslog = data;
60                    }
61                }
62                Source::Klog => {
63                    if let DataFetcher::Text(data) = data {
64                        fetcher.klog = data;
65                    }
66                }
67                Source::Bootlog => {
68                    if let DataFetcher::Text(data) = data {
69                        fetcher.bootlog = data;
70                    }
71                }
72                Source::Annotations => {
73                    if let DataFetcher::KeyValue(data) = data {
74                        fetcher.annotations = data;
75                    }
76                }
77            }
78        }
79        fetcher
80    }
81
82    pub(crate) fn fetch(&self, selector: &SelectorString) -> MetricValue {
83        match selector.selector_type {
84            // Selectors return a vector. Non-wildcarded Inspect selectors will usually return
85            // a vector with a single element, but when a component exposes multiple
86            // `fuchsia.inspect.Tree`s, a non-wildcarded selector may match multiple properties.
87            SelectorType::Inspect => MetricValue::Vector(self.inspect.fetch(selector)),
88        }
89    }
90
91    // Return a vector of errors encountered by contained fetchers.
92    pub fn errors(&self) -> Vec<String> {
93        self.inspect.component_errors.iter().map(|e| format!("{}", e)).collect()
94    }
95}
96
97/// [TrialDataFetcher] stores the key-value lookup for metric names whose values are given as
98/// part of a trial (under the "test" section of the .triage files).
99#[derive(Clone, Debug)]
100pub struct TrialDataFetcher<'a> {
101    values: &'a HashMap<String, JsonValue>,
102    pub(crate) klog: &'a TextFetcher,
103    pub(crate) syslog: &'a TextFetcher,
104    pub(crate) bootlog: &'a TextFetcher,
105    pub(crate) annotations: &'a KeyValueFetcher,
106}
107
108static EMPTY_JSONVALUES: LazyLock<HashMap<String, JsonValue>> = LazyLock::new(HashMap::new);
109
110impl<'a> TrialDataFetcher<'a> {
111    pub fn new(values: &'a HashMap<String, JsonValue>) -> TrialDataFetcher<'a> {
112        TrialDataFetcher {
113            values,
114            klog: TextFetcher::ref_empty(),
115            syslog: TextFetcher::ref_empty(),
116            bootlog: TextFetcher::ref_empty(),
117            annotations: KeyValueFetcher::ref_empty(),
118        }
119    }
120
121    pub fn new_empty() -> TrialDataFetcher<'static> {
122        TrialDataFetcher {
123            values: &EMPTY_JSONVALUES,
124            klog: TextFetcher::ref_empty(),
125            syslog: TextFetcher::ref_empty(),
126            bootlog: TextFetcher::ref_empty(),
127            annotations: KeyValueFetcher::ref_empty(),
128        }
129    }
130
131    pub fn set_syslog(&mut self, fetcher: &'a TextFetcher) {
132        self.syslog = fetcher;
133    }
134
135    pub fn set_klog(&mut self, fetcher: &'a TextFetcher) {
136        self.klog = fetcher;
137    }
138
139    pub fn set_bootlog(&mut self, fetcher: &'a TextFetcher) {
140        self.bootlog = fetcher;
141    }
142
143    pub fn set_annotations(&mut self, fetcher: &'a KeyValueFetcher) {
144        self.annotations = fetcher;
145    }
146
147    pub(crate) fn fetch(&self, name: &str) -> MetricValue {
148        match self.values.get(name) {
149            Some(value) => MetricValue::from(value),
150            None => syntax_error(format!("Value {} not overridden in test", name)),
151        }
152    }
153
154    pub(crate) fn has_entry(&self, name: &str) -> bool {
155        self.values.contains_key(name)
156    }
157}
158
159/// Selector type used to determine how to query target file.
160#[derive(Deserialize, Debug, Clone, PartialEq, Serialize)]
161pub enum SelectorType {
162    /// Selector for Inspect Tree ("inspect.json" files).
163    Inspect,
164}
165
166impl FromStr for SelectorType {
167    type Err = anyhow::Error;
168    fn from_str(selector_type: &str) -> Result<Self, Self::Err> {
169        match selector_type {
170            "INSPECT" => Ok(SelectorType::Inspect),
171            incorrect => bail!("Invalid selector type '{}' - must be INSPECT", incorrect),
172        }
173    }
174}
175
176#[derive(Debug, Clone, PartialEq, Serialize)]
177pub struct SelectorString {
178    pub(crate) full_selector: String,
179    pub selector_type: SelectorType,
180    body: String,
181
182    #[serde(skip_serializing)]
183    parsed_selector: Selector,
184}
185
186impl SelectorString {
187    pub fn body(&self) -> &str {
188        &self.body
189    }
190}
191
192impl TryFrom<String> for SelectorString {
193    type Error = anyhow::Error;
194
195    fn try_from(full_selector: String) -> Result<Self, Self::Error> {
196        let mut string_parts = full_selector.splitn(2, ':');
197        let selector_type =
198            SelectorType::from_str(string_parts.next().ok_or_else(|| anyhow!("Empty selector"))?)?;
199        let body = string_parts.next().ok_or_else(|| anyhow!("Selector needs a :"))?.to_owned();
200        let parsed_selector = selectors::parse_selector::<VerboseError>(&body)?;
201        Ok(SelectorString { full_selector, selector_type, body, parsed_selector })
202    }
203}
204
205#[derive(Debug)]
206pub struct ComponentInspectInfo {
207    processed_data: DiagnosticsHierarchy,
208    moniker: ExtendedMoniker,
209    tree_name: String,
210}
211
212impl ComponentInspectInfo {
213    fn matches_selector(&self, selector: &Selector) -> bool {
214        self.moniker
215            .match_against_selectors_and_tree_name(&self.tree_name, Some(selector))
216            .next()
217            .is_some()
218    }
219}
220
221#[derive(Default, Debug)]
222pub struct KeyValueFetcher {
223    pub map: JsonMap<String, JsonValue>,
224}
225
226impl TryFrom<&str> for KeyValueFetcher {
227    type Error = anyhow::Error;
228
229    fn try_from(json_text: &str) -> Result<Self, Self::Error> {
230        let raw_json =
231            json_text.parse::<JsonValue>().context("Couldn't parse KeyValue text as JSON.")?;
232        match raw_json {
233            JsonValue::Object(map) => Ok(KeyValueFetcher { map }),
234            _ => bail!("Bad json KeyValue data needs to be Object (map)."),
235        }
236    }
237}
238
239impl TryFrom<&JsonMap<String, JsonValue>> for KeyValueFetcher {
240    type Error = anyhow::Error;
241
242    fn try_from(map: &JsonMap<String, JsonValue>) -> Result<Self, Self::Error> {
243        // This doesn't fail today, but that's an implementation detail; don't count on it.
244        Ok(KeyValueFetcher { map: map.clone() })
245    }
246}
247
248static EMPTY_KEY_VALUE_FETCHER: LazyLock<KeyValueFetcher> = LazyLock::new(KeyValueFetcher::default);
249
250impl KeyValueFetcher {
251    pub fn ref_empty() -> &'static Self {
252        &EMPTY_KEY_VALUE_FETCHER
253    }
254
255    pub fn len(&self) -> usize {
256        self.map.len()
257    }
258
259    pub fn fetch(&self, key: &str) -> MetricValue {
260        match self.map.get(key) {
261            Some(value) => MetricValue::from(value),
262            None => missing(format!("Key '{}' not found in annotations", key)),
263        }
264    }
265}
266
267#[derive(Default, Debug)]
268pub struct TextFetcher {
269    pub lines: Vec<String>,
270}
271
272impl From<&str> for TextFetcher {
273    fn from(log_buffer: &str) -> Self {
274        TextFetcher { lines: log_buffer.split('\n').map(|s| s.to_string()).collect::<Vec<_>>() }
275    }
276}
277
278static EMPTY_TEXT_FETCHER: LazyLock<TextFetcher> = LazyLock::new(TextFetcher::default);
279
280impl TextFetcher {
281    pub fn ref_empty() -> &'static Self {
282        &EMPTY_TEXT_FETCHER
283    }
284
285    pub fn contains(&self, pattern: &str) -> bool {
286        let re = match Regex::new(pattern) {
287            Ok(re) => re,
288            _ => return false,
289        };
290        self.lines.iter().any(|s| re.is_match(s))
291    }
292}
293
294#[derive(Default, Debug)]
295pub struct InspectFetcher {
296    pub components: Vec<ComponentInspectInfo>,
297    pub component_errors: Vec<anyhow::Error>,
298}
299
300impl TryFrom<&str> for InspectFetcher {
301    type Error = anyhow::Error;
302
303    fn try_from(json_text: &str) -> Result<Self, Self::Error> {
304        let raw_json =
305            json_text.parse::<JsonValue>().context("Couldn't parse Inspect text as JSON.")?;
306        match raw_json {
307            JsonValue::Array(list) => Self::try_from(list),
308            _ => bail!("Bad json inspect data needs to be array."),
309        }
310    }
311}
312
313impl TryFrom<Vec<JsonValue>> for InspectFetcher {
314    type Error = anyhow::Error;
315
316    fn try_from(component_vec: Vec<JsonValue>) -> Result<Self, Self::Error> {
317        fn extract_json_value(component: &mut JsonValue, key: &'_ str) -> Result<JsonValue, Error> {
318            Ok(component
319                .get_mut(key)
320                .ok_or_else(|| anyhow!("'{}' not found in Inspect component", key))?
321                .take())
322        }
323
324        fn moniker_from(component: &mut JsonValue) -> Result<ExtendedMoniker, anyhow::Error> {
325            let value = extract_json_value(component, "moniker")
326                .or_else(|_| bail!("'moniker' not found in Inspect component"))?;
327            let moniker = ExtendedMoniker::parse_str(
328                value
329                    .as_str()
330                    .ok_or_else(|| anyhow!("Inspect component path wasn't a valid string"))?,
331            )?;
332            Ok(moniker)
333        }
334
335        let components = component_vec.into_iter().map(|mut raw_component| {
336            let moniker = moniker_from(&mut raw_component)?;
337            let tree_name = match extract_json_value(
338                &mut extract_json_value(&mut raw_component, "metadata")?,
339                "name",
340            ) {
341                Ok(n) => n.as_str().unwrap_or(DEFAULT_TREE_NAME).to_string(),
342                // the "name" field might be missing from older systems
343                Err(_) => DEFAULT_TREE_NAME.to_string(),
344            };
345            let raw_contents = extract_json_value(&mut raw_component, "payload").or_else(|_| {
346                extract_json_value(&mut raw_component, "contents").or_else(|_| {
347                    bail!("Neither 'payload' nor 'contents' found in Inspect component")
348                })
349            })?;
350            let processed_data: DiagnosticsHierarchy = match raw_contents {
351                v if v.is_null() => {
352                    // If the payload is null, leave the hierarchy empty.
353                    DiagnosticsHierarchy::new_root()
354                }
355                raw_contents => serde_json::from_value(raw_contents).with_context(|| {
356                    format!(
357                        "Unable to deserialize Inspect contents for {} to node hierarchy",
358                        moniker,
359                    )
360                })?,
361            };
362            Ok(ComponentInspectInfo { moniker, processed_data, tree_name })
363        });
364
365        let mut component_errors = vec![];
366        let components = components
367            .filter_map(|v| match v {
368                Ok(component) => Some(component),
369                Err(e) => {
370                    component_errors.push(e);
371                    None
372                }
373            })
374            .collect::<Vec<_>>();
375        Ok(Self { components, component_errors })
376    }
377}
378
379static EMPTY_INSPECT_FETCHER: LazyLock<InspectFetcher> = LazyLock::new(InspectFetcher::default);
380
381impl InspectFetcher {
382    pub fn ref_empty() -> &'static Self {
383        &EMPTY_INSPECT_FETCHER
384    }
385
386    fn try_fetch(&self, selector_string: &SelectorString) -> Result<Vec<MetricValue>, Error> {
387        let mut intermediate_results = Vec::new();
388        let mut found_component = false;
389        for component in &self.components {
390            if !component.matches_selector(&selector_string.parsed_selector) {
391                continue;
392            }
393            found_component = true;
394            let selector = selector_string.parsed_selector.clone();
395            intermediate_results.push(diagnostics_hierarchy::select_from_hierarchy(
396                &component.processed_data,
397                &selector,
398            )?);
399        }
400
401        if !found_component {
402            return Ok(vec![missing(format!(
403                "No component found matching selector {}",
404                selector_string.body,
405            ))]);
406        }
407
408        let mut result = vec![];
409        for r in intermediate_results {
410            match r {
411                SelectResult::Properties(p) => {
412                    result.extend(p.into_iter().cloned().map(MetricValue::from))
413                }
414                SelectResult::Nodes(n) => {
415                    for node in n {
416                        for _ in &node.children {
417                            result.push(MetricValue::Node);
418                        }
419
420                        for prop in &node.properties {
421                            result.push(MetricValue::from(prop.clone()));
422                        }
423                    }
424                }
425            }
426        }
427
428        Ok(result)
429    }
430
431    pub fn fetch(&self, selector: &SelectorString) -> Vec<MetricValue> {
432        match self.try_fetch(selector) {
433            Ok(v) => v,
434            Err(e) => vec![syntax_error(format!("Fetch {:?} -> {}", selector, e))],
435        }
436    }
437
438    #[cfg(test)]
439    fn fetch_str(&self, selector_str: &str) -> Vec<MetricValue> {
440        match SelectorString::try_from(selector_str.to_owned()) {
441            Ok(selector) => self.fetch(&selector),
442            Err(e) => vec![syntax_error(format!("Bad selector {}: {}", selector_str, e))],
443        }
444    }
445}
446
447#[cfg(test)]
448mod test {
449    use super::*;
450    use crate::metrics::variable::VariableName;
451    use crate::metrics::{Metric, MetricState, Problem, ValueSource};
452    use crate::{assert_problem, make_metrics};
453    use serde_json::Value as JsonValue;
454
455    static LOCAL_M: LazyLock<HashMap<String, JsonValue>> = LazyLock::new(|| {
456        let mut m = HashMap::new();
457        m.insert("foo".to_owned(), JsonValue::from(42));
458        m.insert("a::b".to_owned(), JsonValue::from(7));
459        m
460    });
461    static FOO_42_AB_7_TRIAL_FETCHER: LazyLock<TrialDataFetcher<'static>> =
462        LazyLock::new(|| TrialDataFetcher::new(&LOCAL_M));
463    static LOCAL_F: LazyLock<Vec<DiagnosticData>> = LazyLock::new(|| {
464        let s = r#"[
465            {
466                "data_source": "Inspect",
467                "moniker": "bar",
468                "metadata": {},
469                "payload": { "root": { "bar": 99 }}
470            },
471            {
472                "data_source": "Inspect",
473                "moniker": "bar2",
474                "metadata": {},
475                "payload": { "root": { "bar": 90 }}
476            }
477
478            ]"#;
479        vec![DiagnosticData::new("i".to_string(), Source::Inspect, s.to_string()).unwrap()]
480    });
481    static BAR_99_FILE_FETCHER: LazyLock<FileDataFetcher<'static>> =
482        LazyLock::new(|| FileDataFetcher::new(&LOCAL_F));
483    static BAR_SELECTOR: LazyLock<SelectorString> =
484        LazyLock::new(|| SelectorString::try_from("INSPECT:bar:root:bar".to_owned()).unwrap());
485    static NEW_BAR_SELECTOR: LazyLock<SelectorString> =
486        LazyLock::new(|| SelectorString::try_from("INSPECT:bar2:root:bar".to_owned()).unwrap());
487    static BAD_COMPONENT_SELECTOR: LazyLock<SelectorString> = LazyLock::new(|| {
488        SelectorString::try_from("INSPECT:bad_component:root:bar".to_owned()).unwrap()
489    });
490    static WRONG_SELECTOR: LazyLock<SelectorString> =
491        LazyLock::new(|| SelectorString::try_from("INSPECT:bar:root:oops".to_owned()).unwrap());
492    static LOCAL_DUPLICATES_F: LazyLock<Vec<DiagnosticData>> = LazyLock::new(|| {
493        let s = r#"[
494                {
495                    "data_source": "Inspect",
496                    "moniker": "bootstrap/foo",
497                    "metadata": {},
498                    "payload": null
499                },
500                {
501                    "data_source": "Inspect",
502                    "moniker": "bootstrap/foo",
503                    "metadata": {},
504                    "payload": {"root": {"bar": 10}}
505                }
506            ]"#;
507        vec![DiagnosticData::new("i".to_string(), Source::Inspect, s.to_string()).unwrap()]
508    });
509    static LOCAL_DUPLICATES_FETCHER: LazyLock<FileDataFetcher<'static>> =
510        LazyLock::new(|| FileDataFetcher::new(&LOCAL_DUPLICATES_F));
511    static DUPLICATE_SELECTOR: LazyLock<SelectorString> = LazyLock::new(|| {
512        SelectorString::try_from("INSPECT:bootstrap/foo:root:bar".to_owned()).unwrap()
513    });
514
515    macro_rules! variable {
516        ($name:expr) => {
517            &VariableName::new($name.to_string())
518        };
519    }
520
521    #[fuchsia::test]
522    fn test_file_fetch() {
523        assert_eq!(
524            BAR_99_FILE_FETCHER.fetch(&BAR_SELECTOR),
525            MetricValue::Vector(vec![MetricValue::Int(99)])
526        );
527        assert_eq!(BAR_99_FILE_FETCHER.fetch(&WRONG_SELECTOR), MetricValue::Vector(vec![]),);
528    }
529
530    #[fuchsia::test]
531    fn test_duplicate_file_fetch() {
532        assert_eq!(
533            LOCAL_DUPLICATES_FETCHER.fetch(&DUPLICATE_SELECTOR),
534            MetricValue::Vector(vec![MetricValue::Int(10)])
535        );
536    }
537
538    #[fuchsia::test]
539    fn test_trial_fetch() {
540        assert!(FOO_42_AB_7_TRIAL_FETCHER.has_entry("foo"));
541        assert!(FOO_42_AB_7_TRIAL_FETCHER.has_entry("a::b"));
542        assert!(!FOO_42_AB_7_TRIAL_FETCHER.has_entry("a:b"));
543        assert!(!FOO_42_AB_7_TRIAL_FETCHER.has_entry("oops"));
544        assert_eq!(FOO_42_AB_7_TRIAL_FETCHER.fetch("foo"), MetricValue::Int(42));
545        assert_problem!(
546            FOO_42_AB_7_TRIAL_FETCHER.fetch("oops"),
547            "SyntaxError: Value oops not overridden in test"
548        );
549    }
550
551    #[fuchsia::test]
552    fn test_eval_with_file() {
553        let metrics = make_metrics!({
554            "bar_file":{
555                eval: {
556                    "bar_plus_one": "bar + 1",
557                    "oops_plus_one": "oops + 1"
558                }
559                select: {
560                    "bar": [BAR_SELECTOR],
561                    "wrong_or_bar": [WRONG_SELECTOR, BAR_SELECTOR],
562                    "wrong_or_wrong":  [WRONG_SELECTOR, WRONG_SELECTOR],
563                    "wrong_or_new_bar_or_bar": [WRONG_SELECTOR, NEW_BAR_SELECTOR, BAR_SELECTOR],
564                    "bad_component_or_bar": [BAD_COMPONENT_SELECTOR, BAR_SELECTOR]
565                }
566            },
567            "other_file":{
568                eval: {
569                    "bar": "42"
570                }
571            }
572        });
573
574        let file_state =
575            MetricState::new(&metrics, Fetcher::FileData(BAR_99_FILE_FETCHER.clone()), None);
576        assert_eq!(
577            file_state.evaluate_variable("bar_file", variable!("bar_plus_one")),
578            MetricValue::Int(100)
579        );
580        assert_problem!(
581            file_state.evaluate_variable("bar_file", variable!("oops_plus_one")),
582            "SyntaxError: Metric 'oops' Not Found in 'bar_file'"
583        );
584        assert_eq!(
585            file_state.evaluate_variable("bar_file", variable!("bar")),
586            MetricValue::Vector(vec![MetricValue::Int(99)])
587        );
588        assert_eq!(
589            file_state.evaluate_variable("other_file", variable!("bar")),
590            MetricValue::Int(42)
591        );
592        assert_eq!(
593            file_state.evaluate_variable("other_file", variable!("other_file::bar")),
594            MetricValue::Int(42)
595        );
596        assert_eq!(
597            file_state.evaluate_variable("other_file", variable!("bar_file::bar")),
598            MetricValue::Vector(vec![MetricValue::Int(99)])
599        );
600        assert_eq!(
601            file_state.evaluate_variable("bar_file", variable!("bar")),
602            file_state.evaluate_variable("bar_file", variable!("wrong_or_bar")),
603        );
604        assert_eq!(
605            file_state.evaluate_variable("bar_file", variable!("wrong_or_wrong")),
606            MetricValue::Vector(vec![]),
607        );
608        assert_eq!(
609            file_state.evaluate_variable("bar_file", variable!("wrong_or_new_bar_or_bar")),
610            MetricValue::Vector(vec![MetricValue::Int(90)])
611        );
612        assert_eq!(
613            file_state.evaluate_variable("bar_file", variable!("bad_component_or_bar")),
614            MetricValue::Vector(vec![MetricValue::Int(99)])
615        );
616        assert_problem!(
617            file_state.evaluate_variable("other_file", variable!("bar_plus_one")),
618            "SyntaxError: Metric 'bar_plus_one' Not Found in 'other_file'"
619        );
620        assert_problem!(
621            file_state.evaluate_variable("missing_file", variable!("bar_plus_one")),
622            "SyntaxError: Bad namespace 'missing_file'"
623        );
624        assert_problem!(
625            file_state.evaluate_variable("bar_file", variable!("other_file::bar_plus_one")),
626            "SyntaxError: Metric 'bar_plus_one' Not Found in 'other_file'"
627        );
628    }
629
630    #[fuchsia::test]
631    fn test_eval_with_trial() {
632        // The (broken) "foo" selector should be ignored in favor of the "foo" fetched value.
633        // The file "a" should be completely ignored when testing foo_file.
634        let metrics = make_metrics!({
635            "a":{
636                eval: {
637                "b": "2",
638                "c": "3",
639                "foo": "4",
640                }
641            },
642            "foo_file":{
643                eval: {
644                    "foo_plus_one": "foo + 1",
645                    "oops_plus_one": "oops + 1",
646                    "ab_plus_one": "a::b + 1",
647                    "ac_plus_one": "a::c + 1"
648                }
649                select: {
650                "foo": [BAR_SELECTOR]
651                }
652            }
653        });
654
655        let trial_state =
656            MetricState::new(&metrics, Fetcher::TrialData(FOO_42_AB_7_TRIAL_FETCHER.clone()), None);
657
658        // foo from values shadows foo selector.
659        assert_eq!(
660            trial_state.evaluate_variable("foo_file", variable!("foo")),
661            MetricValue::Int(42)
662        );
663        // Value shadowing also works in expressions.
664        assert_eq!(
665            trial_state.evaluate_variable("foo_file", variable!("foo_plus_one")),
666            MetricValue::Int(43)
667        );
668        // foo can shadow eval as well as selector.
669        assert_eq!(trial_state.evaluate_variable("a", variable!("foo")), MetricValue::Int(42));
670        // A value that's not there should be "SyntaxError" (e.g. not crash)
671        assert_problem!(
672            trial_state.evaluate_variable("foo_file", variable!("oops_plus_one")),
673            "SyntaxError: Metric 'oops' Not Found in 'foo_file'"
674        );
675        // a::b ignores the "b" in file "a" and uses "a::b" from values.
676        assert_eq!(
677            trial_state.evaluate_variable("foo_file", variable!("ab_plus_one")),
678            MetricValue::Int(8)
679        );
680        // a::c should return Missing, not look up c in file a.
681        assert_problem!(
682            trial_state.evaluate_variable("foo_file", variable!("ac_plus_one")),
683            "SyntaxError: Name a::c not in test values and refers outside the file"
684        );
685    }
686
687    #[fuchsia::test]
688    fn inspect_fetcher_new_works() -> Result<(), Error> {
689        assert!(InspectFetcher::try_from("foo").is_err(), "'foo' isn't valid JSON");
690        assert!(InspectFetcher::try_from(r#"{"a":5}"#).is_err(), "Needed an array");
691        assert!(InspectFetcher::try_from("[]").is_ok(), "A JSON array should have worked");
692        Ok(())
693    }
694
695    #[fuchsia::test]
696    fn test_fetch_with_tree_names() {
697        let cases = &[
698            (
699                "INSPECT:core/*:[name=root]root:foo",
700                vec![MetricValue::String("bar".to_string())],
701                r#"[
702  {
703    "data_source": "Inspect",
704    "metadata": {
705      "name": "root",
706      "component_url": "fuchsia-pkg://fuchsia.com/foo#meta/foo.cm",
707      "timestamp": 6532507441581
708    },
709    "moniker": "core/foo",
710    "payload": {
711      "root": {
712        "foo": "bar"
713      }
714    }
715  },
716  {
717    "data_source": "Inspect",
718    "metadata": {
719      "name": "root",
720      "component_url": "fuchsia-pkg://fuchsia.com/baz#meta/baz.cm",
721      "timestamp": 6532507441581
722    },
723    "moniker": "core/baz",
724    "payload": {
725      "root": {
726        "baz": ""
727      }
728    }
729  }
730]
731"#,
732            ),
733            (
734                "INSPECT:core/*:[name=foo-is-bar]root:foo",
735                vec![MetricValue::String("bar".to_string())],
736                r#"[
737  {
738    "data_source": "Inspect",
739    "metadata": {
740      "name": "foo-is-bar",
741      "component_url": "fuchsia-pkg://fuchsia.com/foo#meta/foo.cm",
742      "timestamp": 6532507441581
743    },
744    "moniker": "core/foo",
745    "payload": {
746      "root": {
747        "foo": "bar"
748      }
749    }
750  },
751  {
752    "data_source": "Inspect",
753    "metadata": {
754      "name": "foo-is-qux",
755      "component_url": "fuchsia-pkg://fuchsia.com/foo#meta/foo.cm",
756      "timestamp": 6532507441581
757    },
758    "moniker": "core/foo",
759    "payload": {
760      "root": {
761        "foo": "qux"
762      }
763    }
764  },
765  {
766    "data_source": "Inspect",
767    "metadata": {
768      "name": "root",
769      "component_url": "fuchsia-pkg://fuchsia.com/baz#meta/baz.cm",
770      "timestamp": 6532507441581
771    },
772    "moniker": "core/baz",
773    "payload": {
774      "root": {
775        "baz": ""
776      }
777    }
778  }
779]
780"#,
781            ),
782        ];
783
784        for (selector, expected, json) in cases {
785            let fetcher = InspectFetcher::try_from(*json).unwrap();
786            let metric = fetcher.fetch_str(selector);
787            assert_eq!(expected, &metric, "component list: {:#?}", fetcher.components);
788        }
789    }
790
791    #[fuchsia::test]
792    fn test_fetch() -> Result<(), Error> {
793        // This tests both the moniker/payload and path/content (old-style) Inspect formats.
794        let json_options = vec![
795            r#"[
796        {"moniker":"asdf/foo/qwer", "metadata": {},
797         "payload":{"root":{"dataInt":5, "child":{"dataFloat":2.3}}}},
798        {"moniker":"zxcv/bar/hjkl", "metadata": {},
799         "payload":{"base":{"dataInt":42, "array":[2,3,4], "yes": true}}},
800        {"moniker":"fail_component", "metadata": {},
801         "payload": ["a", "b"]},
802        {"moniker":"missing_component", "metadata": {},
803         "payload": null}
804        ]"#,
805            r#"[
806        {"moniker":"asdf/foo/qwer", "metadata": {},
807         "payload":{"root":{"dataInt":5, "child":{"dataFloat":2.3}}}},
808        {"moniker":"zxcv/bar/hjkl", "metadata": {},
809         "contents":{"base":{"dataInt":42, "array":[2,3,4], "yes": true}}},
810        {"moniker":"fail_component", "metadata": {},
811         "payload": ["a", "b"]},
812        {"moniker":"missing_component", "metadata": {},
813         "payload": null}
814        ]"#,
815        ];
816
817        for json in json_options.into_iter() {
818            let inspect = InspectFetcher::try_from(json)?;
819            assert_eq!(
820                vec!["Unable to deserialize Inspect contents for fail_component to node hierarchy"],
821                inspect.component_errors.iter().map(|e| format!("{}", e)).collect::<Vec<_>>()
822            );
823            macro_rules! assert_wrong {
824                ($selector:expr, $error:expr) => {
825                    let error = inspect.fetch_str($selector);
826                    assert_eq!(error.len(), 1);
827                    assert_problem!(&error[0], $error);
828                };
829            }
830            assert_wrong!("INSPET:*/foo/*:root:dataInt",
831                "SyntaxError: Bad selector INSPET:*/foo/*:root:dataInt: Invalid selector type \'INSPET\' - must be INSPECT");
832            assert_eq!(
833                inspect.fetch_str("INSPECT:*/foo/*:root:dataInt"),
834                vec![MetricValue::Int(5)]
835            );
836            assert_eq!(
837                inspect.fetch_str("INSPECT:*/foo/*:root/child:dataFloat"),
838                vec![MetricValue::Float(2.3)]
839            );
840            assert_eq!(
841                inspect.fetch_str("INSPECT:zxcv/*/hjk*:base:yes"),
842                vec![MetricValue::Bool(true)]
843            );
844            assert_eq!(inspect.fetch_str("INSPECT:*/foo/*:root.dataInt"), vec![]);
845            assert_wrong!(
846                "INSPECT:*/fo/*:root.dataInt",
847                "Missing: No component found matching selector */fo/*:root.dataInt"
848            );
849
850            assert_eq!(inspect.fetch_str("INSPECT:*/foo/*:root/kid:dataInt"), vec![]);
851            assert_eq!(inspect.fetch_str("INSPECT:*/bar/*:base/array:dataInt"), vec![]);
852            assert_eq!(
853                inspect.fetch_str("INSPECT:*/bar/*:base:array"),
854                vec![MetricValue::Vector(vec![
855                    MetricValue::Int(2),
856                    MetricValue::Int(3),
857                    MetricValue::Int(4)
858                ])]
859            );
860        }
861        Ok(())
862    }
863
864    #[fuchsia::test]
865    fn inspect_ref_empty() -> Result<(), Error> {
866        // Make sure it doesn't crash, can be called multiple times and they both work right.
867        let fetcher1 = InspectFetcher::ref_empty();
868        let fetcher2 = InspectFetcher::ref_empty();
869
870        match fetcher1.try_fetch(&SelectorString::try_from("INSPECT:a:b:c".to_string())?).unwrap()
871            [0]
872        {
873            MetricValue::Problem(Problem::Missing(_)) => {}
874            _ => bail!("Should have Missing'd a valid selector"),
875        }
876        match fetcher2.try_fetch(&SelectorString::try_from("INSPECT:a:b:c".to_string())?).unwrap()
877            [0]
878        {
879            MetricValue::Problem(Problem::Missing(_)) => {}
880            _ => bail!("Should have Missing'd a valid selector"),
881        }
882        Ok(())
883    }
884
885    #[fuchsia::test]
886    fn text_fetcher_works() {
887        let fetcher = TextFetcher::from("abcfoo\ndefgfoo");
888        assert!(fetcher.contains("d*g"));
889        assert!(fetcher.contains("foo"));
890        assert!(!fetcher.contains("food"));
891        // Make sure ref_empty() doesn't crash and can be used multiple times.
892        let fetcher1 = TextFetcher::ref_empty();
893        let fetcher2 = TextFetcher::ref_empty();
894        assert!(!fetcher1.contains("a"));
895        assert!(!fetcher2.contains("a"));
896    }
897
898    #[fuchsia::test]
899    fn test_selector_string_parse() -> Result<(), Error> {
900        // Test correct shape of SelectorString and verify no errors on parse for valid selector.
901        let full_selector = "INSPECT:bad_component:root:bar".to_string();
902        let selector_type = SelectorType::Inspect;
903        let body = "bad_component:root:bar".to_string();
904        let parsed_selector = selectors::parse_selector::<VerboseError>(&body)?;
905
906        assert_eq!(
907            SelectorString::try_from("INSPECT:bad_component:root:bar".to_string())?,
908            SelectorString { full_selector, selector_type, body, parsed_selector }
909        );
910
911        // Test that a selector that does not follow the correct syntax results in parse error.
912        assert_eq!(
913            format!(
914                "{:?}",
915                SelectorString::try_from("INSPECT:not a selector".to_string()).err().unwrap()
916            ),
917            "Failed to parse the input. Error: 0: at line 1, in Tag:\nnot a selector\n   ^\n\n"
918        );
919
920        // Test that an invalid selector results in a parse error.
921        assert_eq!(
922            format!(
923                "{:?}",
924            SelectorString::try_from("INSPECT:*/foo/*:root:data:Int".to_string()).err().unwrap()
925            ),
926            "Failed to parse the input. Error: 0: at line 1, in Eof:\n*/foo/*:root:data:Int\n                 ^\n\n"
927        );
928
929        Ok(())
930    }
931
932    // KeyValueFetcher is tested in metrics::test::annotations_work()
933}