Backport: Test: lttng-relayd working directory
[lttng-tools.git] / tests / regression / tools / working-directory / test_relayd_workding_directory
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=22
26
27 source $TESTDIR/utils/utils.sh
28
29
30 #MUST set TESTDIR before calling those functions
31 plan_tests $NUM_TESTS
32
33 print_test_banner "$TEST_DESC"
34 function test_relayd()
35 {
36 local working_dir=$(mktemp -d)
37 local test_dir=$(readlink -f $TESTDIR)
38 local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
39
40 diag "Test lttng-relayd normal mode change working directory"
41
42 # There is no rendez-vous mechanism that can guarantee the good timing
43 # to check if the workdir directory was changed.
44 # In the case of lttng-sessiond this would be achieved using the
45 # --sig-parent option but lttng-relayd does not have this feature yet.
46 # Fall back on using polling of the value and unblock when the value is
47 # the one we expect. In case of a failure the test will hang.
48 $relayd_bin_path --working-directory $working_dir > /dev/null 2>&1 &
49 RELAYD_PIDS=$!
50
51 while true; do
52 local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd)
53 test "${working_dir}" = "${cwd}"
54 if [ $? -eq 0 ]; then
55 # Working dir for process is valid
56 break
57 fi
58 sleep 0.1
59 done
60
61 # If we are here the test passed
62 pass "Working directory changed"
63
64 stop_lttng_relayd
65 rm -rf ${working_dir}
66 }
67
68 function test_relayd_daemon()
69 {
70 local working_dir=$(mktemp -d)
71
72 diag "Test lttng-relayd daemon mode change working directory"
73
74 start_lttng_relayd_opt 1 "-d" "--working-directory $working_dir"
75
76 local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd)
77
78 is "x${cwd}" "x${working_dir}" "Working directory changed"
79
80 stop_lttng_relayd
81 rm -rf ${working_dir}
82 }
83
84 function test_relayd_daemon_no_working_dir()
85 {
86 local expected_working_dir="/"
87
88 diag "Test lttng-relayd daemon mode change working directory"
89
90 start_lttng_relayd_opt 1 "-d" ""
91
92 local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd)
93
94 is "x${cwd}" "x${expected_working_dir}" "Working directory is ${expected_working_dir}"
95
96 stop_lttng_relayd
97 rm -rf ${working_dir}
98 }
99
100 function test_relayd_background()
101 {
102 local working_dir=$(mktemp -d)
103
104 diag "Test lttng-relayd background mode change working directory"
105
106 start_lttng_relayd_opt 1 "-b" "--working-directory $working_dir"
107
108 local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd)
109
110 is "x${cwd}" "x${working_dir}" "Working directory changed"
111
112 stop_lttng_relayd
113 rm -rf ${working_dir}
114 }
115
116 function test_relayd_background_no_working_dir()
117 {
118 local expected_working_dir="/"
119 diag "Test lttng-relayd background working directory"
120
121 start_lttng_relayd_opt 1 "-b" ""
122
123 local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd)
124
125 is "x${cwd}" "x${expected_working_dir}" "Working directory is ${expected_working_dir}"
126
127 stop_lttng_relayd
128 rm -rf ${working_dir}
129 }
130
131 function test_relayd_debug_permission()
132 {
133 local working_dir=$(mktemp -d)
134 local output_pattern='Working directory is not writable'
135 ERROR_OUTPUT_DEST=$(mktemp -u)
136
137 diag "Test lttng-relayd change working directory on non writable directory"
138
139 # Removing write access to working dir
140 okx chmod -w ${working_dir}
141
142 start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
143
144 local cwd=$(readlink /proc/${RELAYD_PIDS}/cwd)
145 is "x${cwd}" "x${working_dir}" "Workind directory changed"
146
147 local output_content="$(cat ${ERROR_OUTPUT_DEST})"
148 like "x${output_content}" "$output_pattern" "Warning about missing write permission is present"
149
150 stop_lttng_relayd
151 rm -rf ${working_dir} ${ERROR_OUTPUT_DEST}
152 ERROR_OUTPUT_DEST=/dev/null
153 }
154
155 function test_relayd_failure()
156 {
157 local working_dir="$(mktemp -d)"
158 local working_dir_imaginary="${working_dir}/imaginary_directory"
159 local output_dest=$(mktemp -u)
160 local output_pattern='Error: Changing working directory'
161 local test_dir=$(readlink -f $TESTDIR)
162 local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
163
164 diag "Test lttng-relayd normal mode change non-existing directory"
165
166 $relayd_bin_path -b --working-directory $working_dir_imaginary > $output_dest 2>&1
167 test $? -eq "1"
168 ok $? "Expect failure to start lttng-relayd for non-existent working directory"
169
170 RELAYD_PIDS=$(pgrep $RELAYD_MATCH)
171 if [ -z "${RELAYD_PIDS}" ]; then
172 pass "No lttng-relayd present"
173 else
174 fail "No lttng-relayd present"
175 stop_lttng_relayd_notap
176 fi
177
178 local output_content="$(cat ${output_dest})"
179 like "x${output_content}" "$output_pattern" "Error about invalid directory"
180
181 rm -rf ${working_dir} ${output_dest}
182 }
183
184 TESTS=(
185 test_relayd
186 test_relayd_daemon
187 test_relayd_daemon_no_working_dir
188 test_relayd_background
189 test_relayd_background_no_working_dir
190 test_relayd_debug_permission
191 test_relayd_failure
192 )
193
194 for fct_test in ${TESTS[@]};
195 do
196 ${fct_test}
197 if [ $? -ne 0 ]; then
198 break;
199 fi
200 done
This page took 0.037335 seconds and 5 git commands to generate.