UNPKG

3 kBPlain TextView Raw
1#!/usr/bin/env bash
2# How to run:
3#
4# ./bootandkill-servers ./test-acceptance
5#
6# this will boot hexo & uppy-server, run the script that you provided as an argument,
7# and tear down the servers
8
9set -o pipefail
10set -o errexit
11set -o nounset
12set -o xtrace
13
14# Set magic variables for current file & dir
15__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
17__base="$(basename ${__file} .sh)"
18__root="$(cd "$(dirname "${__dir}")" && pwd)"
19
20if [ ! -f "${__root}/env.sh" ]; then
21 cp "${__root}/env.example.sh" "${__root}/env.sh"
22fi
23if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
24 source "${__root}/env.sh"
25fi
26if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
27 echo "[${__base}] Env var UPPYSERVER_DROPBOX_KEY still had the example value '${UPPYSERVER_DROPBOX_KEY:-}'. "
28 echo "[${__base}] Please save the actual secrets in '${__root}/env.sh' and try again"
29 exit 1
30fi
31
32function killProcessListeningOnPort () {
33 local port="${1}"
34 lsof -n -i4TCP:${port} | awk '/LISTEN/ {print $2}' |xargs --no-run-if-empty kill -9 || true
35}
36
37function cleanup_servers () {
38 echo "[${__base}] --> Killing any server listening on port 4000"
39 killProcessListeningOnPort 4000 || true
40
41 echo "[${__base}] --> Killing any server listening on port 3020"
42 killProcessListeningOnPort 3020 || true
43
44 kill -9 ${tailPid}
45}
46
47function waitForPortOpen () {
48 local port="${1}"
49 local limit="${2:-100}"
50 local attempts=0
51 echo "[${__base}] waiting on port ${port} to open... "
52 while ! echo exit | nc localhost ${port}; do
53 let "attempts = attempts + 1"
54 echo "[${__base}] still waiting on port ${port} to open... (${attempts} / ${limit}) "
55 sleep 1
56 if [ "${attempts}" -ge "${limit}" ]; then
57 echo "[${__base}] --> Port did not open for ${limit} seconds. Aborting. "
58 exit 1
59 fi
60 done
61}
62
63echo "[${__base}] --> Killing any server listening on port 4000"
64killProcessListeningOnPort 4000 || true
65echo "[${__base}] --> Killing any server listening on port 3020"
66killProcessListeningOnPort 3020 || true
67
68echo "[${__base}] --> Start webserver and uppy-server in the background"
69rm -f nohup.out || true
70touch nohup.out
71nohup npm run test:serve &
72tail -f nohup.out &
73tailPid=${!}
74
75trap cleanup_servers EXIT
76
77
78
79echo "[${__base}] --> Wait for hexo webserver to be online"
80waitForPortOpen 4000
81
82echo "[${__base}] --> Wait for uppy-server to be online"
83waitForPortOpen 3020
84
85echo "[${__base}] --> Running acceptance tests"
86
87
88buildWait=20
89if [ "${TRAVIS:-}" = "true" ]; then
90 buildWait=60
91 echo "Increasing build wait because Travis has no Hexo build cache"
92fi
93
94echo "[${__base}] --> Sleeping ${buildWait}s, because the port may be open, but Hexo may still be injecting/building stuff"
95echo "[${__base}] --> and I don't know yet how to check for that"
96sleep ${buildWait}
97
98# @todo: Instead of this `sleep` we can wait until the required files are in public : )
99
100${@}