Files
libguestfs/python/t/test090RetValues.py
Pino Toscano 9776615301 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.
2016-02-22 16:57:15 +01:00

135 lines
4.1 KiB
Python

# libguestfs Python bindings
# Copyright (C) 2013-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.
# Test all the different return values.
import unittest
import guestfs
class Test090PythonRetValues (unittest.TestCase):
def test_rint (self):
g = guestfs.GuestFS ()
self.assertAlmostEqual (g.internal_test_rint ("10"), 10, places=1)
self.assertRaises (RuntimeError, g.internal_test_rinterr)
def test_rint64 (self):
g = guestfs.GuestFS ()
self.assertAlmostEqual (g.internal_test_rint64 ("10"), 10L, places=1)
self.assertRaises (RuntimeError, g.internal_test_rint64err)
def test_rbool (self):
g = guestfs.GuestFS ()
self.assertTrue (g.internal_test_rbool ("true"))
self.assertFalse (g.internal_test_rbool ("false"))
self.assertRaises (RuntimeError, g.internal_test_rboolerr)
def test_rconststring (self):
g = guestfs.GuestFS ()
self.assertEqual (g.internal_test_rconststring ("test"), "static string")
self.assertRaises (RuntimeError, g.internal_test_rconststringerr)
def test_rconstoptstring (self):
g = guestfs.GuestFS ()
self.assertEqual (g.internal_test_rconstoptstring ("test"), "static string")
# this never fails
self.assertIsNone (g.internal_test_rconstoptstringerr ())
def test_rstring (self):
g = guestfs.GuestFS ()
self.assertEqual (g.internal_test_rstring ("test"), "test")
self.assertRaises (RuntimeError, g.internal_test_rstringerr)
def test_rstringlist (self):
g = guestfs.GuestFS ()
self.assertEqual (g.internal_test_rstringlist ("0"), [])
self.assertEqual (g.internal_test_rstringlist ("5"), ["0", "1", "2", "3", "4"])
self.assertRaises (RuntimeError, g.internal_test_rstringlisterr)
def test_rstruct (self):
g = guestfs.GuestFS ()
s = g.internal_test_rstruct ("unused")
self.assertIsInstance (s, dict)
self.assertEqual (s["pv_name"], "pv0")
self.assertRaises (RuntimeError, g.internal_test_rstructerr)
def test_rstructlist (self):
g = guestfs.GuestFS ()
self.assertEqual (g.internal_test_rstructlist ("0"), [])
l = g.internal_test_rstructlist ("5")
self.assertIsInstance (l, list)
self.assertEqual (len (l), 5)
for i in range (0, 5):
self.assertIsInstance (l[i], dict)
self.assertEqual (l[i]["pv_name"], "pv%d" % i)
self.assertRaises (RuntimeError, g.internal_test_rstructlisterr)
def test_rhashtable_list (self):
g = guestfs.GuestFS (python_return_dict=False)
self.assertEqual (g.internal_test_rhashtable ("0"), [])
r = g.internal_test_rhashtable ("5")
self.assertEqual (r, [ ("0","0"), ("1","1"), ("2","2"),
("3","3"), ("4","4") ])
self.assertRaises (RuntimeError, g.internal_test_rhashtableerr)
def test_rhashtable_dict (self):
g = guestfs.GuestFS (python_return_dict=True)
self.assertEqual (g.internal_test_rhashtable ("0"), {})
r = g.internal_test_rhashtable ("5")
self.assertEqual (r, {"0": "0", "1": "1", "2": "2", "3": "3", "4": "4"})
self.assertRaises (RuntimeError, g.internal_test_rhashtableerr)
def test_rbufferout (self):
g = guestfs.GuestFS ()
self.assertEqual (g.internal_test_rbufferout ("test"), b'test')
self.assertRaises (RuntimeError, g.internal_test_rbufferouterr)