| 1 |
#!/bin/bash -ex |
| 2 |
# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: |
| 3 |
# |
| 4 |
# Copyright (c) 2019 Red Hat, Inc. |
| 5 |
# Author: Sergio Correia <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
|
| 21 |
error() { |
| 22 |
echo "$@" >&2 |
| 23 |
exit 1 |
| 24 |
} |
| 25 |
|
| 26 |
skip_test() { |
| 27 |
local message="$*" |
| 28 |
[ -n "$message" ] && echo "$message" >&2 |
| 29 |
exit 77 |
| 30 |
} |
| 31 |
|
| 32 |
# Find listening port of a process |
| 33 |
process_find_port() { |
| 34 |
local pid="${1}" |
| 35 |
|
| 36 |
[ -z "${pid}" ] && \ |
| 37 |
error "process_find_port: please specify 'pid'" |
| 38 |
|
| 39 |
local port |
| 40 |
port=$(lsof -Pan -p "${pid}" -iTCP -sTCP:LISTEN -Fn | grep '^n.*:' | head -n1 | cut -d: -f2) |
| 41 |
[ -n "${port}" ] && echo "${port}" |
| 42 |
} |
| 43 |
|
| 44 |
# Wait for the server to be operational. |
| 45 |
process_wait_until_port_ready() { |
| 46 |
local pid="${1}" |
| 47 |
local name="${2}" |
| 48 |
|
| 49 |
[ -z "${pid}" ] && \ |
| 50 |
error "process_wait_until_port_ready: please specify 'pid'" |
| 51 |
|
| 52 |
local max_timeout_in_s=5 |
| 53 |
local start elapsed |
| 54 |
start="${SECONDS}" |
| 55 |
while ! process_find_port "${pid}" >/dev/null; do |
| 56 |
elapsed=$((SECONDS - start)) |
| 57 |
if ! ps -A -o pid | awk -v pid="${pid}" '$1==pid {found=1} END {exit !found}'; then |
| 58 |
error "Failed waiting, ${name:-process} terminated" |
| 59 |
elif [ "${elapsed}" -gt "${max_timeout_in_s}" ]; then |
| 60 |
error "Timeout (${max_timeout_in_s}s) waiting for ${name:-process}" |
| 61 |
fi |
| 62 |
sleep 0.1 |
| 63 |
echo -n . >&2 |
| 64 |
done |
| 65 |
} |