test_traffic_annotation_auditor.py 4.3 KB

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