1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 from perfdocs
.gatherer
import Gatherer
11 from perfdocs
.logger
import PerfDocLogger
12 from perfdocs
.utils
import read_file
, read_yaml
14 logger
= PerfDocLogger()
17 Schema for the config.yml file.
18 Expecting a YAML file with a format such as this:
21 manifest: testing/raptor/raptor/raptor.toml
25 description: "Desktop tests."
27 raptor-tp6: "Raptor TP6 tests."
29 description: "Mobile tests"
31 description: "Benchmark tests."
33 wasm: "All wasm tests."
42 "aliases": {"type": "array", "items": {"type": "string"}},
43 "description": {"type": "string"},
44 "matcher": {"type": "string"},
46 "required": ["description", "aliases"],
52 "name": {"type": "string"},
53 "manifest": {"type": "string"},
54 "static-only": {"type": "boolean"},
55 "metrics": {"$ref": "#/definitions/metrics_schema"},
65 "test_name": {"type": "string"},
68 "description": {"type": "string"},
69 "owner": {"type": "string"},
71 "required": ["description"],
76 "required": ["name", "manifest", "static-only", "suites"],
80 class Verifier(object):
82 Verifier is used for validating the perfdocs folders/tree. In the future,
83 the generator will make use of this class to obtain a validated set of
84 descriptions that can be used to build up a document.
87 def __init__(self
, workspace_dir
, taskgraph
=None):
89 Initialize the Verifier.
91 :param str workspace_dir: Path to the top-level checkout directory.
93 self
.workspace_dir
= workspace_dir
94 self
.metrics_info
= {}
95 self
._gatherer
= Gatherer(workspace_dir
, taskgraph
)
96 self
._compiled
_matchers
= {}
98 def _is_yaml_test_match(
99 self
, target_test_name
, test_name
, suite
="", global_descriptions
={}
101 """Determine if a target name (from a YAML) matches with a test."""
102 tb
= os
.path
.basename(target_test_name
)
103 tb
= re
.sub(r
"\..*", "", tb
)
105 # Found an exact match for the test_name
108 # Found a 'fuzzy' match for the test_name
109 # i.e. 'wasm' could exist for all raptor wasm tests
110 global_descriptions
.setdefault(suite
, []).append(test_name
)
113 def _validate_desc_yaml_direction(
114 self
, suite
, framework_info
, yaml_content
, global_descriptions
116 """Validate the descriptions in the YAML.
118 This validation ensures that all tests defined in the YAML exist in the test
119 harness. Failures here suggest that there's a typo in the YAML or that
122 ytests
= yaml_content
["suites"][suite
]
123 global_descriptions
[suite
] = []
124 if not ytests
.get("tests"):
125 # It's possible a suite entry has no tests
128 # Suite found - now check if any tests in YAML
129 # definitions don't exist
130 ytests
= ytests
["tests"]
131 for test_name
in ytests
:
133 for t
in framework_info
["test_list"][suite
]:
134 if self
._is
_yaml
_test
_match
(
135 t
, test_name
, suite
=suite
, global_descriptions
=global_descriptions
141 "Could not find an existing test for {} - bad test name?".format(
144 framework_info
["yml_path"],
148 def _validate_desc_harness_direction(
149 self
, suite
, test_list
, yaml_content
, global_descriptions
151 """Validate that the tests have a description in the YAML.
153 This stage of validation ensures that all the tests have some
154 form of description, or that global descriptions are available.
155 Failures here suggest a new test was added, or the config.yml
158 # If only a description is provided for the suite, assume
159 # that this is a suite-wide description and don't check for
161 stests
= yaml_content
["suites"][suite
].get("tests", None)
167 test_to_manifest
= {}
168 for test_name
, test_info
in test_list
.items():
169 manifest_path
= test_info
.get("path", test_info
.get("manifest", ""))
170 tb
= os
.path
.basename(manifest_path
)
171 tb
= re
.sub(r
"\..*", "", tb
)
173 stests
.get(tb
, None) is not None
174 or stests
.get(test_name
, None) is not None
176 # Test description exists, continue with the next test
179 test_to_manifest
[test_name
] = manifest_path
180 missing_tests
.append(test_name
)
182 # Check if global test descriptions exist (i.e.
183 # ones that cover all of tp6) for the missing tests
185 for mt
in missing_tests
:
187 for test_name
in global_descriptions
[suite
]:
188 # Global test exists for this missing test
189 if mt
.startswith(test_name
):
196 new_mtests
.append(mt
)
199 # Output an error for each manifest with a missing
201 for test_name
in new_mtests
:
203 "Could not find a test description for {}".format(test_name
),
204 test_to_manifest
[test_name
],
207 def _match_metrics(self
, target_metric_name
, target_metric_info
, measured_metrics
):
208 """Find all metrics that match the given information.
210 It either checks for the metric through a direct equality check, and if
211 a regex matcher was provided, we will use that afterwards.
213 verified_metrics
= []
215 metric_names
= target_metric_info
["aliases"] + [target_metric_name
]
216 for measured_metric
in measured_metrics
:
217 if measured_metric
in metric_names
:
218 verified_metrics
.append(measured_metric
)
220 if target_metric_info
.get("matcher", ""):
221 # Compile the regex separately to capture issues in the regex
223 matcher
= self
._compiled
_matchers
.get(target_metric_name
, None)
225 matcher
= re
.compile(target_metric_info
.get("matcher"))
226 self
._compiled
_matchers
[target_metric_name
] = matcher
228 # Search the measured metrics
229 for measured_metric
in measured_metrics
:
230 if matcher
.search(measured_metric
):
231 verified_metrics
.append(measured_metric
)
233 return verified_metrics
235 def _validate_metrics_yaml_direction(
236 self
, suite
, framework_info
, yaml_content
, global_metrics
238 """Validate the metric descriptions in the YAML.
240 This direction (`yaml_direction`) checks that the YAML definitions exist in
241 the test harness as real metrics. Failures here suggest that a metric
242 changed name, is missing an alias, is misnamed, duplicated, or was removed.
244 for global_metric_name
, global_metric_info
in global_metrics
["global"].items():
245 for test
, test_info
in framework_info
["test_list"][suite
].items():
246 verified_metrics
= self
._match
_metrics
(
247 global_metric_name
, global_metric_info
, test_info
.get("metrics", [])
249 if len(verified_metrics
) == 0:
252 if global_metric_info
.get("verified", False):
253 # We already verified this global metric, but add any
254 # extra verified metrics here
255 global_metrics
["verified"].extend(verified_metrics
)
257 global_metric_info
["verified"] = True
258 global_metrics
["yaml-verified"].extend(
259 [global_metric_name
] + global_metric_info
["aliases"]
261 global_metrics
["verified"].extend(
263 + global_metric_info
["aliases"]
267 global_metric_info
.setdefault("location", {}).setdefault(
271 def _validate_metrics_harness_direction(
272 self
, suite
, test_list
, yaml_content
, global_metrics
274 """Validate that metrics in the harness are documented."""
275 # Gather all the metrics being measured
276 all_measured_metrics
= {}
277 for test_name
, test_info
in test_list
.items():
278 metrics
= test_info
.get("metrics", [])
279 for metric
in metrics
:
280 all_measured_metrics
.setdefault(metric
, []).append(test_name
)
282 if len(all_measured_metrics
) == 0:
283 # There are no metrics measured by this suite
286 for metric
, tests
in all_measured_metrics
.items():
287 if metric
not in global_metrics
["verified"]:
288 # Log a warning in all files that have this metric
291 "Missing description for the metric `{}` in test `{}`".format(
295 "path", test_list
[test
].get("manifest", "")
299 def validate_descriptions(self
, framework_info
):
301 Cross-validate the tests found in the manifests and the YAML
302 test definitions. This function doesn't return a valid flag. Instead,
303 the StructDocLogger.VALIDATION_LOG is used to determine validity.
305 The validation proceeds as follows:
306 1. Check that all tests/suites in the YAML exist in the manifests.
307 - At the same time, build a list of global descriptions which
308 define descriptions for groupings of tests.
309 2. Check that all tests/suites found in the manifests exist in the YAML.
310 - For missing tests, check if a global description for them exists.
312 As the validation is completed, errors are output into the validation log
313 for any issues that are found.
315 The same is done for the metrics field expect it also has regex matching,
316 and the definitions cannot be duplicated in a single harness. We make use
317 of two `*verified` fields to simplify the two stages/directions, and checking
320 :param dict framework_info: Contains information about the framework. See
321 `Gatherer.get_test_list` for information about its structure.
323 yaml_content
= framework_info
["yml_content"]
325 # Check for any bad test/suite names in the yaml config file
326 # TODO: Combine global settings into a single dictionary
327 global_descriptions
= {}
329 "global": yaml_content
.get("metrics", {}),
333 for suite
, ytests
in yaml_content
["suites"].items():
334 # Find the suite, then check against the tests within it
335 if framework_info
["test_list"].get(suite
, None) is None:
337 "Could not find an existing suite for {} - bad suite name?".format(
340 framework_info
["yml_path"],
344 # Validate descriptions
345 self
._validate
_desc
_yaml
_direction
(
346 suite
, framework_info
, yaml_content
, global_descriptions
350 self
._validate
_metrics
_yaml
_direction
(
351 suite
, framework_info
, yaml_content
, global_metrics
354 # The suite and test levels were properly checked, but we can only
355 # check the global level after all suites were checked. If the metric
356 # isn't in the verified
357 for global_metric_name
, _
in global_metrics
["global"].items():
358 if global_metric_name
not in global_metrics
["verified"]:
361 "Cannot find documented metric `{}` "
362 "being used in the specified harness `{}`."
363 ).format(global_metric_name
, yaml_content
["name"]),
364 framework_info
["yml_path"],
367 # Check for duplicate metrics/aliases in the verified metrics
368 unique_metrics
= set()
370 for metric
in global_metrics
["yaml-verified"]:
372 metric
in unique_metrics
or unique_metrics
.add(metric
)
373 ) and metric
not in warned
:
375 "Duplicate definitions found for `{}`.".format(metric
),
376 framework_info
["yml_path"],
380 # Check for duplicate metrics in the global level
381 unique_metrics
= set()
383 for metric
, metric_info
in global_metrics
["global"].items():
385 metric
in unique_metrics
or unique_metrics
.add(metric
)
386 ) and metric
not in warned
:
388 "Duplicate definitions found for `{}`.".format(metric
),
389 framework_info
["yml_path"],
391 for alias
in metric_info
.get("aliases", []):
392 unique_metrics
.add(alias
)
396 # Check for any missing tests/suites
397 for suite
, test_list
in framework_info
["test_list"].items():
398 if not yaml_content
["suites"].get(suite
):
399 # Description doesn't exist for the suite
401 "Missing suite description for {}".format(suite
),
402 [t
.get("path") for _
, t
in test_list
.items()],
407 self
._validate
_desc
_harness
_direction
(
408 suite
, test_list
, yaml_content
, global_descriptions
411 self
._validate
_metrics
_harness
_direction
(
412 suite
, test_list
, yaml_content
, global_metrics
415 self
.metrics_info
[framework_info
["name"]] = global_metrics
["global"]
417 def validate_yaml(self
, yaml_path
):
419 Validate that the YAML file has all the fields that are
420 required and parse the descriptions into strings in case
421 some are give as relative file paths.
423 :param str yaml_path: Path to the YAML to validate.
424 :return bool: True/False => Passed/Failed Validation
427 def _get_description(desc
):
429 Recompute the description in case it's a file.
431 desc_path
= pathlib
.Path(self
.workspace_dir
, desc
)
434 if desc_path
.exists() and desc_path
.is_file():
435 with
open(desc_path
, "r") as f
:
442 def _parse_descriptions(content
):
443 for suite
, sinfo
in content
.items():
444 desc
= sinfo
["description"]
445 sinfo
["description"] = _get_description(desc
)
447 # It's possible that the suite has no tests and
448 # only a description. If they exist, then parse them.
450 for test
, desc
in sinfo
["tests"].items():
451 sinfo
["tests"][test
] = _get_description(desc
)
454 yaml_content
= read_yaml(yaml_path
)
457 jsonschema
.validate(instance
=yaml_content
, schema
=CONFIG_SCHEMA
)
458 _parse_descriptions(yaml_content
["suites"])
460 except Exception as e
:
461 logger
.warning("YAML ValidationError: {}".format(str(e
)), yaml_path
)
465 def validate_rst_content(self
, rst_path
, expected_str
):
467 Validate that a given RST has the expected string in it
468 so that the generated documentation can be inserted there.
470 :param str rst_path: Path to the RST file.
471 :return bool: True/False => Passed/Failed Validation
473 rst_content
= read_file(rst_path
)
475 # Check for a {documentation} entry in some line,
476 # if we can't find one, then the validation fails.
478 docs_match
= re
.compile(f
".*{expected_str}.*")
479 for line
in rst_content
:
480 if docs_match
.search(line
):
484 logger
.warning( # noqa: PLE1205
485 f
"Cannot find a '{expected_str}' entry in the given index file",
491 def _check_framework_descriptions(self
, item
):
493 Helper method for validating descriptions
495 framework_info
= self
._gatherer
.get_test_list(item
)
496 self
.validate_descriptions(framework_info
)
498 def validate_tree(self
):
500 Validate the `perfdocs` directory that was found.
501 Returns True if it is good, false otherwise.
503 :return bool: True/False => Passed/Failed Validation
507 # For each framework, check their files and validate descriptions
508 for matched
in self
._gatherer
.perfdocs_tree
:
509 # Get the paths to the YAML and RST for this framework
510 matched_yml
= pathlib
.Path(matched
["path"], matched
["yml"])
511 matched_rst
= pathlib
.Path(matched
["path"], matched
["rst"])
514 "yml": self
.validate_yaml(matched_yml
),
518 if not read_yaml(matched_yml
)["static-only"]:
519 _valid_files
["rst"] = self
.validate_rst_content(
520 matched_rst
, "{documentation}"
523 if matched
.get("metrics"):
524 _valid_files
["metrics"] = self
.validate_rst_content(
525 pathlib
.Path(matched
["path"], matched
["metrics"]),
526 "{metrics_documentation}",
529 # Log independently the errors found for the matched files
530 for file_format
, valid
in _valid_files
.items():
532 logger
.log("File validation error: {}".format(file_format
))
533 if not all(_valid_files
.values()):
537 self
._check
_framework
_descriptions
(matched
)
540 raise Exception("No valid perfdocs directories found")