test_service.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/sh
  2. if [ -z "$1" ]; then
  3. echo 'service name is not specified'
  4. exit -1
  5. fi
  6. SERVICE="$1"
  7. SERVICE_CMD="sudo /sbin/service $SERVICE"
  8. run_test() {
  9. local msg="$1"
  10. local cmd="$2"
  11. local expected_retval="${3:-0}"
  12. local n=${#msg}
  13. echo
  14. echo `printf "%$((${n}+4))s" | tr " " "#"`
  15. echo "# $msg #"
  16. echo `printf "%$((${n}+4))s" | tr " " "#"`
  17. $cmd
  18. local retval=$?
  19. if [[ "$retval" == "$expected_retval" ]]; then
  20. echo "[PASSED]"
  21. else
  22. echo "[FAILED]"
  23. echo "Exit status: $retval, but expected: $expected_retval"
  24. exit $retval
  25. fi
  26. }
  27. run_test "stop should succeed" "$SERVICE_CMD stop" 0
  28. run_test "status on a stopped service should return 1" "$SERVICE_CMD status" 1
  29. run_test "stopping a stopped celery should not fail" "$SERVICE_CMD stop" 0
  30. run_test "start should succeed" "$SERVICE_CMD start" 0
  31. run_test "status on a running service should return 0" "$SERVICE_CMD status" 0
  32. run_test "starting a running service should fail" "$SERVICE_CMD start" 1
  33. run_test "restarting a running service should succeed" "$SERVICE_CMD restart" 0
  34. run_test "status on a restarted service should return 0" "$SERVICE_CMD status" 0
  35. run_test "stop should succeed" "$SERVICE_CMD stop" 0
  36. echo "All tests passed!"