test-acceptance 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env bash
  2. # How to run:
  3. #
  4. # - When using `./bin/test-acceptance handle-servers` this script boots and kills servers too (handy for Travis and quick access)
  5. # - When using `./bin/test-acceptance` this script assumes you have your own servers on localhost running (handy for going in depth)
  6. #
  7. set -o pipefail
  8. set -o errexit
  9. set -o nounset
  10. # set -o xtrace
  11. # Set magic variables for current file & dir
  12. __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  13. __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
  14. __base="$(basename ${__file} .sh)"
  15. __root="$(cd "$(dirname "${__dir}")" && pwd)"
  16. mode="${1:-}"
  17. if [ ! -f "${__root}/env.sh" ]; then
  18. cp "${__root}/env.example.sh" "${__root}/env.sh"
  19. fi
  20. if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
  21. source "${__root}/env.sh"
  22. fi
  23. if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
  24. echo "[${__base}] Env var UPPYSERVER_DROPBOX_KEY still had the example value '${UPPYSERVER_DROPBOX_KEY:-}'. "
  25. echo "[${__base}] Please save the actual secrets in '${__root}/env.sh' and try again"
  26. exit 1
  27. fi
  28. function killProcessListeningOnPort () {
  29. local port="${1}"
  30. lsof -n -i4TCP:${port} | awk '/LISTEN/ {print $2}' |xargs kill -9
  31. }
  32. function waitForPortOpen () {
  33. local port="${1}"
  34. local limit="${2:-60}"
  35. local attempts=0
  36. echo "[${__base}] waiting on port ${port} to open... "
  37. while ! echo exit | nc localhost ${port}; do
  38. let "attempts = attempts + 1"
  39. echo "[${__base}] still waiting on port ${port} to open... (${attempts} / ${limit}) "
  40. sleep 1
  41. if [ "${attempts}" -ge "${limit}" ]; then
  42. echo "[${__base}] --> Port did not open for ${limit} seconds. Aborting. "
  43. exit 1
  44. fi
  45. done
  46. }
  47. function cleanup_servers () {
  48. echo "[${__base}] --> Killing any server listening on port 4000"
  49. killProcessListeningOnPort 4000 || true
  50. echo "[${__base}] --> Killing any server listening on port 8080"
  51. killProcessListeningOnPort 8080 || true
  52. kill -9 ${tailPid}
  53. }
  54. if [ "${mode}" = "handle-servers" ]; then
  55. echo "[${__base}] --> Killing any server listening on port 4000"
  56. killProcessListeningOnPort 4000 || true
  57. echo "[${__base}] --> Killing any server listening on port 8080"
  58. killProcessListeningOnPort 8080 || true
  59. echo "[${__base}] --> Start webserver and uppy-server in the background"
  60. rm -f nohup.out || true
  61. touch nohup.out
  62. nohup npm run start &
  63. tail -f nohup.out &
  64. tailPid=${!}
  65. trap cleanup_servers EXIT
  66. fi
  67. echo "[${__base}] --> Wait for hexo webserver to be online"
  68. waitForPortOpen 4000
  69. echo "[${__base}] --> Wait for uppy-server to be online"
  70. waitForPortOpen 8080
  71. echo "[${__base}] --> Sleeping 20s, because the port may be open, but Hexo may still be injecting/building stuff"
  72. echo "[${__base}] --> and I don't know yet how to check for that"
  73. sleep 20
  74. echo "[${__base}] --> Running acceptance tests"
  75. node test/multipart.spec.js