[Libguestfs] [PATCH 3/4] tests: Add infrastructure for running certain tests as root.

Richard W.M. Jones rjones at redhat.com
Mon Mar 13 14:47:44 UTC 2017


'[sudo] make check-root' is analogous to 'make check-slow'.
---
 Makefile.am              | 17 ++++++++++++++++-
 docs/guestfs-hacking.pod | 37 ++++++++++++++++++++++++++++++++++++-
 tests/test-functions.sh  |  9 +++++++++
 3 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 7250cae..12486de 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -517,6 +517,20 @@ check-slow: build-test-guests
 	done; \
 	exit $$(( $$errors ? 1 : 0 ))
 
+check-root: build-test-guests
+	@if test "$$(id -u)" -ne 0; then \
+	  echo "***"; \
+	  echo "*** error: You must run 'check-root' as root."; \
+	  echo "***"; \
+	  exit 1; \
+	fi
+	@errors=0; \
+	for f in `grep -l '^$@:' $(SUBDIRS:%=%/Makefile.am)`; do \
+	  echo $(MAKE) -C `dirname $$f` $@; \
+	  $(MAKE) -C `dirname $$f` $@ || (( errors++ )); \
+	done; \
+	exit $$(( $$errors ? 1 : 0 ))
+
 build-test-guests:
 	$(MAKE) -C test-data/phony-guests check
 
@@ -590,8 +604,9 @@ help:
 	@echo "make check-with-upstream-qemu     Test using upstream qemu."
 	@echo "make check-with-upstream-libvirt  Test using upstream libvirt."
 	@echo "make check-slow                   Slow/long-running tests."
+	@echo "sudo make check-root              Tests which must be run as root."
 	@echo
-	@echo "make check-all                    Runs all 'check*' rules."
+	@echo "make check-all                    Runs all 'check*' rules except check-root."
 	@echo "make check-release                Runs 'check*' rules required for release."
 	@echo
 	@echo "make installcheck                 Test installed libguestfs packages."
diff --git a/docs/guestfs-hacking.pod b/docs/guestfs-hacking.pod
index df429a1..401dc95 100644
--- a/docs/guestfs-hacking.pod
+++ b/docs/guestfs-hacking.pod
@@ -662,9 +662,44 @@ Add a rule to the F<Makefile.am>:
 
 =back
 
+=item C<sudo make check-root>
+
+Runs some tests which require root privileges.  These are supposed to
+be safe, but take care.  You have to run this as root (eg. using
+L<sudo(8)> explicitly).
+
+To mark a test as requiring root:
+
+=over 4
+
+=item *
+
+Add it to the list of C<TESTS> in the F<Makefile.am>, just like a
+normal test.
+
+=item *
+
+Modify the test so it checks if euid == 0, and if I<not> set it skips
+(ie. returns with exit code 77).  If using C<$TEST_FUNCTIONS>, you can
+call the function C<root_test> for this.
+
+=item *
+
+Add a variable C<ROOT_TESTS> to the F<Makefile.am> listing the root
+tests.
+
+=item *
+
+Add a rule to the F<Makefile.am>:
+
+ check-root:
+   $(MAKE) check TESTS="$(ROOT_TESTS)"
+
+=back
+
 =item C<make check-all>
 
-Equivalent to running all C<make check*> rules.
+Equivalent to running all C<make check*> rules except C<check-root>.
 
 =item C<make check-release>
 
diff --git a/tests/test-functions.sh b/tests/test-functions.sh
index 74ad2ef..e5e8246 100755
--- a/tests/test-functions.sh
+++ b/tests/test-functions.sh
@@ -242,6 +242,15 @@ slow_test ()
     fi
 }
 
+# Root tests should always call this function.  (See guestfs-hacking(1)).
+root_test ()
+{
+    if test "$(id -u)" -ne 0; then
+        echo "$(basename $0): use 'sudo make check-root' to run this test"
+        exit 77
+    fi
+}
+
 do_md5 ()
 {
   case "$(uname)" in
-- 
2.9.3




More information about the Libguestfs mailing list