xref: /aosp_15_r20/external/elfutils/tests/debuginfod-subr.sh (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker# Copyright (C) 2021 Red Hat, Inc.
2*7304104dSAndroid Build Coastguard Worker# This file is part of elfutils.
3*7304104dSAndroid Build Coastguard Worker#
4*7304104dSAndroid Build Coastguard Worker# This file is free software; you can redistribute it and/or modify
5*7304104dSAndroid Build Coastguard Worker# it under the terms of the GNU General Public License as published by
6*7304104dSAndroid Build Coastguard Worker# the Free Software Foundation; either version 3 of the License, or
7*7304104dSAndroid Build Coastguard Worker# (at your option) any later version.
8*7304104dSAndroid Build Coastguard Worker#
9*7304104dSAndroid Build Coastguard Worker# elfutils is distributed in the hope that it will be useful, but
10*7304104dSAndroid Build Coastguard Worker# WITHOUT ANY WARRANTY; without even the implied warranty of
11*7304104dSAndroid Build Coastguard Worker# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*7304104dSAndroid Build Coastguard Worker# GNU General Public License for more details.
13*7304104dSAndroid Build Coastguard Worker#
14*7304104dSAndroid Build Coastguard Worker# You should have received a copy of the GNU General Public License
15*7304104dSAndroid Build Coastguard Worker# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16*7304104dSAndroid Build Coastguard Worker
17*7304104dSAndroid Build Coastguard Worker# sourced from run-debuginfod-*.sh tests (must be bash scripts)
18*7304104dSAndroid Build Coastguard Worker
19*7304104dSAndroid Build Coastguard Worker# We trap ERR and like commands that fail in function to also trap
20*7304104dSAndroid Build Coastguard Workerset -o functrace
21*7304104dSAndroid Build Coastguard Workerset -o errtrace
22*7304104dSAndroid Build Coastguard Worker
23*7304104dSAndroid Build Coastguard Worker. $srcdir/test-subr.sh  # includes set -e
24*7304104dSAndroid Build Coastguard Worker
25*7304104dSAndroid Build Coastguard Workertype curl 2>/dev/null || (echo "need curl"; exit 77)
26*7304104dSAndroid Build Coastguard Workertype rpm2cpio 2>/dev/null || (echo "need rpm2cpio"; exit 77)
27*7304104dSAndroid Build Coastguard Workertype cpio 2>/dev/null || (echo "need cpio"; exit 77)
28*7304104dSAndroid Build Coastguard Workertype bzcat 2>/dev/null || (echo "need bzcat"; exit 77)
29*7304104dSAndroid Build Coastguard Workerbsdtar --version | grep -q zstd && zstd=true || zstd=false
30*7304104dSAndroid Build Coastguard Workerecho "zstd=$zstd bsdtar=`bsdtar --version`"
31*7304104dSAndroid Build Coastguard Worker
32*7304104dSAndroid Build Coastguard Workercleanup()
33*7304104dSAndroid Build Coastguard Worker{
34*7304104dSAndroid Build Coastguard Worker  # No more cleanups after this cleanup
35*7304104dSAndroid Build Coastguard Worker  trap - 0
36*7304104dSAndroid Build Coastguard Worker
37*7304104dSAndroid Build Coastguard Worker  if [ $PID1 -ne 0 ]; then kill $PID1 || : ; wait $PID1 || :; fi
38*7304104dSAndroid Build Coastguard Worker  if [ $PID2 -ne 0 ]; then kill $PID2 || : ; wait $PID2 || :; fi
39*7304104dSAndroid Build Coastguard Worker  rm -rf F R D L Z ${PWD}/foobar ${PWD}/mocktree ${PWD}/.client_cache* ${PWD}/tmp*
40*7304104dSAndroid Build Coastguard Worker  exit_cleanup
41*7304104dSAndroid Build Coastguard Worker}
42*7304104dSAndroid Build Coastguard Worker
43*7304104dSAndroid Build Coastguard Worker# clean up trash if we exit
44*7304104dSAndroid Build Coastguard Workertrap cleanup 0
45*7304104dSAndroid Build Coastguard Worker
46*7304104dSAndroid Build Coastguard Workererrfiles_list=
47*7304104dSAndroid Build Coastguard Workererr() {
48*7304104dSAndroid Build Coastguard Worker    # Don't trap any new errors from now on
49*7304104dSAndroid Build Coastguard Worker    trap - ERR
50*7304104dSAndroid Build Coastguard Worker
51*7304104dSAndroid Build Coastguard Worker    echo ERROR REPORTS
52*7304104dSAndroid Build Coastguard Worker    for port in $PORT1 $PORT2
53*7304104dSAndroid Build Coastguard Worker    do
54*7304104dSAndroid Build Coastguard Worker        echo ERROR REPORT $port metrics
55*7304104dSAndroid Build Coastguard Worker        curl -s http://127.0.0.1:$port/metrics || :
56*7304104dSAndroid Build Coastguard Worker        echo
57*7304104dSAndroid Build Coastguard Worker    done
58*7304104dSAndroid Build Coastguard Worker    for x in $errfiles_list
59*7304104dSAndroid Build Coastguard Worker    do
60*7304104dSAndroid Build Coastguard Worker        echo ERROR REPORT "$x"
61*7304104dSAndroid Build Coastguard Worker        cat $x
62*7304104dSAndroid Build Coastguard Worker        echo
63*7304104dSAndroid Build Coastguard Worker    done
64*7304104dSAndroid Build Coastguard Worker    cleanup
65*7304104dSAndroid Build Coastguard Worker    false # trigger set -e
66*7304104dSAndroid Build Coastguard Worker}
67*7304104dSAndroid Build Coastguard Workertrap err ERR
68*7304104dSAndroid Build Coastguard Worker
69*7304104dSAndroid Build Coastguard Workererrfiles() {
70*7304104dSAndroid Build Coastguard Worker    errfiles_list="$errfiles_list $*"
71*7304104dSAndroid Build Coastguard Worker}
72*7304104dSAndroid Build Coastguard Worker
73*7304104dSAndroid Build Coastguard Worker# We want to run debuginfod in the background.  We also want to start
74*7304104dSAndroid Build Coastguard Worker# it with the same check/installcheck-sensitive LD_LIBRARY_PATH stuff
75*7304104dSAndroid Build Coastguard Worker# that the testrun alias sets.  But: we if we just use
76*7304104dSAndroid Build Coastguard Worker#    testrun .../debuginfod
77*7304104dSAndroid Build Coastguard Worker# it runs in a subshell, with different pid, so not helpful.
78*7304104dSAndroid Build Coastguard Worker#
79*7304104dSAndroid Build Coastguard Worker# So we gather the LD_LIBRARY_PATH with this cunning trick:
80*7304104dSAndroid Build Coastguard Workerldpath=`testrun sh -c 'echo $LD_LIBRARY_PATH'`
81*7304104dSAndroid Build Coastguard Worker
82*7304104dSAndroid Build Coastguard Workerwait_ready4()
83*7304104dSAndroid Build Coastguard Worker{
84*7304104dSAndroid Build Coastguard Worker  port=$1;
85*7304104dSAndroid Build Coastguard Worker  what=$2;
86*7304104dSAndroid Build Coastguard Worker  value=$3;
87*7304104dSAndroid Build Coastguard Worker  timeout=$4;
88*7304104dSAndroid Build Coastguard Worker
89*7304104dSAndroid Build Coastguard Worker  echo "Wait $timeout seconds on $port for metric $what to change to $value"
90*7304104dSAndroid Build Coastguard Worker  while [ $timeout -gt 0 ]; do
91*7304104dSAndroid Build Coastguard Worker    mvalue="$(curl -s http://127.0.0.1:$port/metrics \
92*7304104dSAndroid Build Coastguard Worker              | grep "$what" | awk '{print $NF}')"
93*7304104dSAndroid Build Coastguard Worker    if [ -z "$mvalue" ]; then mvalue=0; fi
94*7304104dSAndroid Build Coastguard Worker      echo "metric $what: $mvalue"
95*7304104dSAndroid Build Coastguard Worker      if [ "$mvalue" -eq "$value" ]; then
96*7304104dSAndroid Build Coastguard Worker        break;
97*7304104dSAndroid Build Coastguard Worker    fi
98*7304104dSAndroid Build Coastguard Worker    sleep 0.5;
99*7304104dSAndroid Build Coastguard Worker    ((timeout--));
100*7304104dSAndroid Build Coastguard Worker  done;
101*7304104dSAndroid Build Coastguard Worker
102*7304104dSAndroid Build Coastguard Worker  if [ $timeout -eq 0 ]; then
103*7304104dSAndroid Build Coastguard Worker    echo "metric $what never changed to $value on port $port"
104*7304104dSAndroid Build Coastguard Worker    err
105*7304104dSAndroid Build Coastguard Worker  fi
106*7304104dSAndroid Build Coastguard Worker}
107*7304104dSAndroid Build Coastguard Worker
108*7304104dSAndroid Build Coastguard Workerwait_ready()
109*7304104dSAndroid Build Coastguard Worker{
110*7304104dSAndroid Build Coastguard Worker  port=$1;
111*7304104dSAndroid Build Coastguard Worker  what=$2;
112*7304104dSAndroid Build Coastguard Worker  value=$3;
113*7304104dSAndroid Build Coastguard Worker  timeout=20;
114*7304104dSAndroid Build Coastguard Worker  wait_ready4 "$port" "$what" "$value" "$timeout"
115*7304104dSAndroid Build Coastguard Worker}
116*7304104dSAndroid Build Coastguard Worker
117*7304104dSAndroid Build Coastguard Worker
118*7304104dSAndroid Build Coastguard Workerarchive_test() {
119*7304104dSAndroid Build Coastguard Worker    __BUILDID=$1
120*7304104dSAndroid Build Coastguard Worker    __SOURCEPATH=$2
121*7304104dSAndroid Build Coastguard Worker    __SOURCESHA1=$3
122*7304104dSAndroid Build Coastguard Worker
123*7304104dSAndroid Build Coastguard Worker    filename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find executable $__BUILDID`
124*7304104dSAndroid Build Coastguard Worker    buildid=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
125*7304104dSAndroid Build Coastguard Worker             -a $filename | grep 'Build ID' | cut -d ' ' -f 7`
126*7304104dSAndroid Build Coastguard Worker    test $__BUILDID = $buildid
127*7304104dSAndroid Build Coastguard Worker    # check that timestamps are plausible - older than the near-present (tmpdir mtime)
128*7304104dSAndroid Build Coastguard Worker    test $filename -ot `pwd`
129*7304104dSAndroid Build Coastguard Worker
130*7304104dSAndroid Build Coastguard Worker    # run again to assure that fdcache is being enjoyed
131*7304104dSAndroid Build Coastguard Worker    filename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find executable $__BUILDID`
132*7304104dSAndroid Build Coastguard Worker    buildid=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
133*7304104dSAndroid Build Coastguard Worker             -a $filename | grep 'Build ID' | cut -d ' ' -f 7`
134*7304104dSAndroid Build Coastguard Worker    test $__BUILDID = $buildid
135*7304104dSAndroid Build Coastguard Worker    test $filename -ot `pwd`
136*7304104dSAndroid Build Coastguard Worker
137*7304104dSAndroid Build Coastguard Worker    filename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $__BUILDID`
138*7304104dSAndroid Build Coastguard Worker    buildid=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
139*7304104dSAndroid Build Coastguard Worker             -a $filename | grep 'Build ID' | cut -d ' ' -f 7`
140*7304104dSAndroid Build Coastguard Worker    test $__BUILDID = $buildid
141*7304104dSAndroid Build Coastguard Worker    test $filename -ot `pwd`
142*7304104dSAndroid Build Coastguard Worker
143*7304104dSAndroid Build Coastguard Worker    if test "x$__SOURCEPATH" != "x"; then
144*7304104dSAndroid Build Coastguard Worker        filename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find source $__BUILDID $__SOURCEPATH`
145*7304104dSAndroid Build Coastguard Worker        hash=`cat $filename | sha1sum | awk '{print $1}'`
146*7304104dSAndroid Build Coastguard Worker        test $__SOURCESHA1 = $hash
147*7304104dSAndroid Build Coastguard Worker        test $filename -ot `pwd`
148*7304104dSAndroid Build Coastguard Worker    fi
149*7304104dSAndroid Build Coastguard Worker}
150*7304104dSAndroid Build Coastguard Worker
151*7304104dSAndroid Build Coastguard Workerget_ports() {
152*7304104dSAndroid Build Coastguard Worker  while true; do
153*7304104dSAndroid Build Coastguard Worker    PORT1=`expr '(' $RANDOM % 50 ')' + $base`
154*7304104dSAndroid Build Coastguard Worker    ss -atn | grep -F ":$PORT1" || break
155*7304104dSAndroid Build Coastguard Worker  done
156*7304104dSAndroid Build Coastguard Worker# Some tests will use two servers, so assign the second var
157*7304104dSAndroid Build Coastguard Worker  while true; do
158*7304104dSAndroid Build Coastguard Worker    PORT2=`expr '(' $RANDOM % 50 ')' + $base + 50`
159*7304104dSAndroid Build Coastguard Worker    ss -atn | grep -F ":$PORT2" || break
160*7304104dSAndroid Build Coastguard Worker  done
161*7304104dSAndroid Build Coastguard Worker
162*7304104dSAndroid Build Coastguard Worker}
163*7304104dSAndroid Build Coastguard Worker
164*7304104dSAndroid Build Coastguard WorkerVERBOSE=-vvv
165*7304104dSAndroid Build Coastguard Worker# We gather the LD_LIBRARY_PATH with this cunning trick:
166*7304104dSAndroid Build Coastguard Workerldpath=`testrun sh -c 'echo $LD_LIBRARY_PATH'`
167*7304104dSAndroid Build Coastguard WorkerPORT1=0
168*7304104dSAndroid Build Coastguard WorkerPORT2=0
169*7304104dSAndroid Build Coastguard WorkerPID1=0
170*7304104dSAndroid Build Coastguard WorkerPID2=0
171*7304104dSAndroid Build Coastguard Worker
172*7304104dSAndroid Build Coastguard Worker
173*7304104dSAndroid Build Coastguard Worker# run $1 as a sh -c command, invert result code
174*7304104dSAndroid Build Coastguard Workerxfail() {
175*7304104dSAndroid Build Coastguard Worker    if sh -c "$1"; then
176*7304104dSAndroid Build Coastguard Worker        false
177*7304104dSAndroid Build Coastguard Worker    else
178*7304104dSAndroid Build Coastguard Worker        true
179*7304104dSAndroid Build Coastguard Worker    fi
180*7304104dSAndroid Build Coastguard Worker}
181