#!/bin/sh
# PCP QA Test No. 1566
#
# Test service notifications API, see  __pmServerNotifyServiceManagerReady(3)
#
# 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

[ "$PCPQA_SYSTEMD" = no ] && _notrun "we're not using systemd here"
# [ -n "$PCP_SYSTEMDUNIT_DIR" ] || _notrun "PCP_SYSTEMUNIT_DIR not set, no systemd"

# handle multiple possible messages ...
# ... __pmServerNotifySystemd: NOTIFY_SOCKET not set: not launched by systemd, or service unit is not using Type=notify
# ... __pmServerNotifyServiceManagerReady: no service manager on this platform
# ... __pmServerNotifyServiceManagerStopping: no service manager on this platform
#
#
_filter_log()
{
    tee -a $seq_full \
    | sed \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]$/DATE/' \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/DATE/' \
        -e 's/([0-9]*)/(PID)/' \
	-e 's/on .* started/on HOST started/' \
	-e 's/__pmServerNotifySystemd: NOTIFY_SOCKET not set: not launched by systemd, or service unit is not using Type=notify/__pmServerNotify... EXPECTED/' \
	-e 's/__pmServerNotifyServiceManagerReady: no service manager on this platform/__pmServerNotify... EXPECTED/' \
	-e 's/__pmServerNotifyServiceManagerStopping: no service manager on this platform/__pmServerNotify... EXPECTED/' \
    # end
}

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

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

mkdir -p $tmp

command=$here/src/test_service_notify

echo;echo === test for service manager notices on non-systemd platform
echo 'Note the warning about "NOTIFY_SOCKET not set" is expected due to -Dservices'
$sudo src/test_service_notify -Dservices -f -l $tmp/test_service_notify.log & >/dev/null 2>&1
pid=$!
# need test_service_notify  to get started ...
#
for x in 1 2 3 4 5
do
    sleep 1
    [ -f $tmp/test_service_notify.log ] && break
done
$sudo $PCP_BINADM_DIR/pmsignal -a -s TERM test_service_notify
# need test_service_notify to get signal and shut up shop ...
#
wait
_filter_log < $tmp/test_service_notify.log; rm -f $tmp/test_service_notify.log

# TODO additional systemd tests coming (using a test-systemd.unit)

# success, all done
status=0
exit
