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