Tests: add an lttng-relayd working directory test
[lttng-tools.git] / tests / regression / tools / working-directory / test_relayd_working_directory
diff --git a/tests/regression/tools/working-directory/test_relayd_working_directory b/tests/regression/tools/working-directory/test_relayd_working_directory
new file mode 100755 (executable)
index 0000000..13e816d
--- /dev/null
@@ -0,0 +1,241 @@
+#!/bin/bash
+#
+# Copyright (C) - 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+#
+# This library is free software; you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation; version 2.1 of the License.
+#
+# This library 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 Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this library; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+TEST_DESC="Change working directory of process"
+
+CURDIR=$(dirname "$0")/
+TESTDIR=$CURDIR/../../../
+
+DIR=$(readlink -f "$TESTDIR")
+
+NUM_TESTS=27
+
+source $TESTDIR/utils/utils.sh
+
+
+#MUST set TESTDIR before calling those functions
+plan_tests $NUM_TESTS
+
+print_test_banner "$TEST_DESC"
+function test_relayd()
+{
+       local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
+       local working_dir
+       local pid
+       local cwd
+
+       working_dir=$(mktemp -d)
+
+       diag "Test lttng-relayd normal mode change working directory"
+
+       # There is no rendez-vous mechanism that can guarantee the good timing
+       # to check if the workdir directory was changed.
+       # In the case of lttng-sessiond this would be achieved using the
+       # --sig-parent option but lttng-relayd does not have this feature yet.
+       # Fall back on using polling of the value and unblock when the value is
+       # the one we expect. In case of a failure the test will hang.
+       $relayd_bin_path --working-directory "$working_dir" > /dev/null 2>&1 &
+       pid=$!
+
+       while true; do
+               cwd=$(readlink "/proc/${pid}/cwd")
+               if test "$working_dir" = "$cwd"; then
+                       # Working dir for process is valid
+                       break
+               fi
+               sleep 0.1
+       done
+
+       # If we are here the test passed
+       pass "Working directory changed"
+
+       stop_lttng_relayd
+       rm -rf "$working_dir"
+}
+
+function test_relayd_daemon()
+{
+       local working_dir
+       local cwd
+       local pid
+
+       working_dir=$(mktemp -d)
+
+       diag "Test lttng-relayd daemon mode change working directory"
+
+       start_lttng_relayd_opt 1 "-d" "--working-directory $working_dir"
+
+       pid=$(pgrep "$RELAYD_MATCH")
+       ok $? "Found lttng-relayd"
+
+       cwd=$(readlink "/proc/${pid}/cwd")
+
+       is "$cwd" "$working_dir" "Working directory changed"
+
+       stop_lttng_relayd
+       rm -rf "$working_dir"
+}
+
+function test_relayd_daemon_no_working_dir()
+{
+       local expected_working_dir="/"
+       local cwd
+       local pid
+
+       diag "Test lttng-relayd daemon mode change working directory"
+
+       start_lttng_relayd_opt 1 "-d" ""
+
+       pid=$(pgrep "$RELAYD_MATCH")
+       ok $? "Found lttng-relayd"
+
+       cwd=$(readlink "/proc/${pid}/cwd")
+
+       is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
+
+       stop_lttng_relayd
+       rm -rf "$working_dir"
+}
+
+function test_relayd_background()
+{
+       local working_dir
+       local cwd
+       local pid
+
+       working_dir=$(mktemp -d)
+
+       diag "Test lttng-relayd background mode change working directory"
+
+       start_lttng_relayd_opt 1 "-b" "--working-directory $working_dir"
+
+       pid=$(pgrep "$RELAYD_MATCH")
+       ok $? "Found lttng-relayd"
+
+       cwd=$(readlink "/proc/${pid}/cwd")
+
+       is "$cwd" "$working_dir" "Working directory changed"
+
+       stop_lttng_relayd
+       rm -rf "$working_dir"
+}
+
+function test_relayd_background_no_working_dir()
+{
+       local expected_working_dir="/"
+       local cwd
+       local pid
+
+       diag "Test lttng-relayd background working directory"
+
+       start_lttng_relayd_opt 1 "-b" ""
+
+       pid=$(pgrep "$RELAYD_MATCH")
+       ok $? "Found lttng-relayd"
+
+       cwd=$(readlink "/proc/${pid}/cwd")
+
+       is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
+
+       stop_lttng_relayd
+       rm -rf "$working_dir"
+}
+
+function test_relayd_debug_permission()
+{
+       local output_pattern='Working directory \".*\" is not writable'
+       local working_dir
+       local cwd
+       local pid
+
+       working_dir=$(mktemp -d)
+
+       diag "Test lttng-relayd change working directory on non writable directory"
+
+       # Removing write access to working dir
+       okx chmod -w "$working_dir"
+
+       # Redirect the error output to a temporary file
+       ERROR_OUTPUT_DEST=$(mktemp)
+       start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
+
+       pid=$(pgrep "$RELAYD_MATCH")
+       ok $? "Found lttng-relayd"
+
+       cwd=$(readlink "/proc/${pid}/cwd")
+       is "$cwd" "$working_dir" "Working directory changed"
+
+       grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
+       ok $? "Warning about missing write permission is present"
+
+       stop_lttng_relayd
+       rm "$ERROR_OUTPUT_DEST"
+       rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
+       ERROR_OUTPUT_DEST=/dev/null
+}
+
+function test_relayd_failure()
+{
+       local output_pattern='Failed to change working directory to'
+       local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
+
+       local working_dir
+       local working_dir_imaginary
+       local output_dest
+       local pid
+
+       working_dir="$(mktemp -d)"
+       working_dir_imaginary="${working_dir}/imaginary_directory"
+       output_dest=$(mktemp)
+
+       diag "Test lttng-relayd normal mode change non-existing directory"
+
+       $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
+       test $? -eq "1"
+       ok $? "Expect failure to start lttng-relayd for non-existent working directory"
+
+       pid=$(pgrep "$RELAYD_MATCH")
+       if [ -z "$pid" ]; then
+               pass "No lttng-relayd present"
+       else
+               fail "No lttng-relayd present"
+               stop_lttng_relayd_notap
+       fi
+
+       grep -q "$output_pattern" "$output_dest"
+       ok $? "Found error message: invalid directory"
+
+       rm "$output_dest"
+       rm -rf "$working_dir"
+}
+
+TESTS=(
+       test_relayd
+       test_relayd_daemon
+       test_relayd_daemon_no_working_dir
+       test_relayd_background
+       test_relayd_background_no_working_dir
+       test_relayd_debug_permission
+       test_relayd_failure
+)
+
+for fct_test in "${TESTS[@]}";
+do
+       if ! ${fct_test}; then
+               break;
+       fi
+done
This page took 0.025746 seconds and 4 git commands to generate.