Cleanup: tests: name all temporary files to better identify leakage
[lttng-tools.git] / tests / regression / tools / working-directory / test_relayd_working_directory
index 956cb780a6e368c7239aa51cff73b03f757bce65..c7e784cca2feaedc20c2d92049d0655fe7db2517 100755 (executable)
@@ -1,19 +1,8 @@
 #!/bin/bash
 #
-# Copyright (C) 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+# 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
+# SPDX-License-Identifier: LGPL-2.1-only
 
 TEST_DESC="Change working directory of process"
 
@@ -33,11 +22,10 @@ print_test_banner "$TEST_DESC"
 function test_relayd()
 {
        local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
-       local working_dir
+       local working_dir=$(realpath "$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")")
        local pid
        local cwd
 
-       working_dir=$(realpath "$(mktemp -d)")
 
        diag "Test lttng-relayd normal mode change working directory"
 
@@ -68,17 +56,15 @@ function test_relayd()
 
 function test_relayd_daemon()
 {
-       local working_dir
        local cwd
        local pid
-
-       working_dir=$(realpath "$(mktemp -d)")
+       local working_dir=$(realpath "$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")")
 
        diag "Test lttng-relayd daemon mode change working directory"
 
        start_lttng_relayd_opt 1 "-d" "--working-directory $working_dir"
 
-       pid=$(pgrep "$RELAYD_MATCH")
+       pid=$(lttng_pgrep "$RELAYD_MATCH")
        ok $? "Found lttng-relayd"
 
        cwd=$(readlink "/proc/${pid}/cwd")
@@ -99,7 +85,7 @@ function test_relayd_daemon_no_working_dir()
 
        start_lttng_relayd_opt 1 "-d" ""
 
-       pid=$(pgrep "$RELAYD_MATCH")
+       pid=$(lttng_pgrep "$RELAYD_MATCH")
        ok $? "Found lttng-relayd"
 
        cwd=$(readlink "/proc/${pid}/cwd")
@@ -112,17 +98,15 @@ function test_relayd_daemon_no_working_dir()
 
 function test_relayd_background()
 {
-       local working_dir
        local cwd
        local pid
-
-       working_dir=$(realpath "$(mktemp -d)")
+       local working_dir=$(realpath "$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")")
 
        diag "Test lttng-relayd background mode change working directory"
 
        start_lttng_relayd_opt 1 "-b" "--working-directory $working_dir"
 
-       pid=$(pgrep "$RELAYD_MATCH")
+       pid=$(lttng_pgrep "$RELAYD_MATCH")
        ok $? "Found lttng-relayd"
 
        cwd=$(readlink "/proc/${pid}/cwd")
@@ -143,7 +127,7 @@ function test_relayd_background_no_working_dir()
 
        start_lttng_relayd_opt 1 "-b" ""
 
-       pid=$(pgrep "$RELAYD_MATCH")
+       pid=$(lttng_pgrep "$RELAYD_MATCH")
        ok $? "Found lttng-relayd"
 
        cwd=$(readlink "/proc/${pid}/cwd")
@@ -156,50 +140,58 @@ function test_relayd_background_no_working_dir()
 
 function test_relayd_debug_permission()
 {
-       local output_pattern='Working directory \".*\" is not writable'
-       local working_dir
-       local cwd
-       local pid
-
-       working_dir=$(realpath "$(mktemp -d)")
+       local is_user
 
        diag "Test lttng-relayd change working directory on non writable directory"
 
-       # Removing write access to working dir
-       okx chmod -w "$working_dir"
+       if [ "$(id -u)" == "0" ]; then
+           is_user=0
+       else
+           is_user=1
+       fi
 
-       # Redirect the error output to a temporary file
-       ERROR_OUTPUT_DEST=$(mktemp)
-       start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
+       skip $is_user "Skipping permission debug output test; operation can't fail as root" 6 ||
+       {
+               local output_pattern='Working directory \".*\" is not writable'
+               local cwd
+               local pid
+               local working_dir=$(realpath "$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")")
 
-       pid=$(pgrep "$RELAYD_MATCH")
-       ok $? "Found lttng-relayd"
+               # Removing write access to working dir
+               okx chmod -w "$working_dir"
 
-       cwd=$(readlink "/proc/${pid}/cwd")
-       is "$cwd" "$working_dir" "Working directory changed"
+               # Redirect the error output to a temporary file
 
-       grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
-       ok $? "Warning about missing write permission is present"
+               ERROR_OUTPUT_DEST=$(mktemp --tmpdir "tmp.${FUNCNAME[0]}_error_output.XXXXXX")
+               start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
 
-       stop_lttng_relayd
-       rm "$ERROR_OUTPUT_DEST"
-       rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
-       ERROR_OUTPUT_DEST=/dev/null
+               pid=$(lttng_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=$(realpath "$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")")
+       local output_dest=$(mktemp --tmpdir "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")
 
-       local working_dir
        local working_dir_imaginary
-       local output_dest
        local pid
 
-       working_dir=$(realpath "$(mktemp -d)")
        working_dir_imaginary="${working_dir}/imaginary_directory"
-       output_dest=$(mktemp)
 
        diag "Test lttng-relayd normal mode change non-existing directory"
 
@@ -207,7 +199,7 @@ function test_relayd_failure()
        test $? -eq "1"
        ok $? "Expect failure to start lttng-relayd for non-existent working directory"
 
-       pid=$(pgrep "$RELAYD_MATCH")
+       pid=$(lttng_pgrep "$RELAYD_MATCH")
        if [ -z "$pid" ]; then
                pass "No lttng-relayd present"
        else
@@ -224,19 +216,16 @@ function test_relayd_failure()
 
 function test_relayd_env()
 {
-       local working_dir
        local cwd
        local pid
-
-       working_dir=$(mktemp -d)
-       working_dir=$(realpath "$(mktemp -d)")
+       local working_dir=$(realpath "$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")")
 
        diag "Test lttng-relayd change working directory from env. variable"
 
        export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir}
        start_lttng_relayd_opt 1 "-b" ""
 
-       pid=$(pgrep "$RELAYD_MATCH")
+       pid=$(lttng_pgrep "$RELAYD_MATCH")
        ok $? "Found lttng-relayd"
 
        cwd=$(readlink "/proc/$pid/cwd")
@@ -250,20 +239,17 @@ function test_relayd_env()
 
 function test_relayd_cmdline_overwrite_env()
 {
-       local working_dir_env
-       local working_dir_cmdline
        local cwd
        local pid
-
-       working_dir_env=$(realpath "$(mktemp -d)")
-       working_dir_cmdline=$(realpath "$(mktemp -d)")
+       local working_dir_env=$(realpath "$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_working_dir_even.XXXXXX")")
+       local working_dir_cmdline=$(realpath "$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_working_dir_cmdline.XXXXXX")")
 
        diag "Test lttng-relayd change working directory command line overwrite env variable"
 
        export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env}
        start_lttng_relayd_opt 1 "-b" "--working-dir ${working_dir_cmdline}"
 
-       pid=$(pgrep "$RELAYD_MATCH")
+       pid=$(lttng_pgrep "$RELAYD_MATCH")
        ok $? "Found lttng-relayd"
 
        cwd=$(readlink "/proc/$pid/cwd")
This page took 0.027503 seconds and 4 git commands to generate.