ctdb-tests: Fix logic to handle PATH additions for tests
authorMartin Schwenke <martin@meltin.net>
Wed, 15 Mar 2017 04:50:46 +0000 (15:50 +1100)
committerAmitay Isaacs <amitay@samba.org>
Mon, 14 Aug 2017 03:15:25 +0000 (05:15 +0200)
When using non-standard test subdirectories, the current code can fail
to find the test bin directory and stupidly just adds /bin to PATH.

Switch to using CTDB_TESTS_ARE_INSTALLED along with some sanity checks
to determine the mode of operation.

With this change, test directories can now be created as
subdirectories of arbitrary component directories.  Tests can then be
run directly, either by specifying the subdirectory or individual test
cases.

Integration into the top-level tests/ directory is then done via a
symbolic link, which enables 2 things:

* Ability to run a directory of test cases from top level by simply
  specifying the link name.

* Ease of installation - the installation code just works with the
  symbolic link.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/tests/scripts/common.sh

index dea0502cbd1509af6dcaa9e426bd4d14bb0e9720..4140da7135084c5990d362dda0a706cabaf906e1 100644 (file)
@@ -14,28 +14,32 @@ if [ $(dirname "$TEST_SUBDIR") = "." ] ; then
     TEST_SUBDIR=$(cd "$TEST_SUBDIR" ; pwd)
 fi
 
-_test_dir=$(dirname "$TEST_SUBDIR")
-
 # If we are running from within the source tree then, depending on the
 # tests that we're running, we may need to add the top level bin/ and
-# tools/ subdirectories to $PATH.  This means we need a way of
-# determining if we're running from within the source tree.  There is
-# no use looking outside the tests/ subdirectory because anything
-# above that level may be meaningless and outside our control.
-# Therefore, we'll use existence of $_test_dir/run_tests.sh to
-# indicate that we're running in-tree - on a system where the tests
-# have been installed, this file will be absent (renamed and placed in
-# some bin/ directory).
-if [ -f "${_test_dir}/run_tests.sh" ] ; then
-    ctdb_dir=$(dirname "$_test_dir")
-
-    _tools_dir="${ctdb_dir}/tools"
-    if [ -d "$_tools_dir" ] ; then
-       PATH="${_tools_dir}:$PATH"
-    fi
+# tools/ subdirectories to $PATH.  In this case, sanity check that
+# run_tests.sh is in the expected place.  If the tests are installed
+# then sanity check that TEST_BIN_DIR is set.
+if $CTDB_TESTS_ARE_INSTALLED ; then
+       if [ -z "$TEST_BIN_DIR" ] ; then
+               die "CTDB_TESTS_ARE_INSTALLED but TEST_BIN_DIR not set"
+       fi
+
+       _test_bin_dir="$TEST_BIN_DIR"
+else
+       if [ ! -f "${CTDB_TEST_DIR}/run_tests.sh" ] ; then
+               die "Tests not installed but can't find run_tests.sh"
+       fi
+
+       ctdb_dir=$(dirname "$CTDB_TEST_DIR")
+
+       _tools_dir="${ctdb_dir}/tools"
+       if [ -d "$_tools_dir" ] ; then
+               PATH="${_tools_dir}:$PATH"
+       fi
+
+       _test_bin_dir="${ctdb_dir}/bin"
 fi
 
-_test_bin_dir="${TEST_BIN_DIR:-${ctdb_dir}/bin}"
 case "$_test_bin_dir" in
     /*) : ;;
     *) _test_bin_dir="${PWD}/${_test_bin_dir}" ;;