mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
python: tests: refactor to use unittest's discovery
Instead of running all the tests manually, the unittest module has a 'discovery' mode to run tests by importing them from a directory: this requires the tests to have different filenames, since they need to be imported as modules now (hence an empty __init__.py is added), and the current naming does not match the convention. Using unittest as loader/runner brings another change: tests skipped as whole cannot be done anymore with exit(77), since they are not run but imported: thus introduce an helper module with decorators applied to the test classes to skip them according to the current checks. This also gets us nicer recordings in the unittest log. Due to the relative imports (needed for the helper code), it is no more possible to execute tests anymore by invoking them manually; although it is possible to run single tests, still using unittest's runner: $ cd python python$ ../run python -m unittest discover -v t test010Load.py This does not change anything in what the tests do/check.
This commit is contained in:
@@ -16,26 +16,7 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
errors=0
|
||||
|
||||
guestsdir="$(cd ../test-data/phony-guests && pwd)"
|
||||
export guestsdir
|
||||
|
||||
for f in $srcdir/t/*.py; do
|
||||
$PYTHON $f
|
||||
r=$?
|
||||
case $r in
|
||||
0) ;;
|
||||
77)
|
||||
echo "$f: test skipped"
|
||||
;;
|
||||
*)
|
||||
echo "FAIL: $f"
|
||||
((errors++))
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ $errors -gt 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
$PYTHON -m unittest discover -v
|
||||
|
||||
0
python/t/__init__.py
Normal file
0
python/t/__init__.py
Normal file
@@ -20,6 +20,3 @@ import unittest
|
||||
class Test010Load (unittest.TestCase):
|
||||
def test_import (self):
|
||||
import guestfs
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -34,6 +34,3 @@ class Test070OptArgs (unittest.TestCase):
|
||||
|
||||
def tearDown (self):
|
||||
self.g.close ()
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -41,6 +41,3 @@ class Test080Version (unittest.TestCase):
|
||||
|
||||
def test_extra (self):
|
||||
self.assertIsInstance (self.version['extra'], str)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -132,7 +132,3 @@ class Test090PythonRetValues (unittest.TestCase):
|
||||
self.assertEqual (g.internal_test_rbufferout ("test"), b'test')
|
||||
|
||||
self.assertRaises (RuntimeError, g.internal_test_rbufferouterr)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -32,6 +32,3 @@ class Test100Launch (unittest.TestCase):
|
||||
self.assertEqual (g.lvs (), ["/dev/VG/LV1", "/dev/VG/LV2"])
|
||||
g.shutdown ()
|
||||
g.close ()
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -36,6 +36,3 @@ class Test410CloseEvent (unittest.TestCase):
|
||||
self.assertEqual (close_invoked, 0)
|
||||
g.close ()
|
||||
self.assertEqual (close_invoked, 1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -52,6 +52,3 @@ class Test420LogMessages (unittest.TestCase):
|
||||
g.close ()
|
||||
|
||||
self.assertNotEqual (log_invoked, 0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -52,6 +52,3 @@ class Test800ExplicitClose (unittest.TestCase):
|
||||
|
||||
del g
|
||||
self.assertEqual (close_invoked, 1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -35,6 +35,3 @@ class Test810RHBZ811650 (unittest.TestCase):
|
||||
|
||||
def tearDown (self):
|
||||
os.unlink ("rhbz811650.img")
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -23,38 +23,14 @@ from subprocess import check_output
|
||||
import unittest
|
||||
import random
|
||||
import string
|
||||
import re
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
import guestfs
|
||||
from .tests_helper import *
|
||||
|
||||
try:
|
||||
import libvirt
|
||||
except:
|
||||
print ("skipping test: could not import python-libvirt")
|
||||
exit (77)
|
||||
|
||||
# If the backend is not libvirt, skip the test.
|
||||
backend = guestfs.GuestFS().get_backend()
|
||||
rex = re.compile ("^libvirt")
|
||||
if not rex.match (backend):
|
||||
print ("skipping test: backend is not libvirt")
|
||||
exit (77)
|
||||
|
||||
# If the architecture doesn't support IDE, skip the test.
|
||||
machine = platform.machine ()
|
||||
rex = re.compile ("i.86")
|
||||
if machine != "x86_64" and not rex.match (machine):
|
||||
print ("skipping test: arch is not x86 and does not support IDE")
|
||||
exit (77)
|
||||
|
||||
conn = libvirt.open (None)
|
||||
|
||||
# Check we're using the version of libvirt-python that has c_pointer() methods.
|
||||
if not "c_pointer" in dir (conn):
|
||||
print ("skipping test: libvirt-python doesn't support c_pointer()")
|
||||
exit (77)
|
||||
|
||||
@skipUnlessArchMatches ("(i.86|x86_64)") # If the architecture doesn't support IDE, skip the test.
|
||||
@skipUnlessGuestfsBackendIs ('libvirt')
|
||||
@skipUnlessLibirtHasCPointer ()
|
||||
class Test820RHBZ912499 (unittest.TestCase):
|
||||
def setUp (self):
|
||||
# Create a test disk.
|
||||
@@ -87,6 +63,9 @@ class Test820RHBZ912499 (unittest.TestCase):
|
||||
""" % (self.domname, self.filename)
|
||||
|
||||
def test_rhbz912499 (self):
|
||||
import libvirt
|
||||
|
||||
conn = libvirt.open (None)
|
||||
dom = conn.createXML (self.xml,
|
||||
libvirt.VIR_DOMAIN_START_AUTODESTROY)
|
||||
self.assertIsNotNone (dom)
|
||||
@@ -113,6 +92,3 @@ class Test820RHBZ912499 (unittest.TestCase):
|
||||
|
||||
def tearDown (self):
|
||||
os.unlink (self.filename)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
@@ -22,31 +22,21 @@
|
||||
|
||||
import unittest
|
||||
import os
|
||||
import sys
|
||||
import guestfs
|
||||
from .tests_helper import *
|
||||
|
||||
guestsdir = os.environ['guestsdir']
|
||||
|
||||
try:
|
||||
import libvirt
|
||||
except:
|
||||
print ("could not import python-libvirt")
|
||||
exit (77)
|
||||
|
||||
conn = libvirt.open ("test:///%s/guests.xml" % guestsdir)
|
||||
|
||||
# Check we're using the version of libvirt-python that has c_pointer() methods.
|
||||
if not "c_pointer" in dir (conn):
|
||||
print ("skipping test: libvirt-python doesn't support c_pointer()")
|
||||
exit (77)
|
||||
|
||||
@skipUnlessLibirtHasCPointer ()
|
||||
class Test910Libvirt (unittest.TestCase):
|
||||
def test_libvirt (self):
|
||||
import libvirt
|
||||
|
||||
conn = libvirt.open ("test:///%s/guests.xml" % guestsdir)
|
||||
dom = conn.lookupByName ("blank-disk")
|
||||
|
||||
g = guestfs.GuestFS ()
|
||||
|
||||
r = g.add_libvirt_dom (dom, readonly=1)
|
||||
self.assertEqual (r, 1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main ()
|
||||
63
python/t/tests_helper.py
Normal file
63
python/t/tests_helper.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# libguestfs Python bindings
|
||||
# Copyright (C) 2016 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.
|
||||
|
||||
# Utilities for the tests of the Python bindings.
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
def skipUnlessLibirtHasCPointer ():
|
||||
"""
|
||||
Skip the current class/method if:
|
||||
(a) libvirt cannot be imported (e.g. not installed)
|
||||
(b) connection objects don't have the c_pointer() method (too old)
|
||||
"""
|
||||
try:
|
||||
import libvirt
|
||||
except:
|
||||
return unittest.skip ("could not import libvirt")
|
||||
# Check we're using the version of libvirt-python that has c_pointer() methods.
|
||||
if not "c_pointer" in dir (libvirt.open (None)):
|
||||
return unittest.skip ("libvirt-python doesn't support c_pointer()")
|
||||
return lambda func: func
|
||||
|
||||
|
||||
def skipUnlessGuestfsBackendIs (wanted):
|
||||
"""
|
||||
Skip the current class/method if the default backend of libguestfs
|
||||
is not 'wanted'.
|
||||
"""
|
||||
import guestfs
|
||||
backend = guestfs.GuestFS ().get_backend ()
|
||||
# Match both "backend" and "backend:etc"
|
||||
if not (backend == wanted or backend.startswith (wanted + ":")):
|
||||
return unittest.skip ("the current backend is not %s" % wanted)
|
||||
return lambda func: func
|
||||
|
||||
|
||||
def skipUnlessArchMatches (arch_re):
|
||||
"""
|
||||
Skip the current class/method if the current architecture does not match
|
||||
the regexp specified.
|
||||
"""
|
||||
import platform
|
||||
import re
|
||||
machine = platform.machine ()
|
||||
rex = re.compile (arch_re)
|
||||
if not rex.match (machine):
|
||||
return unittest.skip ("the current architecture (%s) does not match '%s'" % (machine, arch_re))
|
||||
return lambda func: func
|
||||
Reference in New Issue
Block a user