From f3630ec4ac23f0bc4f2e9b438310eadb5d3ebcb4 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Fri, 18 May 2018 16:24:04 -0400 Subject: [PATCH] Tests: add an lttng-relayd working directory test MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau --- configure.ac | 1 + src/common/daemonize.c | 2 +- tests/regression/Makefile.am | 3 +- tests/regression/tools/Makefile.am | 2 +- .../tools/working-directory/Makefile.am | 16 ++ .../test_relayd_working_directory | 241 ++++++++++++++++++ tests/utils/utils.sh | 21 +- 7 files changed, 273 insertions(+), 13 deletions(-) create mode 100644 tests/regression/tools/working-directory/Makefile.am create mode 100755 tests/regression/tools/working-directory/test_relayd_working_directory diff --git a/configure.ac b/configure.ac index 616608659..7212ed195 100644 --- a/configure.ac +++ b/configure.ac @@ -1140,6 +1140,7 @@ AC_CONFIG_FILES([ tests/regression/tools/rotation/Makefile tests/regression/tools/base-path/Makefile tests/regression/tools/metadata/Makefile + tests/regression/tools/working-directory/Makefile tests/regression/ust/Makefile tests/regression/ust/nprocesses/Makefile tests/regression/ust/high-throughput/Makefile diff --git a/src/common/daemonize.c b/src/common/daemonize.c index 88ec9f134..40cef3840 100644 --- a/src/common/daemonize.c +++ b/src/common/daemonize.c @@ -118,7 +118,7 @@ int lttng_daemonize(pid_t *child_ppid, int *completion_flag, /* * From this point on, the parent can exit and the child - * is now an operationnal session daemon ready to serve + * is now an operational session daemon ready to serve * clients and applications. */ exit(EXIT_SUCCESS); diff --git a/tests/regression/Makefile.am b/tests/regression/Makefile.am index 73eb9f7da..29201ed30 100644 --- a/tests/regression/Makefile.am +++ b/tests/regression/Makefile.am @@ -32,7 +32,8 @@ TESTS = tools/filtering/test_invalid_filter \ tools/rotation/test_kernel \ tools/rotation/test_save_load_mi \ tools/rotation/test_schedule_api \ - tools/metadata/test_kernel + tools/metadata/test_kernel \ + tools/working-directory/test_relayd_working_directory if HAVE_LIBLTTNG_UST_CTL SUBDIRS += ust diff --git a/tests/regression/tools/Makefile.am b/tests/regression/tools/Makefile.am index c50467599..6fd5f3505 100644 --- a/tests/regression/tools/Makefile.am +++ b/tests/regression/tools/Makefile.am @@ -1,3 +1,3 @@ SUBDIRS = streaming filtering health tracefile-limits snapshots live exclusion save-load mi \ wildcard crash regen-metadata regen-statedump notification rotation \ - base-path metadata + base-path metadata working-directory diff --git a/tests/regression/tools/working-directory/Makefile.am b/tests/regression/tools/working-directory/Makefile.am new file mode 100644 index 000000000..3f6631ff5 --- /dev/null +++ b/tests/regression/tools/working-directory/Makefile.am @@ -0,0 +1,16 @@ +noinst_SCRIPTS = test_relayd_working_directory +EXTRA_DIST = test_relayd_working_directory + +all-local: + @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ + for script in $(EXTRA_DIST); do \ + cp -f $(srcdir)/$$script $(builddir); \ + done; \ + fi + +clean-local: + @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ + for script in $(EXTRA_DIST); do \ + rm -f $(builddir)/$$script; \ + done; \ + fi 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 index 000000000..13e816d8a --- /dev/null +++ b/tests/regression/tools/working-directory/test_relayd_working_directory @@ -0,0 +1,241 @@ +#!/bin/bash +# +# Copyright (C) - 2018 Jonathan Rajotte +# +# 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 diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh index 0b126a706..a8b9b3bdf 100644 --- a/tests/utils/utils.sh +++ b/tests/utils/utils.sh @@ -397,22 +397,23 @@ function lttng_disable_kernel_channel_fail() function start_lttng_relayd_opt() { local withtap=$1 - local opt=$2 + local process_mode=$2 + local opt=$3 DIR=$(readlink -f "$TESTDIR") - if [ -z "$(pgrep "$RELAYD_MATCH")" ]; then + if [ -z $(pgrep $RELAYD_MATCH) ]; then # shellcheck disable=SC2086 - "$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" -b $opt 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST - #"$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" $opt -vvv >>/tmp/relayd.log 2>&1 & + $DIR/../src/bin/lttng-relayd/$RELAYD_BIN $process_mode $opt 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST + #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 & if [ $? -eq 1 ]; then - if [ "$withtap" -eq "1" ]; then - fail "Start lttng-relayd (opt: $opt)" + if [ $withtap -eq "1" ]; then + fail "Start lttng-relayd (process mode: $process_mode opt: $opt)" fi return 1 else - if [ "$withtap" -eq "1" ]; then - pass "Start lttng-relayd (opt: $opt)" + if [ $withtap -eq "1" ]; then + pass "Start lttng-relayd (process mode: $process_mode opt: $opt)" fi fi else @@ -422,12 +423,12 @@ function start_lttng_relayd_opt() function start_lttng_relayd() { - start_lttng_relayd_opt 1 "$@" + start_lttng_relayd_opt 1 "-b" "$@" } function start_lttng_relayd_notap() { - start_lttng_relayd_opt 0 "$@" + start_lttng_relayd_opt 0 "-b" "$@" } function stop_lttng_relayd_opt() -- 2.34.1