run.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. while [ "$#" -gt 0 ]; do
  37. case $1 in
  38. -h|--help)
  39. help
  40. exit 0
  41. ;;
  42. --app)
  43. WHICH_APP="$2"
  44. shift 2
  45. ;;
  46. --only-up)
  47. ONLY_UP='yes'
  48. shift 1
  49. ;;
  50. --keep-up)
  51. KEEP_UP='yes'
  52. shift 1
  53. ;;
  54. --attach)
  55. ATTACH='yes'
  56. shift 1
  57. ;;
  58. --stop)
  59. STOP='yes'
  60. shift 1
  61. ;;
  62. --console)
  63. CONSOLE='yes'
  64. shift 1
  65. ;;
  66. --ci)
  67. IS_CI='yes'
  68. shift 1
  69. ;;
  70. --)
  71. shift 1
  72. REBAR3CT="$*"
  73. shift $#
  74. ;;
  75. *)
  76. echo "unknown option $1"
  77. exit 1
  78. ;;
  79. esac
  80. done
  81. if [ "${WHICH_APP}" = 'novalue' ]; then
  82. echo "must provide --app arg"
  83. help
  84. exit 1
  85. fi
  86. if [[ "${WHICH_APP}" == lib-ee* && (-z "${PROFILE+x}" || "${PROFILE}" != emqx-enterprise) ]]; then
  87. echo 'You are trying to run an enterprise test case without the emqx-enterprise profile.'
  88. echo 'This will most likely not work.'
  89. echo ''
  90. echo 'Run "export PROFILE=emqx-enterprise" and "make" to fix this'
  91. exit 1
  92. fi
  93. ERLANG_CONTAINER='erlang'
  94. DOCKER_CT_ENVS_FILE="${WHICH_APP}/docker-ct"
  95. case "${WHICH_APP}" in
  96. lib-ee*)
  97. ## ensure enterprise profile when testing lib-ee applications
  98. export PROFILE='emqx-enterprise'
  99. ;;
  100. *)
  101. export PROFILE="${PROFILE:-emqx}"
  102. ;;
  103. esac
  104. if [ -f "$DOCKER_CT_ENVS_FILE" ]; then
  105. # shellcheck disable=SC2002
  106. CT_DEPS="$(cat "$DOCKER_CT_ENVS_FILE" | xargs)"
  107. fi
  108. CT_DEPS="${ERLANG_CONTAINER} ${CT_DEPS:-}"
  109. FILES=( )
  110. for dep in ${CT_DEPS}; do
  111. case "${dep}" in
  112. erlang)
  113. FILES+=( '.ci/docker-compose-file/docker-compose.yaml' )
  114. ;;
  115. toxiproxy)
  116. FILES+=( '.ci/docker-compose-file/docker-compose-toxiproxy.yaml' )
  117. ;;
  118. influxdb)
  119. FILES+=( '.ci/docker-compose-file/docker-compose-influxdb-tcp.yaml'
  120. '.ci/docker-compose-file/docker-compose-influxdb-tls.yaml' )
  121. ;;
  122. mongo)
  123. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-single-tcp.yaml'
  124. '.ci/docker-compose-file/docker-compose-mongo-single-tls.yaml' )
  125. ;;
  126. mongo_rs_sharded)
  127. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-replicaset-tcp.yaml'
  128. '.ci/docker-compose-file/docker-compose-mongo-sharded-tcp.yaml' )
  129. ;;
  130. redis)
  131. FILES+=( '.ci/docker-compose-file/docker-compose-redis-single-tcp.yaml'
  132. '.ci/docker-compose-file/docker-compose-redis-single-tls.yaml'
  133. '.ci/docker-compose-file/docker-compose-redis-sentinel-tcp.yaml'
  134. '.ci/docker-compose-file/docker-compose-redis-sentinel-tls.yaml' )
  135. ;;
  136. redis_cluster)
  137. FILES+=( '.ci/docker-compose-file/docker-compose-redis-cluster-tcp.yaml'
  138. '.ci/docker-compose-file/docker-compose-redis-cluster-tls.yaml' )
  139. ;;
  140. mysql)
  141. FILES+=( '.ci/docker-compose-file/docker-compose-mysql-tcp.yaml'
  142. '.ci/docker-compose-file/docker-compose-mysql-tls.yaml' )
  143. ;;
  144. pgsql)
  145. FILES+=( '.ci/docker-compose-file/docker-compose-pgsql-tcp.yaml'
  146. '.ci/docker-compose-file/docker-compose-pgsql-tls.yaml' )
  147. ;;
  148. kafka)
  149. FILES+=( '.ci/docker-compose-file/docker-compose-kafka.yaml' )
  150. ;;
  151. tdengine)
  152. FILES+=( '.ci/docker-compose-file/docker-compose-tdengine-restful.yaml' )
  153. ;;
  154. clickhouse)
  155. FILES+=( '.ci/docker-compose-file/docker-compose-clickhouse.yaml' )
  156. ;;
  157. dynamo)
  158. FILES+=( '.ci/docker-compose-file/docker-compose-dynamo.yaml' )
  159. ;;
  160. rocketmq)
  161. FILES+=( '.ci/docker-compose-file/docker-compose-rocketmq.yaml' )
  162. ;;
  163. *)
  164. echo "unknown_ct_dependency $dep"
  165. exit 1
  166. ;;
  167. esac
  168. done
  169. F_OPTIONS=""
  170. for file in "${FILES[@]}"; do
  171. F_OPTIONS="$F_OPTIONS -f $file"
  172. done
  173. DOCKER_USER="$(id -u)"
  174. export DOCKER_USER
  175. TTY=''
  176. if [[ -t 1 ]]; then
  177. TTY='-t'
  178. fi
  179. # ensure directory with secrets is created by current user before running compose
  180. mkdir -p /tmp/emqx-ci/emqx-shared-secret
  181. if [ "$STOP" = 'no' ]; then
  182. # some left-over log file has to be deleted before a new docker-compose up
  183. rm -f '.ci/docker-compose-file/redis/*.log'
  184. set +e
  185. # shellcheck disable=2086 # no quotes for F_OPTIONS
  186. $DC $F_OPTIONS up -d --build --remove-orphans
  187. RESULT=$?
  188. if [ $RESULT -ne 0 ]; then
  189. mkdir -p _build/test/logs
  190. LOG='_build/test/logs/docker-compose.log'
  191. echo "Dumping docker-compose log to $LOG"
  192. # shellcheck disable=2086 # no quotes for F_OPTIONS
  193. $DC $F_OPTIONS logs --no-color --timestamps > "$LOG"
  194. exit 1
  195. fi
  196. set -e
  197. fi
  198. # rebar, mix and hex cache directory need to be writable by $DOCKER_USER
  199. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "mkdir -p /.cache /.hex /.mix && chown $DOCKER_USER /.cache /.hex /.mix"
  200. # need to initialize .erlang.cookie manually here because / is not writable by $DOCKER_USER
  201. 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"
  202. # the user must exist inside the container for `whoami` to work
  203. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "useradd --uid $DOCKER_USER -M -d / emqx" || true
  204. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "chown -R $DOCKER_USER /var/lib/secret" || true
  205. if [ "$ONLY_UP" = 'yes' ]; then
  206. exit 0
  207. fi
  208. set +e
  209. if [ "$STOP" = 'yes' ]; then
  210. # shellcheck disable=2086 # no quotes for F_OPTIONS
  211. $DC $F_OPTIONS down --remove-orphans
  212. elif [ "$ATTACH" = 'yes' ]; then
  213. docker exec -it "$ERLANG_CONTAINER" bash
  214. elif [ "$CONSOLE" = 'yes' ]; then
  215. docker exec -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "make run"
  216. else
  217. if [ -z "${REBAR3CT:-}" ]; then
  218. docker exec -e IS_CI="$IS_CI" -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "BUILD_WITHOUT_QUIC=1 make ${WHICH_APP}-ct"
  219. else
  220. docker exec -e IS_CI="$IS_CI" -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "./rebar3 ct $REBAR3CT"
  221. fi
  222. RESULT=$?
  223. if [ "$RESULT" -ne 0 ]; then
  224. LOG='_build/test/logs/docker-compose.log'
  225. echo "Dumping docker-compose log to $LOG"
  226. # shellcheck disable=2086 # no quotes for F_OPTIONS
  227. $DC $F_OPTIONS logs --no-color --timestamps > "$LOG"
  228. fi
  229. if [ "$KEEP_UP" != 'yes' ]; then
  230. # shellcheck disable=2086 # no quotes for F_OPTIONS
  231. $DC $F_OPTIONS down
  232. fi
  233. exit "$RESULT"
  234. fi