lint: Apply shellcheck suggestions for bash scripts in test/*

This commit is contained in:
Wolfgang Walther
2021-04-18 13:51:39 +02:00
committed by Wolfgang Walther
parent eee5d5d482
commit 280b89d87c
2 changed files with 25 additions and 25 deletions
+8 -8
View File
@@ -2,7 +2,7 @@
usage() { usage() {
echo "$0 <db-uri> <test-database>" echo "$0 <db-uri> <test-database>"
exit -1 exit 1
} }
if [ -z "$1" ]; then if [ -z "$1" ]; then
@@ -20,19 +20,19 @@ if [[ $1 != postgres://* ]]; then
usage usage
fi fi
BASEPATH=$( cd $(dirname $0) ; pwd -P ) BASEPATH=$( cd "$(dirname "$0")" && pwd -P )
URI="$1" URI="$1"
#Extract host and port--we need this to form the new connection string #Extract host and port--we need this to form the new connection string
HOST_PORT=$(echo $URI | cut -d'/' -f3 | cut -d'@' -f2 ) HOST_PORT=$(echo "$URI" | cut -d'/' -f3 | cut -d'@' -f2 )
DB=$2 DB=$2
# Specify the username of choice, or let the script create a random unique user by appending the database name # Specify the username of choice, or let the script create a random unique user by appending the database name
TEST_USER_NAME=postgrest_test_authenticator TEST_USER_NAME=postgrest_test_authenticator
# New password will get assigned only if the user does not already exist # New password will get assigned only if the user does not already exist
# Otherwise make sure to provide the correct password for the existing user # Otherwise make sure to provide the correct password for the existing user
TEST_USER_PASS=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1) TEST_USER_PASS=$(< /dev/urandom env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" -Xq >/dev/null -c 'select rolcreatedb from pg_authid where rolname = current_user;' 2>/dev/null if ! PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" -Xq >/dev/null -c 'select rolcreatedb from pg_authid where rolname = current_user;' 2>/dev/null;
if [ $? -ne 0 ]; then then
echo "ERROR: Please specify the user with 'Create DB' permissions, and ensure that the default database for the username exists." echo "ERROR: Please specify the user with 'Create DB' permissions, and ensure that the default database for the username exists."
exit 1 exit 1
fi fi
@@ -60,9 +60,9 @@ ALTER DATABASE $DB SET LC_TIME = 'POSIX';
EOF EOF
#Remove database path from the connection uri--prevents setting up the new database name with PGDATABASE #Remove database path from the connection uri--prevents setting up the new database name with PGDATABASE
URI=$(echo $URI | cut -d'/' -f1-3) URI=$(echo "$URI" | cut -d'/' -f1-3)
PGDATABASE=$DB PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" --set=db=$DB -Xq <<EOF PGDATABASE=$DB PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" --set=db="$DB" -Xq <<EOF
CREATE EXTENSION IF NOT EXISTS pgcrypto; CREATE EXTENSION IF NOT EXISTS pgcrypto;
ALTER DATABASE ${DB} SET request.jwt.claim.id = '-1'; ALTER DATABASE ${DB} SET request.jwt.claim.id = '-1';
EOF EOF
+17 -17
View File
@@ -18,15 +18,15 @@ trap "kill 0" int term exit
currentTest=1 currentTest=1
failedTests=0 failedTests=0
result(){ echo "$1 $currentTest $2"; currentTest=$(( $currentTest + 1 )); } result(){ echo "$1 $currentTest $2"; currentTest=$(( currentTest + 1 )); }
ok(){ result 'ok' "- $1"; } ok(){ result 'ok' "- $1"; }
ko(){ result 'not ok' "- $1"; failedTests=$(( $failedTests + 1 )); } ko(){ result 'not ok' "- $1"; failedTests=$(( failedTests + 1 )); }
pgrStart(){ postgrest +RTS -p -h > /dev/null & pgrPID="$!"; } pgrStart(){ postgrest +RTS -p -h > /dev/null & pgrPID="$!"; }
pgrStop(){ kill "$pgrPID" 2>/dev/null; } pgrStop(){ kill "$pgrPID" 2>/dev/null; }
checkPgrStarted(){ checkPgrStarted(){
while pgrStarted && test $(rootStatus) -ne 200 while pgrStarted && test "$(rootStatus)" -ne 200
do do
sleep 1 sleep 1
done done
@@ -39,9 +39,9 @@ rootStatus(){
jsonKeyTest(){ jsonKeyTest(){
pgrStart pgrStart
checkPgrStarted checkPgrStarted
factor=$(( 3*$(numfmt --from=si $1)/4 )) # 3/4 on $1 is need to maintain the specified size because of base64 factor=$(( 3*$(numfmt --from=si "$1")/4 )) # 3/4 on $1 is need to maintain the specified size because of base64
payload="{\"blob\" : \"$(dd if=/dev/zero bs=$factor count=1 status=none | base64)\"}" payload="{\"blob\" : \"$(dd if=/dev/zero bs=$factor count=1 status=none | base64)\"}"
httpStatus=$(echo $payload | curl -s -H "Content-Type: application/json" --request $2 -d @- -w '%{http_code}' http://localhost:$pgrPort$3 | tr -d '"') httpStatus=$(echo "$payload" | curl -s -H "Content-Type: application/json" --request "$2" -d @- -w '%{http_code}' http://localhost:"$pgrPort""$3" | tr -d '"')
if test "$httpStatus" -ge 200 && test "$httpStatus" -lt 210 if test "$httpStatus" -ge 200 && test "$httpStatus" -lt 210
then then
pgrStop pgrStop
@@ -49,10 +49,10 @@ jsonKeyTest(){
do do
sleep 1 sleep 1
done done
BYTES_FMT=$(cat postgrest.prof | grep -o -P '(?<=alloc =).*(?=bytes)' | tr -d ' ') BYTES_FMT=$(< postgrest.prof grep -o -P '(?<=alloc =).*(?=bytes)' | tr -d ' ')
BYTES=$(echo $BYTES_FMT | tr -d ',') BYTES=$(echo "$BYTES_FMT" | tr -d ',')
MAX_BYTES=$(numfmt --from=si $4) MAX_BYTES=$(numfmt --from=si "$4")
if test $BYTES -le $MAX_BYTES if test "$BYTES" -le "$MAX_BYTES"
then then
ok "$2 $3: with a json key of $1 the memory usage($BYTES_FMT bytes) is less than $4" ok "$2 $3: with a json key of $1 the memory usage($BYTES_FMT bytes) is less than $4"
else else
@@ -69,14 +69,14 @@ postJsonArrayTest(){
checkPgrStarted checkPgrStarted
arr=() arr=()
arr+=('[') arr+=('[')
for i in $(seq 1 $(expr $1 - 1)) for i in $(seq 1 $(("$1" - 1)))
do do
arr+=("{\"id\": $i, \"body\": \"xxxxxxx\"},") arr+=("{\"id\": $i, \"body\": \"xxxxxxx\"},")
done done
arr+=("{\"id\": $1, \"body\": \"xxxxxxx\"}") arr+=("{\"id\": $1, \"body\": \"xxxxxxx\"}")
arr+=(']') arr+=(']')
payload=$(echo ${arr[*]}) payload="${arr[*]}"
httpStatus=$(echo $payload | curl -s -H "Content-Type: application/json" -d @- -w '%{http_code}' http://localhost:$pgrPort$2 | tr -d '"') httpStatus=$(echo "$payload" | curl -s -H "Content-Type: application/json" -d @- -w '%{http_code}' http://localhost:"$pgrPort""$2" | tr -d '"')
if test "$httpStatus" -ge 200 && test "$httpStatus" -lt 210 if test "$httpStatus" -ge 200 && test "$httpStatus" -lt 210
then then
pgrStop pgrStop
@@ -84,11 +84,11 @@ postJsonArrayTest(){
do do
sleep 1 sleep 1
done done
BYTES_FMT=$(cat postgrest.prof | grep -o -P '(?<=alloc =).*(?=bytes)' | tr -d ' ') BYTES_FMT=$(< postgrest.prof grep -o -P '(?<=alloc =).*(?=bytes)' | tr -d ' ')
BYTES=$(echo $BYTES_FMT | tr -d ',') BYTES=$(echo "$BYTES_FMT" | tr -d ',')
MAX_BYTES=$(numfmt --from=si $3) MAX_BYTES=$(numfmt --from=si "$3")
PAYLOAD_SIZE=$(echo $payload | wc -c | numfmt --to=si) PAYLOAD_SIZE=$(echo "$payload" | wc -c | numfmt --to=si)
if test $BYTES -le $MAX_BYTES if test "$BYTES" -le "$MAX_BYTES"
then then
ok "POST $2: with a json payload of $PAYLOAD_SIZE that has $1 array values the memory usage($BYTES_FMT bytes) is less than $3" ok "POST $2: with a json payload of $PAYLOAD_SIZE that has $1 array values the memory usage($BYTES_FMT bytes) is less than $3"
else else