diff --git a/ovirtlago/cmd.py b/ovirtlago/cmd.py index f7d0b3d..aa1677e 100755 --- a/ovirtlago/cmd.py +++ b/ovirtlago/cmd.py @@ -91,12 +91,19 @@ def do_ovirt_revert(prefix, snapshot_name, **kwargs): help='Path to tests file to run', metavar='TEST_FILE', ) +@cli_plugin_add_argument( + '--junitxml-file', + help='Directory/file name to store junit xml file', + dest='junitxml_file', + type=str, + default=None, +) @in_ovirt_prefix @with_logging -def do_ovirt_runtest(prefix, test_file, **kwargs): +def do_ovirt_runtest(prefix, test_file, junitxml_file, **kwargs): if not os.path.exists(test_file): raise RuntimeError('Test file not found') - if not prefix.run_test(test_file): + if not prefix.run_test(test_file, junitxml_file): raise RuntimeError('Some tests failed') diff --git a/ovirtlago/prefix.py b/ovirtlago/prefix.py index 2d0148e..aee9ca9 100644 --- a/ovirtlago/prefix.py +++ b/ovirtlago/prefix.py @@ -24,6 +24,7 @@ import time from configparser import SafeConfigParser import sys +from future.builtins import super import nose.core import nose.config @@ -49,6 +50,61 @@ log_task = functools.partial(log_utils.log_task, logger=LOGGER) +def _create_output_filename( + default_dir, default_filename, output_filename=None +): + """ + Given a default_dir, default_filename, output_filename(optional) + returns the result_path + + Args: + default_dir (str): Containing the default directory for keeping + the log file + default_filename (str): Containing the default filename for the + log file + output_filename (str): Containing the requested filename for + the log file (can be a filename or a full/relative path) + + Returns: + str: results_path represents the fullname of the output file + ( dir + basename ) + """ + + if output_filename: + dirname, basename = os.path.split(output_filename) + if dirname: + default_dir = dirname + if basename: + default_filename = basename + + results_path = os.path.abspath( + os.path.join( + default_dir, + default_filename, + ) + ) + return results_path + + +def _safe_makedir(path): + """ + Given a path recursivley create the directories and + don't fail if already exists + + Args: + path (str): Containing the path to be created + + Raises: + OSError: if it fails to create the directory + (if directory exists no exception will be raise) + """ + try: + os.makedirs(path) + except OSError as e: + if e.errno != os.errno.EEXIST: + raise + + class OvirtPrefix(Prefix): VIRT_ENV_CLASS = virt.OvirtVirtEnv @@ -174,17 +230,16 @@ def prepare_repo( self.save() @reposetup.with_repo_server - def run_test(self, path): + def run_test(self, path, junitxml_file=None): with LogTask('Run test: %s' % os.path.basename(path)): env = os.environ.copy() env['LAGO_PREFIX'] = self.paths.prefix - results_path = os.path.abspath( - os.path.join( - self.paths.prefix, - '%s.junit.xml' % os.path.basename(path), - ) + results_path = _create_output_filename( + self.paths.prefix, + os.path.basename(path) + ".junit.xml", junitxml_file ) + _safe_makedir(os.path.dirname(results_path)) extra_args = [ '--with-xunit', '--xunit-file=%s' % results_path, diff --git a/test-requires.txt b/test-requires.txt index 28120fd..846a628 100644 --- a/test-requires.txt +++ b/test-requires.txt @@ -12,3 +12,4 @@ lago mock ovirt-engine-sdk-python ovirt-engine-sdk===3.2.0.11-SNAPSHOT +future \ No newline at end of file diff --git a/tests/functional/ovirt.runtest.bats b/tests/functional/ovirt.runtest.bats index 8eebb6e..3bbb204 100644 --- a/tests/functional/ovirt.runtest.bats +++ b/tests/functional/ovirt.runtest.bats @@ -41,6 +41,24 @@ unset LAGO__START__WAIT_SUSPEND done } +@test "ovirt.runtest: simple runtest to verify junitxml_dir" { + common.is_initialized "$WORKDIR" || skip "Workdir not initiated" + cd "$FIXTURES" + + local testfiles=( + "001_basic_test.py" + ) + + for testfile in "${testfiles[@]}"; do + local junitxml_file="$PREFIX/junit_xml/$testfile.junit.xml" + helpers.run_ok "$LAGOCLI" ovirt runtest --junitxml-file "$junitxml_file" "$testfile" + helpers.contains "$output" "${testfile%.*}.test_pass" + helpers.is_file "$junitxml_file" + helpers.contains \ + "$(cat $junitxml_file)" \ + 'errors="0"' + done +} @test "ovirt.runtest: failing a test fails the run" { common.is_initialized "$WORKDIR" || skip "Workdir not initiated" diff --git a/tests/unit/ovirtlago/test_ovirtlago_junitxml.py b/tests/unit/ovirtlago/test_ovirtlago_junitxml.py new file mode 100644 index 0000000..78191fa --- /dev/null +++ b/tests/unit/ovirtlago/test_ovirtlago_junitxml.py @@ -0,0 +1,54 @@ +# +# Copyright 2017 Red Hat, Inc. +# +# 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 the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Refer to the README and COPYING files for full details of the license +# +import pytest + +from ovirtlago import prefix + + +class TestJunitXMLdir(object): + @pytest.mark.parametrize( + "default_dir, default_file, junitxml_file, expect", + [ + ( + '/home/default', 'test01.py.junit.xml', 'foo.xml', + '/home/default/foo.xml' + ), + ( + '/home/default', 'test01.py.junit.xml', 'foo', + '/home/default/foo' + ), + ( + '/home/default', 'test01.py.junit.xml', '/home/xxxx', + '/home/xxxx' + ), + ( + '/home/default', 'test01.py.junit.xml', '/home/xxxx/', + '/home/xxxx/test01.py.junit.xml' + ), + ( + '/home/default', 'test01.py.junit.xml', '', + '/home/default/test01.py.junit.xml' + ), + ], + ) + def test_filename(self, default_dir, default_file, junitxml_file, expect): + assert prefix._create_output_filename( + default_dir, default_file, junitxml_file + ) == expect