提交 fa0e77a7 编写于 作者: L Lukáš Doktor 提交者: Lucas Meneghel Rodrigues

scripts.avocado-bash-utils: Add Avocado bash utils

This patch is initial support for people using custom bash scripts
with avocado. Tests should use:

    PATH=$(avocado "exec-path"):$PATH

and then they can utilize the helpers.

This version contain functions to write to Test.log the same way it's
possible from python including failing the test with TestWarn in case
avocado_warn was used.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 39618007
......@@ -55,6 +55,11 @@ these days a framework) to perform automated testing.
%{_docdir}/avocado/avocado-rest-client.rst
%exclude %{python_sitelib}/avocado/plugins/htmlresult.py*
%exclude %{python_sitelib}/avocado/plugins/resources/htmlresult/*
%{_libexecdir}/avocado/avocado-bash-utils
%{_libexecdir}/avocado/avocado_debug
%{_libexecdir}/avocado/avocado_error
%{_libexecdir}/avocado/avocado_info
%{_libexecdir}/avocado/avocado_warn
%package plugins-output-html
Summary: Avocado HTML report plugin
......
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2013-2014
"""
Libexec PATHs modifier
"""
import os
import sys
from avocado.core import exit_codes, output
from avocado.plugins import plugin
class ExecPath(plugin.Plugin):
"""
Implements the avocado 'exec-path' subcommand
"""
name = 'exec_path'
enabled = True
priority = 0
view = None
def configure(self, parser):
"""
Add the subparser for the exec-path action.
:param parser: Main test runner parser.
"""
self.parser = parser.subcommands.add_parser(
'exec-path',
help='Returns path to avocado bash libraries and exits.')
super(ExecPath, self).configure(self.parser)
def run(self, args):
"""
Print libexec path and finish
:param args: Command line args received from the run subparser.
"""
self.view = output.View(app_args=args, use_paginator=False)
if 'VIRTUAL_ENV' in os.environ:
self.view.notify(event='minor', msg='libexec')
elif os.path.exists('/usr/libexec/avocado'):
self.view.notify(event='minor', msg='/usr/libexec/avocado')
elif os.path.exists('/usr/lib/avocado'):
self.view.notify(event='minor', msg='/usr/lib/avocado')
else:
self.view.notify(event='error',
msg="Can't locate avocado libexec path")
sys.exit(exit_codes.AVOCADO_FAIL)
return sys.exit(exit_codes.AVOCADO_ALL_OK)
......@@ -11,6 +11,7 @@
# This code was inspired in the autotest project,
# client/shared/test.py
# Authors: Martin J Bligh <mbligh@google.com>, Andy Whitcroft <apw@shadowen.org>
import re
"""
Contains the base test implementation, used as a base for the actual
......@@ -524,6 +525,9 @@ class SimpleTest(Test):
Run an arbitrary command that returns either 0 (PASS) or !=0 (FAIL).
"""
re_avocado_log = re.compile(r'^\d\d:\d\d:\d\d DEBUG\| \[stdout\]'
r' \d\d:\d\d:\d\d WARN \|')
def __init__(self, path, params=None, base_logdir=None, tag=None, job=None):
self.path = os.path.abspath(path)
super(SimpleTest, self).__init__(name=path, base_logdir=base_logdir,
......@@ -543,9 +547,8 @@ class SimpleTest(Test):
:param result: :class:`avocado.utils.process.CmdResult` instance.
"""
run_info = str(result)
for line in run_info.splitlines():
self.log.info(line)
self.log.info("Exit status: %s", result.exit_status)
self.log.info("Duration: %s", result.duration)
def action(self):
"""
......@@ -562,6 +565,14 @@ class SimpleTest(Test):
self._log_detailed_cmd_info(details.result)
raise exceptions.TestFail(details)
def runTest(self, result=None):
super(SimpleTest, self).runTest(result)
for line in open(self.logfile):
if self.re_avocado_log.match(line):
raise exceptions.TestWarn("Test passed but there were warnings"
" on stdout during execution. Check "
"the log for details.")
class MissingTest(Test):
......
......@@ -711,6 +711,20 @@ Here are the current variables that Avocado exports to the tests:
| * | All variables from --multiplex-file | TIMEOUT=60; IO_WORKERS=10; VM_BYTES=512M; ... |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
Simple Tests BASH extensions
============================
To enhance simple tests one can use supported set of libraries we created. The
only requirement is to use::
PATH=$(avocado "exec-path"):$PATH
which injects path to avocado utils into shell PATH. Take a look into
``avocado exec-path`` to see list of available functions and take a look at
``examples/tests/simplewarning.sh`` for inspiration.
Wrap Up
=======
......
#!/bin/sh
PATH=$(avocado "exec-path"):$PATH
avocado_debug "Debug message"
avocado_info "Info message"
avocado_warn "Warning message (should cause this test to finish with warning)"
avocado_error "Error message (ordinary message not changing the results)"
echo "Simple output without log-level specification"
exit 0 # no error reported
#!/bin/sh
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; specifically version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2014
# Write to avocado_log
# First argument is the log level
avocado_log() {
LEVEL=`printf '%-5s' $1`
shift
echo "`date '+%H:%M:%S'` $LEVEL| $*"
}
#!/bin/sh
. avocado-bash-utils
avocado_log DEBUG $*
#!/bin/sh
. avocado-bash-utils
avocado_log ERROR $*
#!/bin/sh
. avocado-bash-utils
avocado_log INFO $*
#!/bin/sh
. avocado-bash-utils
avocado_log WARN $*
......@@ -20,6 +20,7 @@ import sys
# simple magic for using scripts within a source tree
basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if os.path.isdir(os.path.join(basedir, 'avocado')):
os.environ['PATH'] += ":" + os.path.join(basedir, 'libexec')
sys.path.append(basedir)
from avocado.cli.app import AvocadoApp
......
......@@ -266,6 +266,24 @@ class RunnerSimpleTest(unittest.TestCase):
self.assertEqual(result.exit_status, expected_rc,
"Avocado did not return rc %d:\n%s" % (expected_rc, result))
def test_simplewarning(self):
"""
simplewarning.sh uses the avocado-bash-utils
"""
os.chdir(basedir)
cmd_line = ('./scripts/avocado run --sysinfo=off '
'examples/tests/simplewarning.sh --show-job-log')
result = process.run(cmd_line, ignore_status=True)
self.assertEqual(result.exit_status, 0,
"Avocado did not return rc 0:\n%s" %
(result))
self.assertIn('DEBUG| Debug message', result.stdout, result)
self.assertIn('INFO | Info message', result.stdout, result)
self.assertIn('WARN | Warning message (should cause this test to '
'finish with warning)', result.stdout, result)
self.assertIn('ERROR| Error message (ordinary message not changing '
'the results)', result.stdout, result)
def tearDown(self):
self.pass_script.remove()
self.fail_script.remove()
......
......@@ -48,6 +48,15 @@ def get_tests_dir():
return get_dir(['usr', 'share', 'avocado', 'tests'], ['tests'])
def get_avocado_libexec_dir():
if VIRTUAL_ENV:
return get_dir(['libexec'])
elif os.path.exists('/usr/libexec'): # RHEL-like distro
return get_dir(['usr', 'libexec', 'avocado'])
else: # Debian-like distro
return get_dir(['usr', 'lib', 'avocado'])
def get_data_files():
data_files = [(get_dir(['etc', 'avocado']), ['etc/avocado/avocado.conf'])]
data_files += [(get_dir(['etc', 'avocado', 'conf.d']),
......@@ -65,6 +74,7 @@ def get_data_files():
glob.glob('examples/wrappers/*.sh'))]
data_files += [(get_dir(['usr', 'share', 'avocado', 'api'], ['api']),
glob.glob('examples/api/*/*.py'))]
data_files.append((get_avocado_libexec_dir(), glob.glob('libexec/*')))
return data_files
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册