#!/bin/sh
# PCP QA Test No. 1090
# Reproduce https://github.com/performancecopilot/pcp/issues/14
# originally, but since then exercises context re-connection in
# the PMWEBAPI.
#
# Copyright (c) 2015 Ken McDonell.  All Rights Reserved.
# Copyright (c) 2019 Red Hat.
#

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

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

_check_series
which curl >/dev/null 2>&1 || _notrun "No curl binary installed"

# only restart pmproxy if it was running before the QA test starts
restart_pmproxy=false
[ -n "`_get_pids_by_name pmproxy`" ] && restart_pmproxy=true

_cleanup()
{
    cd $here
    _service pmproxy stop >/dev/null 2>&1
    _restore_auto_restart pmproxy
    $restart_pmproxy && _service pmproxy restart >/dev/null 2>&1
    rm -rf $tmp.*
}

status=1	# failure is the default!
port=44322
trap "_cleanup; exit \$status" 0 1 2 3 15

_dump_curl()
{
    echo "$1 curl ..." >>$seq_full
    echo "<stdout>" >>$seq_full
    ( cat $tmp.out; echo ) >>$seq_full
    echo "<stderr>" >>$seq_full
    ( cat $tmp.err; echo ) >>$seq_full
}

_get_context()
{
    # get a pmapi context reference from pmwebapi
    curl -s "http://localhost:$port/pmapi/context?hostspec=localhost&polltimeout=50000" >$tmp.out 2>$tmp.err
    _dump_curl context
    # { [...,] "context": 2062287553 [,...] }
    pmjson -n < $tmp.out \
	    | $PCP_AWK_PROG '$1 == "\"context\":" { print $2 }' \
	    | sed -e 's/,//g' \
    #end
}

_get_metrics()
{
    # fetch metrics from context $1
    #
    curl -s http://localhost:$port/pmapi/"$1"/_metric?prefix=sample.long >$tmp.out 2>$tmp.err
    _dump_curl metrics
    # metrics are in lines like  ...
    # { [...,] "name": "sample.long.one" [,...] }
    pmjson < $tmp.out \
	| grep '"name"' \
	| sed -e 's/^ *//g' \
	| sed -e 's/,$//g' \
	> $tmp.metrics
    if [ -s $tmp.metrics ]
    then
	echo "Got these metrics ..."
	cat $tmp.metrics
    else
	echo "Failed to get metrics ..."
	pmjson < $tmp.out \
	    | sed -e '/"context":/d' \
	    > $tmp.nometrics
	cat $tmp.nometrics
    fi
    #cat $PCP_LOG_DIR/pmproxy/pmproxy.log
}

_stop_auto_restart pmproxy
if ! _service pmproxy stop >/dev/null 2>&1; then _exit 1; fi

if ! _service pmproxy start; then _exit 1; fi \
| _filter_pmproxy_start
_wait_for_pmproxy $port || _exit 1

# real QA test starts here
ctx1=`_get_context`
echo "ctx1=$ctx1" >>$seq_full
if [ -z "$ctx1" ]
then
    echo "Arrg, failed to get context number from ..."
    cat $tmp.out; echo
    exit
fi
echo "Got first context number."
_get_metrics $ctx1

# kill off pmcd
echo "Killing off pmcd ..."
if ! _service pmcd stop 2>&1; then _exit 1; fi | _filter_pcp_stop

echo "Retrying first context ..."
_get_metrics $ctx1

ctx2=`_get_context`
echo "ctx2=$ctx2" >>$seq_full
if [ -n "$ctx2" ]
then
    echo "Warning: got second context number from ..."
    cat $tmp.out; echo
    # ok, even though this is wrong (although it used to be that way),
    # let's try and get some metrics ...
    #
    _get_metrics $ctx2
else
    echo "No context number, as expected."
fi

# start pmcd
echo "Restarting pmcd ..."
if ! _service pmcd start 2>&1; then _exit 1; fi | _filter_pcp_start
_wait_for_pmcd || _exit 1

ctx3=`_get_context`
echo "ctx3=$ctx3" >>$seq_full
if [ -z "$ctx3" ]
then
    echo "Arrg, failed to get context number from ..."
    cat $tmp.out; echo
    exit
fi
echo "Got third context number."
_get_metrics $ctx3

echo "Retrying first context ..."
_get_metrics $ctx1

# success, all done
status=0
exit
