Home | History | Annotate | Download | only in results
      1 # Copyright (c) 2013 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 """Module containing utility functions for reporting results."""
      6 
      7 import logging
      8 import os
      9 import re
     10 
     11 from pylib import constants
     12 from pylib.results.flakiness_dashboard import results_uploader
     13 
     14 
     15 def _LogToFile(results, test_type, suite_name):
     16   """Log results to local files which can be used for aggregation later."""
     17   log_file_path = os.path.join(constants.GetOutDirectory(), 'test_logs')
     18   if not os.path.exists(log_file_path):
     19     os.mkdir(log_file_path)
     20   full_file_name = os.path.join(
     21       log_file_path, re.sub(r'\W', '_', test_type).lower() + '.log')
     22   if not os.path.exists(full_file_name):
     23     with open(full_file_name, 'w') as log_file:
     24       print >> log_file, '\n%s results for %s build %s:' % (
     25           test_type, os.environ.get('BUILDBOT_BUILDERNAME'),
     26           os.environ.get('BUILDBOT_BUILDNUMBER'))
     27     logging.info('Writing results to %s.', full_file_name)
     28 
     29   logging.info('Writing results to %s.', full_file_name)
     30   with open(full_file_name, 'a') as log_file:
     31     shortened_suite_name = suite_name[:25] + (suite_name[25:] and '...')
     32     print >> log_file, '%s%s' % (shortened_suite_name.ljust(30),
     33                                  results.GetShortForm())
     34 
     35 
     36 def _LogToFlakinessDashboard(results, test_type, test_package,
     37                              flakiness_server):
     38   """Upload results to the flakiness dashboard"""
     39   logging.info('Upload results for test type "%s", test package "%s" to %s',
     40                test_type, test_package, flakiness_server)
     41 
     42   try:
     43     # TODO(jbudorick): remove Instrumentation once instrumentation tests
     44     # switch to platform mode.
     45     if test_type in ('instrumentation', 'Instrumentation'):
     46       if flakiness_server == constants.UPSTREAM_FLAKINESS_SERVER:
     47         assert test_package in ['ContentShellTest',
     48                                 'ChromePublicTest',
     49                                 'ChromeSyncShellTest',
     50                                 'AndroidWebViewTest',
     51                                 'SystemWebViewShellLayoutTest']
     52         dashboard_test_type = ('%s_instrumentation_tests' %
     53                                test_package.lower().rstrip('test'))
     54       # Downstream server.
     55       else:
     56         dashboard_test_type = 'Chromium_Android_Instrumentation'
     57 
     58     elif test_type == 'gtest':
     59       dashboard_test_type = test_package
     60 
     61     else:
     62       logging.warning('Invalid test type')
     63       return
     64 
     65     results_uploader.Upload(
     66         results, flakiness_server, dashboard_test_type)
     67 
     68   except Exception: # pylint: disable=broad-except
     69     logging.exception('Failure while logging to %s', flakiness_server)
     70 
     71 
     72 def LogFull(results, test_type, test_package, annotation=None,
     73             flakiness_server=None):
     74   """Log the tests results for the test suite.
     75 
     76   The results will be logged three different ways:
     77     1. Log to stdout.
     78     2. Log to local files for aggregating multiple test steps
     79        (on buildbots only).
     80     3. Log to flakiness dashboard (on buildbots only).
     81 
     82   Args:
     83     results: An instance of TestRunResults object.
     84     test_type: Type of the test (e.g. 'Instrumentation', 'Unit test', etc.).
     85     test_package: Test package name (e.g. 'ipc_tests' for gtests,
     86                   'ContentShellTest' for instrumentation tests)
     87     annotation: If instrumenation test type, this is a list of annotations
     88                 (e.g. ['Smoke', 'SmallTest']).
     89     flakiness_server: If provider, upload the results to flakiness dashboard
     90                       with this URL.
     91     """
     92   if not results.DidRunPass():
     93     logging.critical('*' * 80)
     94     logging.critical('Detailed Logs')
     95     logging.critical('*' * 80)
     96     for line in results.GetLogs().splitlines():
     97       logging.critical(line)
     98   logging.critical('*' * 80)
     99   logging.critical('Summary')
    100   logging.critical('*' * 80)
    101   for line in results.GetGtestForm().splitlines():
    102     logging.critical(line)
    103   logging.critical('*' * 80)
    104 
    105   if os.environ.get('BUILDBOT_BUILDERNAME'):
    106     # It is possible to have multiple buildbot steps for the same
    107     # instrumenation test package using different annotations.
    108     if annotation and len(annotation) == 1:
    109       suite_name = annotation[0]
    110     else:
    111       suite_name = test_package
    112     _LogToFile(results, test_type, suite_name)
    113 
    114     if flakiness_server:
    115       _LogToFlakinessDashboard(results, test_type, test_package,
    116                                flakiness_server)
    117