xref: /aosp_15_r20/external/cronet/testing/scripts/test_traffic_annotation_auditor.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env python
2# Copyright 2017 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""//testing/scripts wrapper for the network traffic annotation auditor checks.
7This script is used to run traffic_annotation_auditor_tests.py on an FYI bot to
8check that traffic_annotation_auditor has the same results when heuristics that
9help it run fast and spam free on trybots are disabled."""
10
11import json
12import os
13import re
14import sys
15import tempfile
16import traceback
17
18# Add src/testing/ into sys.path for importing common without pylint errors.
19sys.path.append(
20    os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
21from scripts import common
22
23WINDOWS_SHEET_CONFIG = {
24  "spreadsheet_id": "1TmBr9jnf1-hrjntiVBzT9EtkINGrtoBYFMWad2MBeaY",
25  "annotations_sheet_name": "Annotations",
26  "chrome_version_sheet_name": "Chrome Version",
27  "silent_change_columns": [],
28  "last_update_column_name": "Last Update",
29}
30
31
32CHROMEOS_SHEET_CONFIG = {
33  "spreadsheet_id": "1928goWKy6LVdF9Nl5nV1OD260YC10dHsdrnHEGdGsg8",
34  "annotations_sheet_name": "Annotations",
35  "chrome_version_sheet_name": "Chrome Version",
36  "silent_change_columns": [],
37  "last_update_column_name": "Last Update",
38}
39
40def is_windows():
41  return os.name == 'nt'
42
43def is_chromeos(build_path):
44  current_platform = get_current_platform_from_gn_args(build_path)
45  return current_platform == "chromeos"
46
47
48def get_sheet_config(build_path):
49  if is_windows():
50    return WINDOWS_SHEET_CONFIG
51  if is_chromeos(build_path):
52    return CHROMEOS_SHEET_CONFIG
53  return None
54
55
56def get_current_platform_from_gn_args(build_path):
57  if sys.platform.startswith("linux") and build_path is not None:
58    try:
59      with open(os.path.join(build_path, "args.gn")) as f:
60        gn_args = f.read()
61      if not gn_args:
62        logger.info("Could not retrieve args.gn")
63
64      pattern = re.compile(r"^\s*target_os\s*=\s*\"chromeos\"\s*$",
65                           re.MULTILINE)
66      if pattern.search(gn_args):
67        return "chromeos"
68
69    except(ValueError, OSError) as e:
70      logger.info(e)
71
72  return None
73
74def main_run(args):
75  annotations_file = tempfile.NamedTemporaryFile()
76  annotations_filename = annotations_file.name
77  annotations_file.close()
78
79  build_path = os.path.join(args.paths['checkout'], 'out', args.build_config_fs)
80  command_line = [
81      sys.executable,
82      os.path.join(common.SRC_DIR, 'tools', 'traffic_annotation', 'scripts',
83                   'traffic_annotation_auditor_tests.py'),
84      '--build-path',
85      build_path,
86      '--annotations-file',
87      annotations_filename,
88  ]
89  rc = common.run_command(command_line)
90
91  # Update the Google Sheets on success, but only on the Windows and ChromeOS
92  # trybot.
93  sheet_config = get_sheet_config(build_path)
94  try:
95    if rc == 0 and sheet_config is not None:
96      print("Tests succeeded. Updating annotations sheet...")
97
98      config_file = tempfile.NamedTemporaryFile(delete=False, mode='w+')
99      json.dump(sheet_config, config_file, indent=4)
100      config_filename = config_file.name
101      config_file.close()
102      vpython_path = 'vpython3.bat' if is_windows() else 'vpython3'
103
104      command_line = [
105        vpython_path,
106        os.path.join(common.SRC_DIR, 'tools', 'traffic_annotation', 'scripts',
107                   'update_annotations_sheet.py'),
108        '--yes',
109        '--config-file',
110        config_filename,
111        '--annotations-file',
112        annotations_filename,
113      ]
114      rc = common.run_command(command_line)
115      cleanup_file(config_filename)
116    else:
117      print("Test failed without updating the annotations sheet.")
118  except (ValueError, OSError) as e:
119    print("Error updating the annotations sheet", e)
120    traceback.print_exc()
121  finally:
122    cleanup_file(annotations_filename)
123    failures = ['Please refer to stdout for errors.'] if rc else []
124    common.record_local_script_results(
125       'test_traffic_annotation_auditor', args.output, failures, True)
126
127  return rc
128
129def cleanup_file(filename):
130  try:
131    os.remove(filename)
132  except OSError:
133    print("Could not remove file: ", filename)
134
135def main_compile_targets(args):
136  json.dump(['traffic_annotation_proto'], args.output)
137
138
139if __name__ == '__main__':
140  funcs = {
141    'run': main_run,
142    'compile_targets': main_compile_targets,
143  }
144  sys.exit(common.run_script(sys.argv[1:], funcs))
145