# GNU Shepherd --- Test respawnable services.
# Copyright © 2013, 2014, 2016, 2023 Ludovic Courtès <ludo@gnu.org>
#
# This file is part of the GNU Shepherd.
#
# The GNU Shepherd is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# The GNU Shepherd is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the GNU Shepherd.  If not, see <http://www.gnu.org/licenses/>.

shepherd --version
herd --version

socket="t-socket-$$"
conf="t-conf-$$"
log="t-log-$$"
stamp="t-stamp-$$"
service3_pid="t-service3-pid-$$"
pid="t-pid-$$"

herd="herd -s $socket"

trap "cat $log || true ;
  rm -f $socket $conf $stamp $log ;
  test -f $pid && kill \`cat $pid\` || true ; rm -f $pid ;
  test -f ${service3_pid}-v1 && kill \`cat ${service3_pid}-v1\` || true ;
  test -f ${service3_pid}-v2 && kill \`cat ${service3_pid}-v2\` || true ;
  rm -f ${service3_pid}-v1 ${service3_pid}-v2" EXIT

function wait_for_file
{
    i=0
    while ! test -f "$1" && test $i -lt 20
    do
	sleep 0.3
	i=`expr $i + 1`
    done
    test -f "$1"
}

function assert_not_respawned
{
    count=10
    i=0
    while test $i -lt $count;
    do
        pid="`cat "$1"`" || true
        if test -n "$pid" && kill $pid; then
            break
        else
            true
        fi
        sleep 0.3
        i=`expr $i + 1`
    done

    test $i -ge $count
}

cat > "$conf"<<EOF
;; Disable respawn throttling.
(default-respawn-limit '(+inf.0 . 1))

(register-services
 (list
  (service
    '(test3)
    #:start (make-forkexec-constructor
             ;; Exit normally after a short delay.
	     '("$SHELL" "-c"
	       "echo spawninggg...; echo \$\$ > $PWD/${service3_pid}-v1 ; sleep 1")
	     #:pid-file "$PWD/${service3_pid}-v1")
    #:stop  (make-kill-destructor)
    #:respawn? #t)))
EOF

rm -f "$pid"
shepherd -I -s "$socket" -c "$conf" -l "$log" --pid="$pid" &

# Wait till it's ready.
wait_for_file "$pid"

shepherd_pid="`cat $pid`"

kill -0 $shepherd_pid
test -S "$socket"

$herd start test3
test -f "${service3_pid}-v1"

$herd stop test3
$herd disable test3 # TODO I think this shouldn't be needed, but it is.

assert_not_respawned "${service3_pid}-v1"

$herd enable test3 # TODO delme, see above
$herd start test3

# Prepare a replacement for test3, which is essentially the same, just a new
# version.
cat > "$conf"<<EOF
(register-services
 (list
  (service
    '(test3)
    #:start (make-forkexec-constructor
             ;; Exit normally after a short delay.
	     '("$SHELL" "-c"
	       "echo \$\$ > $PWD/${service3_pid}-v2 ; sleep 1.1")
	     #:pid-file "$PWD/${service3_pid}-v2")
    #:stop  (make-kill-destructor)
    #:respawn? #t)))
EOF

# Cause 'test3' to have a replacement.
$herd load root "$conf"
# Why though? We even left test3 running before loading the new configuration.
$herd status test3 | grep 'It is stopped'
$herd start test3
$herd status test3 | grep 'It is running'
$herd stop test3

# v2 is stopped, and it is not respawned
assert_not_respawned "${service3_pid}-v2"

# TODO this next one fails, with or without the $herd disable test3.
# It means that the already replaced service remains in a respawn loop.
$herd disable test3
assert_not_respawned "${service3_pid}-v1"

$herd stop root
