1# Copyright 2024 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import pathlib 6from typing import Dict, List, Type 7 8from pyfakefs import fake_filesystem_unittest 9 10import crossbench.path 11from crossbench import plt 12from crossbench.benchmarks.loading.loadline_presets import \ 13 LoadLineTabletBenchmark 14from crossbench.cli.config.probe import ProbeListConfig 15from crossbench.helper import ChangeCWD 16from crossbench.helper.path_finder import default_chromium_candidates 17from crossbench.probes.all import GENERAL_PURPOSE_PROBES 18from crossbench.probes.probe import Probe 19from tests import test_helper 20 21PROBE_LOOKUP: Dict[str, Type[Probe]] = { 22 probe_cls.NAME: probe_cls for probe_cls in GENERAL_PURPOSE_PROBES 23} 24 25 26class ProbeConfigTestCase(fake_filesystem_unittest.TestCase): 27 """Parse all example probe configs in config/probe and config/doc/probe 28 29 More detailed tests should go into dedicated probe/test_{PROBE_NAME}.py 30 files. 31 """ 32 33 def setUp(self) -> None: 34 self.real_config_dir = test_helper.config_dir() 35 super().setUp() 36 self.setUpPyfakefs(modules_to_reload=[crossbench.path]) 37 if test_helper.is_google_env(): 38 self.fs.add_real_directory("/build/cas") 39 self.set_up_required_paths() 40 41 def set_up_required_paths(self): 42 chrome_dir = default_chromium_candidates(plt.PLATFORM)[0] 43 self.fs.create_dir(chrome_dir / "v8") 44 self.fs.create_dir(chrome_dir / "chrome") 45 self.fs.create_dir(chrome_dir / ".git") 46 47 perfetto_tools = chrome_dir / "third_party/perfetto/tools" 48 self.fs.create_file(perfetto_tools / "traceconv") 49 self.fs.create_file(perfetto_tools / "trace_processor") 50 51 def _test_parse_config_dir(self, 52 real_config_dir: pathlib.Path) -> List[Probe]: 53 probes = [] 54 self.fs.add_real_directory( 55 real_config_dir, lazy_read=not test_helper.is_google_env()) 56 for probe_config in real_config_dir.glob("**/*.config.hjson"): 57 with ChangeCWD(probe_config.parent): 58 probes += self._parse_config(probe_config) 59 return probes 60 61 def _parse_config(self, config_file: pathlib.Path) -> List[Probe]: 62 probe_name = config_file.parent.name 63 if probe_name not in PROBE_LOOKUP: 64 probe_name = config_file.name.split(".")[0] 65 probe_cls = PROBE_LOOKUP[probe_name] 66 67 probes = ProbeListConfig.parse_path(config_file).probes 68 self.assertTrue(probes) 69 self.assertTrue( 70 any(map(lambda probe: isinstance(probe, probe_cls), probes))) 71 for probe in probes: 72 self.assertFalse(probe.is_attached) 73 return probes 74 75 def test_parse_example_configs(self): 76 probe_config_presets = self.real_config_dir / "probe" 77 probes = self._test_parse_config_dir(probe_config_presets) 78 self.assertTrue(probes) 79 80 def test_parse_doc_configs(self): 81 probe_config_doc = self.real_config_dir / "doc/probe" 82 probes = self._test_parse_config_dir(probe_config_doc) 83 self.assertTrue(probes) 84 85 def test_parse_loadline_configs(self): 86 probe_config = LoadLineTabletBenchmark.default_probe_config_path() 87 self.fs.add_real_file(probe_config) 88 probes = ProbeListConfig.parse_path(probe_config).probes 89 self.assertTrue(probes) 90 91 92if __name__ == "__main__": 93 test_helper.run_pytest(__file__) 94