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