Add infrastructure to run tests under GTestDBus
authorSimon McVittie <simon.mcvittie@collabora.co.uk>
Wed, 20 Mar 2013 10:36:26 +0000 (10:36 +0000)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Wed, 20 Mar 2013 11:48:25 +0000 (11:48 +0000)
For now, all library TestCase subclasses except the one for key-files
override this back to "do nothing" and rely on being run under
with-session-bus.sh, because I haven't checked whether they survive
having the D-Bus session bus disconnected from under them.

Bug: https://bugzilla.gnome.org/show_bug.cgi?id=690830
Reviewed-by: Philip Withnall <philip@tecnocode.co.uk>
[added comments about the undesirable dbus-1 dependency -smcv]
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
configure.ac
tests/lib/Makefile.am
tests/lib/eds/test-case.vala
tests/lib/libsocialweb/test-case.vala
tests/lib/telepathy/test-case.vala
tests/lib/test-case-helper.c
tests/lib/test-case.vala
tests/lib/tracker/test-case.vala

index 803671a..8f5166c 100644 (file)
@@ -189,7 +189,8 @@ PKG_CHECK_MODULES([GLIB],
                    gobject-2.0 >= $GLIB_REQUIRED])
 PKG_CHECK_MODULES([GMODULE], [gmodule-no-export-2.0])
 PKG_CHECK_MODULES([GIO], [gio-2.0 >= $GLIB_REQUIRED])
-PKG_CHECK_MODULES([DBUS_GLIB], [dbus-glib-1])
+# FIXME: get rid of this when our dependencies stop using dbus-1 (GNOME#696177)
+PKG_CHECK_MODULES([DBUS_GLIB], [dbus-glib-1 dbus-1])
 
 PKG_CHECK_MODULES([GEE], [gee-0.8 >= $GEE_REQUIRED])
 
index 2b0b314..7d1b6d9 100644 (file)
@@ -41,6 +41,7 @@ libfolks_test_la_CFLAGS = \
        $(GLIB_CFLAGS) \
        $(GIO_CFLAGS) \
        $(GEE_CFLAGS) \
+       $(DBUS_GLIB_CFLAGS) \
        $(NULL)
 
 libfolks_test_la_CPPFLAGS = \
@@ -55,6 +56,7 @@ libfolks_test_la_LIBADD = \
        $(GLIB_LIBS) \
        $(GIO_LIBS) \
        $(GEE_LIBS) \
+       $(DBUS_GLIB_LIBS) \
        $(NULL)
 
 libfolks_test_la_VALAFLAGS = \
@@ -65,6 +67,7 @@ libfolks_test_la_VALAFLAGS = \
        --pkg gobject-2.0 \
        --pkg gio-2.0 \
        --pkg gee-0.8 \
+       --pkg dbus-glib-1 \
        --pkg folks \
        --library folks-test \
        -H folks-test.h \
index 8465589..6b8446b 100644 (file)
@@ -57,6 +57,12 @@ public class EdsTest.TestCase : Folks.TestCase
           true);
     }
 
+  public override void private_bus_up ()
+    {
+      /* Don't do anything. We're currently relying on
+       * being wrapped in with-session-bus-eds.sh. */
+    }
+
   public override void set_up ()
     {
       base.set_up ();
index 3ad094e..2d83bb1 100644 (file)
@@ -55,6 +55,12 @@ public class LibsocialwebTest.TestCase : Folks.TestCase
       Environment.set_variable ("FOLKS_PRIMARY_STORE", "", true);
     }
 
+  public override void private_bus_up ()
+    {
+      /* Don't do anything. We're currently relying on
+       * being wrapped in with-session-bus.sh. */
+    }
+
   /**
    * Set up the libsocialweb test backend and wait for it to become ready.
    */
index db1048d..f2b9f10 100644 (file)
@@ -116,6 +116,12 @@ public class TpfTest.TestCase : Folks.TestCase
       this.create_tp_backend ();
     }
 
