#!/bin/sh
# PCP QA Test No. 1820
# Exercise memory allocation and time window bugs in pcp-atopsar(1).
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
if [ $# -eq 0 ]
then
    echo "QA output created by $seq"
else
    echo "QA output created by $seq $*"
fi

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

[ -f $PCP_BINADM_DIR/pcp-atopsar ] || _notrun "system monitoring tools not installed"

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

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

_filter()
{
    sed \
	-e 's;^ *[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]$;DATESTAMP;g' \
	-e '/initial metric value(s)/d' \
    #end
}

# real QA test starts here
export PCP_HOSTZONE=1
export PCP_ARCHIVE=$here/archives/pcp-atop-log

echo && echo "=== Check memory allocation issue:"
$PCP_BINADM_DIR/pcp-atopsar -x -R 5 2 10 | _filter

echo && echo "=== Check time window issue:"
$PCP_BINADM_DIR/pcp-atopsar -x -b 16:14:29 -e 16:14:33 1 | _filter

echo && echo "=== Argument handling issue:"
export PCP_ARCHIVE_DIR=$tmp.archives
mkdir -p "$PCP_ARCHIVE_DIR/$hostname"
cp $PCP_ARCHIVE.* "$PCP_ARCHIVE_DIR/$hostname"
unset PCP_HOSTZONE PCP_ARCHIVE
pcp -z atopsar >$tmp.noargs 2>&1
pcp -z atopsar -H >$tmp.-H-arg 2>&1
diff $tmp.noargs $tmp.-H-arg >/dev/null
if [ $? -eq 0 ]
then
    echo Passed - same output detected
    cat $tmp.noargs | _filter
else
    echo Bad, expected matching output
    echo === noargs:
    cat $tmp.noargs
    echo === -H-arg:
    cat $tmp.-H-arg
fi

# success, all done
status=0
exit
