blob: 94d198bc099f3f83f8d0a0db1b4bb21e950e8283 [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
9import socket
10import time
11
12import common
13
14try:
15 import sponge
16except ImportError:
17 logging.debug('Module failed to be imported: sponge')
18 sponge = None
19
20from autotest_lib.client.common_lib import decorators
21from autotest_lib.client.common_lib import global_config
22from autotest_lib.site_utils import job_directories
23from autotest_lib.tko import models
24from autotest_lib.tko import utils as tko_utils
25
26
27CONFIG=global_config.global_config
28
29RETRIEVE_LOGS_CGI = CONFIG.get_config_value(
30 'BUG_REPORTING', 'retrieve_logs_cgi', default='')
31RESULTS_URL_FMT = RETRIEVE_LOGS_CGI + 'results/%s-%s/%s'
32USE_PROD_SERVER = CONFIG.get_config_value(
33 'SERVER', 'use_prod_sponge_server', default=False, type=bool)
34
35@decorators.test_module_available(sponge)
36def upload_results_in_test(test, test_pass=True, acts_summary=None):
37 """Upload test results to Sponge.
38
39 @param test: A test object.
40 @param test_pass: True if test passed, False otherwise. Default is set to
41 True. When test results are reported inside test, the test is
42 considered to success, or exception like TestFail would have been
43 raised if the test has failed.
44 @param acts_summary: Path to the json file of ACTS test summary.
45 """
46 try:
47 # job keyval file has the information about the test job except
48 # `job_finished`, which is written after the test is actually finished.
49 # Therefore, the `end_time` for a Sponge invocation is set to current
50 # time.
51 job_keyvals = models.test.parse_job_keyval(test.resultsdir)
52 status = 'GOOD' if test_pass else 'FAIL'
53 job_id = job_directories.get_job_id_or_task_id(test.resultsdir)
54 results_dir = tko_utils.find_toplevel_job_dir(test.resultsdir)
55 dut = test.job.machines[0] if len(test.job.machines) > 0 else ''
56 results_url = RESULTS_URL_FMT % (job_id, test.job.user, dut)
57
58 invocation_url = sponge.upload_utils.Upload(
59 job_id=job_id,
60 test_name=test.tagged_testname,
61 dut=','.join(test.job.machines),
62 drone=job_keyvals.get('drone', socket.gethostname()),
63 status=status,
64 start_time=job_keyvals['job_started'],
65 end_time=time.time(),
66 results_dir=results_dir,
67 results_url=results_url,
68 acts_summary=acts_summary,
69 use_prod_server=USE_PROD_SERVER)
70 logging.debug('Test result is uploaded to Sponge: %s', invocation_url)
71 return invocation_url
72 except Exception as e:
73 logging.exception('Failed to upload to Sponge: %s', e)