+  public override void private_bus_up ()
+    {
+      /* Don't do anything. We're currently relying on
+       * being wrapped in with-session-bus.sh. */
+    }
+
   /**
    * Virtual method to create the keyfile backend. Currently called by
    * the constructor (once per process), but might move into set_up() later.
index 2b0c6d5..47bf8f4 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <glib.h>
 #include <glib-object.h>
+#include <dbus/dbus.h>
 
 typedef struct {
     gpointer self;
@@ -89,3 +90,15 @@ folks_test_case_add_test_helper (FolksTestCase *self,
       folks_test_case_weak_method_test,
       folks_test_case_weak_method_teardown);
 }
+
+void
+_folks_test_case_dbus_1_set_no_exit_on_disconnect (void)
+{
+  DBusConnection *conn = dbus_bus_get (DBUS_BUS_SESSION, NULL);
+
+  if (conn != NULL)
+    {
+      dbus_connection_set_exit_on_disconnect (conn, FALSE);
+      dbus_connection_unref (conn);
+    }
+}
index 76eecee..f6f9a4e 100644 (file)
@@ -42,10 +42,57 @@ public abstract class Folks.TestCase : Object
       LogAdaptor.set_up ();
       this._suite = new GLib.TestSuite (name);
 
+      this.private_bus_up ();
+
       /* By default, no backend is allowed. Subclasses must override. */
       Environment.set_variable ("FOLKS_BACKENDS_ALLOWED", "", true);
     }
 
+  /**
+   * A private D-Bus session, normally created by private_bus_up()
+   * from the constructor.
+   *
+   * This is per-process, not per-test, because the session bus's
+   * address is frequently treated as process-global (for instance,
+   * libdbus will cache a single session bus connection indefinitely).
+   */
+  public GLib.TestDBus? test_dbus = null;
+
+  /**
+   * If true, libraries involved in this test use dbus-1 (or dbus-glib-1)
+   * so we need to turn off its exit-on-disconnect feature.
+   *
+   * FIXME: when our dependencies stop needing this, this whole feature
+   * can be removed (GNOME#696177).
+   */
+  public virtual bool uses_dbus_1
+    {
+      get
+        {
+          return false;
+        }
+    }
+
+  /**
+   * Start the temporary D-Bus session.
+   *
+   * This is per-process, not per-test, for the reasons mentioned for
+   * //test_dbus//.
+   */
+  public virtual void private_bus_up ()
+    {
+      this.test_dbus = new GLib.TestDBus (GLib.TestDBusFlags.NONE);
+      var test_dbus = (!) this.test_dbus;
+
+      test_dbus.up ();
+
+      /* Tell subprocesses that we're running in a private D-Bus
+       * session, so certain operations that would otherwise be dangerous
+       * are OK. */
+      Environment.set_variable ("FOLKS_TESTS_SANDBOXED_DBUS", "no-services",
+          true);
+    }
+
   public void register ()
     {
       TestSuite.get_root ().add_suite (this._suite);
@@ -81,6 +128,8 @@ public abstract class Folks.TestCase : Object
     {
     }
 
+  internal extern static void _dbus_1_set_no_exit_on_disconnect ();
+
   /**
    * Clean up after all tests. If you have more than one test case, this
    * will be called once, the last time only. It should undo the
@@ -94,6 +143,14 @@ public abstract class Folks.TestCase : Object
    */
   public virtual void final_tear_down ()
     {
+      if (this.uses_dbus_1)
+        TestCase._dbus_1_set_no_exit_on_disconnect ();
+
+      if (this.test_dbus != null)
+        {
+          ((!) this.test_dbus).down ();
+          this.test_dbus = null;
+        }
     }
 
   ~TestCase ()
index d49edf6..b4dfa5c 100644 (file)
@@ -63,6 +63,12 @@ public class TrackerTest.TestCase : Folks.TestCase
       this.tracker_backend = new TrackerTest.Backend ();
     }
 
+  public override void private_bus_up ()
+    {
+      /* Don't do anything. We're currently relying on
+       * being wrapped in with-session-bus-tracker.sh. */
+    }
+
   public override void tear_down ()
     {
       if (this.tracker_backend != null)