• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2#
3# Copyright (C) 2019-2021 Red Hat, Inc.
4# This file is part of elfutils.
5#
6# This file is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10#
11# elfutils is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19. $srcdir/debuginfod-subr.sh
20
21# for test case debugging, uncomment:
22set -x
23unset VALGRIND_CMD
24
25DB=${PWD}/.debuginfod_tmp.sqlite
26export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache
27export DEBUGINFOD_URLS='http://127.0.0.1:0' # Note invalid, will trigger error_count metric
28tempfiles $DB
29# Clean old dirictories
30mkdir D L F
31
32# not tempfiles F R L D Z - they are directories which we clean up manually
33ln -s ${abs_builddir}/dwfllines L/foo   # any program not used elsewhere in this test
34# This variable is essential and ensures no time-race for claiming ports occurs
35# set base to a unique multiple of 100 not used in any other 'run-debuginfod-*' test
36base=9000
37get_ports
38
39# Launch server which will be unable to follow symlinks
40env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -d ${DB} -F -U -t0 -g0 -p $PORT1 L D F > vlog$PORT1 2>&1 &
41PID1=$!
42tempfiles vlog$PORT1
43errfiles vlog$PORT1
44
45wait_ready $PORT1 'ready' 1
46
47# Wait till initial scan is done
48wait_ready $PORT1 'thread_work_total{role="traverse"}' 1
49wait_ready $PORT1 'thread_work_pending{role="scan"}' 0
50wait_ready $PORT1 'thread_busy{role="scan"}' 0
51########################################################################
52# Compile a simple program, strip its debuginfo and save the build-id.
53# Also move the debuginfo into another directory so that elfutils
54# cannot find it without debuginfod.
55echo "int main() { return 0; }" > ${PWD}/prog.c
56tempfiles prog.c
57# Create a subdirectory to confound source path names
58mkdir foobar
59gcc -Wl,--build-id -g -o prog ${PWD}/foobar///./../prog.c
60testrun ${abs_top_builddir}/src/strip -g -f prog.debug ${PWD}/prog
61BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
62          -a prog | grep 'Build ID' | cut -d ' ' -f 7`
63
64mv prog F
65mv prog.debug F
66kill -USR1 $PID1
67# Wait till both files are in the index.
68wait_ready $PORT1 'thread_work_total{role="traverse"}' 2
69wait_ready $PORT1 'thread_work_pending{role="scan"}' 0
70wait_ready $PORT1 'thread_busy{role="scan"}' 0
71
72wait_ready $PORT1 'thread_busy{role="http-buildid"}' 0
73wait_ready $PORT1 'thread_busy{role="http-metrics"}' 1
74
75export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache2
76mkdir -p $DEBUGINFOD_CACHE_PATH
77# NB: run in -L symlink-following mode for the L subdir
78env LD_LIBRARY_PATH=$ldpath DEBUGINFOD_URLS=http://127.0.0.1:$PORT1 ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -d ${DB}_2 -F -U -p $PORT2 -L L D > vlog$PORT2 2>&1 &
79PID2=$!
80tempfiles vlog$PORT2
81errfiles vlog$PORT2
82tempfiles ${DB}_2
83
84wait_ready $PORT2 'ready' 1
85wait_ready $PORT2 'thread_work_total{role="traverse"}' 1
86wait_ready $PORT2 'thread_work_pending{role="scan"}' 0
87wait_ready $PORT2 'thread_busy{role="scan"}' 0
88
89wait_ready $PORT2 'thread_busy{role="http-buildid"}' 0
90wait_ready $PORT2 'thread_busy{role="http-metrics"}' 1
91
92# have clients contact the new server
93export DEBUGINFOD_URLS=http://127.0.0.1:$PORT2
94# Use fresh cache for debuginfod-find client requests
95export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache3
96mkdir -p $DEBUGINFOD_CACHE_PATH
97
98if type bsdtar 2>/dev/null; then
99    # copy in the deb files
100    cp -rvp ${abs_srcdir}/debuginfod-debs/*deb D
101    kill -USR1 $PID2
102    wait_ready $PORT2 'thread_work_total{role="traverse"}' 2
103    wait_ready $PORT2 'thread_work_pending{role="scan"}' 0
104    wait_ready $PORT2 'thread_busy{role="scan"}' 0
105
106    # All debs need to be in the index
107    debs=$(find D -name \*.deb | wc -l)
108    wait_ready $PORT2 'scanned_files_total{source=".deb archive"}' `expr $debs`
109    ddebs=$(find D -name \*.ddeb | wc -l)
110    wait_ready $PORT2 'scanned_files_total{source=".ddeb archive"}' `expr $ddebs`
111
112    # ubuntu
113    archive_test f17a29b5a25bd4960531d82aa6b07c8abe84fa66 "" ""
114fi
115
116testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
117
118# send a request to stress XFF and User-Agent federation relay;
119# we'll grep for the two patterns in vlog$PORT1
120curl -s -H 'User-Agent: TESTCURL' -H 'X-Forwarded-For: TESTXFF' $DEBUGINFOD_URLS/buildid/deaddeadbeef00000000/debuginfo -o /dev/null || true
121
122grep UA:TESTCURL vlog$PORT1
123grep XFF:TESTXFF vlog$PORT1
124
125# confirm that first server can't resolve symlinked info in L/ but second can
126BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
127         -a L/foo | grep 'Build ID' | cut -d ' ' -f 7`
128file L/foo
129file -L L/foo
130export DEBUGINFOD_URLS=http://127.0.0.1:$PORT1
131rm -rf $DEBUGINFOD_CACHE_PATH
132testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID && false || true
133rm -f $DEBUGINFOD_CACHE_PATH/$BUILDID/debuginfo # drop negative-hit file
134export DEBUGINFOD_URLS=http://127.0.0.1:$PORT2
135testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
136
137# test again with scheme free url
138export DEBUGINFOD_URLS=127.0.0.1:$PORT1
139rm -rf $DEBUGINFOD_CACHE_PATH
140testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID && false || true
141rm -f $DEBUGINFOD_CACHE_PATH/$BUILDID/debuginfo # drop negative-hit file
142export DEBUGINFOD_URLS=127.0.0.1:$PORT2
143testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
144# test parallel queries in client
145export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache3
146mkdir -p $DEBUGINFOD_CACHE_PATH
147export DEBUGINFOD_URLS="BAD http://127.0.0.1:$PORT1 127.0.0.1:$PORT1 http://127.0.0.1:$PORT2 DNE"
148
149testrun ${abs_builddir}/debuginfod_build_id_find -e F/prog 1
150
151########################################################################
152# Fetch some metrics
153curl -s http://127.0.0.1:$PORT1/badapi
154curl -s http://127.0.0.1:$PORT1/metrics
155curl -s http://127.0.0.1:$PORT2/metrics
156curl -s http://127.0.0.1:$PORT1/metrics | grep -q 'http_responses_total.*result.*error'
157curl -s http://127.0.0.1:$PORT2/metrics | grep -q 'http_responses_total.*result.*upstream'
158curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_duration_milliseconds_count'
159curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_duration_milliseconds_sum'
160curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_transfer_bytes_count'
161curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_transfer_bytes_sum'
162curl -s http://127.0.0.1:$PORT1/metrics | grep 'fdcache_'
163curl -s http://127.0.0.1:$PORT1/metrics | grep 'error_count'
164curl -s http://127.0.0.1:$PORT1/metrics | grep 'traversed_total'
165curl -s http://127.0.0.1:$PORT1/metrics | grep 'scanned_bytes_total'
166
167# And generate a few errors into the second debuginfod's logs, for analysis just below
168curl -s http://127.0.0.1:$PORT2/badapi > /dev/null || true
169curl -s http://127.0.0.1:$PORT2/buildid/deadbeef/debuginfo > /dev/null || true
170# NB: this error is used to seed the 404 failure for the survive-404 tests
171
172# Confirm bad artifact types are rejected without leaving trace
173curl -s http://127.0.0.1:$PORT2/buildid/deadbeef/badtype > /dev/null || true
174(curl -s http://127.0.0.1:$PORT2/metrics | grep 'badtype') && false
175
176# Confirm that reused curl connections survive 404 errors.
177# The rm's force an uncached fetch (in both servers and client cache)
178rm -f .client_cache*/$BUILDID/debuginfo
179testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
180rm -f .client_cache*/$BUILDID/debuginfo
181testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
182testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
183testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
184rm -f .client_cache*/$BUILDID/debuginfo
185testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
186
187# Confirm that some debuginfod client pools are being used
188curl -s http://127.0.0.1:$PORT2/metrics | grep 'dc_pool_op.*reuse'
189
190# Trigger a flood of requests against the same archive content file.
191# Use a file that hasn't been previously extracted in to make it
192# likely that even this test debuginfod will experience concurrency
193# and impose some "after-you" delays.
194(for i in `seq 100`; do
195    curl -s http://127.0.0.1:$PORT1/buildid/87c08d12c78174f1082b7c888b3238219b0eb265/executable >/dev/null &
196 done;
197 wait)
198curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_after_you.*'
199# If we could guarantee some minimum number of seconds of CPU time, we
200# could assert that the after_you metrics show some nonzero amount of
201# waiting.  A few hundred ms is typical on this developer's workstation.
202
203kill $PID1
204kill $PID2
205wait $PID1
206wait $PID2
207PID1=0
208PID2=0
209exit 0
210
211