one-shot.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # GNU Shepherd --- Test one-shot services.
  2. # Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
  3. #
  4. # This file is part of the GNU Shepherd.
  5. #
  6. # The GNU Shepherd is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or (at
  9. # your option) any later version.
  10. #
  11. # The GNU Shepherd is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with the GNU Shepherd. If not, see <http://www.gnu.org/licenses/>.
  18. shepherd --version
  19. herd --version
  20. socket="t-socket-$$"
  21. conf="t-conf-$$"
  22. confdir="t-confdir-$$"
  23. log="t-log-$$"
  24. stamp="t-stamp-$$"
  25. pid="t-pid-$$"
  26. herd="herd -s $socket"
  27. trap "cat $log || true; rm -f $socket $conf $stamp $log;
  28. test -f $pid && kill \`cat $pid\` || true; rm -f $pid" EXIT
  29. cat > "$conf"<<EOF
  30. (use-modules (srfi srfi-26))
  31. (register-services
  32. (make <service>
  33. #:provides '(always-fail)
  34. #:start (const #f)
  35. #:one-shot? #t)
  36. (make <service>
  37. #:provides '(test)
  38. #:start (lambda _
  39. (call-with-output-file "$stamp"
  40. (cut display "foo" <>))
  41. #t)
  42. #:one-shot? #t)
  43. (make <service>
  44. #:provides '(test-2)
  45. #:requires '(test)
  46. #:start (lambda _
  47. (call-with-output-file "$stamp-2"
  48. (cut display "bar" <>))
  49. #t)
  50. #:stop (lambda _
  51. (delete-file "$stamp-2")
  52. #f)))
  53. EOF
  54. rm -f "$pid"
  55. shepherd -I -s "$socket" -c "$conf" -l "$log" --pid="$pid" &
  56. # Wait till it's ready.
  57. while ! test -f "$pid" ; do sleep 0.3 ; done
  58. shepherd_pid="`cat $pid`"
  59. kill -0 $shepherd_pid
  60. test -S "$socket"
  61. # Make sure we notice startup failures of one-shot services.
  62. if $herd start always-fail; then false; else true; fi
  63. for i in 1 2 3
  64. do
  65. rm -f "$stamp"
  66. $herd start test
  67. test -f "$stamp"
  68. $herd status test | grep stopped.*one-shot
  69. grep "test.*started" "$log"
  70. $herd stop test # no-op since it's already stopped
  71. done
  72. $herd status | grep -i '^one-shot'
  73. $herd status
  74. rm -f "$stamp" "$stamp-2"
  75. $herd start test-2
  76. test -f "$stamp"
  77. test -f "$stamp-2"
  78. $herd status test | grep stopped.*one-shot
  79. $herd status test-2 | grep started
  80. $herd stop test-2
  81. if test -f "$stamp-2"; then false; else true; fi