From b535363e7e7a000cca3651790f911b2d0605968f Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Wed, 3 Nov 2010 16:56:34 +0000 Subject: [PATCH] capitests: Test some basic aspects of the C API. --- .gitignore | 2 ++ capitests/Makefile.am | 50 +++++++++++++++++++++---------- capitests/test-config.c | 68 ++++++++++++++++++++++++++++++++++++++++++ capitests/test-create-handle.c | 42 ++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 15 deletions(-) create mode 100644 capitests/test-config.c create mode 100644 capitests/test-create-handle.c diff --git a/.gitignore b/.gitignore index 527335d..406e756 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,8 @@ autom4te.cache *.bak bindtests.tmp capitests/test-command +capitests/test-config +capitests/test-create-handle capitests/test*.img capitests/tests capitests/tests.c diff --git a/capitests/Makefile.am b/capitests/Makefile.am index 65a7240..1374d9b 100644 --- a/capitests/Makefile.am +++ b/capitests/Makefile.am @@ -1,5 +1,5 @@ # libguestfs -# Copyright (C) 2009 Red Hat Inc. +# Copyright (C) 2009-2010 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 @@ -23,17 +23,16 @@ BUILT_SOURCES = $(generator_built) EXTRA_DIST = $(BUILT_SOURCES) -# Tests. These are auto-generated from the test descriptions -# in the generator. +check_PROGRAMS = \ + tests \ + test-command \ + test-create-handle \ + test-config -check_PROGRAMS = tests test-command - -tests_SOURCES = tests.c -tests_CFLAGS = -I$(top_srcdir)/src -I$(top_builddir)/src \ - $(WARN_CFLAGS) $(WERROR_CFLAGS) -tests_LDADD = $(top_builddir)/src/libguestfs.la - -TESTS = tests +TESTS = \ + tests \ + test-create-handle \ + test-config TESTS_ENVIRONMENT = \ SKIP_TEST_COMMAND=$(shell ldd test-command | grep -sq 'not a dynamic executable' || echo 1) \ SKIP_TEST_COMMAND_LINES=$(shell ldd test-command | grep -sq 'not a dynamic executable' || echo 1) \ @@ -41,13 +40,34 @@ TESTS_ENVIRONMENT = \ LIBGUESTFS_PATH=$(top_builddir)/appliance \ $(VG) -# Run the tests under valgrind. - -valgrind: - $(MAKE) check VG="valgrind --quiet --leak-check=full" +tests_SOURCES = tests.c +tests_CFLAGS = -I$(top_srcdir)/src -I$(top_builddir)/src \ + $(WARN_CFLAGS) $(WERROR_CFLAGS) +tests_LDADD = $(top_builddir)/src/libguestfs.la # This binary must be statically linked. It is used for testing # the "guestfs_command" and "guestfs_command_lines" functions. test_command_SOURCES = test-command.c test_command_LDFLAGS = -all-static + +# Hand-written C API tests. + +test_create_handle_SOURCES = test-create-handle.c +test_create_handle_CFLAGS = \ + -I$(top_srcdir)/src -I$(top_builddir)/src \ + $(WARN_CFLAGS) $(WERROR_CFLAGS) +test_create_handle_LDADD = \ + $(top_builddir)/src/libguestfs.la + +test_config_SOURCES = test-config.c +test_config_CFLAGS = \ + -I$(top_srcdir)/src -I$(top_builddir)/src \ + $(WARN_CFLAGS) $(WERROR_CFLAGS) +test_config_LDADD = \ + $(top_builddir)/src/libguestfs.la + +# Run the tests under valgrind. + +valgrind: + $(MAKE) check VG="valgrind --quiet --leak-check=full" diff --git a/capitests/test-config.c b/capitests/test-config.c new file mode 100644 index 0000000..c541123 --- /dev/null +++ b/capitests/test-config.c @@ -0,0 +1,68 @@ +/* libguestfs + * Copyright (C) 2010 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. + */ + +#include + +#include +#include +#include +#include + +#include "guestfs.h" + +int +main (int argc, char *argv[]) +{ + guestfs_h *g; + int r; + + g = guestfs_create (); + if (g == NULL) { + fprintf (stderr, "failed to create handle\n"); + exit (EXIT_FAILURE); + } + + /* If these fail, the default error handler will print an error + * message to stderr, so we don't need to print anything. This code + * is very pedantic, but after all we are testing the details of the + * C API. + */ + + if (guestfs_set_verbose (g, 1) == -1) + exit (EXIT_FAILURE); + r = guestfs_get_verbose (g); + if (r == -1) + exit (EXIT_FAILURE); + if (!r) { + fprintf (stderr, "set_verbose not true\n"); + exit (EXIT_FAILURE); + } + if (guestfs_set_verbose (g, 0) == -1) + exit (EXIT_FAILURE); + r = guestfs_get_verbose (g); + if (r == -1) + exit (EXIT_FAILURE); + if (r) { + fprintf (stderr, "set_verbose not false\n"); + exit (EXIT_FAILURE); + } + + guestfs_close (g); + + exit (EXIT_SUCCESS); +} diff --git a/capitests/test-create-handle.c b/capitests/test-create-handle.c new file mode 100644 index 0000000..edea19f --- /dev/null +++ b/capitests/test-create-handle.c @@ -0,0 +1,42 @@ +/* libguestfs + * Copyright (C) 2010 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. + */ + +#include + +#include +#include +#include +#include + +#include "guestfs.h" + +int +main (int argc, char *argv[]) +{ + guestfs_h *g; + + g = guestfs_create (); + if (g == NULL) { + fprintf (stderr, "failed to create handle\n"); + exit (EXIT_FAILURE); + } + + guestfs_close (g); + + exit (EXIT_SUCCESS); +} -- 1.8.3.1