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