blob: 06e4c25da5ae0fbd34332f2122806ce9c83e253c [file] [log] [blame]
Dan Shi4f46aaf2016-06-25 00:21:41 -07001# Copyright 2016 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"""This module contains utilities for test to report result to Sponge.
6"""
7
8import logging
Dan Shi4c33b6a2016-08-18 16:11:31 -07009import os
Dan Shi4f46aaf2016-06-25 00:21:41 -070010import socket
11import time
Dan Shi47199f02016-10-13 17:15:26 -070012import traceback
Dan Shi4f46aaf2016-06-25 00:21:41 -070013
14import common
15
16try:
17 import sponge
18except ImportError:
19 logging.debug('Module failed to be imported: sponge')
20 sponge = None
21
22from autotest_lib.client.common_lib import decorators
23from autotest_lib.client.common_lib import global_config
Dan Shi47199f02016-10-13 17:15:26 -070024from autotest_lib.client.common_lib.cros.graphite import autotest_es
Dan Shi4f46aaf2016-06-25 00:21:41 -070025from autotest_lib.site_utils import job_directories
26from autotest_lib.tko import models
27from autotest_lib.tko import utils as tko_utils
28
29
30CONFIG=global_config.global_config
31
32RETRIEVE_LOGS_CGI = CONFIG.get_config_value(
33 'BUG_REPORTING', 'retrieve_logs_cgi', default='')
34RESULTS_URL_FMT = RETRIEVE_LOGS_CGI + 'results/%s-%s/%s'
35USE_PROD_SERVER = CONFIG.get_config_value(
36 'SERVER', 'use_prod_sponge_server', default=False, type=bool)
37
Dan Shi47199f02016-10-13 17:15:26 -070038# Type string of metadata.
39_SPONGE_UPLOAD_FAILURE_TYPE = 'sponge_upload_failure'
40
Dan Shi4f46aaf2016-06-25 00:21:41 -070041@decorators.test_module_available(sponge)
42def upload_results_in_test(test, test_pass=True, acts_summary=None):
43 """Upload test results to Sponge.
44
45 @param test: A test object.
46 @param test_pass: True if test passed, False otherwise. Default is set to
47 True. When test results are reported inside test, the test is
48 considered to success, or exception like TestFail would have been
49 raised if the test has failed.
50 @param acts_summary: Path to the json file of ACTS test summary.
Dan Shi47199f02016-10-13 17:15:26 -070051
52 @return: A url to the Sponge invocation.
Dan Shi4f46aaf2016-06-25 00:21:41 -070053 """
54 try:
55 # job keyval file has the information about the test job except
56 # `job_finished`, which is written after the test is actually finished.
57 # Therefore, the `end_time` for a Sponge invocation is set to current
58 # time.
59 job_keyvals = models.test.parse_job_keyval(test.resultsdir)
60 status = 'GOOD' if test_pass else 'FAIL'
61 job_id = job_directories.get_job_id_or_task_id(test.resultsdir)
62 results_dir = tko_utils.find_toplevel_job_dir(test.resultsdir)
63 dut = test.job.machines[0] if len(test.job.machines) > 0 else ''
64 results_url = RESULTS_URL_FMT % (job_id, test.job.user, dut)
65
66 invocation_url = sponge.upload_utils.Upload(
67 job_id=job_id,
68 test_name=test.tagged_testname,
69 dut=','.join(test.job.machines),
70 drone=job_keyvals.get('drone', socket.gethostname()),
71 status=status,
72 start_time=job_keyvals['job_started'],
73 end_time=time.time(),
74 results_dir=results_dir,
75 results_url=results_url,
76 acts_summary=acts_summary,
77 use_prod_server=USE_PROD_SERVER)
78 logging.debug('Test result is uploaded to Sponge: %s', invocation_url)
79 return invocation_url
80 except Exception as e:
Dan Shi47199f02016-10-13 17:15:26 -070081 metadata = {'method': 'upload_results_in_test',
82 'job_id': job_id, 'error': str(e),
83 'details': traceback.format_exc()}
84 autotest_es.post(use_http=True, type_str=_SPONGE_UPLOAD_FAILURE_TYPE,
85 metadata=metadata)
Dan Shi4f46aaf2016-06-25 00:21:41 -070086 logging.exception('Failed to upload to Sponge: %s', e)
Dan Shi4c33b6a2016-08-18 16:11:31 -070087
88
89@decorators.test_module_available(sponge)
Dan Shi47199f02016-10-13 17:15:26 -070090def upload_results(job, log=logging.debug):
Dan Shi4c33b6a2016-08-18 16:11:31 -070091 """Upload test results to Sponge with given job details.
92
93 @param job: A job object created by tko/parsers.
Dan Shi47199f02016-10-13 17:15:26 -070094 @param log: Logging method, default is logging.debug.
95
96 @return: A url to the Sponge invocation.
Dan Shi4c33b6a2016-08-18 16:11:31 -070097 """
98 job_id = job_directories.get_job_id_or_task_id(job.dir)
99 results_dir = tko_utils.find_toplevel_job_dir(job.dir)
100 results_url = RESULTS_URL_FMT % (job_id, job.user, job.machine)
101
102 # Try to locate test_run_summary.json file for android_ACTS test.
103 acts_summary = os.path.join(results_dir, 'latest', 'test_run_summary.json')
104 if not os.path.exists(acts_summary):
105 acts_summary = None
106
107 status = 'GOOD'
108 for test in job.tests:
109 if test.status != 'GOOD':
110 status = test.status
111 break
112
113 try:
114 invocation_url = sponge.upload_utils.Upload(
115 job_id=job_id,
116 test_name=job.label,
117 dut=job.machine,
118 drone=job.keyval_dict.get('drone', socket.gethostname()),
119 status=status,
120 start_time=job.keyval_dict['job_started'],
Dan Shiacdeb812016-11-15 14:26:33 -0800121 end_time=job.keyval_dict.get('job_finished', time.time()),
Dan Shi4c33b6a2016-08-18 16:11:31 -0700122 results_dir=results_dir,
123 results_url=results_url,
124 acts_summary=acts_summary,
125 job=job,
126 use_prod_server=USE_PROD_SERVER)
Dan Shi47199f02016-10-13 17:15:26 -0700127 log('Test result is uploaded to Sponge: %s' % invocation_url)
Dan Shi4c33b6a2016-08-18 16:11:31 -0700128 return invocation_url
129 except Exception as e:
Dan Shi47199f02016-10-13 17:15:26 -0700130 metadata = {'method': 'upload_results',
131 'job_id': job_id, 'error': str(e),
132 'details': traceback.format_exc()}
133 autotest_es.post(use_http=True, type_str=_SPONGE_UPLOAD_FAILURE_TYPE,
134 metadata=metadata)
135 log('Failed to upload to Sponge: %s\nDetails:\n%s' %
136 (e, traceback.format_exc()))