[Libguestfs] [PATCH v6 19/41] daemon: Add unit tests of the ‘Utils’ module.

Richard W.M. Jones
Thu, 15 Jun 2017 17:06:09 -0000

---
 .gitignore                   |  1 +
 daemon/Makefile.am           | 44 +++++++++++++++++++++++++++++++++++++++-
 daemon/daemon_utils_tests.ml | 48 ++++++++++++++++++++++++++++++++++++++++++++
 daemon/dummy.c               |  2 ++
 docs/C_SOURCE_FILES          |  1 +
 5 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 50f322974..6fceba555 100644
--- a/.gitignore
+++ b/.gitignore
@@ -169,6 +169,7 @@ Makefile.in
 /daemon/actions.h
 /daemon/callbacks.ml
 /daemon/caml-stubs.c
+/daemon/daemon_utils_tests
 /daemon/dispatch.c
 /daemon/guestfsd
 /daemon/guestfsd.8
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index e616de73b..306f371a1 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -56,6 +56,7 @@ BUILT_SOURCES = \
 EXTRA_DIST = \
 	$(generator_built) \
 	$(SOURCES_MLI) $(SOURCES_ML) \
+	daemon_utils_tests.ml \
 	guestfsd.pod

 if INSTALL_DAEMON
@@ -283,7 +284,9 @@ XOBJECTS = $(BOBJECTS:.cmo=.cmx)
 OCAMLPACKAGES = \
 	-package str,unix,hivex \
 	-I $(top_srcdir)/common/mlstdutils \
-	-I $(top_srcdir)/common/mlutils
+	-I $(top_srcdir)/common/mlutils \
+	-I $(top_builddir)/common/utils/.libs \
+	-I $(top_builddir)/common/cleanups/.libs

 OCAMLFLAGS = $(OCAML_FLAGS) $(OCAML_WARN_FLAGS)

@@ -310,6 +313,45 @@ camldaemon.o: $(OBJECTS)
 	    -linkpkg mlcutils.$(MLARCHIVE) mlstdutils.$(MLARCHIVE) \
 	    $(OBJECTS)

+# Unit tests.
+
+check_PROGRAMS = daemon_utils_tests
+TESTS = daemon_utils_tests
+
+daemon_utils_tests_SOURCES = dummy.c
+daemon_utils_tests_CPPFLAGS = \
+	-I. \
+	-I$(top_builddir) \
+	-I$(shell $(OCAMLC) -where) \
+	-I$(top_srcdir)/lib
+daemon_utils_tests_BOBJECTS = \
+	utils.cmo \
+	daemon_utils_tests.cmo
+daemon_utils_tests_XOBJECTS = $(daemon_utils_tests_BOBJECTS:.cmo=.cmx)
+
+if !HAVE_OCAMLOPT
+daemon_utils_tests_THEOBJECTS = $(daemon_utils_tests_BOBJECTS)
+else
+daemon_utils_tests_THEOBJECTS = $(daemon_utils_tests_XOBJECTS)
+endif
+
+OCAMLLINKFLAGS = \
+	mlcutils.$(MLARCHIVE) \
+	mlstdutils.$(MLARCHIVE) \
+	$(LINK_CUSTOM_OCAMLC_ONLY)
+
+daemon_utils_tests_DEPENDENCIES = \
+	$(daemon_utils_tests_THEOBJECTS) \
+	$(top_srcdir)/ocaml-link.sh
+daemon_utils_tests_LINK = \
+	$(top_srcdir)/ocaml-link.sh -- \
+	  $(OCAMLFIND) $(BEST) $(OCAMLFLAGS) $(OCAMLLINKFLAGS) \
+	  $(OCAMLPACKAGES) \
+	  $(daemon_utils_tests_THEOBJECTS) -o $@
+
+check-valgrind:
+	$(MAKE) VG="@VG@" check
+
 # OCaml dependencies.
 depend: .depend

diff --git a/daemon/daemon_utils_tests.ml b/daemon/daemon_utils_tests.ml
new file mode 100644
index 000000000..892509d89
--- /dev/null
+++ b/daemon/daemon_utils_tests.ml
@@ -0,0 +1,48 @@
+(* guestfs-inspection
+ * Copyright (C) 2009-2017 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.
+ *)
+
+open Unix
+open Printf
+
+open Utils
+
+(* Test prog_exists. *)
+let () +  assert (prog_exists "ls");
+  assert (prog_exists "true")
+
+(* Test command, commandr. *)
+let () +  ignore (command "true" []);
+
+  let r, _, _ = commandr "false" [] in
+  assert (r = 1)
+
+(* Test split_device_partition. *)
+let () +  assert (split_device_partition "/dev/sda1" =
("sda", 1));
+  assert (split_device_partition "/dev/sdb" = ("sdb", 0));
+  assert (split_device_partition "/dev/ubda9" = ("ubda",
9));
+  assert (split_device_partition "/dev/md0p1" = ("md0", 1))
+  (* XXX The function is buggy:
+  assert (split_device_partition "/dev/md0" = ("md0", 0))
*)
+
+(* Test proc_unmangle_path. *)
+let () +  assert (proc_unmangle_path "\\040" = " ");
+  assert (proc_unmangle_path "\\040\\040" = "  ")
diff --git a/daemon/dummy.c b/daemon/dummy.c
new file mode 100644
index 000000000..ebab6198c
--- /dev/null
+++ b/daemon/dummy.c
@@ -0,0 +1,2 @@
+/* Dummy source, to be used for OCaml-based tools with no C sources. */
+enum { foo = 1 };
diff --git a/docs/C_SOURCE_FILES b/docs/C_SOURCE_FILES
index 99de982a6..7e8770a9b 100644
--- a/docs/C_SOURCE_FILES
+++ b/docs/C_SOURCE_FILES
@@ -94,6 +94,7 @@ daemon/dispatch.c
 daemon/dmesg.c
 daemon/dropcaches.c
 daemon/du.c
+daemon/dummy.c
 daemon/echo-daemon.c
 daemon/ext2.c
 daemon/fallocate.c
--
2.13.0