Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Uploads the results to the flakiness dashboard server.""" |
| 6 | # pylint: disable=E1002,R0201 |
| 7 | |
| 8 | import logging |
| 9 | import os |
| 10 | import shutil |
| 11 | import tempfile |
| 12 | import xml |
| 13 | |
| 14 | |
| 15 | from devil.utils import cmd_helper |
| 16 | from pylib.constants import host_paths |
| 17 | from pylib.results.flakiness_dashboard import json_results_generator |
| 18 | from pylib.utils import repo_utils |
| 19 | |
| 20 | |
| 21 | |
| 22 | class JSONResultsGenerator(json_results_generator.JSONResultsGeneratorBase): |
| 23 | """Writes test results to a JSON file and handles uploading that file to |
| 24 | the test results server. |
| 25 | """ |
| 26 | def __init__(self, builder_name, build_name, build_number, tmp_folder, |
| 27 | test_results_map, test_results_server, test_type, master_name): |
| 28 | super(JSONResultsGenerator, self).__init__( |
| 29 | builder_name=builder_name, |
| 30 | build_name=build_name, |
| 31 | build_number=build_number, |
| 32 | results_file_base_path=tmp_folder, |
| 33 | builder_base_url=None, |
| 34 | test_results_map=test_results_map, |
| 35 | svn_repositories=(('webkit', 'third_party/WebKit'), |
| 36 | ('chrome', '.')), |
| 37 | test_results_server=test_results_server, |
| 38 | test_type=test_type, |
| 39 | master_name=master_name) |
| 40 | |
| 41 | #override |
| 42 | def _GetModifierChar(self, test_name): |
| 43 | if test_name not in self._test_results_map: |
| 44 | return self.__class__.NO_DATA_RESULT |
| 45 | |
| 46 | return self._test_results_map[test_name].modifier |
| 47 | |
| 48 | #override |
| 49 | def _GetSVNRevision(self, in_directory): |
| 50 | """Returns the git/svn revision for the given directory. |
| 51 | |
| 52 | Args: |
| 53 | in_directory: The directory relative to src. |
| 54 | """ |
| 55 | def _is_git_directory(in_directory): |
| 56 | """Returns true if the given directory is in a git repository. |
| 57 | |
| 58 | Args: |
| 59 | in_directory: The directory path to be tested. |
| 60 | """ |
| 61 | if os.path.exists(os.path.join(in_directory, '.git')): |
| 62 | return True |
| 63 | parent = os.path.dirname(in_directory) |
| 64 | if parent == host_paths.DIR_SOURCE_ROOT or parent == in_directory: |
| 65 | return False |
| 66 | return _is_git_directory(parent) |
| 67 | |
| 68 | in_directory = os.path.join(host_paths.DIR_SOURCE_ROOT, in_directory) |
| 69 | |
| 70 | if not os.path.exists(os.path.join(in_directory, '.svn')): |
| 71 | if _is_git_directory(in_directory): |
| 72 | return repo_utils.GetGitHeadSHA1(in_directory) |
| 73 | else: |
| 74 | return '' |
| 75 | |
| 76 | output = cmd_helper.GetCmdOutput(['svn', 'info', '--xml'], cwd=in_directory) |
| 77 | try: |
| 78 | dom = xml.dom.minidom.parseString(output) |
| 79 | return dom.getElementsByTagName('entry')[0].getAttribute('revision') |
| 80 | except xml.parsers.expat.ExpatError: |
| 81 | return '' |
| 82 | return '' |
| 83 | |
| 84 | |
| 85 | class ResultsUploader(object): |
| 86 | """Handles uploading buildbot tests results to the flakiness dashboard.""" |
| 87 | def __init__(self, tests_type): |
| 88 | self._build_number = os.environ.get('BUILDBOT_BUILDNUMBER') |
| 89 | self._builder_name = os.environ.get('BUILDBOT_BUILDERNAME') |
| 90 | self._tests_type = tests_type |
| 91 | |
| 92 | if not self._build_number or not self._builder_name: |
| 93 | raise Exception('You should not be uploading tests results to the server' |
| 94 | 'from your local machine.') |
| 95 | |
| 96 | upstream = (tests_type != 'Chromium_Android_Instrumentation') |
| 97 | if upstream: |
| 98 | # TODO(frankf): Use factory properties (see buildbot/bb_device_steps.py) |
| 99 | # This requires passing the actual master name (e.g. 'ChromiumFYI' not |
| 100 | # 'chromium.fyi'). |
| 101 | from slave import slave_utils # pylint: disable=F0401 |
| 102 | self._build_name = slave_utils.SlaveBuildName(host_paths.DIR_SOURCE_ROOT) |
| 103 | self._master_name = slave_utils.GetActiveMaster() |
| 104 | else: |
| 105 | self._build_name = 'chromium-android' |
| 106 | buildbot_branch = os.environ.get('BUILDBOT_BRANCH') |
| 107 | if not buildbot_branch: |
| 108 | buildbot_branch = 'master' |
| 109 | else: |
| 110 | # Ensure there's no leading "origin/" |
| 111 | buildbot_branch = buildbot_branch[buildbot_branch.find('/') + 1:] |
| 112 | self._master_name = '%s-%s' % (self._build_name, buildbot_branch) |
| 113 | |
| 114 | self._test_results_map = {} |
| 115 | |
| 116 | def AddResults(self, test_results): |
| 117 | # TODO(frankf): Differentiate between fail/crash/timeouts. |
| 118 | conversion_map = [ |
| 119 | (test_results.GetPass(), False, |
| 120 | json_results_generator.JSONResultsGeneratorBase.PASS_RESULT), |
| 121 | (test_results.GetFail(), True, |
| 122 | json_results_generator.JSONResultsGeneratorBase.FAIL_RESULT), |
| 123 | (test_results.GetCrash(), True, |
| 124 | json_results_generator.JSONResultsGeneratorBase.FAIL_RESULT), |
| 125 | (test_results.GetTimeout(), True, |
| 126 | json_results_generator.JSONResultsGeneratorBase.FAIL_RESULT), |
| 127 | (test_results.GetUnknown(), True, |
| 128 | json_results_generator.JSONResultsGeneratorBase.NO_DATA_RESULT), |
| 129 | ] |
| 130 | |
| 131 | for results_list, failed, modifier in conversion_map: |
| 132 | for single_test_result in results_list: |
| 133 | test_result = json_results_generator.TestResult( |
| 134 | test=single_test_result.GetName(), |
| 135 | failed=failed, |
| 136 | elapsed_time=single_test_result.GetDuration() / 1000) |
| 137 | # The WebKit TestResult object sets the modifier it based on test name. |
| 138 | # Since we don't use the same test naming convention as WebKit the |
| 139 | # modifier will be wrong, so we need to overwrite it. |
| 140 | test_result.modifier = modifier |
| 141 | |
| 142 | self._test_results_map[single_test_result.GetName()] = test_result |
| 143 | |
| 144 | def Upload(self, test_results_server): |
| 145 | if not self._test_results_map: |
| 146 | return |
| 147 | |
| 148 | tmp_folder = tempfile.mkdtemp() |
| 149 | |
| 150 | try: |
| 151 | results_generator = JSONResultsGenerator( |
| 152 | builder_name=self._builder_name, |
| 153 | build_name=self._build_name, |
| 154 | build_number=self._build_number, |
| 155 | tmp_folder=tmp_folder, |
| 156 | test_results_map=self._test_results_map, |
| 157 | test_results_server=test_results_server, |
| 158 | test_type=self._tests_type, |
| 159 | master_name=self._master_name) |
| 160 | |
| 161 | json_files = ["incremental_results.json", "times_ms.json"] |
| 162 | results_generator.GenerateJSONOutput() |
| 163 | results_generator.GenerateTimesMSFile() |
| 164 | results_generator.UploadJSONFiles(json_files) |
| 165 | except Exception as e: # pylint: disable=broad-except |
| 166 | logging.error("Uploading results to test server failed: %s.", e) |
| 167 | finally: |
| 168 | shutil.rmtree(tmp_folder) |
| 169 | |
| 170 | |
| 171 | def Upload(results, flakiness_dashboard_server, test_type): |
| 172 | """Reports test results to the flakiness dashboard for Chrome for Android. |
| 173 | |
| 174 | Args: |
| 175 | results: test results. |
| 176 | flakiness_dashboard_server: the server to upload the results to. |
| 177 | test_type: the type of the tests (as displayed by the flakiness dashboard). |
| 178 | """ |
| 179 | uploader = ResultsUploader(test_type) |
| 180 | uploader.AddResults(results) |
| 181 | uploader.Upload(flakiness_dashboard_server) |