#!/bin/sh
# PCP QA Test No. 1433
# pmproxy rc script stop/start with timeseries option.
#
# 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_redis_server
eval `pmconfig -L ipv6 unix_domain_sockets`
[ "$ipv6" = "true" ] || _notrun "No IPv6 support"
[ "$unix_domain_sockets" = "true" ] || _notrun "No Unix socket support"

_cleanup()
{
    cd $here
    _restore_config $PCP_PMPROXYOPTIONS_PATH
    if $pmproxy_was_running
    then
	echo "Restart pmproxy ..." >>$here/$seq.full
	_service pmproxy restart >>$here/$seq.full 2>&1
	_wait_for_pmproxy
    else
	echo "Stopping pmproxy ..." >>$here/$seq.full
	_service pmproxy stop >>$here/$seq.full 2>&1
	_wait_pmproxy_end
    fi
    $sudo rm -rf $tmp $tmp.*
}

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

pmproxy_was_running=false
[ -f $PCP_RUN_DIR/pmproxy.pid ] && pmproxy_was_running=true
echo "pmproxy_was_running=$pmproxy_was_running" >>$here/$seq.full

# real QA test starts here
_save_config $PCP_PMPROXYOPTIONS_PATH

echo "# Option added by PCP QA test $seq" > $tmp.local
echo --timeseries >> $tmp.local
$sudo cp $tmp.local $PCP_PMPROXYOPTIONS_PATH

_service pmproxy stop >/dev/null 2>&1
_wait_pmproxy_end
# give any running pmproxy a chance to shutdown cleanly
#
sleep 2
_service pmproxy start 2>&1 | _filter_pmproxy_start
_wait_for_pmproxy

_service pmproxy stop 2>&1 | _filter_pmproxy_start
_wait_pmproxy_end
_filter_pmproxy_log < $PCP_LOG_DIR/pmproxy/pmproxy.log

# systemd has a default limits ... systemd.unit(5) describe
# StartLimitIntervalSec and StartLimitBurst but not the default values
# which are hidden in *.conf files below /etc/systemd and appear
# to have defaults like:
#	DefaultStartLimitIntervalSec=10s
#	DefaultStartLimitBurst=5
# ... this test has 2 (re)starts, so we need it to run for 4 seconds
# to avoid hitting the limit
#
sleep 4

# success, all done
status=0
exit
