| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) - 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 4 | # |
| 5 | # This library is free software; you can redistribute it and/or modify it under |
| 6 | # the terms of the GNU Lesser General Public License as published by the Free |
| 7 | # Software Foundation; version 2.1 of the License. |
| 8 | # |
| 9 | # This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 12 | # details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU Lesser General Public License |
| 15 | # along with this library; if not, write to the Free Software Foundation, Inc., |
| 16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | |
| 18 | TEST_DESC="Change working directory of process" |
| 19 | |
| 20 | CURDIR=$(dirname $0)/ |
| 21 | TESTDIR=$CURDIR/../../../ |
| 22 | |
| 23 | DIR=$(readlink -f $TESTDIR) |
| 24 | |
| 25 | NUM_TESTS=28 |
| 26 | |
| 27 | source $TESTDIR/utils/utils.sh |
| 28 | |
| 29 | #MUST set TESTDIR before calling those functions |
| 30 | plan_tests $NUM_TESTS |
| 31 | |
| 32 | print_test_banner "$TEST_DESC" |
| 33 | function test_relayd() |
| 34 | { |
| 35 | local working_dir=$(mktemp -d) |
| 36 | local test_dir=$(readlink -f $TESTDIR) |
| 37 | local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" |
| 38 | |
| 39 | diag "Test lttng-relayd normal mode change working directory" |
| 40 | |
| 41 | # There is no rendez-vous mechanism that can guarantee the good timing |
| 42 | # to check if the workdir directory was changed. |
| 43 | # In the case of lttng-sessiond this would be achieved using the |
| 44 | # --sig-parent option but lttng-relayd does not have this feature yet. |
| 45 | # Fall back on using polling of the value and unblock when the value is |
| 46 | # the one we expect. In case of a failure the test will hang. |
| 47 | $relayd_bin_path --working-directory $working_dir > /dev/null 2>&1 & |
| 48 | RELAYD_PIDS=$! |
| 49 | |
| 50 | while true; do |
| 51 | local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd) |
| 52 | test "${working_dir}" = "${cwd}" |
| 53 | if [ $? -eq 0 ]; then |
| 54 | # Working dir for process is valid |
| 55 | break |
| 56 | fi |
| 57 | sleep 0.1 |
| 58 | done |
| 59 | |
| 60 | # If we are here the test passed |
| 61 | pass "Working directory changed" |
| 62 | |
| 63 | stop_lttng_relayd |
| 64 | rm -rf ${working_dir} |
| 65 | } |
| 66 | |
| 67 | function test_relayd_daemon() |
| 68 | { |
| 69 | local working_dir=$(mktemp -d) |
| 70 | |
| 71 | diag "Test lttng-relayd daemon mode change working directory" |
| 72 | |
| 73 | start_lttng_relayd_opt 1 "-d" "--working-directory $working_dir" |
| 74 | |
| 75 | local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd) |
| 76 | |
| 77 | is "x${cwd}" "x${working_dir}" "Working directory changed" |
| 78 | |
| 79 | stop_lttng_relayd |
| 80 | rm -rf ${working_dir} |
| 81 | } |
| 82 | |
| 83 | function test_relayd_daemon_no_working_dir() |
| 84 | { |
| 85 | local expected_working_dir="/" |
| 86 | |
| 87 | diag "Test lttng-relayd daemon mode change working directory" |
| 88 | |
| 89 | start_lttng_relayd_opt 1 "-d" "" |
| 90 | |
| 91 | local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd) |
| 92 | |
| 93 | is "x${cwd}" "x${expected_working_dir}" "Working directory is ${expected_working_dir}" |
| 94 | |
| 95 | stop_lttng_relayd |
| 96 | rm -rf ${working_dir} |
| 97 | } |
| 98 | |
| 99 | function test_relayd_background() |
| 100 | { |
| 101 | local working_dir=$(mktemp -d) |
| 102 | |
| 103 | diag "Test lttng-relayd background mode change working directory" |
| 104 | |
| 105 | start_lttng_relayd_opt 1 "-b" "--working-directory $working_dir" |
| 106 | |
| 107 | local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd) |
| 108 | |
| 109 | is "x${cwd}" "x${working_dir}" "Working directory changed" |
| 110 | |
| 111 | stop_lttng_relayd |
| 112 | rm -rf ${working_dir} |
| 113 | } |
| 114 | |
| 115 | function test_relayd_background_no_working_dir() |
| 116 | { |
| 117 | local expected_working_dir="/" |
| 118 | diag "Test lttng-relayd background working directory" |
| 119 | |
| 120 | start_lttng_relayd_opt 1 "-b" "" |
| 121 | |
| 122 | local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd) |
| 123 | |
| 124 | is "x${cwd}" "x${expected_working_dir}" "Working directory is ${expected_working_dir}" |
| 125 | |
| 126 | stop_lttng_relayd |
| 127 | rm -rf ${working_dir} |
| 128 | } |
| 129 | |
| 130 | function test_relayd_debug_permission() |
| 131 | { |
| 132 | local working_dir=$(mktemp -d) |
| 133 | local output_pattern='Working directory is not writable' |
| 134 | ERROR_OUTPUT_DEST=$(mktemp -u) |
| 135 | |
| 136 | diag "Test lttng-relayd change working directory on non writable directory" |
| 137 | |
| 138 | # Removing write access to working dir |
| 139 | okx chmod -w ${working_dir} |
| 140 | |
| 141 | start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir" |
| 142 | |
| 143 | local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd) |
| 144 | is "x${cwd}" "x${working_dir}" "Workind directory changed" |
| 145 | |
| 146 | local output_content="$(cat ${ERROR_OUTPUT_DEST})" |
| 147 | like "x${output_content}" "$output_pattern" "Warning about missing write permission is present" |
| 148 | |
| 149 | stop_lttng_relayd |
| 150 | rm -rf ${working_dir} ${ERROR_OUTPUT_DEST} |
| 151 | ERROR_OUTPUT_DEST=/dev/null |
| 152 | } |
| 153 | |
| 154 | function test_relayd_failure() |
| 155 | { |
| 156 | local working_dir="$(mktemp -d)" |
| 157 | local working_dir_imaginary="${working_dir}/imaginary_directory" |
| 158 | local output_dest=$(mktemp -u) |
| 159 | local output_pattern='Error: Changing working directory' |
| 160 | local test_dir=$(readlink -f $TESTDIR) |
| 161 | local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" |
| 162 | |
| 163 | diag "Test lttng-relayd normal mode change non-existing directory" |
| 164 | |
| 165 | $relayd_bin_path -b --working-directory $working_dir_imaginary > $output_dest 2>&1 |
| 166 | test $? -eq "1" |
| 167 | ok $? "Expect failure to start lttng-relayd for non-existent working directory" |
| 168 | |
| 169 | RELAYD_PIDS=$(pgrep $RELAYD_MATCH) |
| 170 | if [ -z "${RELAYD_PIDS}" ]; then |
| 171 | pass "No lttng-relayd present" |
| 172 | else |
| 173 | fail "No lttng-relayd present" |
| 174 | stop_lttng_relayd_notap |
| 175 | fi |
| 176 | |
| 177 | local output_content="$(cat ${output_dest})" |
| 178 | like "x${output_content}" "$output_pattern" "Error about invalid directory" |
| 179 | |
| 180 | rm -rf ${working_dir} ${output_dest} |
| 181 | } |
| 182 | |
| 183 | function test_relayd_env() |
| 184 | { |
| 185 | local working_dir=$(mktemp -d) |
| 186 | |
| 187 | diag "Test lttng-relayd change working directory from env. variable" |
| 188 | |
| 189 | export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir} |
| 190 | start_lttng_relayd_opt 1 "-b" "" |
| 191 | |
| 192 | local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd) |
| 193 | |
| 194 | is "x${cwd}" "x${working_dir}" "Working directory changed" |
| 195 | |
| 196 | stop_lttng_relayd |
| 197 | rm -rf ${working_dir} |
| 198 | unset LTTNG_RELAYD_WORKING_DIRECTORY |
| 199 | } |
| 200 | |
| 201 | function test_relayd_cmdline_overwrite_env() |
| 202 | { |
| 203 | local working_dir_env=$(mktemp -d) |
| 204 | local working_dir_cmdline=$(mktemp -d) |
| 205 | |
| 206 | diag "Test lttng-relayd change working directory command line overwrite env variable" |
| 207 | |
| 208 | export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env} |
| 209 | start_lttng_relayd_opt 1 "-b" "--working-dir ${working_dir_cmdline}" |
| 210 | |
| 211 | local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd) |
| 212 | |
| 213 | is "x${cwd}" "x${working_dir_cmdline}" "Working directory is the one from command line" |
| 214 | |
| 215 | stop_lttng_relayd |
| 216 | rm -rf ${working_dir_env} ${working_dir_cmdline} |
| 217 | unset LTTNG_RELAYD_WORKING_DIRECTORY |
| 218 | } |
| 219 | TESTS=( |
| 220 | test_relayd |
| 221 | test_relayd_daemon |
| 222 | test_relayd_daemon_no_working_dir |
| 223 | test_relayd_background |
| 224 | test_relayd_background_no_working_dir |
| 225 | test_relayd_debug_permission |
| 226 | test_relayd_failure |
| 227 | test_relayd_env |
| 228 | test_relayd_cmdline_overwrite_env |
| 229 | ) |
| 230 | |
| 231 | for fct_test in ${TESTS[@]}; |
| 232 | do |
| 233 | ${fct_test} |
| 234 | if [ $? -ne 0 ]; then |
| 235 | break; |
| 236 | fi |
| 237 | done |