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