blob: 17a17ea6b918e77af7628de406e80e83790c3a9c [file] [log] [blame]
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import collections
import logging
import common
from autotest_lib.client.common_lib import global_config
from autotest_lib.server import site_utils
from autotest_lib.server.cros.dynamic_suite import job_status
from autotest_lib.server.cros.dynamic_suite import reporting_utils
from autotest_lib.server.cros.dynamic_suite import tools
from autotest_lib.site_utils import gmail_lib
from chromite.lib import metrics
except ImportError:
metrics = site_utils.metrics_mock
EMAIL_CREDS_FILE = global_config.global_config.get_config_value(
'NOTIFICATIONS', 'gmail_api_credentials_test_failure', default=None)
class TestBug(object):
Wrap up all information needed to make an intelligent report about an
issue. Each TestBug has a search marker associated with it that can be
used to find similar reports.
def __init__(self, build, chrome_version, suite, result):
@param build: The build type, of the form <board>/<milestone>-<release>.
eg: x86-mario-release/R25-4321.0.0
@param chrome_version: The chrome version associated with the build.
eg: 28.0.1498.1
@param suite: The name of the suite that this test run is a part of.
@param result: The status of the job associated with this issue.
This contains the status, job id, test name, hostname
and reason for issue.
""" = build
self.chrome_version = chrome_version
self.suite = suite = tools.get_test_name(build, suite, result.test_name)
self.reason = result.reason
# The result_owner is used to find results and logs.
self.result_owner = result.owner
self.hostname = result.hostname
self.job_id =
# Aborts, server/client job failures or a test failure without a
# reason field need lab attention. Lab bugs for the aborted case
# are disabled till is resolved.
self.lab_error = job_status.is_for_infrastructure_fail(result)
# The owner is who the bug is assigned to.
self.owner = '' = []
self.components = []
if result.is_warn():
self.labels = ['Test-Warning']
self.status = 'Warning'
self.labels = []
self.status = 'Failure'
def title(self):
"""Combines information about this bug into a title string."""
return '[%s] %s %s on %s' % (self.suite,,
def summary(self):
"""Combines information about this bug into a summary string."""
links = self._get_links_for_failure()
template = ('This report is automatically generated to track the '
'following %(status)s:\n'
'Test: %(test)s.\n'
'Suite: %(suite)s.\n'
'Chrome Version: %(chrome_version)s.\n'
'Build: %(build)s.\n\nReason:\n%(reason)s.\n'
'build artifacts: %(build_artifacts)s.\n'
'results log: %(results_log)s.\n'
'status log: %(status_log)s.\n'
'job link: %(job)s.\n\n'
'You may want to check the test history: '
'You may also want to check the test retry dashboard in '
'case this is a flakey test: %(retry_url)s\n')
specifics = {
'status': self.status,
'suite': self.suite,
'chrome_version': self.chrome_version,
'reason': self.reason,
'build_artifacts': links.artifacts,
'results_log': links.results,
'status_log': links.status_log,
'job': links.job,
'test_history_url': links.test_history_url,
'retry_url': links.retry_url,
return template % specifics
# TO-DO(shuqianz) Fix the dedupe failing issue because reason contains
# special characters after
# being fixed.
def search_marker(self):
"""Return an Anchor that we can use to dedupe this exact bug."""
board = ''
board = site_utils.ParseBuildName([0]
except site_utils.ParseBuildNameException as e:
# Substitute the board name for a placeholder. We try both build and
# release board name variants.
reason = self.reason
if board:
for b in (board, board.replace('_', '-')):
reason = reason.replace(b, 'BOARD_PLACEHOLDER')
return "%s{%s,%s,%s}" % ('Test%s' % self.status, self.suite,, reason)
def _get_links_for_failure(self):
"""Returns a named tuple of links related to this failure."""
links = collections.namedtuple('links', ('results,'
return links(reporting_utils.link_result_logs(
self.job_id, self.result_owner, self.hostname),
self.job_id, self.result_owner, self.hostname),
ReportResult = collections.namedtuple('ReportResult', ['bug_id', 'update_count'])
def send_email(bug, bug_template):
"""Send email to the owner and cc's to notify the TestBug.
@param bug: TestBug instance.
@param bug_template: A template dictionary specifying the default bug
filing options for failures in this suite.
to_set = set( if else set()
if bug.owner:
if bug_template.get('cc'):
to_set = to_set.union(bug_template.get('cc'))
if bug_template.get('owner'):
recipients = ', '.join(to_set)
if not recipients:
logging.warning('No owner/cc found. Will skip sending a mail.')
success = False
recipients, bug.title(), bug.summary(), retry=False,
success = True
.increment(fields={'success': success}))