From c6628970d4b56bdc9c15bd82f2efd37547e98155 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Fri, 26 Feb 2010 09:04:11 -0500
Subject: [PATCH] Add --with-test-dir option to configure

All 'make check' tests will chdir() into this directory before
running the suite. This provides the option of having temporary
files generated in a tmpfs or ramdisk
---
 src/Makefile.am                  |   55 +++++++++++++++++++++++++------------
 src/conf_macros.m4               |   12 ++++++++
 src/configure.ac                 |    4 +++
 src/db/sysdb.c                   |    4 +-
 src/tests/auth-tests.c           |    3 ++
 src/tests/check_and_open-tests.c |    4 +++
 src/tests/common.c               |   18 +++++++++++-
 src/tests/common.h               |    2 +
 src/tests/fail_over-tests.c      |    2 +
 src/tests/files-tests.c          |    3 ++
 src/tests/find_uid-tests.c       |    4 +++
 src/tests/ipa_ldap_opt-tests.c   |    4 +++
 src/tests/ipa_timerules-tests.c  |    2 +
 src/tests/krb5_utils-tests.c     |    4 +++
 src/tests/refcount-tests.c       |    2 +
 src/tests/resolv-tests.c         |    2 +
 src/tests/stress-tests.c         |    3 ++
 src/tests/strtonum-tests.c       |    3 ++
 src/tests/sysdb-tests.c          |    3 ++
 19 files changed, 113 insertions(+), 21 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index fa62702..93de663 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -450,6 +450,15 @@ sss_groupshow_LDADD = \
 # Feature Tests #
 #################
 if HAVE_CHECK
+noinst_LTLIBRARIES += \
+    libsss_test_common.la
+
+libsss_test_common_la_SOURCES = \
+    tests/common.c
+libsss_test_common_la_LIBADD = \
+    $(SSSD_LIBS) \
+    $(CHECK_LIBS)
+
 sysdb_tests_DEPENDENCIES = \
     $(ldblib_LTLIBRARIES)
 sysdb_tests_SOURCES = \
@@ -461,7 +470,8 @@ sysdb_tests_CFLAGS = \
     $(CHECK_CFLAGS)
 sysdb_tests_LDADD = \
     $(SSSD_LIBS) \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 strtonum_tests_SOURCES = \
     tests/strtonum-tests.c \
@@ -472,7 +482,8 @@ strtonum_tests_CFLAGS = \
     $(CHECK_CFLAGS)
 strtonum_tests_LDADD = \
     $(SSSD_LIBS) \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 krb5_utils_tests_SOURCES = \
     tests/krb5_utils-tests.c \
@@ -488,7 +499,8 @@ krb5_utils_tests_CFLAGS = \
 krb5_utils_tests_LDADD = \
     $(SSSD_LIBS)\
     $(CARES_LIBS) \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 
 check_and_open_tests_SOURCES = \
@@ -499,12 +511,14 @@ check_and_open_tests_CFLAGS = \
     $(AM_CFLAGS) \
     $(CHECK_CFLAGS)
 check_and_open_tests_LDADD = \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 FILES_TESTS_LIBS = \
     $(CHECK_LIBS) \
     $(POPT_LIBS) \
-    $(TALLOC_LIBS)
+    $(TALLOC_LIBS) \
+    libsss_test_common.la
 if BUILD_SELINUX
     FILES_TESTS_LIBS += $(SELINUX_LIBS)
 endif
@@ -518,7 +532,8 @@ files_tests_CFLAGS = \
     $(AM_CFLAGS) \
     $(CHECK_CFLAGS)
 files_tests_LDADD = \
-    $(FILES_TESTS_LIBS)
+    $(FILES_TESTS_LIBS) \
+    libsss_test_common.la
 
 SSSD_RESOLV_TESTS_OBJ = \
     $(SSSD_RESOLV_OBJ)
@@ -531,7 +546,6 @@ endif
 
 resolv_tests_SOURCES = \
     tests/resolv-tests.c \
