#!/bin/sh
# PCP QA Test No. 1214
# https://github.com/performancecopilot/pcp/issues/859
#
# Copyright (c) 2020 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_valgrind
_check_series

#
# pmproxy: redis.c:1868: redisProcessCallBacks: Assertion `(c->flags & REDIS_SUBSCRIBED || c->flags & REDIS_MONITORING)'
# Request callback, reply is NOT NULL and we are still connected but not subscribed or monitoring.
_notrun "pmseries querying redis via pmproxy trips assert and pmproxy aborts. query redis directly is OK"

_cleanup()
{
    echo
    cd $here
    echo == Shutdown local redis and pmproxy ...
    [ -n "$proxypid" ] && kill -TERM "$proxypid"
    [ -n "$options" ] && redis-cli $options shutdown
    echo finished
    $sudo rm -rf $tmp $tmp.*
}

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

# real QA test starts here
echo "=== Start test redis-server ..."
redisport=`_find_free_port`
redis-server --port $redisport > $tmp.redis 2>&1 &
echo "PING"
pmsleep 0.125
options="-p $redisport"
redis-cli $options ping
echo
echo "=== Start test pmproxy ..."
proxyport=`_find_free_port`
username=`id -u -n`
cat > $tmp.conf <<EOF
[pmproxy]
pcp.enabled = true
http.enabled = true
redis.enabled = true
[pmseries]
enabled = true
servers = localhost:$redisport
EOF
$PCP_BINADM_DIR/pmproxy -f -p $proxyport -s $tmp.socket \
	-U $username -l $tmp.log -c $tmp.conf &
proxypid=$!
pmsleep 0.5
echo === initial pmproxy log === >> $seq.full
cat $tmp.log >> $seq.full

echo "== load test data for verification" | tee -a $here/$seq.full
pmseries $options --load $here/archives/proc > $here/$seq.full
echo === test data load complete === >> $seq.full

metrics="kernel.all.load disk.dev.read"
for metric in $metrics
do
    echo === Verifying $metric series >> $seq.full

    echo
    echo == pmseries query for $metric to redis directly
    pmseries -p $redisport $metric | tee -a $here/$seq.full > $tmp.out
    redis_nseries=`wc -l < $tmp.out`
    if [ -z "$redis_nseries" -o "$redis_nseries" -le 0 ]
    then
	echo pmseries -p $redisport $metric failed
	status=1
    fi

    echo
    echo == pmseries query for $metric to redis via pmproxy
    pmseries -p $proxyport $metric | tee -a $here/$seq.full > $tmp.out
    proxy_nseries=`wc -l < $tmp.out`
    if [ -z "$proxy_nseries" -o "$proxy_nseries" -le 0 ]
    then
	echo pmseries -p $proxyport $metric failed
	echo proxy_nseries=$proxy_nseries
	status=2
    fi

    if [ "$redis_nseries" -ne "$proxy_nseries" ]
    then
	echo redis=$redis_nseries differs to pmproxy=$proxy_nseries
	status=3
    fi

    echo === pmproxy log === >> $seq.full
    cat $tmp.log >> $seq.full
    if [ $status -ne 0 ]
    then
	[ $status -eq 1 ] && $_valgrind_clean_assert \
		pmseries -p $redisport $metric >> $seq.full 2>&1
	[ $status -ge 2 ] && $_valgrind_clean_assert \
		pmseries -p $proxyport $metric >> $seq.full 2>&1
    fi
done

exit
