run.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/usr/bin/env bash
  2. ## This script runs CT (and necessary dependencies) in docker container(s)
  3. set -euo pipefail
  4. # ensure dir
  5. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/../.."
  6. help() {
  7. echo
  8. echo "-h|--help: To display this usage info"
  9. echo "--app lib_dir/app_name: For which app to run start docker-compose, and run common tests"
  10. echo "--console: Start EMQX in console mode but do not run test cases"
  11. echo "--attach: Attach to the Erlang docker container without running any test case"
  12. echo "--stop: Stop running containers for the given app"
  13. echo "--only-up: Only start the testbed but do not run CT"
  14. echo "--keep-up: Keep the testbed running after CT"
  15. echo "--ci: Set this flag in GitHub action to enforce no tests are skipped"
  16. echo "--: If any, all args after '--' are passed to rebar3 ct"
  17. echo " otherwise it runs the entire app's CT"
  18. }
  19. set +e
  20. if docker compose version; then
  21. DC='docker compose'
  22. elif command -v docker-compose; then
  23. DC='docker-compose'
  24. else
  25. echo 'Neither "docker compose" or "docker-compose" are available, stop.'
  26. exit 1
  27. fi
  28. set -e
  29. WHICH_APP='novalue'
  30. CONSOLE='no'
  31. KEEP_UP='no'
  32. ONLY_UP='no'
  33. ATTACH='no'
  34. STOP='no'
  35. IS_CI='no'
  36. ODBC_REQUEST='no'
  37. while [ "$#" -gt 0 ]; do
  38. case $1 in
  39. -h|--help)
  40. help
  41. exit 0
  42. ;;
  43. --app)
  44. WHICH_APP="${2%/}"
  45. shift 2
  46. ;;
  47. --only-up)
  48. ONLY_UP='yes'
  49. shift 1
  50. ;;
  51. --keep-up)
  52. KEEP_UP='yes'
  53. shift 1
  54. ;;
  55. --attach)
  56. ATTACH='yes'
  57. shift 1
  58. ;;
  59. --stop)
  60. STOP='yes'
  61. shift 1
  62. ;;
  63. --console)
  64. CONSOLE='yes'
  65. shift 1
  66. ;;
  67. --ci)
  68. IS_CI='yes'
  69. shift 1
  70. ;;
  71. --)
  72. shift 1
  73. REBAR3CT="$*"
  74. shift $#
  75. ;;
  76. *)
  77. echo "unknown option $1"
  78. exit 1
  79. ;;
  80. esac
  81. done
  82. if [ "${WHICH_APP}" = 'novalue' ]; then
  83. echo "must provide --app arg"
  84. help
  85. exit 1
  86. fi
  87. if [[ "${WHICH_APP}" == lib-ee* && (-z "${PROFILE+x}" || "${PROFILE}" != emqx-enterprise) ]]; then
  88. echo 'You are trying to run an enterprise test case without the emqx-enterprise profile.'
  89. echo 'This will most likely not work.'
  90. echo ''
  91. echo 'Run "export PROFILE=emqx-enterprise" and "make" to fix this'
  92. exit 1
  93. fi
  94. ERLANG_CONTAINER='erlang'
  95. DOCKER_CT_ENVS_FILE="${WHICH_APP}/docker-ct"
  96. case "${WHICH_APP}" in
  97. lib-ee*)
  98. ## ensure enterprise profile when testing lib-ee applications
  99. export PROFILE='emqx-enterprise'
  100. ;;
  101. apps/*)
  102. if [[ -f "${WHICH_APP}/BSL.txt" ]]; then
  103. export PROFILE='emqx-enterprise'
  104. else
  105. export PROFILE='emqx'
  106. fi
  107. ;;
  108. *)
  109. export PROFILE="${PROFILE:-emqx}"
  110. ;;
  111. esac
  112. if [ -f "$DOCKER_CT_ENVS_FILE" ]; then
  113. # shellcheck disable=SC2002
  114. CT_DEPS="$(cat "$DOCKER_CT_ENVS_FILE" | xargs)"
  115. fi
  116. CT_DEPS="${ERLANG_CONTAINER} ${CT_DEPS:-}"
  117. FILES=( )
  118. for dep in ${CT_DEPS}; do
  119. case "${dep}" in
  120. erlang)
  121. FILES+=( '.ci/docker-compose-file/docker-compose.yaml' )
  122. ;;
  123. toxiproxy)
  124. FILES+=( '.ci/docker-compose-file/docker-compose-toxiproxy.yaml' )
  125. ;;
  126. influxdb)
  127. FILES+=( '.ci/docker-compose-file/docker-compose-influxdb-tcp.yaml'
  128. '.ci/docker-compose-file/docker-compose-influxdb-tls.yaml' )
  129. ;;
  130. mongo)
  131. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-single-tcp.yaml'
  132. '.ci/docker-compose-file/docker-compose-mongo-single-tls.yaml' )
  133. ;;
  134. mongo_rs_sharded)
  135. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-replicaset-tcp.yaml'
  136. '.ci/docker-compose-file/docker-compose-mongo-sharded-tcp.yaml' )
  137. ;;
  138. redis)
  139. FILES+=( '.ci/docker-compose-file/docker-compose-redis-single-tcp.yaml'
  140. '.ci/docker-compose-file/docker-compose-redis-single-tls.yaml'
  141. '.ci/docker-compose-file/docker-compose-redis-sentinel-tcp.yaml'
  142. '.ci/docker-compose-file/docker-compose-redis-sentinel-tls.yaml' )
  143. ;;
  144. redis_cluster)
  145. FILES+=( '.ci/docker-compose-file/docker-compose-redis-cluster-tcp.yaml'
  146. '.ci/docker-compose-file/docker-compose-redis-cluster-tls.yaml' )
  147. ;;
  148. mysql)
  149. FILES+=( '.ci/docker-compose-file/docker-compose-mysql-tcp.yaml'
  150. '.ci/docker-compose-file/docker-compose-mysql-tls.yaml' )
  151. ;;
  152. pgsql)
  153. FILES+=( '.ci/docker-compose-file/docker-compose-pgsql-tcp.yaml'
  154. '.ci/docker-compose-file/docker-compose-pgsql-tls.yaml' )
  155. ;;
  156. kafka)
  157. FILES+=( '.ci/docker-compose-file/docker-compose-kafka.yaml' )
  158. ;;
  159. tdengine)
  160. FILES+=( '.ci/docker-compose-file/docker-compose-tdengine-restful.yaml' )
  161. ;;
  162. clickhouse)
  163. FILES+=( '.ci/docker-compose-file/docker-compose-clickhouse.yaml' )
  164. ;;
  165. dynamo)
  166. FILES+=( '.ci/docker-compose-file/docker-compose-dynamo.yaml' )
  167. ;;
  168. rocketmq)
  169. FILES+=( '.ci/docker-compose-file/docker-compose-rocketmq.yaml' )
  170. ;;
  171. cassandra)
  172. FILES+=( '.ci/docker-compose-file/docker-compose-cassandra.yaml' )
  173. ;;
  174. sqlserver)
  175. ODBC_REQUEST='yes'
  176. FILES+=( '.ci/docker-compose-file/docker-compose-sqlserver.yaml' )
  177. ;;
  178. opents)
  179. FILES+=( '.ci/docker-compose-file/docker-compose-opents.yaml' )
  180. ;;
  181. pulsar)
  182. FILES+=( '.ci/docker-compose-file/docker-compose-pulsar.yaml' )
  183. ;;
  184. oracle)
  185. FILES+=( '.ci/docker-compose-file/docker-compose-oracle.yaml' )
  186. ;;
  187. iotdb)
  188. FILES+=( '.ci/docker-compose-file/docker-compose-iotdb.yaml' )
  189. ;;
  190. rabbitmq)
  191. FILES+=( '.ci/docker-compose-file/docker-compose-rabbitmq.yaml' )
  192. ;;
  193. *)
  194. echo "unknown_ct_dependency $dep"
  195. exit 1
  196. ;;
  197. esac
  198. done
  199. if [ "$ODBC_REQUEST" = 'yes' ]; then
  200. INSTALL_ODBC="./scripts/install-msodbc-driver.sh"
  201. else
  202. INSTALL_ODBC="echo 'msodbc driver not requested'"
  203. fi
  204. F_OPTIONS=""
  205. for file in "${FILES[@]}"; do
  206. F_OPTIONS="$F_OPTIONS -f $file"
  207. done
  208. DOCKER_USER="$(id -u)"
  209. export DOCKER_USER
  210. TTY=''
  211. if [[ -t 1 ]]; then
  212. TTY='-t'
  213. fi
  214. # ensure directory with secrets is created by current user before running compose
  215. mkdir -p /tmp/emqx-ci/emqx-shared-secret
  216. if [ "$STOP" = 'no' ]; then
  217. # some left-over log file has to be deleted before a new docker-compose up
  218. rm -f '.ci/docker-compose-file/redis/*.log'
  219. set +e
  220. # shellcheck disable=2086 # no quotes for F_OPTIONS
  221. $DC $F_OPTIONS up -d --build --remove-orphans
  222. RESULT=$?
  223. if [ $RESULT -ne 0 ]; then
  224. mkdir -p _build/test/logs
  225. LOG='_build/test/logs/docker-compose.log'
  226. echo "Dumping docker-compose log to $LOG"
  227. # shellcheck disable=2086 # no quotes for F_OPTIONS
  228. $DC $F_OPTIONS logs --no-color --timestamps > "$LOG"
  229. exit 1
  230. fi
  231. set -e
  232. fi
  233. # rebar, mix and hex cache directory need to be writable by $DOCKER_USER
  234. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "mkdir -p /.cache /.hex /.mix && chown $DOCKER_USER /.cache /.hex /.mix"
  235. # need to initialize .erlang.cookie manually here because / is not writable by $DOCKER_USER
  236. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "openssl rand -base64 16 > /.erlang.cookie && chown $DOCKER_USER /.erlang.cookie && chmod 0400 /.erlang.cookie"
  237. # the user must exist inside the container for `whoami` to work
  238. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "useradd --uid $DOCKER_USER -M -d / emqx" || true
  239. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "chown -R $DOCKER_USER /var/lib/secret" || true
  240. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "$INSTALL_ODBC" || true
  241. if [ "$ONLY_UP" = 'yes' ]; then
  242. exit 0
  243. fi
  244. set +e
  245. if [ "$STOP" = 'yes' ]; then
  246. # shellcheck disable=2086 # no quotes for F_OPTIONS
  247. $DC $F_OPTIONS down --remove-orphans
  248. elif [ "$ATTACH" = 'yes' ]; then
  249. docker exec -it "$ERLANG_CONTAINER" bash
  250. elif [ "$CONSOLE" = 'yes' ]; then
  251. docker exec -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "make run"
  252. else
  253. if [ -z "${REBAR3CT:-}" ]; then
  254. docker exec -e IS_CI="$IS_CI" -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "BUILD_WITHOUT_QUIC=1 make ${WHICH_APP}-ct"
  255. else
  256. docker exec -e IS_CI="$IS_CI" -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "./rebar3 ct $REBAR3CT"
  257. fi
  258. RESULT=$?
  259. if [ "$RESULT" -ne 0 ]; then
  260. LOG='_build/test/logs/docker-compose.log'
  261. echo "Dumping docker-compose log to $LOG"
  262. # shellcheck disable=2086 # no quotes for F_OPTIONS
  263. $DC $F_OPTIONS logs --no-color --timestamps > "$LOG"
  264. fi
  265. if [ "$KEEP_UP" != 'yes' ]; then
  266. # shellcheck disable=2086 # no quotes for F_OPTIONS
  267. $DC $F_OPTIONS down
  268. fi
  269. exit "$RESULT"
  270. fi