-    tests/common.c \
     $(SSSD_UTIL_OBJ) \
     $(SSSD_RESOLV_TESTS_OBJ)
 resolv_tests_CFLAGS = \
@@ -541,22 +555,22 @@ resolv_tests_CFLAGS = \
 resolv_tests_LDADD = \
     $(SSSD_LIBS) \
     $(CHECK_LIBS) \
-    $(CARES_LIBS)
+    $(CARES_LIBS) \
+    libsss_test_common.la
 
 refcount_tests_SOURCES = \
     tests/refcount-tests.c \
-    tests/common.c \
     $(CHECK_OBJ) \
     $(SSSD_UTIL_OBJ)
 refcount_tests_CFLAGS = \
     $(CHECK_CFLAGS)
 refcount_tests_LDADD = \
     $(SSSD_LIBS) \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 fail_over_tests_SOURCES = \
     tests/fail_over-tests.c \
-    tests/common.c \
     $(SSSD_FAILOVER_OBJ) \
     $(CHECK_OBJ) \
     $(SSSD_UTIL_OBJ)
@@ -565,12 +579,12 @@ fail_over_tests_CFLAGS = \
 fail_over_tests_LDADD = \
     $(SSSD_LIBS) \
     $(CHECK_LIBS) \
-    $(CARES_LIBS)
+    $(CARES_LIBS) \
+    libsss_test_common.la
 
 ipa_timerules_tests_SOURCES = \
     providers/ipa/ipa_timerules.c \
     tests/ipa_timerules-tests.c \
-    tests/common.c \
     $(SSSD_DEBUG_OBJ)
 ipa_timerules_tests_CFLAGS = \
     $(AM_CFLAGS) \
@@ -582,7 +596,8 @@ ipa_timerules_tests_LDADD = \
     $(POPT_LIBS) \
     $(PCRE_LIBS) \
     $(TALLOC_LIBS) \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 find_uid_tests_SOURCES = \
     tests/find_uid-tests.c \
@@ -596,7 +611,8 @@ find_uid_tests_CFLAGS = \
 find_uid_tests_LDADD = \
     $(TALLOC_LIBS) \
     $(DHASH_LIBS) \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 auth_tests_SOURCES = \
     tests/auth-tests.c \
@@ -606,7 +622,8 @@ auth_tests_CFLAGS = \
     $(CHECK_CFLAGS)
 auth_tests_LDADD = \
     $(SSSD_LIBS) \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 ipa_ldap_opt_tests_SOURCES = \
     tests/ipa_ldap_opt-tests.c
@@ -614,7 +631,8 @@ ipa_ldap_opt_tests_CFLAGS = \
     $(AM_CFLAGS) \
     $(CHECK_CFLAGS)
 ipa_ldap_opt_tests_LDADD = \
-    $(CHECK_LIBS)
+    $(CHECK_LIBS) \
+    libsss_test_common.la
 
 endif
 
@@ -622,7 +640,8 @@ stress_tests_SOURCES = \
     tests/stress-tests.c \
     $(SSSD_UTIL_OBJ)
 stress_tests_LDADD = \
-    $(SSSD_LIBS)
+    $(SSSD_LIBS) \
+    libsss_test_common.la
 
 noinst_PROGRAMS = pam_test_client
 pam_test_client_SOURCES = sss_client/pam_test_client.c
diff --git a/src/conf_macros.m4 b/src/conf_macros.m4
index 86ccf5d..6323db2 100644
--- a/src/conf_macros.m4
+++ b/src/conf_macros.m4
@@ -205,3 +205,15 @@ AC_DEFUN([WITH_SELINUX],
     AM_CONDITIONAL([BUILD_SELINUX], [test x"$with_selinux" = xyes])
   ])
 
