libvirt-is-version returns successfully in case the available version of
libvirt is greater or equal than the specified major/minor/release
values.
---
.gitignore | 1 +
Makefile.am | 1 +
configure.ac | 1 +
tests/libvirt/Makefile.am | 32 ++++++++++++++++
tests/libvirt/libvirt-is-version.c | 76 ++++++++++++++++++++++++++++++++++++++
5 files changed, 111 insertions(+)
create mode 100644 tests/libvirt/Makefile.am
create mode 100644 tests/libvirt/libvirt-is-version.c
diff --git a/.gitignore b/.gitignore
index f8e6c71..c7da2f2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -485,6 +485,7 @@ Makefile.in
/tests/guests/stamp-fedora-md.img
/tests/guests/ubuntu.img
/tests/guests/windows.img
+/tests/libvirt/libvirt-is-version
/tests/mount-local/test-parallel-mount-local
/tests/mountable/test-internal-parse-mountable
/tests/parallel/test-parallel
diff --git a/Makefile.am b/Makefile.am
index e39d11f..dbffdda 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -34,6 +34,7 @@ SUBDIRS += appliance
if ENABLE_APPLIANCE
SUBDIRS += tests/qemu
SUBDIRS += tests/guests
+SUBDIRS += tests/libvirt
SUBDIRS += tests/c-api
SUBDIRS += tests/tmpdirs
SUBDIRS += tests/protocol
diff --git a/configure.ac b/configure.ac
index c07462e..a454570 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1756,6 +1756,7 @@ AC_CONFIG_FILES([Makefile
tests/hotplug/Makefile
tests/http/Makefile
tests/journal/Makefile
+ tests/libvirt/Makefile
tests/luks/Makefile
tests/lvm/Makefile
tests/md/Makefile
diff --git a/tests/libvirt/Makefile.am b/tests/libvirt/Makefile.am
new file mode 100644
index 0000000..bf5088c
--- /dev/null
+++ b/tests/libvirt/Makefile.am
@@ -0,0 +1,32 @@
+# libguestfs
+# Copyright (C) 2014 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 $(top_srcdir)/subdir-rules.mk
+
+if HAVE_LIBVIRT
+noinst_PROGRAMS = libvirt-is-version
+
+libvirt_is_version_SOURCES = \
+ libvirt-is-version.c
+
+libvirt_is_version_LDADD = \
+ $(LIBVIRT_LIBS)
+
+libvirt_is_version_CFLAGS = \
+ $(WARN_CFLAGS) $(WERROR_CFLAGS) \
+ $(LIBVIRT_CFLAGS)
+endif
diff --git a/tests/libvirt/libvirt-is-version.c b/tests/libvirt/libvirt-is-version.c
new file mode 100644
index 0000000..47f2e25
--- /dev/null
+++ b/tests/libvirt/libvirt-is-version.c
@@ -0,0 +1,76 @@
+/* libguestfs
+ * Copyright (C) 2014 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 <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <libvirt/libvirt.h>
+
+static unsigned int argtoint (const char *prog, const char *arg);
+
+int
+main (int argc, char *argv[])
+{
+ unsigned long ver;
+ unsigned int major = 0, minor = 0, release = 0;
+
+ switch (argc) {
+ case 4:
+ release = argtoint (argv[0], argv[3]);
+ /*FALLTHROUGH*/
+ case 3:
+ minor = argtoint (argv[0], argv[2]);
+ /*FALLTHROUGH*/
+ case 2:
+ major = argtoint (argv[0], argv[1]);
+ break;
+ case 1:
+ fprintf (stderr, "%s: not enough arguments: MAJOR [MINOR [PATCH]]\n",
+ argv[0]);
+ exit (EXIT_FAILURE);
+ break;
+ }
+
+ virInitialize ();
+
+ virGetVersion (&ver, NULL, NULL);
+
+ return ver >= (major * 1000000 + minor * 1000 + release)
+ ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+static unsigned int
+argtoint (const char *prog, const char *arg)
+{
+ long int res;
+ char *endptr;
+
+ errno = 0;
+ res = strtol (arg, &endptr, 10);
+ if (errno || *endptr) {
+ fprintf (stderr, "%s: cannot parse integer argument '%s'.\n", prog,
arg);
+ exit (EXIT_FAILURE);
+ }
+
+ return (unsigned int) res;
+}
--
1.8.3.1