#!/bin/sh
# PCP QA Test No. 1466
# test migrate_pid_service rc-proc.sh function
#
# Copyright (c) 2021 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

[ -n "$PCP_SYSTEMDUNIT_DIR" ] || _notrun "PCP_SYSTEMDUNIT_DIR not set, so no systemd here"
[ -f $PCP_SYSTEMDUNIT_DIR/pmcd.service ] || _notrun "$PCP_SYSTEMDUNIT_DIR/pmcd.service not found"

_cleanup()
{
    cd $here
    $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

# get to a known starting point as far as systemd is concerned
#
_service pmcd restart >>$here/$seq.full 2>&1
_wait_for_pmcd

# real QA test starts here

# add a process to the pmcd service cgroup
pmsleep 5 >/dev/null 2>&1 & pid=$!
$sudo sh -c ". $PCP_SHARE_DIR/lib/rc-proc.sh; migrate_pid_service $pid pmcd.service"
pmsleep 2
if systemctl status pmcd.service 2>&1 | grep -q -s pmsleep
then
    echo PASSED, found pmsleep process in pmcd.service cgroup
else
    echo FAILED, did not find pmsleep process in pmcd.service cgroup | tee -a $seq.full
    $sudo sh -c ". $PCP_SHARE_DIR/lib/rc-proc.sh; migrate_pid_service -v $pid pmcd.service" | tee -a $seq.full 2>&1
    cgroot=`mount | $PCP_AWK_PROG '/^cgroup/ {print $3; exit}'`
    systemctl status pmcd | tee -a $seq.full
    echo cgroot=$cgroot | tee -a $seq.full
    cgslice=`systemctl status pmcd.service | $PCP_AWK_PROG '/CGroup:/ {print $2}'`
    echo cgslice=${cgslice} | tee -a $seq.full
    find ${cgroot} -type d -name pmcd.service -print -exec ls -l {} ";" | tee -a $seq.full
fi

# Avoid systemd StartLimit* limits and make sure the pmsleep has exited
pmsleep 5

# all done
status=0
exit