+AC_DEFUN([WITH_TEST_DIR],
+  [ AC_ARG_WITH([test-dir],
+                [AC_HELP_STRING([--with-test-dir=PATH],
+                                [Directory used for make check temporary files [$builddir]]
+                               )
+                ]
+               )
+    TEST_DIR=$with_test_dir
+    AC_SUBST(TEST_DIR)
+    AC_DEFINE_UNQUOTED(TEST_DIR, "$with_test_dir", [Directory used for 'make check' temporary files])
+  ])
+
diff --git a/src/configure.ac b/src/configure.ac
index 4198ee9..d7a7a77 100644
--- a/src/configure.ac
+++ b/src/configure.ac
@@ -65,6 +65,7 @@ WITH_PUBCONF_PATH
 WITH_PIPE_PATH
 WITH_INIT_DIR
 WITH_SHADOW_UTILS_PATH
+WITH_TEST_DIR
 WITH_MANPAGES
 WITH_XML_CATALOG
 WITH_KRB5_PLUGIN_PATH
@@ -141,5 +142,8 @@ AM_CONDITIONAL([HAVE_DOXYGEN], [test x$DOXYGEN != xfalse ])
 
 AM_CONDITIONAL([HAVE_CHECK], [test x$have_check != x])
 
+abs_build_dir=`pwd`
+AC_DEFINE_UNQUOTED([ABS_BUILD_DIR], ["$abs_build_dir"], [Absolute path to the build directory])
+
 AC_CONFIG_FILES([Makefile doxy.config po/Makefile.in])
 AC_OUTPUT
diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index b3f81a0..41dd4f3 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -974,7 +974,7 @@ static int sysdb_check_upgrade_02(TALLOC_CTX *mem_ctx,
     }
 
 #ifdef SYSDB_TEST
-    ldb_set_modules_dir(ldb, "./.libs");
+    ldb_set_modules_dir(ctx->ldb, ABS_BUILD_DIR"/.libs");
 #endif
 
     ret = ldb_connect(ldb, ldb_file, 0, NULL);
@@ -1542,7 +1542,7 @@ static int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
     }
 
 #ifdef SYSDB_TEST
-    ldb_set_modules_dir(ctx->ldb, "./.libs");
+    ldb_set_modules_dir(ctx->ldb, ABS_BUILD_DIR"/.libs");
 #endif
 
     ret = ldb_connect(ctx->ldb, ctx->ldb_file, 0, NULL);
diff --git a/src/tests/auth-tests.c b/src/tests/auth-tests.c
index 71215bc..6486409 100644
--- a/src/tests/auth-tests.c
+++ b/src/tests/auth-tests.c
@@ -37,6 +37,7 @@
 #include "util/util.h"
 #include "confdb/confdb.h"
 #include "db/sysdb.h"
+#include "tests/common.h"
 
 #define TESTS_PATH "tests_auth"
 #define TEST_CONF_FILE "tests_conf.ldb"
@@ -319,6 +320,8 @@ int main(int argc, const char *argv[])
     }
     poptFreeContext(pc);
 
