• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 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
9
10from autotest_lib.site_utils.sponge_lib import autotest_dynamic_job
11from autotest_lib.client.common_lib import decorators
12
13try:
14    from sponge import upload_utils
15except ImportError:
16    logging.debug('Module failed to be imported: sponge')
17    upload_utils = None
18
19
20
21class SpongeLogHandler(logging.Handler):
22    """Helper log handler for logging during sponge."""
23    def __init__(self, log_func):
24        super(SpongeLogHandler, self).__init__()
25        self.log_func = log_func
26
27    def emit(self, record):
28        log_entry = self.format(record)
29        self.log_func(log_entry)
30
31
32
33@decorators.test_module_available(upload_utils)
34def upload_results(job, log=logging.debug):
35    """Upload test results to Sponge with given job details.
36
37    @param job: A job object created by tko/parsers.
38    @param log: Logging method, default is logging.debug.
39
40    @return: A url to the Sponge invocation.
41    """
42    start_level = logging.getLogger().level
43
44    log_handler = SpongeLogHandler(log)
45    logging.getLogger().addHandler(log_handler)
46    logging.getLogger().setLevel(logging.DEBUG)
47
48    logging.info("added log handler")
49
50    try:
51        logging.info('Starting sponge upload.')
52        info = autotest_dynamic_job.DynamicJobInfo(job)
53        return upload_utils.UploadInfo(info)
54    except:
55        logging.exception('Failed to upload to sponge.')
56    finally:
57        logging.getLogger().removeHandler(log_handler)
58        logging.getLogger().setLevel(start_level)
59