[Libguestfs] [PATCH 3/3] inspection: Add unit tests of the rules compiler.

Richard W.M. Jones rjones at redhat.com
Wed Dec 2 22:05:30 UTC 2015


---
 .gitignore                |  2 ++
 generator/main.ml         |  4 +++-
 inspection/Makefile.am    | 25 +++++++++++++++++++++-
 inspection/test-harness.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++
 inspection/test1.rules    | 43 +++++++++++++++++++++++++++++++++++++
 po/POTFILES               |  2 ++
 6 files changed, 128 insertions(+), 2 deletions(-)
 create mode 100644 inspection/test-harness.c
 create mode 100644 inspection/test1.rules

diff --git a/.gitignore b/.gitignore
index b5e5a77..752dd70 100644
--- a/.gitignore
+++ b/.gitignore
@@ -252,6 +252,8 @@ Makefile.in
 /inspection/guestfs-inspection.8
 /inspection/rules.c
 /inspection/stamp-guestfs-inspection.pod
+/inspection/test1
+/inspection/test1.c
 /inspector/actual-*.xml
 /inspector/stamp-virt-inspector.pod
 /inspector/test-xmllint.sh
diff --git a/generator/main.ml b/generator/main.ml
index 7473e89..4e819a4 100644
--- a/generator/main.ml
+++ b/generator/main.ml
@@ -212,7 +212,9 @@ Run it from the top source directory using the command
   output_to "customize/customize-synopsis.pod" generate_customize_synopsis_pod;
   output_to "customize/customize-options.pod" generate_customize_options_pod;
 
-  (* Run the rules compiler to generate inspection rules. *)
+  (* Run the rules compiler to generate test cases and inspection rules. *)
+  output_to "inspection/test1.c"
+            (Rules_compiler.compile "inspection/test1.rules");
   output_to "inspection/rules.c"
             (Rules_compiler.compile "inspection/inspection.rules");
 
diff --git a/inspection/Makefile.am b/inspection/Makefile.am
index 3020ff4..733529e 100644
--- a/inspection/Makefile.am
+++ b/inspection/Makefile.am
@@ -18,7 +18,8 @@
 include $(top_srcdir)/subdir-rules.mk
 
 generator_built = \
-	rules.c
+	rules.c \
+	test1.c
 
 BUILT_SOURCES = \
 	$(generator_built)
@@ -85,3 +86,25 @@ stamp-guestfs-inspection.pod: guestfs-inspection.pod
 	  --license GPLv2+ \
 	  $<
 	touch $@
+
+# Tests.
+
+TESTS = test1
+check_PROGRAMS = test1
+
+test1_SOURCES = \
+	cleanups.c \
+	facts.c \
+	inspection.h \
+	test-harness.c \
+	test1.c \
+	utils.c
+
+test1_LDADD = $(guestfs_inspection_LDADD)
+test1_CPPFLAGS = $(guestfs_inspection_CPPFLAGS) -DTEST1
+test1_CFLAGS = $(guestfs_inspection_CFLAGS)
+
+# XXX Why is this necessary?
+test1.c: ../generator/stamp-generator
+	rm -f $<
+	$(MAKE) -C ../generator
diff --git a/inspection/test-harness.c b/inspection/test-harness.c
new file mode 100644
index 0000000..350f81f
--- /dev/null
+++ b/inspection/test-harness.c
@@ -0,0 +1,54 @@
+/* guestfs-inspection tests
+ * Copyright (C) 2009-2015 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 <assert.h>
+
+#include "inspection.h"
+
+int verbose = 1;
+
+/* Required by the gnulib 'error' module. */
+const char *program_name = "test";
+
+int
+main (int argc, char *argv[])
+{
+  /* Run the rules. */
+  rules ();
+
+  /* Check the true facts are what we expect. */
+#ifdef TEST1
+  CLEANUP_FREE fact *f1 = create_fact ("Distro", "/dev/sda1", "Debian", NULL);
+  assert (is_fact (true, f1));
+
+  CLEANUP_FREE fact *f2 = create_fact ("Distro", "/dev/sda2", "Fedora", NULL);
+  assert (is_fact (true, f2));
+
+  CLEANUP_FREE fact *f3 = create_fact ("Distro", "/dev/sda3", "RHEL", NULL);
+  assert (is_fact (true, f3));
+#else
+#error "unknown test case"
+#endif
+
+  exit (EXIT_SUCCESS);
+}
diff --git a/inspection/test1.rules b/inspection/test1.rules
new file mode 100644
index 0000000..5903b46
--- /dev/null
+++ b/inspection/test1.rules
@@ -0,0 +1,43 @@
+/* -*- prolog -*- */
+
+Filesystem("/dev/sda1").
+Filesystem("/dev/sda2").
+Filesystem("/dev/sda3").
+
+File("/dev/sda1", "/etc/fstab").
+File("/dev/sda1", "/etc/debian_version").
+File("/dev/sda2", "/etc/fstab").
+File("/dev/sda2", "/etc/redhat-release").
+File("/dev/sda2", "/etc/fedora-release").
+File("/dev/sda3", "/etc/fstab").
+File("/dev/sda3", "/etc/fedora-release") :- false.
+File("/dev/sda3", "/etc/redhat-release").
+
+Symlink("/dev/sda1", "/bin").
+Directory("/dev/sda1", "/lib").
+Directory("/dev/sda1", "/etc").
+Directory("/dev/sda2", "/bin").
+Directory("/dev/sda2", "/etc").
+Directory("/dev/sda2", "/lib").
+Directory("/dev/sda3", "/bin").
+Directory("/dev/sda3", "/etc").
+Directory("/dev/sda3", "/lib").
+
+UnixRoot(fs) :-
+    Filesystem(fs),
+    (Directory(fs, "/bin"); Symlink(fs, "/bin")),
+    (File(fs, "/etc/fstab"); Symlink(fs, "/etc/fstab")),
+    (Directory(fs, "/lib"); Symlink(fs, "/lib")).
+
+Distro(rootfs, "RHEL") :-
+    UnixRoot(rootfs),
+    File(rootfs, "/etc/redhat-release"),
+    ! File(rootfs, "/etc/fedora-release").
+
+Distro(rootfs, "Fedora") :-
+    UnixRoot(rootfs),
+    File(rootfs, "/etc/fedora-release").
+
+Distro(rootfs, "Debian") :-
+    UnixRoot(rootfs),
+    File(rootfs, "/etc/debian_version").
diff --git a/po/POTFILES b/po/POTFILES
index a88d0d0..8719f55 100644
--- a/po/POTFILES
+++ b/po/POTFILES
@@ -260,6 +260,8 @@ inspection/cleanups.c
 inspection/facts.c
 inspection/inspection.c
 inspection/mount.c
+inspection/test-harness.c
+inspection/test1.c
 inspection/utils.c
 inspector/inspector.c
 java/com_redhat_et_libguestfs_GuestFS.c
-- 
2.5.0




More information about the Libguestfs mailing list