+    tests_set_cwd();
+
     ret = clean_db_dir();
     if (ret != EOK) {
         fprintf(stderr, "Could not delete the db directory (%d) (%s)\n",
diff --git a/src/tests/check_and_open-tests.c b/src/tests/check_and_open-tests.c
index 32cf09f..7ec8f3b 100644
--- a/src/tests/check_and_open-tests.c
+++ b/src/tests/check_and_open-tests.c
@@ -29,6 +29,7 @@
 #include <sys/stat.h>
 
 #include "util/util.h"
+#include "tests/common.h"
 
 #define SUFFIX ".symlink"
 
@@ -208,6 +209,9 @@ Suite *check_and_open_suite (void)
 int main(void)
 {
   int number_failed;
+
+  tests_set_cwd();
+
   Suite *s = check_and_open_suite ();
   SRunner *sr = srunner_create (s);
   /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/common.c b/src/tests/common.c
index 50dc61b..4ae1f49 100644
--- a/src/tests/common.c
+++ b/src/tests/common.c
@@ -27,9 +27,9 @@
 
 #include "tests/common.h"
 #include "util/dlinklist.h"
-#include "util/util.h"
 
 TALLOC_CTX *global_talloc_context = NULL;
+#include "util/util.h"
 
 struct size_snapshot {
     struct size_snapshot *prev;
@@ -107,3 +107,19 @@ leak_check_teardown(void)
     }
     check_leaks(global_talloc_context, 0);
 }
+
+void
+tests_set_cwd(void)
+{
+    int ret;
+
+    ret = chdir(TEST_DIR);
+    if (ret == -1) {
+        if (strlen(TEST_DIR)) {
+            fprintf(stderr,
+                    "Could not chdir to [%s].\n"
+                    "Attempting to continue with current dir\n",
+                    TEST_DIR);
+        }
+    }
+}
diff --git a/src/tests/common.h b/src/tests/common.h
index 5d4b086..576d705 100644
--- a/src/tests/common.h
+++ b/src/tests/common.h
@@ -42,4 +42,6 @@ void _check_leaks_pop(TALLOC_CTX *ctx, const char *location);
 void leak_check_setup(void);
 void leak_check_teardown(void);
 
+void tests_set_cwd(void);
+
 #endif /* !__TESTS_COMMON_H__ */
diff --git a/src/tests/fail_over-tests.c b/src/tests/fail_over-tests.c
index 4e97a1e..1864a09 100644
--- a/src/tests/fail_over-tests.c
+++ b/src/tests/fail_over-tests.c
@@ -300,6 +300,8 @@ main(int argc, const char *argv[])
     poptFreeContext(pc);
     debug_level = debug;
 
+    tests_set_cwd();
+
     suite = create_suite();
     sr = srunner_create(suite);
     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/files-tests.c b/src/tests/files-tests.c
index 2ebe659..2267a60 100644
--- a/src/tests/files-tests.c
+++ b/src/tests/files-tests.c
@@ -34,6 +34,7 @@
 #include "config.h"
 #include "tools/tools_util.h"
 #include "util/util.h"
+#include "tests/common.h"
 
 static char tpl_dir[] = "file-tests-dir-XXXXXX";
 static char *dir_path;
@@ -312,6 +313,8 @@ int main(int argc, char *argv[])
     poptFreeContext(pc);
     debug_level = debug;
 
+    tests_set_cwd();
+
     Suite *s = files_suite();
     SRunner *sr = srunner_create(s);
     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/find_uid-tests.c b/src/tests/find_uid-tests.c
index 9eafadd..8d1362f 100644
--- a/src/tests/find_uid-tests.c
+++ b/src/tests/find_uid-tests.c
@@ -29,6 +29,7 @@
 #include <check.h>
 
 #include "util/find_uid.h"
+#include "tests/common.h"
 
 
 START_TEST(test_check_if_uid_is_active_success)
@@ -115,6 +116,9 @@ int main(void)
 {
     debug_level = 255;
     int number_failed;
+
+    tests_set_cwd();
+
     Suite *s = find_uid_suite ();
     SRunner *sr = srunner_create (s);
     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/ipa_ldap_opt-tests.c b/src/tests/ipa_ldap_opt-tests.c
index 215f94a..4b4667a 100644
--- a/src/tests/ipa_ldap_opt-tests.c
+++ b/src/tests/ipa_ldap_opt-tests.c
@@ -27,6 +27,7 @@
 
 #include "providers/ipa/ipa_common.h"
 #include "providers/ldap/sdap.h"
+#include "tests/common.h"
 
 START_TEST(test_check_num_opts)
 {
@@ -49,6 +50,9 @@ Suite *ipa_ldap_opt_suite (void)
 int main(void)
 {
     int number_failed;
+
+    tests_set_cwd();
+
     Suite *s = ipa_ldap_opt_suite ();
     SRunner *sr = srunner_create (s);
     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/ipa_timerules-tests.c b/src/tests/ipa_timerules-tests.c
index 0a7be90..29fc7e4 100644
--- a/src/tests/ipa_timerules-tests.c
+++ b/src/tests/ipa_timerules-tests.c
@@ -569,6 +569,8 @@ int main(int argc, const char *argv[])
     }
     debug_level = debug;
 
+    tests_set_cwd();
+
     timelib_suite = create_timelib_suite();
     sr = srunner_create(timelib_suite);
     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/krb5_utils-tests.c b/src/tests/krb5_utils-tests.c
index 362daf4..64cf9c3 100644
--- a/src/tests/krb5_utils-tests.c
+++ b/src/tests/krb5_utils-tests.c
@@ -27,6 +27,7 @@
 
 #include "providers/krb5/krb5_utils.h"
 #include "providers/krb5/krb5_auth.h"
+#include "tests/common.h"
 
 #define BASE "/abc/def"
 
@@ -296,6 +297,9 @@ Suite *krb5_utils_suite (void)
 int main(void)
 {
   int number_failed;
+
+  tests_set_cwd();
+
   Suite *s = krb5_utils_suite ();
   SRunner *sr = srunner_create (s);
   /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/refcount-tests.c b/src/tests/refcount-tests.c
index db2a256..5e85b6f 100644
--- a/src/tests/refcount-tests.c
+++ b/src/tests/refcount-tests.c
@@ -220,6 +220,8 @@ int main(int argc, const char *argv[])
     poptFreeContext(pc);
     debug_level = debug;
 
+    tests_set_cwd();
+
     suite = create_suite();
     sr = srunner_create(suite);
     srunner_set_fork_status(sr, CK_FORK);
diff --git a/src/tests/resolv-tests.c b/src/tests/resolv-tests.c
index e93d0f1..c0133b9 100644
--- a/src/tests/resolv-tests.c
+++ b/src/tests/resolv-tests.c
@@ -595,6 +595,8 @@ int main(int argc, const char *argv[])
     poptFreeContext(pc);
     debug_level = debug;
 
+    tests_set_cwd();
+
     resolv_suite = create_resolv_suite();
     sr = srunner_create(resolv_suite);
     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/stress-tests.c b/src/tests/stress-tests.c
index 9450531..3b53735 100644
--- a/src/tests/stress-tests.c
+++ b/src/tests/stress-tests.c
@@ -30,6 +30,7 @@
 #include <errno.h>
 
 #include "util/util.h"
+#include "tests/common.h"
 
 #define DEFAULT_START   10
 #define DEFAULT_STOP    20
@@ -247,6 +248,8 @@ int main(int argc, const char *argv[])
     }
     poptFreeContext(pc);
 
+    tests_set_cwd();
+
     verbose = pc_verbosity;
 
     if (pc_prefix) {
diff --git a/src/tests/strtonum-tests.c b/src/tests/strtonum-tests.c
index 7b9cf52..a7131c1 100644
--- a/src/tests/strtonum-tests.c
+++ b/src/tests/strtonum-tests.c
@@ -25,6 +25,7 @@
 #include <popt.h>
 #include "util/util.h"
 #include "util/strtonum.h"
+#include "tests/common.h"
 
 /********************
  * Utility routines *
@@ -445,6 +446,8 @@ int main(int argc, const char *argv[]) {
     }
     poptFreeContext(pc);
 
+    tests_set_cwd();
+
     strtonum_suite = create_strtonum_suite();
     sr = srunner_create(strtonum_suite);
     /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index 8b486b6..c6661ae 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -29,6 +29,7 @@
 #include "util/util.h"
 #include "confdb/confdb_setup.h"
 #include "db/sysdb_private.h"
+#include "tests/common.h"
 
 #define TESTS_PATH "tests_sysdb"
 #define TEST_CONF_FILE "tests_conf.ldb"
@@ -3297,6 +3298,8 @@ int main(int argc, const char *argv[]) {
     }
     poptFreeContext(pc);
 
+    tests_set_cwd();
+
     ret = unlink(TESTS_PATH"/"LOCAL_SYSDB_FILE);
     if (ret != EOK && errno != ENOENT) {
         fprintf(stderr, "Could not delete the test ldb file (%d) (%s)\n",
-- 
1.6.6.1

