#!/bin/sh
# PCP QA Test No. 1877
# Verify that pmproxy does not connect to Redis if it's not required
#
# Copyright (c) 2021 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_check_redis_server

_cleanup()
{
    cd $here
    [ -n "$pmproxy_pid" ] && $signal -s TERM $pmproxy_pid
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
signal=$PCP_BINADM_DIR/pmsignal

username=`id -u -n`

$sudo rm -rf $tmp $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15


# real QA test starts here
echo
echo "=== empty configuration file, Redis not required"
echo > $tmp.pmproxy.conf
pmproxyport=`_find_free_port`
pmproxy -f -U $username -x $tmp.err -l $tmp.pmproxy.log -p $pmproxyport -s $tmp.pmproxy.socket -c $tmp.pmproxy.conf &
pmproxy_pid=$!
# check pmproxy has started and is available for requests
pmcd_wait -h localhost@localhost:$pmproxyport -v -t 5sec
grep -o "Redis.*" $tmp.pmproxy.log
cat $tmp.pmproxy.log >>$seq.full
$signal -s TERM $pmproxy_pid
pmproxy_pid=""

echo
echo "=== configuration file with pmseries REST API enabled, Redis is required"
cat <<EOF > $tmp.pmproxy.conf
[pmseries]
enabled = true
EOF
pmproxyport=`_find_free_port`
pmproxy -f -U $username -x $tmp.err -l $tmp.pmproxy.log -p $pmproxyport -s $tmp.pmproxy.socket -c $tmp.pmproxy.conf &
pmproxy_pid=$!
# check pmproxy has started and is available for requests
pmcd_wait -h localhost@localhost:$pmproxyport -v -t 5sec
grep -o "Redis.*" $tmp.pmproxy.log
cat $tmp.pmproxy.log >>$seq.full
$signal -s TERM $pmproxy_pid
pmproxy_pid=""

# success, all done
exit
