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