From 7004106119c5c56c93085ccfb1250b3561b5b6ad Mon Sep 17 00:00:00 2001
From: Bernardo Dal Seno <bdalseno@google.com>
Date: Tue, 13 Dec 2011 17:16:42 +0100
Subject: [PATCH] unit tests: added fakeroot support

Tests ending in "-runasroot" or "-runasroot.py" are run through fakeroot,
so activity that needs root privileges can be tested.

Signed-off-by: Bernardo Dal Seno <bdalseno@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 .gitignore                              |  1 +
 Makefile.am                             |  9 ++++++++-
 autotools/{testrunner => testrunner.in} | 14 +++++++++++++-
 configure.ac                            |  9 +++++++++
 4 files changed, 31 insertions(+), 2 deletions(-)
 rename autotools/{testrunner => testrunner.in} (74%)

diff --git a/.gitignore b/.gitignore
index 1b82e7c2c..86e80c24e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -24,6 +24,7 @@
 /autotools/missing
 /autotools/py-compile
 /autotools/replace_vars.sed
+/autotools/testrunner
 /config.log
 /config.status
 /configure
diff --git a/Makefile.am b/Makefile.am
index 89f5bf33f..a240dc512 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -137,6 +137,7 @@ CLEANFILES = \
 	$(PYTHON_BOOTSTRAP) \
 	epydoc.conf \
 	autotools/replace_vars.sed \
+	autotools/testrunner \
 	daemons/daemon-util \
 	daemons/ganeti-cleaner \
 	devel/upload \
@@ -430,6 +431,7 @@ docpng = $(patsubst %.dot,%.png,$(docdot))
 # Things to build but not to install (add it to EXTRA_DIST if it should be
 # distributed)
 noinst_DATA = \
+	autotools/testrunner \
 	devel/upload \
 	doc/html \
 	$(BUILT_EXAMPLES) \
@@ -566,7 +568,7 @@ EXTRA_DIST = \
 	autotools/convert-constants \
 	autotools/docpp \
 	autotools/gen-coverage \
-	autotools/testrunner \
+	autotools/testrunner.in \
 	$(RUN_IN_TEMPDIR) \
 	daemons/daemon-util.in \
 	daemons/ganeti-cleaner.in \
@@ -838,6 +840,10 @@ tools/kvm-ifup: tools/kvm-ifup.in $(REPLACE_VARS_SED)
 	sed -f $(REPLACE_VARS_SED) < $< > $@
 	chmod +x $@
 
+autotools/testrunner: autotools/testrunner.in $(REPLACE_VARS_SED)
+	sed -f $(REPLACE_VARS_SED) < $< > $@
+	chmod u+x $@
+
 devel/upload: devel/upload.in $(REPLACE_VARS_SED)
 	sed -f $(REPLACE_VARS_SED) < $< > $@
 	chmod u+x $@
@@ -1048,6 +1054,7 @@ $(REPLACE_VARS_SED): Makefile
 	  echo 's#@GNTMASTERDGROUP@#$(MASTERD_GROUP)#g'; \
 	  echo 's#@GNTDAEMONSGROUP@#$(DAEMONS_GROUP)#g'; \
 	  echo 's#@CUSTOM_ENABLE_CONFD@#$(ENABLE_CONFD)#g'; \
+	  echo 's#@FAKEROOT@#$(FAKEROOT_PATH)#g'; \
 	} > $@
 
 # Using deferred evaluation
diff --git a/autotools/testrunner b/autotools/testrunner.in
similarity index 74%
rename from autotools/testrunner
rename to autotools/testrunner.in
index d933613d3..879b4c7a7 100755
--- a/autotools/testrunner
+++ b/autotools/testrunner.in
@@ -1,7 +1,7 @@
 #!/bin/bash
 #
 
-# Copyright (C) 2010 Google Inc.
+# Copyright (C) 2010, 2011 Google 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
@@ -20,9 +20,21 @@
 
 set -e
 
+FAKEROOT='@FAKEROOT@'
+
 filename=$1
 
+execasroot() {
+  if [[ -z "$FAKEROOT" ]]; then
+    echo "'fakeroot' not found at configure time" >&2
+    exit 1
+  fi
+  exec "$FAKEROOT" "$@"
+}
+
 case "$filename" in
+  *-runasroot.py) execasroot $PYTHON "$@" ;;
   *.py) exec $PYTHON "$@" ;;
+  *-runasroot) execasroot "$@" ;;
   *) exec "$@" ;;
 esac
diff --git a/configure.ac b/configure.ac
index b672d956c..63a33d393 100644
--- a/configure.ac
+++ b/configure.ac
@@ -487,6 +487,15 @@ AM_CONDITIONAL([WANT_HTOOLS], [test x$HTOOLS = xyes])
 AM_CONDITIONAL([WANT_HTOOLSTESTS], [test "x$GHC_PKG_QUICKCHECK" != x])
 AM_CONDITIONAL([WANT_HTOOLSAPIDOC], [test x$HTOOLS_APIDOC = xyes])
 
+# Check for fakeroot
+AC_ARG_VAR(FAKEROOT_PATH, [fakeroot path])
+AC_PATH_PROG(FAKEROOT_PATH, [fakeroot], [])
+if test -z "$FAKEROOT_PATH"; then
+  AC_MSG_WARN(m4_normalize([fakeroot not found, tests that must run as root
+                            will not be executed]))
+fi
+AM_CONDITIONAL([HAS_FAKEROOT], [test "x$FAKEROOT_PATH" != x])
+
 SOCAT_USE_ESCAPE=
 AC_ARG_ENABLE([socat-escape],
   [AS_HELP_STRING([--enable-socat-escape],
-- 
GitLab