Debian Codegraph @

test/integration/targets/stacktrace_dump/runme.sh

raw
1 #!/usr/bin/env bash
2
3 set -eux
4
5 # Get the debug directory where stacktrace files are written
6 DEBUG_DIR="${ANSIBLE_HOME:-$HOME/.ansible}/debug"
7
8 test_controller_debug_directory() {
9 echo "=== Test: Stacktrace dump to debug directory ==="
10 echo "Using debug directory: $DEBUG_DIR"
11
12 # Start ansible-playbook in background with a long-running task
13 ansible-playbook -i inventory playbook.yml &
14 local ANSIBLE_PID
15 ANSIBLE_PID=$!
16
17 # Give it time to start executing and spawn workers
18 sleep 2
19
20 # Find all subprocess PIDs
21 local CHILD_PIDS
22 CHILD_PIDS=$(pgrep -P $ANSIBLE_PID 2>/dev/null || true)
23
24 # Send SIGUSR1 signal to main process and all children
25 kill -SIGUSR1 $ANSIBLE_PID
26 # shellcheck disable=SC2086
27 for child_pid in $CHILD_PIDS; do
28 kill -SIGUSR1 $child_pid 2>/dev/null || true
29 done
30
31 # Give it time to write the files
32 sleep 1
33
34 # Find the stacktrace file for main process in debug directory
35 local STACKTRACE_FILE
36 STACKTRACE_FILE=$(find -L "$DEBUG_DIR" -name "ansible-${ANSIBLE_PID}.debug" 2>/dev/null | head -1)
37
38 if [[ -z "$STACKTRACE_FILE" ]]; then
39 echo "FAIL: Stacktrace file not found in $DEBUG_DIR for main process"
40 kill $ANSIBLE_PID 2>/dev/null || true
41 exit 1
42 fi
43
44 echo "Found stacktrace file for main process: $STACKTRACE_FILE"
45
46 # Verify file contains expected content
47 if ! grep -q "Process ${ANSIBLE_PID} stacktrace" "$STACKTRACE_FILE"; then
48 echo "FAIL: Stacktrace file missing process stacktrace header"
49 cat "$STACKTRACE_FILE"
50 kill $ANSIBLE_PID 2>/dev/null || true
51 exit 1
52 fi
53
54 if ! grep -q "Thread stacktraces" "$STACKTRACE_FILE"; then
55 echo "FAIL: Stacktrace file missing thread stacktraces header"
56 cat "$STACKTRACE_FILE"
57 kill $ANSIBLE_PID 2>/dev/null || true
58 exit 1
59 fi
60
61 # Check for child process stacktrace files
62 local CHILD_PIDS_COUNT=0
63 local CHILD_FILES_FOUND=0
64 for child_pid in $CHILD_PIDS; do
65 CHILD_PIDS_COUNT=$((CHILD_PIDS_COUNT + 1))
66 local CHILD_FILE
67 CHILD_FILE=$(find -L "$DEBUG_DIR" -name "ansible-${child_pid}.debug" 2>/dev/null | head -1)
68 if [[ -n "$CHILD_FILE" ]]; then
69 echo "Found stacktrace file for child process $child_pid: $CHILD_FILE"
70 CHILD_FILES_FOUND=$((CHILD_FILES_FOUND + 1))
71 rm -f "$CHILD_FILE"
72 fi
73 done
74
75 if [[ $CHILD_PIDS_COUNT -gt 0 ]] && [[ $CHILD_FILES_FOUND -ne $CHILD_PIDS_COUNT ]]; then
76 echo "FAIL: Expected stacktrace files for all $CHILD_PIDS_COUNT child processes, but found only $CHILD_FILES_FOUND"
77 kill $ANSIBLE_PID 2>/dev/null || true
78 exit 1
79 else
80 echo "Found stacktrace files for $CHILD_FILES_FOUND child process(es)"
81 fi
82
83 # Clean up
84 kill $ANSIBLE_PID 2>/dev/null || true
85 wait $ANSIBLE_PID 2>/dev/null || true
86 rm -f "$STACKTRACE_FILE"
87
88 echo "Test PASSED"
89 }
90
91 test_multiple_signals_append() {
92 echo "=== Test: Multiple SIGUSR1 signals append to file ==="
93
94 # Start ansible-playbook in background
95 ansible-playbook -i inventory playbook.yml &
96 local ANSIBLE_PID
97 ANSIBLE_PID=$!
98
99 # Give it time to start executing
100 sleep 2
101
102 # Send SIGUSR1 signal twice
103 kill -SIGUSR1 $ANSIBLE_PID
104 sleep 1
105 kill -SIGUSR1 $ANSIBLE_PID
106 sleep 1
107
108 # Find the stacktrace file
109 local STACKTRACE_FILE
110 STACKTRACE_FILE=$(find -L "$DEBUG_DIR" -name "ansible-${ANSIBLE_PID}.debug" 2>/dev/null | head -1)
111
112 if [[ -z "$STACKTRACE_FILE" ]]; then
113 echo "FAIL: Stacktrace file not found"
114 kill $ANSIBLE_PID 2>/dev/null || true
115 exit 1
116 fi
117
118 # Count the number of stacktrace entries (should be 2)
119 local COUNT
120 COUNT=$(grep -c "Process ${ANSIBLE_PID} stacktrace" "$STACKTRACE_FILE")
121
122 if [[ "$COUNT" -ne 2 ]]; then
123 echo "FAIL: Expected 2 stacktrace entries, found $COUNT"
124 cat "$STACKTRACE_FILE"
125 kill $ANSIBLE_PID 2>/dev/null || true
126 exit 1
127 fi
128
129 echo "Found $COUNT stacktrace entries as expected"
130
131 # Clean up
132 kill $ANSIBLE_PID 2>/dev/null || true
133 wait $ANSIBLE_PID 2>/dev/null || true
134 rm -f "$STACKTRACE_FILE"
135
136 echo "Test PASSED"
137 }
138
139 test_symlink_not_followed() {
140 echo "=== Test: Symlink at stacktrace path is not followed ==="
141
142 # Start ansible-playbook in background
143 ansible-playbook -i inventory playbook.yml &
144 local ANSIBLE_PID
145 ANSIBLE_PID=$!
146
147 # Give it time to start executing and pre-create the debug directory
148 sleep 2
149
150 # Pre-plant a symlink at the predictable stacktrace path pointing at a canary
151 # file. A correct handler must refuse to follow it (O_NOFOLLOW).
152 local CANARY
153 CANARY=$(mktemp)
154 rm -f "$CANARY"
155 local SYMLINK="$DEBUG_DIR/ansible-${ANSIBLE_PID}.debug"
156 rm -f "$SYMLINK"
157 ln -s "$CANARY" "$SYMLINK"
158
159 # Signal the process to trigger the stacktrace write
160 kill -SIGUSR1 $ANSIBLE_PID
161 sleep 1
162
163 # The canary must NOT have been created/written through the symlink
164 if [[ -e "$CANARY" ]]; then
165 echo "FAIL: stacktrace write followed the symlink and wrote to $CANARY"
166 kill $ANSIBLE_PID 2>/dev/null || true
167 rm -f "$CANARY" "$SYMLINK"
168 exit 1
169 fi
170
171 # The planted symlink should remain a symlink (open failed, nothing replaced it)
172 if [[ ! -L "$SYMLINK" ]]; then
173 echo "FAIL: expected the planted symlink to remain untouched at $SYMLINK"
174 kill $ANSIBLE_PID 2>/dev/null || true
175 rm -f "$CANARY" "$SYMLINK"
176 exit 1
177 fi
178
179 echo "Symlink was not followed and canary was not written"
180
181 # Clean up
182 kill $ANSIBLE_PID 2>/dev/null || true
183 wait $ANSIBLE_PID 2>/dev/null || true
184 rm -f "$CANARY" "$SYMLINK"
185
186 echo "Test PASSED"
187 }
188
189 # Run all tests
190 test_controller_debug_directory
191 test_multiple_signals_append
192 test_symlink_not_followed
193
194 echo "=== All tests PASSED ==="