diff --git a/docs/guestfs-building.pod b/docs/guestfs-building.pod index 825ff91d2..069315671 100644 --- a/docs/guestfs-building.pod +++ b/docs/guestfs-building.pod @@ -303,6 +303,10 @@ Optional. Used to build the Python bindings. For building Python 2 or Python 3 bindings, see L below. +=item Python C + +Optional. Used to run the Python testsuite. + =item Ruby =item rake diff --git a/python/t/010-load.py b/python/t/010-load.py index 36accf833..aa1470617 100644 --- a/python/t/010-load.py +++ b/python/t/010-load.py @@ -15,4 +15,11 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import guestfs +import unittest + +class Test010Load (unittest.TestCase): + def test_import (self): + import guestfs + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/070-optargs.py b/python/t/070-optargs.py index 7be0ce64d..c3e9ddd80 100644 --- a/python/t/070-optargs.py +++ b/python/t/070-optargs.py @@ -15,11 +15,25 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import unittest import os import guestfs -g = guestfs.GuestFS (python_return_dict=True) -g.add_drive ("/dev/null") -g.add_drive ("/dev/null", readonly = True) -g.add_drive ("/dev/null", iface = "virtio", format = "raw") -g.close () +class Test070OptArgs (unittest.TestCase): + def setUp (self): + self.g = guestfs.GuestFS (python_return_dict=True) + + def test_no_optargs (self): + self.g.add_drive ("/dev/null") + + def test_one_optarg (self): + self.g.add_drive ("/dev/null", readonly = True) + + def test_two_optargs (self): + self.g.add_drive ("/dev/null", iface = "virtio", format = "raw") + + def tearDown (self): + self.g.close () + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/100-launch.py b/python/t/100-launch.py index 152846e47..7c70eccdd 100644 --- a/python/t/100-launch.py +++ b/python/t/100-launch.py @@ -15,18 +15,23 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import unittest import os import guestfs -g = guestfs.GuestFS (python_return_dict=True) -g.add_drive_scratch (500 * 1024 * 1024) -g.launch () +class Test100Launch (unittest.TestCase): + def test_launch (self): + g = guestfs.GuestFS (python_return_dict=True) + g.add_drive_scratch (500 * 1024 * 1024) + g.launch () -g.pvcreate ("/dev/sda") -g.vgcreate ("VG", ["/dev/sda"]) -g.lvcreate ("LV1", "VG", 200) -g.lvcreate ("LV2", "VG", 200) -if (g.lvs () != ["/dev/VG/LV1", "/dev/VG/LV2"]): - raise "Error: g.lvs() returned incorrect result" -g.shutdown () -g.close () + g.pvcreate ("/dev/sda") + g.vgcreate ("VG", ["/dev/sda"]) + g.lvcreate ("LV1", "VG", 200) + g.lvcreate ("LV2", "VG", 200) + self.assertEqual (g.lvs (), ["/dev/VG/LV1", "/dev/VG/LV2"]) + g.shutdown () + g.close () + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/410-close-event.py b/python/t/410-close-event.py index 965b5d107..5aa82a4af 100644 --- a/python/t/410-close-event.py +++ b/python/t/410-close-event.py @@ -15,23 +15,27 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import unittest import os import guestfs -g = guestfs.GuestFS (python_return_dict=True) - close_invoked = 0 def close_callback (ev, eh, buf, array): global close_invoked close_invoked += 1 -# Register a callback for the close event. -g.set_event_callback (close_callback, guestfs.EVENT_CLOSE) +class Test410CloseEvent (unittest.TestCase): + def test_close_event (self): + g = guestfs.GuestFS (python_return_dict=True) -# Close the handle. The close callback should be invoked. -if close_invoked != 0: - raise "Error: close_invoked should be 0" -g.close () -if close_invoked != 1: - raise "Error: close_invoked should be 1" + # Register a callback for the close event. + g.set_event_callback (close_callback, guestfs.EVENT_CLOSE) + + # Close the handle. The close callback should be invoked. + self.assertEqual (close_invoked, 0) + g.close () + self.assertEqual (close_invoked, 1) + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/420-log-messages.py b/python/t/420-log-messages.py index 470c07a46..002098e4d 100644 --- a/python/t/420-log-messages.py +++ b/python/t/420-log-messages.py @@ -15,11 +15,10 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import unittest import os import guestfs -g = guestfs.GuestFS (python_return_dict=True) - log_invoked = 0 def log_callback (ev,eh,buf,array): @@ -33,20 +32,26 @@ def log_callback (ev,eh,buf,array): print ("python event logged: event=%s eh=%d buf='%s' array=%s" % (guestfs.event_to_string (ev), eh, buf, array)) -# Register an event callback for all log messages. -events = guestfs.EVENT_APPLIANCE | guestfs.EVENT_LIBRARY \ - | guestfs.EVENT_WARNING | guestfs.EVENT_TRACE -g.set_event_callback (log_callback, events) +class Test420LogMessages (unittest.TestCase): + def test_log_messages (self): + g = guestfs.GuestFS (python_return_dict=True) -# Now make sure we see some messages. -g.set_trace (1) -g.set_verbose (1) + # Register an event callback for all log messages. + events = guestfs.EVENT_APPLIANCE | guestfs.EVENT_LIBRARY \ + | guestfs.EVENT_WARNING | guestfs.EVENT_TRACE + g.set_event_callback (log_callback, events) -# Do some stuff. -g.add_drive_ro ("/dev/null") -g.set_autosync (1) + # Now make sure we see some messages. + g.set_trace (1) + g.set_verbose (1) -g.close () + # Do some stuff. + g.add_drive_ro ("/dev/null") + g.set_autosync (1) -if log_invoked == 0: - raise "Error: log_invoked should be > 0" + g.close () + + self.assertFalse (log_invoked == 0) + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/800-explicit-close.py b/python/t/800-explicit-close.py index 9b8ff84ce..9b425afe9 100644 --- a/python/t/800-explicit-close.py +++ b/python/t/800-explicit-close.py @@ -17,43 +17,45 @@ # Test implicit vs explicit closes of the handle (RHBZ#717786). +import unittest import os import guestfs -g = guestfs.GuestFS (python_return_dict=True) - -g.close () # explicit close -del g # implicit close - should be no error/warning - -# Expect an exception if we call a method on a closed handle. -g = guestfs.GuestFS (python_return_dict=True) -g.close () -try: - g.set_memsize (512) - raise Exception("expected an exception from previous statement") -except guestfs.ClosedHandle: - pass -del g - -# Verify that the handle is really being closed by g.close, by setting -# up a close event and testing that it happened. -g = guestfs.GuestFS (python_return_dict=True) - close_invoked = 0 def close_callback (ev, eh, buf, array): global close_invoked close_invoked += 1 -g.set_event_callback (close_callback, guestfs.EVENT_CLOSE) +class Test800ExplicitClose (unittest.TestCase): + def test_explicit_close (self): + g = guestfs.GuestFS (python_return_dict=True) -if close_invoked != 0: - raise Exception("close_invoked should be 0") + g.close () # explicit close + del g # implicit close - should be no error/warning -g.close () -if close_invoked != 1: - raise Exception("close_invoked should be 1") + # Expect an exception if we call a method on a closed handle. + g = guestfs.GuestFS (python_return_dict=True) + g.close () + try: + g.set_memsize (512) + raise Exception("expected an exception from previous statement") + except guestfs.ClosedHandle: + pass + del g -del g -if close_invoked != 1: - raise Exception("close_invoked should be 1") + # Verify that the handle is really being closed by g.close, by + # setting up a close event and testing that it happened. + g = guestfs.GuestFS (python_return_dict=True) + + g.set_event_callback (close_callback, guestfs.EVENT_CLOSE) + + self.assertEqual (close_invoked, 0) + g.close () + self.assertEqual (close_invoked, 1) + + del g + self.assertEqual (close_invoked, 1) + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/810-rhbz811650.py b/python/t/810-rhbz811650.py index 56d806254..c812c30d7 100644 --- a/python/t/810-rhbz811650.py +++ b/python/t/810-rhbz811650.py @@ -15,21 +15,29 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import unittest import os import guestfs -g = guestfs.GuestFS (python_return_dict=True) +class Test810RHBZ811650 (unittest.TestCase): + def test_rhbz811650 (self): + g = guestfs.GuestFS (python_return_dict=True) -g.disk_create ("rhbz811650.img", "raw", 500 * 1024 * 1024) + g.disk_create ("rhbz811650.img", "raw", 500 * 1024 * 1024) -# Deliberate error: the disk format is supposed to be raw. -g.add_drive ("rhbz811650.img", format="qcow2"); + # Deliberate error: the disk format is supposed to be raw. + g.add_drive ("rhbz811650.img", format="qcow2"); -# Because error() wasn't being called, guestfs_last_error would return -# NULL, causing a segfault in the Python bindings (RHBZ#811650). -try: - g.launch () -except: - pass + # Because error() wasn't being called, guestfs_last_error + # would return NULL, causing a segfault in the Python bindings + # (RHBZ#811650). + try: + g.launch () + except: + pass -os.unlink ("rhbz811650.img") + def tearDown (self): + os.unlink ("rhbz811650.img") + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/820-rhbz912499.py b/python/t/820-rhbz912499.py index 1909a3798..3cf9bbaef 100644 --- a/python/t/820-rhbz912499.py +++ b/python/t/820-rhbz912499.py @@ -20,6 +20,7 @@ # https://bugzilla.redhat.com/1075164#c7 from subprocess import check_output +import unittest import random import string import re @@ -54,16 +55,19 @@ if not "c_pointer" in dir (conn): print ("skipping test: libvirt-python doesn't support c_pointer()") exit (77) -# Create a test disk. -filename = os.getcwd () + "/820-rhbz912499.img" -guestfs.GuestFS().disk_create (filename, "raw", 1024*1024*1024) +class Test820RHBZ912499 (unittest.TestCase): + def setUp (self): + # Create a test disk. + self.filename = os.getcwd () + "/820-rhbz912499.img" + guestfs.GuestFS().disk_create (self.filename, "raw", 1024*1024*1024) -# Create a new domain. This won't work, it will just hang when -# booted. But that's sufficient for the test. -domname = ''.join (random.choice (string.ascii_uppercase) for _ in range (8)) -domname = "tmp-" + domname + # Create a new domain. This won't work, it will just hang when + # booted. But that's sufficient for the test. + self.domname = ''.join (random.choice (string.ascii_uppercase) + for _ in range (8)) + self.domname = "tmp-" + self.domname -xml = """ + self.xml = """ %s 1048576 @@ -80,32 +84,35 @@ xml = """ -""" % (domname, filename) +""" % (self.domname, self.filename) -dom = conn.createXML (xml, libvirt.VIR_DOMAIN_START_AUTODESTROY) -if dom == None: - raise "could not create temporary domain (%s)" % domname + def test_rhbz912499 (self): + dom = conn.createXML (self.xml, + libvirt.VIR_DOMAIN_START_AUTODESTROY) + self.assertFalse (dom == None) -print ("temporary domain %s is running" % domname) + print ("temporary domain %s is running" % self.domname) -# Libvirt should have labelled the disk. -print ("before starting libguestfs") -before = check_output (["ls", "-Z", filename]) -print ("disk label = %s" % before) + # Libvirt should have labelled the disk. + print ("before starting libguestfs") + before = check_output (["ls", "-Z", self.filename]) + print ("disk label = %s" % before) -# Now see if we can open the domain with libguestfs without -# disturbing the label. -g = guestfs.GuestFS () -r = g.add_libvirt_dom (dom, readonly = 1) -if r != 1: - raise "unexpected return value from add_libvirt_dom (%d)" % r -g.launch () + # Now see if we can open the domain with libguestfs without + # disturbing the label. + g = guestfs.GuestFS () + r = g.add_libvirt_dom (dom, readonly = 1) + self.assertEqual (r, 1) + g.launch () -print ("after starting libguestfs") -after = check_output (["ls", "-Z", filename]) -print ("disk label = %s" % after) + print ("after starting libguestfs") + after = check_output (["ls", "-Z", self.filename]) + print ("disk label = %s" % after) -if before != after: - raise "disk label was changed unexpectedly" + self.assertEqual (before, after) -os.unlink (filename) + def tearDown (self): + os.unlink (self.filename) + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/900-python-dict.py b/python/t/900-python-dict.py index 8ba0e87fe..7d78e8643 100644 --- a/python/t/900-python-dict.py +++ b/python/t/900-python-dict.py @@ -18,17 +18,30 @@ # Test python-specific python_return_dict parameter. from types import * +import unittest import os import guestfs -g = guestfs.GuestFS (python_return_dict=False) +class Test900PythonDict (unittest.TestCase): + def test_python_no_dict (self): + g = guestfs.GuestFS (python_return_dict=False) -r = g.internal_test_rhashtable ("5") -if type(r) != list or r != [ ("0","0"), ("1","1"), ("2","2"), ("3","3"), ("4","4") ]: - raise Exception ("python_return_dict=False: internal_test_rhashtable returned %s" % r) + r = g.internal_test_rhashtable ("5") + self.assertTrue (isinstance (r, list)) + self.assertEqual (r, [ ("0","0"), ("1","1"), ("2","2"), + ("3","3"), ("4","4") ]) -g = guestfs.GuestFS (python_return_dict=True) + def test_python_dict (self): + g = guestfs.GuestFS (python_return_dict=True) -r = g.internal_test_rhashtable ("5") -if type(r) != dict or sorted (r.keys()) != ["0","1","2","3","4"] or r["0"] != "0" or r["1"] != "1" or r["2"] != "2" or r["3"] != "3" or r["4"] != "4": - raise Exception ("python_return_dict=True: internal_test_rhashtable returned %s" % r) + r = g.internal_test_rhashtable ("5") + self.assertTrue (isinstance (r, dict)) + self.assertEqual (sorted (r.keys()), ["0","1","2","3","4"]) + self.assertEqual (r["0"], "0") + self.assertEqual (r["1"], "1") + self.assertEqual (r["2"], "2") + self.assertEqual (r["3"], "3") + self.assertEqual (r["4"], "4") + +if __name__ == '__main__': + unittest.main () diff --git a/python/t/910-libvirt.py b/python/t/910-libvirt.py index f6c99c9f7..1d7037139 100644 --- a/python/t/910-libvirt.py +++ b/python/t/910-libvirt.py @@ -20,6 +20,7 @@ # working by testing it. See: # https://bugzilla.redhat.com/show_bug.cgi?id=1075164 +import unittest import os import guestfs @@ -38,11 +39,14 @@ if not "c_pointer" in dir (conn): print ("skipping test: libvirt-python doesn't support c_pointer()") exit (77) -dom = conn.lookupByName ("blank-disk") +class Test910Libvirt (unittest.TestCase): + def test_libvirt (self): + dom = conn.lookupByName ("blank-disk") -g = guestfs.GuestFS () + g = guestfs.GuestFS () -r = g.add_libvirt_dom (dom, readonly=1) + r = g.add_libvirt_dom (dom, readonly=1) + self.assertEqual (r, 1) -if r != 1: - raise "unexpected return value from add_libvirt_dom (%d)" % r +if __name__ == '__main__': + unittest.main ()