summaryrefslogtreecommitdiffstats
path: root/capitests
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2011-03-10 12:32:22 +0000
committerRichard W.M. Jones <rjones@redhat.com>2011-03-15 12:16:50 +0000
commit4e0cf4dbf8a8a96288f70114fdc3939da0aa7ad1 (patch)
tree2418e32479a965eb392d08f2e648c53714118a91 /capitests
parent6d6b7edd1102f8383643866bf358e494e0d518ef (diff)
downloadlibguestfs-4e0cf4dbf8a8a96288f70114fdc3939da0aa7ad1.tar.gz
libguestfs-4e0cf4dbf8a8a96288f70114fdc3939da0aa7ad1.tar.xz
libguestfs-4e0cf4dbf8a8a96288f70114fdc3939da0aa7ad1.zip
New event API (RHBZ#664558).
This API allows more than one callback to be registered for each event, makes it possible to call the API from other languages, and allows [nearly all] log, debug and trace messages to be rerouted from stderr. An older version of this API was discussed on the mailing list here: https://www.redhat.com/archives/libguestfs/2010-December/msg00081.html https://www.redhat.com/archives/libguestfs/2011-January/msg00012.html This also updates guestfish to use the new API for its progress bars.
Diffstat (limited to 'capitests')
-rw-r--r--capitests/Makefile.am13
-rw-r--r--capitests/test-debug-to-file.c89
-rw-r--r--capitests/test-private-data.c35
3 files changed, 135 insertions, 2 deletions
diff --git a/capitests/Makefile.am b/capitests/Makefile.am
index 83e62c8e..89d29e0b 100644
--- a/capitests/Makefile.am
+++ b/capitests/Makefile.am
@@ -31,7 +31,8 @@ check_PROGRAMS = \
test-config \
test-add-drive-opts \
test-last-errno \
- test-private-data
+ test-private-data \
+ test-debug-to-file
TESTS = \
tests \
@@ -40,7 +41,8 @@ TESTS = \
test-config \
test-add-drive-opts \
test-last-errno \
- test-private-data
+ test-private-data \
+ test-debug-to-file
# The API behind this test is not baked yet.
#if HAVE_LIBVIRT
@@ -112,6 +114,13 @@ test_private_data_CFLAGS = \
test_private_data_LDADD = \
$(top_builddir)/src/libguestfs.la
+test_debug_to_file_SOURCES = test-debug-to-file.c
+test_debug_to_file_CFLAGS = \
+ -I$(top_srcdir)/src -I$(top_builddir)/src \
+ $(WARN_CFLAGS) $(WERROR_CFLAGS)
+test_debug_to_file_LDADD = \
+ $(top_builddir)/src/libguestfs.la
+
#if HAVE_LIBVIRT
#test_add_libvirt_dom_SOURCES = test-add-libvirt-dom.c
#test_add_libvirt_dom_CFLAGS = \
diff --git a/capitests/test-debug-to-file.c b/capitests/test-debug-to-file.c
new file mode 100644
index 00000000..6d1b619e
--- /dev/null
+++ b/capitests/test-debug-to-file.c
@@ -0,0 +1,89 @@
+/* libguestfs
+ * Copyright (C) 2011 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 that we can use the new event API to capture all debugging
+ * messages to a file.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "guestfs.h"
+
+static void
+debug_to_file (guestfs_h *g,
+ void *opaque,
+ uint64_t event,
+ int event_handle,
+ int flags,
+ const char *buf, size_t buf_len,
+ const uint64_t *array, size_t array_len)
+{
+ FILE *fp = opaque;
+
+ fwrite (buf, 1, buf_len, fp);
+}
+
+int
+main (int argc, char *argv[])
+{
+ guestfs_h *g;
+ const char *filename = "test.log";
+ FILE *debugfp;
+
+ debugfp = fopen (filename, "w");
+ if (debugfp == NULL) {
+ perror (filename);
+ exit (EXIT_FAILURE);
+ }
+
+ g = guestfs_create ();
+ if (g == NULL) {
+ fprintf (stderr, "failed to create handle\n");
+ exit (EXIT_FAILURE);
+ }
+
+ if (guestfs_set_event_callback
+ (g, debug_to_file,
+ GUESTFS_EVENT_LIBRARY|GUESTFS_EVENT_APPLIANCE|GUESTFS_EVENT_TRACE,
+ 0, debugfp) == -1)
+ exit (EXIT_FAILURE);
+
+ if (guestfs_set_verbose (g, 1) == -1)
+ exit (EXIT_FAILURE);
+
+ if (guestfs_set_trace (g, 1) == -1)
+ exit (EXIT_FAILURE);
+
+ if (guestfs_add_drive_opts (g, "/dev/null",
+ GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
+ GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
+ -1) == -1)
+ exit (EXIT_FAILURE);
+
+ if (guestfs_launch (g) == -1)
+ exit (EXIT_FAILURE);
+
+ guestfs_close (g);
+
+ exit (EXIT_SUCCESS);
+}
diff --git a/capitests/test-private-data.c b/capitests/test-private-data.c
index fad88b58..f2ff647b 100644
--- a/capitests/test-private-data.c
+++ b/capitests/test-private-data.c
@@ -30,6 +30,34 @@
#define PREFIX "test_"
+static size_t close_callback_called = 0;
+
+/* This callback deletes all test keys in the handle. */
+static void
+close_callback (guestfs_h *g,
+ void *opaque,
+ uint64_t event,
+ int event_handle,
+ int flags,
+ const char *buf, size_t buf_len,
+ const uint64_t *array, size_t array_len)
+{
+ const char *key;
+ void *data;
+
+ close_callback_called++;
+
+ again:
+ data = guestfs_first_private (g, &key);
+ while (data != NULL) {
+ if (strncmp (key, PREFIX, strlen (PREFIX)) == 0) {
+ guestfs_set_private (g, key, NULL);
+ goto again;
+ }
+ data = guestfs_next_private (g, &key);
+ }
+}
+
int
main (int argc, char *argv[])
{
@@ -44,6 +72,10 @@ main (int argc, char *argv[])
exit (EXIT_FAILURE);
}
+ if (guestfs_set_event_callback (g, close_callback, GUESTFS_EVENT_CLOSE,
+ 0, NULL) == -1)
+ exit (EXIT_FAILURE);
+
guestfs_set_private (g, PREFIX "a", (void *) 1);
guestfs_set_private (g, PREFIX "b", (void *) 2);
guestfs_set_private (g, PREFIX "c", (void *) 3);
@@ -79,7 +111,10 @@ main (int argc, char *argv[])
}
assert (count == 1);
+ /* Closing should implicitly call the close_callback function. */
guestfs_close (g);
+ assert (close_callback_called == 1);
+
exit (EXIT_SUCCESS);
}