# 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-$$"
service1_pid="t-service1-pid-$$"
service2_pid="t-service2-pid-$$"
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 $service1_pid && kill \`cat $service1_pid\` || true ;
  test -f $service2_pid && kill \`cat $service2_pid\` || true ;
  test -f ${service3_pid}-v1 && kill \`cat ${service3_pid}-v1\` || true ;
  test -f ${service3_pid}-v2 && kill \`cat ${service3_pid}-v2\` || true ;
  rm -f $service1_pid $service2_pid ${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
}

function assert_killed_service_is_respawned
{
    old_pid="`cat "$1"`"
    rm "$1"
    kill $old_pid

    wait_for_file "$1"
    test -f "$1"
    new_pid="`cat "$1"`"

    test "$old_pid" -ne "$new_pid"
    kill -0 "$new_pid"
}

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

(register-services
 (list
  (service
    '(test1)
    #:start (make-forkexec-constructor
	     '("$SHELL" "-c"
	       "echo \$\$ > $PWD/$service1_pid ; while true ; do sleep 1 ; done"))
    #:stop  (make-kill-destructor)
    #:respawn? #t)
  (service
    '(test2)
    #:start (make-forkexec-constructor
	     ;; The 'sleep' below is just to make it more likely
	     ;; that synchronization issues in handling #:pid-file
	     ;; would be caught.
	     '("$SHELL" "-c"
	       "sleep 0.7 ; echo \$\$ > $PWD/$service2_pid ; while true ; do sleep 1 ; done")
	     #:pid-file "$PWD/$service2_pid")
    #:stop  (make-kill-destructor)
    #:respawn? #t)
  (service
    '(test3)
    #:start (make-forkexec-constructor
             ;; This one will exit normally after a short delay.
	     '("$SHELL" "-c"
	       "echo \$\$ > $PWD/${service3_pid}-v1 ; sleep 1")
	     #:pid-file "$PWD/${service3_pid}-v1")
    #:stop  (make-kill-destructor)
    #:respawn? #t)))

;;(start-service (lookup-service 'test1))
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 this shouldn't be needed

assert_not_respawned "${service3_pid}-v1"

$herd enable test3 # TODO this shouldn't be needed
$herd start test3

cat > "$conf"<<EOF
(register-services
 (list
  (service
    '(test3)
    #:start (make-forkexec-constructor
             ;; This one will 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"
$herd stop test3
$herd disable test3

assert_not_respawned "${service3_pid}-v2"
# TODO this fails
assert_not_respawned "${service3_pid}-v1"

$herd stop root
