Skip to content

Commit

Permalink
Merge pull request #54 from galitf/runtests
Browse files Browse the repository at this point in the history
runtest gets junitxml-dir name to keep xml files
  • Loading branch information
ovirt-infra authored Jul 30, 2018
2 parents 60abfb0 + c41f89e commit a221c5f
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 8 deletions.
11 changes: 9 additions & 2 deletions ovirtlago/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down
67 changes: 61 additions & 6 deletions ovirtlago/prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import time
from configparser import SafeConfigParser
import sys
from future.builtins import super

import nose.core
import nose.config
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test-requires.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ lago
mock
ovirt-engine-sdk-python
ovirt-engine-sdk===3.2.0.11-SNAPSHOT
future
18 changes: 18 additions & 0 deletions tests/functional/ovirt.runtest.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/ovirtlago/test_ovirtlago_junitxml.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a221c5f

Please sign in to comment.