1use 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#[derive(Clone, Debug)]
26pub enum Fetcher<'a> {
27 FileData(FileDataFetcher<'a>),
28 TrialData(TrialDataFetcher<'a>),
29}
30
31#[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 SelectorType::Inspect => MetricValue::Vector(self.inspect.fetch(selector)),
88 }
89 }
90
91 pub fn errors(&self) -> Vec<String> {
93 self.inspect.component_errors.iter().map(|e| format!("{e}")).collect()
94 }
95}
96
97#[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 {name} not overridden in test")),
151 }
152 }
153
154 pub(crate) fn has_entry(&self, name: &str) -> bool {
155 self.values.contains_key(name)
156 }
157}
158
159#[derive(Deserialize, Debug, Clone, PartialEq, Serialize)]
161pub enum SelectorType {
162 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 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 '{key}' not found in annotations")),
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 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 DiagnosticsHierarchy::new_root()
354 }
355 raw_contents => serde_json::from_value(raw_contents).with_context(|| {
356 format!(
357 "Unable to deserialize Inspect contents for {moniker} to node hierarchy",
358 )
359 })?,
360 };
361 Ok(ComponentInspectInfo { moniker, processed_data, tree_name })
362 });
363
364 let mut component_errors = vec![];
365 let components = components
366 .filter_map(|v| match v {
367 Ok(component) => Some(component),
368 Err(e) => {
369 component_errors.push(e);
370 None
371 }
372 })
373 .collect::<Vec<_>>();
374 Ok(Self { components, component_errors })
375 }
376}
377
378static EMPTY_INSPECT_FETCHER: LazyLock<InspectFetcher> = LazyLock::new(InspectFetcher::default);
379
380impl InspectFetcher {
381 pub fn ref_empty() -> &'static Self {
382 &EMPTY_INSPECT_FETCHER
383 }
384
385 fn try_fetch(&self, selector_string: &SelectorString) -> Result<Vec<MetricValue>, Error> {
386 let mut intermediate_results = Vec::new();
387 let mut found_component = false;
388 for component in &self.components {
389 if !component.matches_selector(&selector_string.parsed_selector) {
390 continue;
391 }
392 found_component = true;
393 let selector = selector_string.parsed_selector.clone();
394 intermediate_results.push(diagnostics_hierarchy::select_from_hierarchy(
395 &component.processed_data,
396 &selector,
397 )?);
398 }
399
400 if !found_component {
401 return Ok(vec![missing(format!(
402 "No component found matching selector {}",
403 selector_string.body,
404 ))]);
405 }
406
407 let mut result = vec![];
408 for r in intermediate_results {
409 match r {
410 SelectResult::Properties(p) => {
411 result.extend(p.into_iter().cloned().map(MetricValue::from))
412 }
413 SelectResult::Nodes(n) => {
414 for node in n {
415 for _ in &node.children {
416 result.push(MetricValue::Node);
417 }
418
419 for prop in &node.properties {
420 result.push(MetricValue::from(prop.clone()));
421 }
422 }
423 }
424 }
425 }
426
427 Ok(result)
428 }
429
430 pub fn fetch(&self, selector: &SelectorString) -> Vec<MetricValue> {
431 match self.try_fetch(selector) {
432 Ok(v) => v,
433 Err(e) => vec![syntax_error(format!("Fetch {selector:?} -> {e}"))],
434 }
435 }
436
437 #[cfg(test)]
438 fn fetch_str(&self, selector_str: &str) -> Vec<MetricValue> {
439 match SelectorString::try_from(selector_str.to_owned()) {
440 Ok(selector) => self.fetch(&selector),
441 Err(e) => vec![syntax_error(format!("Bad selector {selector_str}: {e}"))],
442 }
443 }
444}
445
446#[cfg(test)]
447mod test {
448 use super::*;
449 use crate::metrics::variable::VariableName;
450 use crate::metrics::{Metric, MetricState, Problem, ValueSource};
451 use crate::{assert_problem, make_metrics};
452 use serde_json::Value as JsonValue;
453
454 static LOCAL_M: LazyLock<HashMap<String, JsonValue>> = LazyLock::new(|| {
455 let mut m = HashMap::new();
456 m.insert("foo".to_owned(), JsonValue::from(42));
457 m.insert("a::b".to_owned(), JsonValue::from(7));
458 m
459 });
460 static FOO_42_AB_7_TRIAL_FETCHER: LazyLock<TrialDataFetcher<'static>> =
461 LazyLock::new(|| TrialDataFetcher::new(&LOCAL_M));
462 static LOCAL_F: LazyLock<Vec<DiagnosticData>> = LazyLock::new(|| {
463 let s = r#"[
464 {
465 "data_source": "Inspect",
466 "moniker": "bar",
467 "metadata": {},
468 "payload": { "root": { "bar": 99 }}
469 },
470 {
471 "data_source": "Inspect",
472 "moniker": "bar2",
473 "metadata": {},
474 "payload": { "root": { "bar": 90 }}
475 }
476
477 ]"#;
478 vec![DiagnosticData::new("i".to_string(), Source::Inspect, s.to_string()).unwrap()]
479 });
480 static BAR_99_FILE_FETCHER: LazyLock<FileDataFetcher<'static>> =
481 LazyLock::new(|| FileDataFetcher::new(&LOCAL_F));
482 static BAR_SELECTOR: LazyLock<SelectorString> =
483 LazyLock::new(|| SelectorString::try_from("INSPECT:bar:root:bar".to_owned()).unwrap());
484 static NEW_BAR_SELECTOR: LazyLock<SelectorString> =
485 LazyLock::new(|| SelectorString::try_from("INSPECT:bar2:root:bar".to_owned()).unwrap());
486 static BAD_COMPONENT_SELECTOR: LazyLock<SelectorString> = LazyLock::new(|| {
487 SelectorString::try_from("INSPECT:bad_component:root:bar".to_owned()).unwrap()
488 });
489 static WRONG_SELECTOR: LazyLock<SelectorString> =
490 LazyLock::new(|| SelectorString::try_from("INSPECT:bar:root:oops".to_owned()).unwrap());
491 static LOCAL_DUPLICATES_F: LazyLock<Vec<DiagnosticData>> = LazyLock::new(|| {
492 let s = r#"[
493 {
494 "data_source": "Inspect",
495 "moniker": "bootstrap/foo",
496 "metadata": {},
497 "payload": null
498 },
499 {
500 "data_source": "Inspect",
501 "moniker": "bootstrap/foo",
502 "metadata": {},
503 "payload": {"root": {"bar": 10}}
504 }
505 ]"#;
506 vec![DiagnosticData::new("i".to_string(), Source::Inspect, s.to_string()).unwrap()]
507 });
508 static LOCAL_DUPLICATES_FETCHER: LazyLock<FileDataFetcher<'static>> =
509 LazyLock::new(|| FileDataFetcher::new(&LOCAL_DUPLICATES_F));
510 static DUPLICATE_SELECTOR: LazyLock<SelectorString> = LazyLock::new(|| {
511 SelectorString::try_from("INSPECT:bootstrap/foo:root:bar".to_owned()).unwrap()
512 });
513
514 macro_rules! variable {
515 ($name:expr) => {
516 &VariableName::new($name.to_string())
517 };
518 }
519
520 #[fuchsia::test]
521 fn test_file_fetch() {
522 assert_eq!(
523 BAR_99_FILE_FETCHER.fetch(&BAR_SELECTOR),
524 MetricValue::Vector(vec![MetricValue::Int(99)])
525 );
526 assert_eq!(BAR_99_FILE_FETCHER.fetch(&WRONG_SELECTOR), MetricValue::Vector(vec![]),);
527 }
528
529 #[fuchsia::test]
530 fn test_duplicate_file_fetch() {
531 assert_eq!(
532 LOCAL_DUPLICATES_FETCHER.fetch(&DUPLICATE_SELECTOR),
533 MetricValue::Vector(vec![MetricValue::Int(10)])
534 );
535 }
536
537 #[fuchsia::test]
538 fn test_trial_fetch() {
539 assert!(FOO_42_AB_7_TRIAL_FETCHER.has_entry("foo"));
540 assert!(FOO_42_AB_7_TRIAL_FETCHER.has_entry("a::b"));
541 assert!(!FOO_42_AB_7_TRIAL_FETCHER.has_entry("a:b"));
542 assert!(!FOO_42_AB_7_TRIAL_FETCHER.has_entry("oops"));
543 assert_eq!(FOO_42_AB_7_TRIAL_FETCHER.fetch("foo"), MetricValue::Int(42));
544 assert_problem!(
545 FOO_42_AB_7_TRIAL_FETCHER.fetch("oops"),
546 "SyntaxError: Value oops not overridden in test"
547 );
548 }
549
550 #[fuchsia::test]
551 fn test_eval_with_file() {
552 let metrics = make_metrics!({
553 "bar_file":{
554 eval: {
555 "bar_plus_one": "bar + 1",
556 "oops_plus_one": "oops + 1"
557 }
558 select: {
559 "bar": [BAR_SELECTOR],
560 "wrong_or_bar": [WRONG_SELECTOR, BAR_SELECTOR],
561 "wrong_or_wrong": [WRONG_SELECTOR, WRONG_SELECTOR],
562 "wrong_or_new_bar_or_bar": [WRONG_SELECTOR, NEW_BAR_SELECTOR, BAR_SELECTOR],
563 "bad_component_or_bar": [BAD_COMPONENT_SELECTOR, BAR_SELECTOR]
564 }
565 },
566 "other_file":{
567 eval: {
568 "bar": "42"
569 }
570 }
571 });
572
573 let file_state =
574 MetricState::new(&metrics, Fetcher::FileData(BAR_99_FILE_FETCHER.clone()), None);
575 assert_eq!(
576 file_state.evaluate_variable("bar_file", variable!("bar_plus_one")),
577 MetricValue::Int(100)
578 );
579 assert_problem!(
580 file_state.evaluate_variable("bar_file", variable!("oops_plus_one")),
581 "SyntaxError: Metric 'oops' Not Found in 'bar_file'"
582 );
583 assert_eq!(
584 file_state.evaluate_variable("bar_file", variable!("bar")),
585 MetricValue::Vector(vec![MetricValue::Int(99)])
586 );
587 assert_eq!(
588 file_state.evaluate_variable("other_file", variable!("bar")),
589 MetricValue::Int(42)
590 );
591 assert_eq!(
592 file_state.evaluate_variable("other_file", variable!("other_file::bar")),
593 MetricValue::Int(42)
594 );
595 assert_eq!(
596 file_state.evaluate_variable("other_file", variable!("bar_file::bar")),
597 MetricValue::Vector(vec![MetricValue::Int(99)])
598 );
599 assert_eq!(
600 file_state.evaluate_variable("bar_file", variable!("bar")),
601 file_state.evaluate_variable("bar_file", variable!("wrong_or_bar")),
602 );
603 assert_eq!(
604 file_state.evaluate_variable("bar_file", variable!("wrong_or_wrong")),
605 MetricValue::Vector(vec![]),
606 );
607 assert_eq!(
608 file_state.evaluate_variable("bar_file", variable!("wrong_or_new_bar_or_bar")),
609 MetricValue::Vector(vec![MetricValue::Int(90)])
610 );
611 assert_eq!(
612 file_state.evaluate_variable("bar_file", variable!("bad_component_or_bar")),
613 MetricValue::Vector(vec![MetricValue::Int(99)])
614 );
615 assert_problem!(
616 file_state.evaluate_variable("other_file", variable!("bar_plus_one")),
617 "SyntaxError: Metric 'bar_plus_one' Not Found in 'other_file'"
618 );
619 assert_problem!(
620 file_state.evaluate_variable("missing_file", variable!("bar_plus_one")),
621 "SyntaxError: Bad namespace 'missing_file'"
622 );
623 assert_problem!(
624 file_state.evaluate_variable("bar_file", variable!("other_file::bar_plus_one")),
625 "SyntaxError: Metric 'bar_plus_one' Not Found in 'other_file'"
626 );
627 }
628
629 #[fuchsia::test]
630 fn test_eval_with_trial() {
631 let metrics = make_metrics!({
634 "a":{
635 eval: {
636 "b": "2",
637 "c": "3",
638 "foo": "4",
639 }
640 },
641 "foo_file":{
642 eval: {
643 "foo_plus_one": "foo + 1",
644 "oops_plus_one": "oops + 1",
645 "ab_plus_one": "a::b + 1",
646 "ac_plus_one": "a::c + 1"
647 }
648 select: {
649 "foo": [BAR_SELECTOR]
650 }
651 }
652 });
653
654 let trial_state =
655 MetricState::new(&metrics, Fetcher::TrialData(FOO_42_AB_7_TRIAL_FETCHER.clone()), None);
656
657 assert_eq!(
659 trial_state.evaluate_variable("foo_file", variable!("foo")),
660 MetricValue::Int(42)
661 );
662 assert_eq!(
664 trial_state.evaluate_variable("foo_file", variable!("foo_plus_one")),
665 MetricValue::Int(43)
666 );
667 assert_eq!(trial_state.evaluate_variable("a", variable!("foo")), MetricValue::Int(42));
669 assert_problem!(
671 trial_state.evaluate_variable("foo_file", variable!("oops_plus_one")),
672 "SyntaxError: Metric 'oops' Not Found in 'foo_file'"
673 );
674 assert_eq!(
676 trial_state.evaluate_variable("foo_file", variable!("ab_plus_one")),
677 MetricValue::Int(8)
678 );
679 assert_problem!(
681 trial_state.evaluate_variable("foo_file", variable!("ac_plus_one")),
682 "SyntaxError: Name a::c not in test values and refers outside the file"
683 );
684 }
685
686 #[fuchsia::test]
687 fn inspect_fetcher_new_works() -> Result<(), Error> {
688 assert!(InspectFetcher::try_from("foo").is_err(), "'foo' isn't valid JSON");
689 assert!(InspectFetcher::try_from(r#"{"a":5}"#).is_err(), "Needed an array");
690 assert!(InspectFetcher::try_from("[]").is_ok(), "A JSON array should have worked");
691 Ok(())
692 }
693
694 #[fuchsia::test]
695 fn test_fetch_with_tree_names() {
696 let cases = &[
697 (
698 "INSPECT:core/*:[name=root]root:foo",
699 vec![MetricValue::String("bar".to_string())],
700 r#"[
701 {
702 "data_source": "Inspect",
703 "metadata": {
704 "name": "root",
705 "component_url": "fuchsia-pkg://fuchsia.com/foo#meta/foo.cm",
706 "timestamp": 6532507441581
707 },
708 "moniker": "core/foo",
709 "payload": {
710 "root": {
711 "foo": "bar"
712 }
713 }
714 },
715 {
716 "data_source": "Inspect",
717 "metadata": {
718 "name": "root",
719 "component_url": "fuchsia-pkg://fuchsia.com/baz#meta/baz.cm",
720 "timestamp": 6532507441581
721 },
722 "moniker": "core/baz",
723 "payload": {
724 "root": {
725 "baz": ""
726 }
727 }
728 }
729]
730"#,
731 ),
732 (
733 "INSPECT:core/*:[name=foo-is-bar]root:foo",
734 vec![MetricValue::String("bar".to_string())],
735 r#"[
736 {
737 "data_source": "Inspect",
738 "metadata": {
739 "name": "foo-is-bar",
740 "component_url": "fuchsia-pkg://fuchsia.com/foo#meta/foo.cm",
741 "timestamp": 6532507441581
742 },
743 "moniker": "core/foo",
744 "payload": {
745 "root": {
746 "foo": "bar"
747 }
748 }
749 },
750 {
751 "data_source": "Inspect",
752 "metadata": {
753 "name": "foo-is-qux",
754 "component_url": "fuchsia-pkg://fuchsia.com/foo#meta/foo.cm",
755 "timestamp": 6532507441581
756 },
757 "moniker": "core/foo",
758 "payload": {
759 "root": {
760 "foo": "qux"
761 }
762 }
763 },
764 {
765 "data_source": "Inspect",
766 "metadata": {
767 "name": "root",
768 "component_url": "fuchsia-pkg://fuchsia.com/baz#meta/baz.cm",
769 "timestamp": 6532507441581
770 },
771 "moniker": "core/baz",
772 "payload": {
773 "root": {
774 "baz": ""
775 }
776 }
777 }
778]
779"#,
780 ),
781 ];
782
783 for (selector, expected, json) in cases {
784 let fetcher = InspectFetcher::try_from(*json).unwrap();
785 let metric = fetcher.fetch_str(selector);
786 assert_eq!(expected, &metric, "component list: {:#?}", fetcher.components);
787 }
788 }
789
790 #[fuchsia::test]
791 fn test_fetch() -> Result<(), Error> {
792 let json_options = vec![
794 r#"[
795 {"moniker":"asdf/foo/qwer", "metadata": {},
796 "payload":{"root":{"dataInt":5, "child":{"dataFloat":2.3}}}},
797 {"moniker":"zxcv/bar/hjkl", "metadata": {},
798 "payload":{"base":{"dataInt":42, "array":[2,3,4], "yes": true}}},
799 {"moniker":"fail_component", "metadata": {},
800 "payload": ["a", "b"]},
801 {"moniker":"missing_component", "metadata": {},
802 "payload": null}
803 ]"#,
804 r#"[
805 {"moniker":"asdf/foo/qwer", "metadata": {},
806 "payload":{"root":{"dataInt":5, "child":{"dataFloat":2.3}}}},
807 {"moniker":"zxcv/bar/hjkl", "metadata": {},
808 "contents":{"base":{"dataInt":42, "array":[2,3,4], "yes": true}}},
809 {"moniker":"fail_component", "metadata": {},
810 "payload": ["a", "b"]},
811 {"moniker":"missing_component", "metadata": {},
812 "payload": null}
813 ]"#,
814 ];
815
816 for json in json_options.into_iter() {
817 let inspect = InspectFetcher::try_from(json)?;
818 assert_eq!(
819 vec!["Unable to deserialize Inspect contents for fail_component to node hierarchy"],
820 inspect.component_errors.iter().map(|e| format!("{e}")).collect::<Vec<_>>()
821 );
822 macro_rules! assert_wrong {
823 ($selector:expr, $error:expr) => {
824 let error = inspect.fetch_str($selector);
825 assert_eq!(error.len(), 1);
826 assert_problem!(&error[0], $error);
827 };
828 }
829 assert_wrong!("INSPET:*/foo/*:root:dataInt",
830 "SyntaxError: Bad selector INSPET:*/foo/*:root:dataInt: Invalid selector type \'INSPET\' - must be INSPECT");
831 assert_eq!(
832 inspect.fetch_str("INSPECT:*/foo/*:root:dataInt"),
833 vec![MetricValue::Int(5)]
834 );
835 assert_eq!(
836 inspect.fetch_str("INSPECT:*/foo/*:root/child:dataFloat"),
837 vec![MetricValue::Float(2.3)]
838 );
839 assert_eq!(
840 inspect.fetch_str("INSPECT:zxcv/*/hjk*:base:yes"),
841 vec![MetricValue::Bool(true)]
842 );
843 assert_eq!(inspect.fetch_str("INSPECT:*/foo/*:root.dataInt"), vec![]);
844 assert_wrong!(
845 "INSPECT:*/fo/*:root.dataInt",
846 "Missing: No component found matching selector */fo/*:root.dataInt"
847 );
848
849 assert_eq!(inspect.fetch_str("INSPECT:*/foo/*:root/kid:dataInt"), vec![]);
850 assert_eq!(inspect.fetch_str("INSPECT:*/bar/*:base/array:dataInt"), vec![]);
851 assert_eq!(
852 inspect.fetch_str("INSPECT:*/bar/*:base:array"),
853 vec![MetricValue::Vector(vec![
854 MetricValue::Int(2),
855 MetricValue::Int(3),
856 MetricValue::Int(4)
857 ])]
858 );
859 }
860 Ok(())
861 }
862
863 #[fuchsia::test]
864 fn inspect_ref_empty() -> Result<(), Error> {
865 let fetcher1 = InspectFetcher::ref_empty();
867 let fetcher2 = InspectFetcher::ref_empty();
868
869 match fetcher1.try_fetch(&SelectorString::try_from("INSPECT:a:b:c".to_string())?).unwrap()
870 [0]
871 {
872 MetricValue::Problem(Problem::Missing(_)) => {}
873 _ => bail!("Should have Missing'd a valid selector"),
874 }
875 match fetcher2.try_fetch(&SelectorString::try_from("INSPECT:a:b:c".to_string())?).unwrap()
876 [0]
877 {
878 MetricValue::Problem(Problem::Missing(_)) => {}
879 _ => bail!("Should have Missing'd a valid selector"),
880 }
881 Ok(())
882 }
883
884 #[fuchsia::test]
885 fn text_fetcher_works() {
886 let fetcher = TextFetcher::from("abcfoo\ndefgfoo");
887 assert!(fetcher.contains("d*g"));
888 assert!(fetcher.contains("foo"));
889 assert!(!fetcher.contains("food"));
890 let fetcher1 = TextFetcher::ref_empty();
892 let fetcher2 = TextFetcher::ref_empty();
893 assert!(!fetcher1.contains("a"));
894 assert!(!fetcher2.contains("a"));
895 }
896
897 #[fuchsia::test]
898 fn test_selector_string_parse() -> Result<(), Error> {
899 let full_selector = "INSPECT:bad_component:root:bar".to_string();
901 let selector_type = SelectorType::Inspect;
902 let body = "bad_component:root:bar".to_string();
903 let parsed_selector = selectors::parse_selector::<VerboseError>(&body)?;
904
905 assert_eq!(
906 SelectorString::try_from("INSPECT:bad_component:root:bar".to_string())?,
907 SelectorString { full_selector, selector_type, body, parsed_selector }
908 );
909
910 assert_eq!(
912 format!(
913 "{:?}",
914 SelectorString::try_from("INSPECT:not a selector".to_string()).err().unwrap()
915 ),
916 "Failed to parse the input. Error: 0: at line 1, in Tag:\nnot a selector\n ^\n\n"
917 );
918
919 assert_eq!(
921 format!(
922 "{:?}",
923 SelectorString::try_from("INSPECT:*/foo/*:root:data:Int".to_string()).err().unwrap()
924 ),
925 "Failed to parse the input. Error: 0: at line 1, in Eof:\n*/foo/*:root:data:Int\n ^\n\n"
926 );
927
928 Ok(())
929 }
930
931 }