mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
518 lines
14 KiB
Python
518 lines
14 KiB
Python
8 years ago
|
"""Classes for storing and processing test results."""
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
8 years ago
|
|
||
|
import datetime
|
||
|
import json
|
||
7 years ago
|
import os
|
||
8 years ago
|
|
||
5 years ago
|
from . import types as t
|
||
5 years ago
|
|
||
5 years ago
|
from .util import (
|
||
8 years ago
|
display,
|
||
5 years ago
|
make_dirs,
|
||
5 years ago
|
to_bytes,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .config import (
|
||
8 years ago
|
TestConfig,
|
||
8 years ago
|
)
|
||
|
|
||
|
|
||
|
def calculate_best_confidence(choices, metadata):
|
||
|
"""
|
||
|
:type choices: tuple[tuple[str, int]]
|
||
|
:type metadata: Metadata
|
||
|
:rtype: int
|
||
|
"""
|
||
|
best_confidence = 0
|
||
|
|
||
|
for path, line in choices:
|
||
|
confidence = calculate_confidence(path, line, metadata)
|
||
|
best_confidence = max(confidence, best_confidence)
|
||
|
|
||
|
return best_confidence
|
||
|
|
||
|
|
||
|
def calculate_confidence(path, line, metadata):
|
||
|
"""
|
||
|
:type path: str
|
||
|
:type line: int
|
||
|
:type metadata: Metadata
|
||
|
:rtype: int
|
||
|
"""
|
||
|
ranges = metadata.changes.get(path)
|
||
|
|
||
|
# no changes were made to the file
|
||
|
if not ranges:
|
||
|
return 0
|
||
|
|
||
|
# changes were made to the same file and line
|
||
|
if any(r[0] <= line <= r[1] in r for r in ranges):
|
||
|
return 100
|
||
|
|
||
|
# changes were made to the same file and the line number is unknown
|
||
|
if line == 0:
|
||
|
return 75
|
||
|
|
||
|
# changes were made to the same file and the line number is different
|
||
|
return 50
|
||
|
|
||
8 years ago
|
|
||
5 years ago
|
class TestResult:
|
||
8 years ago
|
"""Base class for test results."""
|
||
6 years ago
|
def __init__(self, command, test, python_version=None):
|
||
8 years ago
|
"""
|
||
|
:type command: str
|
||
|
:type test: str
|
||
|
:type python_version: str
|
||
|
"""
|
||
|
self.command = command
|
||
|
self.test = test
|
||
|
self.python_version = python_version
|
||
|
self.name = self.test or self.command
|
||
|
|
||
|
if self.python_version:
|
||
|
self.name += '-python-%s' % self.python_version
|
||
|
|
||
|
try:
|
||
|
import junit_xml
|
||
|
except ImportError:
|
||
|
junit_xml = None
|
||
|
|
||
|
self.junit = junit_xml
|
||
|
|
||
|
def write(self, args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
|
self.write_console()
|
||
|
self.write_bot(args)
|
||
|
|
||
|
if args.lint:
|
||
|
self.write_lint()
|
||
|
|
||
6 years ago
|
if args.junit:
|
||
8 years ago
|
if self.junit:
|
||
|
self.write_junit(args)
|
||
|
else:
|
||
|
display.warning('Skipping junit xml output because the `junit-xml` python package was not found.', unique=True)
|
||
|
|
||
|
def write_console(self):
|
||
|
"""Write results to console."""
|
||
|
|
||
|
def write_lint(self):
|
||
|
"""Write lint results to stdout."""
|
||
|
|
||
|
def write_bot(self, args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
|
|
||
|
def write_junit(self, args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
|
|
||
|
def create_path(self, directory, extension):
|
||
|
"""
|
||
|
:type directory: str
|
||
|
:type extension: str
|
||
|
:rtype: str
|
||
|
"""
|
||
|
path = 'test/results/%s/ansible-test-%s' % (directory, self.command)
|
||
|
|
||
|
if self.test:
|
||
|
path += '-%s' % self.test
|
||
|
|
||
|
if self.python_version:
|
||
|
path += '-python-%s' % self.python_version
|
||
|
|
||
|
path += extension
|
||
|
|
||
|
return path
|
||
|
|
||
|
def save_junit(self, args, test_case, properties=None):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
:type test_case: junit_xml.TestCase
|
||
|
:type properties: dict[str, str] | None
|
||
|
:rtype: str | None
|
||
|
"""
|
||
|
path = self.create_path('junit', '.xml')
|
||
|
|
||
|
test_suites = [
|
||
|
self.junit.TestSuite(
|
||
|
name='ansible-test',
|
||
|
test_cases=[test_case],
|
||
|
timestamp=datetime.datetime.utcnow().replace(microsecond=0).isoformat(),
|
||
|
properties=properties,
|
||
|
),
|
||
|
]
|
||
|
|
||
|
report = self.junit.TestSuite.to_xml_string(test_suites=test_suites, prettyprint=True, encoding='utf-8')
|
||
|
|
||
|
if args.explain:
|
||
|
return
|
||
|
|
||
|
with open(path, 'wb') as xml:
|
||
5 years ago
|
xml.write(to_bytes(report))
|
||
8 years ago
|
|
||
|
|
||
6 years ago
|
class TestTimeout(TestResult):
|
||
|
"""Test timeout."""
|
||
|
def __init__(self, timeout_duration):
|
||
|
"""
|
||
|
:type timeout_duration: int
|
||
|
"""
|
||
6 years ago
|
super(TestTimeout, self).__init__(command='timeout', test='')
|
||
6 years ago
|
|
||
|
self.timeout_duration = timeout_duration
|
||
|
|
||
6 years ago
|
def write(self, args):
|
||
6 years ago
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
|
message = 'Tests were aborted after exceeding the %d minute time limit.' % self.timeout_duration
|
||
6 years ago
|
|
||
|
# Include a leading newline to improve readability on Shippable "Tests" tab.
|
||
|
# Without this, the first line becomes indented.
|
||
|
output = '''
|
||
|
One or more of the following situations may be responsible:
|
||
6 years ago
|
|
||
|
- Code changes have resulted in tests that hang or run for an excessive amount of time.
|
||
|
- Tests have been added which exceed the time limit when combined with existing tests.
|
||
|
- Test infrastructure and/or external dependencies are operating slower than normal.'''
|
||
|
|
||
|
if args.coverage:
|
||
|
output += '\n- Additional overhead from collecting code coverage has resulted in tests exceeding the time limit.'
|
||
|
|
||
|
output += '\n\nConsult the console log for additional details on where the timeout occurred.'
|
||
|
|
||
6 years ago
|
timestamp = datetime.datetime.utcnow().replace(microsecond=0).isoformat()
|
||
|
|
||
|
# hack to avoid requiring junit-xml, which isn't pre-installed on Shippable outside our test containers
|
||
|
xml = '''
|
||
|
<?xml version="1.0" encoding="utf-8"?>
|
||
|
<testsuites disabled="0" errors="1" failures="0" tests="1" time="0.0">
|
||
|
\t<testsuite disabled="0" errors="1" failures="0" file="None" log="None" name="ansible-test" skipped="0" tests="1" time="0" timestamp="%s" url="None">
|
||
|
\t\t<testcase classname="timeout" name="timeout">
|
||
|
\t\t\t<error message="%s" type="error">%s</error>
|
||
|
\t\t</testcase>
|
||
|
\t</testsuite>
|
||
|
</testsuites>
|
||
|
''' % (timestamp, message, output)
|
||
|
|
||
|
path = self.create_path('junit', '.xml')
|
||
6 years ago
|
|
||
6 years ago
|
with open(path, 'w') as junit_fd:
|
||
6 years ago
|
junit_fd.write(xml.lstrip())
|
||
6 years ago
|
|
||
|
|
||
8 years ago
|
class TestSuccess(TestResult):
|
||
|
"""Test success."""
|
||
|
def write_junit(self, args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
|
test_case = self.junit.TestCase(classname=self.command, name=self.name)
|
||
|
|
||
|
self.save_junit(args, test_case)
|
||
|
|
||
|
|
||
|
class TestSkipped(TestResult):
|
||
|
"""Test skipped."""
|
||
|
def write_console(self):
|
||
|
"""Write results to console."""
|
||
|
display.info('No tests applicable.', verbosity=1)
|
||
|
|
||
|
def write_junit(self, args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
|
test_case = self.junit.TestCase(classname=self.command, name=self.name)
|
||
|
test_case.add_skipped_info('No tests applicable.')
|
||
|
|
||
|
self.save_junit(args, test_case)
|
||
|
|
||
|
|
||
|
class TestFailure(TestResult):
|
||
|
"""Test failure."""
|
||
|
def __init__(self, command, test, python_version=None, messages=None, summary=None):
|
||
|
"""
|
||
|
:type command: str
|
||
|
:type test: str
|
||
8 years ago
|
:type python_version: str | None
|
||
|
:type messages: list[TestMessage] | None
|
||
7 years ago
|
:type summary: unicode | None
|
||
8 years ago
|
"""
|
||
|
super(TestFailure, self).__init__(command, test, python_version)
|
||
|
|
||
8 years ago
|
if messages:
|
||
5 years ago
|
messages = sorted(messages)
|
||
7 years ago
|
else:
|
||
|
messages = []
|
||
8 years ago
|
|
||
7 years ago
|
self.messages = messages
|
||
8 years ago
|
self.summary = summary
|
||
|
|
||
8 years ago
|
def write(self, args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
|
if args.metadata.changes:
|
||
|
self.populate_confidence(args.metadata)
|
||
|
|
||
|
super(TestFailure, self).write(args)
|
||
|
|
||
8 years ago
|
def write_console(self):
|
||
|
"""Write results to console."""
|
||
|
if self.summary:
|
||
|
display.error(self.summary)
|
||
|
else:
|
||
|
if self.python_version:
|
||
|
specifier = ' on python %s' % self.python_version
|
||
|
else:
|
||
|
specifier = ''
|
||
|
|
||
|
display.error('Found %d %s issue(s)%s which need to be resolved:' % (len(self.messages), self.test or self.command, specifier))
|
||
|
|
||
|
for message in self.messages:
|
||
8 years ago
|
display.error(message.format(show_confidence=True))
|
||
8 years ago
|
|
||
|
def write_lint(self):
|
||
|
"""Write lint results to stdout."""
|
||
|
if self.summary:
|
||
|
command = self.format_command()
|
||
|
message = 'The test `%s` failed. See stderr output for details.' % command
|
||
5 years ago
|
path = ''
|
||
8 years ago
|
message = TestMessage(message, path)
|
||
|
print(message)
|
||
|
else:
|
||
|
for message in self.messages:
|
||
|
print(message)
|
||
|
|
||
|
def write_junit(self, args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
|
title = self.format_title()
|
||
|
output = self.format_block()
|
||
|
|
||
|
test_case = self.junit.TestCase(classname=self.command, name=self.name)
|
||
|
|
||
|
# Include a leading newline to improve readability on Shippable "Tests" tab.
|
||
|
# Without this, the first line becomes indented.
|
||
|
test_case.add_failure_info(message=title, output='\n%s' % output)
|
||
|
|
||
|
self.save_junit(args, test_case)
|
||
|
|
||
|
def write_bot(self, args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
"""
|
||
7 years ago
|
docs = self.find_docs()
|
||
7 years ago
|
message = self.format_title(help_link=docs)
|
||
|
output = self.format_block()
|
||
8 years ago
|
|
||
8 years ago
|
if self.messages:
|
||
|
verified = all((m.confidence or 0) >= 50 for m in self.messages)
|
||
|
else:
|
||
|
verified = False
|
||
|
|
||
8 years ago
|
bot_data = dict(
|
||
8 years ago
|
verified=verified,
|
||
7 years ago
|
docs=docs,
|
||
8 years ago
|
results=[
|
||
|
dict(
|
||
|
message=message,
|
||
|
output=output,
|
||
|
),
|
||
|
],
|
||
|
)
|
||
|
|
||
|
path = self.create_path('bot', '.json')
|
||
|
|
||
|
if args.explain:
|
||
|
return
|
||
|
|
||
5 years ago
|
make_dirs(os.path.dirname(path))
|
||
|
|
||
8 years ago
|
with open(path, 'w') as bot_fd:
|
||
8 years ago
|
json.dump(bot_data, bot_fd, indent=4, sort_keys=True)
|
||
|
bot_fd.write('\n')
|
||
|
|
||
8 years ago
|
def populate_confidence(self, metadata):
|
||
|
"""
|
||
|
:type metadata: Metadata
|
||
|
"""
|
||
|
for message in self.messages:
|
||
|
if message.confidence is None:
|
||
|
message.confidence = calculate_confidence(message.path, message.line, metadata)
|
||
|
|
||
8 years ago
|
def format_command(self):
|
||
|
"""
|
||
|
:rtype: str
|
||
|
"""
|
||
|
command = 'ansible-test %s' % self.command
|
||
|
|
||
|
if self.test:
|
||
|
command += ' --test %s' % self.test
|
||
|
|
||
|
if self.python_version:
|
||
|
command += ' --python %s' % self.python_version
|
||
|
|
||
|
return command
|
||
|
|
||
7 years ago
|
def find_docs(self):
|
||
|
"""
|
||
|
:rtype: str
|
||
|
"""
|
||
|
testing_docs_url = 'https://docs.ansible.com/ansible/devel/dev_guide/testing'
|
||
|
testing_docs_dir = 'docs/docsite/rst/dev_guide/testing'
|
||
|
|
||
|
url = '%s/%s/' % (testing_docs_url, self.command)
|
||
|
path = os.path.join(testing_docs_dir, self.command)
|
||
|
|
||
|
if self.test:
|
||
|
url += '%s.html' % self.test
|
||
|
path = os.path.join(path, '%s.rst' % self.test)
|
||
|
|
||
|
if os.path.exists(path):
|
||
|
return url
|
||
|
|
||
|
return None
|
||
|
|
||
7 years ago
|
def format_title(self, help_link=None):
|
||
8 years ago
|
"""
|
||
7 years ago
|
:type help_link: str | None
|
||
8 years ago
|
:rtype: str
|
||
|
"""
|
||
|
command = self.format_command()
|
||
|
|
||
|
if self.summary:
|
||
7 years ago
|
reason = 'the error'
|
||
8 years ago
|
else:
|
||
7 years ago
|
reason = '1 error' if len(self.messages) == 1 else '%d errors' % len(self.messages)
|
||
8 years ago
|
|
||
7 years ago
|
if help_link:
|
||
7 years ago
|
help_link_markup = ' [[explain](%s)]' % help_link
|
||
7 years ago
|
else:
|
||
|
help_link_markup = ''
|
||
|
|
||
7 years ago
|
title = 'The test `%s`%s failed with %s:' % (command, help_link_markup, reason)
|
||
8 years ago
|
|
||
|
return title
|
||
|
|
||
|
def format_block(self):
|
||
|
"""
|
||
|
:rtype: str
|
||
|
"""
|
||
|
if self.summary:
|
||
|
block = self.summary
|
||
|
else:
|
||
7 years ago
|
block = '\n'.join(m.format() for m in self.messages)
|
||
8 years ago
|
|
||
|
message = block.strip()
|
||
|
|
||
|
# Hack to remove ANSI color reset code from SubprocessError messages.
|
||
|
message = message.replace(display.clear, '')
|
||
|
|
||
|
return message
|
||
|
|
||
|
|
||
5 years ago
|
class TestMessage:
|
||
8 years ago
|
"""Single test message for one file."""
|
||
8 years ago
|
def __init__(self, message, path, line=0, column=0, level='error', code=None, confidence=None):
|
||
8 years ago
|
"""
|
||
|
:type message: str
|
||
|
:type path: str
|
||
|
:type line: int
|
||
|
:type column: int
|
||
|
:type level: str
|
||
|
:type code: str | None
|
||
8 years ago
|
:type confidence: int | None
|
||
8 years ago
|
"""
|
||
5 years ago
|
self.__path = path
|
||
|
self.__line = line
|
||
|
self.__column = column
|
||
|
self.__level = level
|
||
|
self.__code = code
|
||
|
self.__message = message
|
||
|
|
||
8 years ago
|
self.confidence = confidence
|
||
8 years ago
|
|
||
5 years ago
|
@property
|
||
|
def path(self): # type: () -> str
|
||
|
"""Return the path."""
|
||
|
return self.__path
|
||
|
|
||
|
@property
|
||
|
def line(self): # type: () -> int
|
||
|
"""Return the line number, or 0 if none is available."""
|
||
|
return self.__line
|
||
|
|
||
|
@property
|
||
|
def column(self): # type: () -> int
|
||
|
"""Return the column number, or 0 if none is available."""
|
||
|
return self.__column
|
||
|
|
||
|
@property
|
||
|
def level(self): # type: () -> str
|
||
|
"""Return the level."""
|
||
|
return self.__level
|
||
|
|
||
|
@property
|
||
|
def code(self): # type: () -> t.Optional[str]
|
||
|
"""Return the code, if any."""
|
||
|
return self.__code
|
||
|
|
||
|
@property
|
||
|
def message(self): # type: () -> str
|
||
|
"""Return the message."""
|
||
|
return self.__message
|
||
|
|
||
|
@property
|
||
|
def tuple(self): # type: () -> t.Tuple[str, int, int, str, t.Optional[str], str]
|
||
|
"""Return a tuple with all the immutable values of this test message."""
|
||
|
return self.__path, self.__line, self.__column, self.__level, self.__code, self.__message
|
||
|
|
||
|
def __lt__(self, other):
|
||
|
return self.tuple < other.tuple
|
||
|
|
||
|
def __le__(self, other):
|
||
|
return self.tuple <= other.tuple
|
||
|
|
||
|
def __eq__(self, other):
|
||
|
return self.tuple == other.tuple
|
||
|
|
||
|
def __ne__(self, other):
|
||
|
return self.tuple != other.tuple
|
||
|
|
||
|
def __gt__(self, other):
|
||
|
return self.tuple > other.tuple
|
||
|
|
||
|
def __ge__(self, other):
|
||
|
return self.tuple >= other.tuple
|
||
|
|
||
|
def __hash__(self):
|
||
|
return hash(self.tuple)
|
||
|
|
||
8 years ago
|
def __str__(self):
|
||
8 years ago
|
return self.format()
|
||
|
|
||
|
def format(self, show_confidence=False):
|
||
|
"""
|
||
|
:type show_confidence: bool
|
||
|
:rtype: str
|
||
|
"""
|
||
5 years ago
|
if self.__code:
|
||
|
msg = '%s %s' % (self.__code, self.__message)
|
||
8 years ago
|
else:
|
||
5 years ago
|
msg = self.__message
|
||
8 years ago
|
|
||
8 years ago
|
if show_confidence and self.confidence is not None:
|
||
|
msg += ' (%d%%)' % self.confidence
|
||
|
|
||
5 years ago
|
return '%s:%s:%s: %s' % (self.__path, self.__line, self.__column, msg)
|