[gvfs] gvfs-test: Move temporary D-BUS setup from run-in-tree.sh



commit b1e7b70aa4db582743637fef4b2a00f19787d46f
Author: Martin Pitt <martinpitt gnome org>
Date:   Wed Nov 28 09:55:50 2012 +0100

    gvfs-test: Move temporary D-BUS setup from run-in-tree.sh
    
    For adding further tests such as for trash://, we want to be able to use a
    temporary private $XDG_DATA_HOME, and also ensure that we do not touch the
    user's configuration in any way by setting a temporary $XDG_CONFIG_HOME as
    well. (The latter needs to be in the actual $HOME until
    https://bugzilla.gnome.org/show_bug.cgi?id=142568 gets fixed)
    
    To achieve this, set up the temporary session D-BUS in gvfs-test itself (so
    that it sees our new XDG environment variables), and stop setting up a session
    D-BUS in run-in-tree.sh when running under make. Retain the funtionality in
    run-in-tree.sh for calling the script manually.
    
    This also allows us to capture the output of the D-BUS daemon and its attached
    processes. Show its stdout and stderr on test case failures for easier
    debugging, and enable general GLib and gvfs debug messages.
    
    In addition this simplifies the handling of $LIBSMB_PROG, as we can now set it
    in the test suite and can stop setting it in the Makefile.

 test/Makefile.am    |    6 +---
 test/gvfs-test      |   74 ++++++++++++++++++++++++++++++++++++++++++++++++--
 test/run-in-tree.sh |   29 +++++++++++---------
 3 files changed, 89 insertions(+), 20 deletions(-)
---
diff --git a/test/Makefile.am b/test/Makefile.am
index 7c740b5..1f48ebd 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -63,14 +63,12 @@ endif
 noinst_DATA= $(CONFIG_FILES)
 
 # run tests against build tree
-# $LIBSMB_PROG needs to be set in the local dbus-daemon, but only if we
-# run tests; it doesn't help on the client-side
 check: $(CONFIG_FILES) gvfs-test
-	env LIBSMB_PROG="nc localhost 1445" $(srcdir)/run-in-tree.sh $(srcdir)/gvfs-test $(TEST_NAMES)
+	$(srcdir)/run-in-tree.sh $(srcdir)/gvfs-test $(TEST_NAMES)
 
 # run tests against the installed system packages
 installcheck-local: gvfs-test
-	env LIBSMB_PROG="nc localhost 1445" $(srcdir)/gvfs-test $(TEST_NAMES)
+	$(srcdir)/gvfs-test $(TEST_NAMES)
 
 CLEANFILES=$(CONFIG_FILES)
 
diff --git a/test/gvfs-test b/test/gvfs-test
index d82b9b4..9ac57c0 100755
--- a/test/gvfs-test
+++ b/test/gvfs-test
@@ -61,6 +61,10 @@ english_messages = not lc or lc.startswith('en_')
 PTYPE_DISK = 0 
 PTYPE_CDROM = 5 
 
+# local D-BUS daemon if we don't run under gvfs-testbed
+dbus_daemon = None
+
+
 class GvfsTestCase(unittest.TestCase):
     '''Gvfs tests base class.
 
@@ -72,6 +76,22 @@ class GvfsTestCase(unittest.TestCase):
     def tearDown(self):
         shutil.rmtree(self.workdir)
 
+    def run(self, result=None):
+        '''Show dbus daemon output on failed tests'''
+
+        if result:
+            orig_err_fail = len(result.errors) + len(result.failures)
+        super().run(result)
+        # always read the logs, so that we only get the ones relevant to this
+        # particular test case
+        if dbus_daemon:
+            dbus_out = dbus_daemon.stdout.read()
+            dbus_err = dbus_daemon.stderr.read()
+            if result and len(result.errors) + len(result.failures) > orig_err_fail:
+                print('\n----- dbus stdout -----\n%s\n----- dbus stderr -----\n%s\n' %
+                      (dbus_out and dbus_out.decode('UTF-8') or '',
+                       dbus_err and dbus_err.decode('UTF-8') or ''))
+
     def program_code_out_err(self, argv):
         '''Return (exitcode, stdout, stderr) from a program call.'''
 
@@ -630,8 +650,6 @@ class Ftp(GvfsTestCase):
             self.assertEqual(contents, b'hello world\n')
 
 
- unittest skipUnless(in_testbed or 'LIBSMB_PROG' in os.environ,
-                     'not running under gvfs-testbed or LIBSMB_PROG="nc localhost 1445"')
 class Smb(GvfsTestCase):
     def setUp(self):
         '''start local smbd as user if we are not in test bed'''
@@ -1301,6 +1319,47 @@ DAVLockDB DAVLock
             self.unmount(uri)
 
 
+def start_dbus():
+    '''Run a local D-BUS daemon under temporary XDG directories
+    
+    Return temporary XDG home directory.
+    '''
+
+    global dbus_daemon
+
+    # use temporary config/data/runtime directories; NB that these need to be
+    # in g_get_home_dir(), otherwise you can't trash files as this doesn't work
+    # across fs boundaries
+    # if/once https://bugzilla.gnome.org/show_bug.cgi?id=142568 gets fixed, we
+    # can put it into a proper temp dir again
+    temp_home = tempfile.mkdtemp(prefix='gvfs_test', dir=GLib.get_home_dir())
+    os.environ['XDG_CONFIG_HOME'] = os.path.join(temp_home, 'config')
+    os.environ['XDG_DATA_HOME'] = os.path.join(temp_home, 'data')
+
+    # run local D-BUS
+    if os.path.exists('session.conf'):
+        dbus_conf = 'session.conf'
+    else:
+        dbus_conf = os.path.join(os.path.dirname(__file__), 'session.conf')
+    env = os.environ.copy()
+    env['G_MESSAGES_DEBUG'] = 'all'
+    env['GVFS_DEBUG'] = 'all'
+    env['LIBSMB_PROG'] = "nc localhost 1445"
+    dbus_daemon = subprocess.Popen(
+        ['dbus-daemon', '--config-file', dbus_conf, '--print-address=1'],
+        stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
+    addr = dbus_daemon.stdout.readline().decode()
+    os.environ['DBUS_SESSION_BUS_ADDRESS'] = addr
+
+    # set dbus output to nonblocking
+    flags = fcntl.fcntl(dbus_daemon.stdout, fcntl.F_GETFL)
+    fcntl.fcntl(dbus_daemon.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
+    flags = fcntl.fcntl(dbus_daemon.stderr, fcntl.F_GETFL)
+    fcntl.fcntl(dbus_daemon.stderr, fcntl.F_SETFL, flags | os.O_NONBLOCK)
+
+    return temp_home
+
+
 if __name__ == '__main__':
     # do not break tests due to translations
     try:
@@ -1308,4 +1367,13 @@ if __name__ == '__main__':
     except KeyError:
         pass
     os.environ['LC_ALL'] = 'C'
-    unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))
+    
+    if not in_testbed:
+        temp_home = start_dbus()
+    try:
+        unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))
+    finally:
+        if not in_testbed:
+            dbus_daemon.terminate()
+            dbus_daemon.wait()
+            shutil.rmtree(temp_home)
diff --git a/test/run-in-tree.sh b/test/run-in-tree.sh
index db39446..8498a52 100755
--- a/test/run-in-tree.sh
+++ b/test/run-in-tree.sh
@@ -12,21 +12,24 @@ export GVFS_MONITOR_DIR=`pwd`
 export PATH=`pwd`/../programs:$PATH
 export GIO_EXTRA_MODULES=`pwd`/../client/.libs:`pwd`/../monitor/proxy/.libs
 
-if [ -e $(pwd)/session.conf ]; then
-    # case for out-of tree build (distcheck)
-    DBUS_CONF=`pwd`/session.conf
-else
-    # case for calling this manually in a built tree
-    DBUS_CONF=`dirname $0`/session.conf
-fi
+# Start a custom session dbus, unless we run under "make check" (test suite
+# starts its own)
+if [ -z "$MAKEFLAGS" ]; then
+    if [ -e $(pwd)/session.conf ]; then
+        # case for out-of tree build (distcheck)
+        DBUS_CONF=`pwd`/session.conf
+    else
+        # case for calling this manually in a built tree
+        DBUS_CONF=`dirname $0`/session.conf
+    fi
 
-# Start a custom session dbus
-PIDFILE=`mktemp`
-export DBUS_SESSION_BUS_ADDRESS=`dbus-daemon --config-file=$DBUS_CONF --fork --print-address=1 --print-pid=3 3>${PIDFILE}`
-DBUS_SESSION_BUS_PID=`cat $PIDFILE`
-rm $PIDFILE
+    PIDFILE=`mktemp`
+    export DBUS_SESSION_BUS_ADDRESS=`dbus-daemon --config-file=$DBUS_CONF --fork --print-address=1 --print-pid=3 3>${PIDFILE}`
+    DBUS_SESSION_BUS_PID=`cat $PIDFILE`
+    rm $PIDFILE
 
-trap "kill -9 $DBUS_SESSION_BUS_PID" SIGINT SIGTERM EXIT
+    trap "kill -9 $DBUS_SESSION_BUS_PID" SIGINT SIGTERM EXIT
+fi
 
 $@
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]