run.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "--suites SUITE1,SUITE2: Comma separated SUITE names to run. e.g. apps/emqx/test/emqx_SUITE.erl"
  11. echo "--console: Start EMQX in console mode but do not run test cases"
  12. echo "--attach: Attach to the Erlang docker container without running any test case"
  13. echo "--stop: Stop running containers for the given app"
  14. echo "--only-up: Only start the testbed but do not run CT"
  15. echo "--keep-up: Keep the testbed running after CT"
  16. }
  17. WHICH_APP='novalue'
  18. CONSOLE='no'
  19. KEEP_UP='no'
  20. ONLY_UP='no'
  21. SUITES=''
  22. ATTACH='no'
  23. STOP='no'
  24. while [ "$#" -gt 0 ]; do
  25. case $1 in
  26. -h|--help)
  27. help
  28. exit 0
  29. ;;
  30. --app)
  31. WHICH_APP="$2"
  32. shift 2
  33. ;;
  34. --only-up)
  35. ONLY_UP='yes'
  36. shift 1
  37. ;;
  38. --keep-up)
  39. KEEP_UP='yes'
  40. shift 1
  41. ;;
  42. --attach)
  43. ATTACH='yes'
  44. shift 1
  45. ;;
  46. --stop)
  47. STOP='yes'
  48. shift 1
  49. ;;
  50. --console)
  51. CONSOLE='yes'
  52. shift 1
  53. ;;
  54. --suites)
  55. SUITES="$2"
  56. shift 2
  57. ;;
  58. *)
  59. echo "unknown option $1"
  60. exit 1
  61. ;;
  62. esac
  63. done
  64. if [ "${WHICH_APP}" = 'novalue' ]; then
  65. echo "must provide --app arg"
  66. exit 1
  67. fi
  68. ERLANG_CONTAINER='erlang'
  69. DOCKER_CT_ENVS_FILE="${WHICH_APP}/docker-ct"
  70. case "${WHICH_APP}" in
  71. lib-ee*)
  72. ## ensure enterprise profile when testing lib-ee applications
  73. export PROFILE='emqx-enterprise'
  74. ;;
  75. *)
  76. export PROFILE="${PROFILE:-emqx}"
  77. ;;
  78. esac
  79. if [ -f "$DOCKER_CT_ENVS_FILE" ]; then
  80. # shellcheck disable=SC2002
  81. CT_DEPS="$(cat "$DOCKER_CT_ENVS_FILE" | xargs)"
  82. fi
  83. CT_DEPS="${ERLANG_CONTAINER} ${CT_DEPS:-}"
  84. FILES=( )
  85. for dep in ${CT_DEPS}; do
  86. case "${dep}" in
  87. erlang)
  88. FILES+=( '.ci/docker-compose-file/docker-compose.yaml' )
  89. ;;
  90. toxiproxy)
  91. FILES+=( '.ci/docker-compose-file/docker-compose-toxiproxy.yaml' )
  92. ;;
  93. influxdb)
  94. FILES+=( '.ci/docker-compose-file/docker-compose-influxdb-tcp.yaml'
  95. '.ci/docker-compose-file/docker-compose-influxdb-tls.yaml' )
  96. ;;
  97. mongo)
  98. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-single-tcp.yaml'
  99. '.ci/docker-compose-file/docker-compose-mongo-single-tls.yaml' )
  100. ;;
  101. mongo_rs_sharded)
  102. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-replicaset-tcp.yaml'
  103. '.ci/docker-compose-file/docker-compose-mongo-sharded-tcp.yaml' )
  104. ;;
  105. redis)
  106. FILES+=( '.ci/docker-compose-file/docker-compose-redis-single-tcp.yaml'
  107. '.ci/docker-compose-file/docker-compose-redis-single-tls.yaml'
  108. '.ci/docker-compose-file/docker-compose-redis-sentinel-tcp.yaml'
  109. '.ci/docker-compose-file/docker-compose-redis-sentinel-tls.yaml' )
  110. ;;
  111. redis_cluster)
  112. FILES+=( '.ci/docker-compose-file/docker-compose-redis-cluster-tcp.yaml'
  113. '.ci/docker-compose-file/docker-compose-redis-cluster-tls.yaml' )
  114. ;;
  115. mysql)
  116. FILES+=( '.ci/docker-compose-file/docker-compose-mysql-tcp.yaml'
  117. '.ci/docker-compose-file/docker-compose-mysql-tls.yaml' )
  118. ;;
  119. pgsql)
  120. FILES+=( '.ci/docker-compose-file/docker-compose-pgsql-tcp.yaml'
  121. '.ci/docker-compose-file/docker-compose-pgsql-tls.yaml' )
  122. ;;
  123. kafka)
  124. # Kafka container generates root owned ssl files
  125. # the files are shared with EMQX (with a docker volume)
  126. NEED_ROOT=yes
  127. FILES+=( '.ci/docker-compose-file/docker-compose-kafka.yaml' )
  128. ;;
  129. *)
  130. echo "unknown_ct_dependency $dep"
  131. exit 1
  132. ;;
  133. esac
  134. done
  135. F_OPTIONS=""
  136. for file in "${FILES[@]}"; do
  137. F_OPTIONS="$F_OPTIONS -f $file"
  138. done
  139. ORIG_UID_GID="$UID:$UID"
  140. if [[ "${NEED_ROOT:-}" == 'yes' ]]; then
  141. export UID_GID='root:root'
  142. else
  143. # Passing $UID to docker-compose to be used in erlang container
  144. # as owner of the main process to avoid git repo permissions issue.
  145. # Permissions issue happens because we are mounting local filesystem
  146. # where files are owned by $UID to docker container where it's using
  147. # root (UID=0) by default, and git is not happy about it.
  148. export UID_GID="$ORIG_UID_GID"
  149. fi
  150. if [ "$STOP" = 'no' ]; then
  151. # shellcheck disable=2086 # no quotes for F_OPTIONS
  152. docker-compose $F_OPTIONS up -d --build --remove-orphans
  153. fi
  154. # /emqx is where the source dir is mounted to the Erlang container
  155. # in .ci/docker-compose-file/docker-compose.yaml
  156. TTY=''
  157. if [[ -t 1 ]]; then
  158. TTY='-t'
  159. fi
  160. echo "Fixing file owners and permissions for $UID_GID"
  161. # rebar and hex cache directory need to be writable by $UID
  162. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "mkdir -p /.cache && chown $UID_GID /.cache && chown -R $UID_GID /emqx"
  163. # need to initialize .erlang.cookie manually here because / is not writable by $UID
  164. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "openssl rand -base64 16 > /.erlang.cookie && chown $UID_GID /.erlang.cookie && chmod 0400 /.erlang.cookie"
  165. restore_ownership() {
  166. if [[ "$ORIG_UID_GID" != "$UID_GID" ]]; then
  167. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "chown -R $ORIG_UID_GID /emqx"
  168. fi
  169. }
  170. if [ "$ONLY_UP" = 'yes' ]; then
  171. exit 0
  172. fi
  173. set +e
  174. if [ "$STOP" = 'yes' ]; then
  175. # shellcheck disable=2086 # no quotes for F_OPTIONS
  176. docker-compose $F_OPTIONS down --remove-orphans
  177. elif [ "$ATTACH" = 'yes' ]; then
  178. docker exec -it "$ERLANG_CONTAINER" bash
  179. restore_ownership
  180. elif [ "$CONSOLE" = 'yes' ]; then
  181. docker exec -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "make run"
  182. restore_ownership
  183. else
  184. docker exec -e PROFILE="$PROFILE" -i $TTY -e EMQX_CT_SUITES="$SUITES" "$ERLANG_CONTAINER" bash -c "BUILD_WITHOUT_QUIC=1 make ${WHICH_APP}-ct"
  185. RESULT=$?
  186. restore_ownership
  187. if [ $RESULT -ne 0 ]; then
  188. LOG='_build/test/logs/docker-compose.log'
  189. echo "Dumping docker-compose log to $LOG"
  190. # shellcheck disable=2086 # no quotes for F_OPTIONS
  191. docker-compose $F_OPTIONS logs --no-color --timestamps > "$LOG"
  192. fi
  193. if [ "$KEEP_UP" != 'yes' ]; then
  194. # shellcheck disable=2086 # no quotes for F_OPTIONS
  195. docker-compose $F_OPTIONS down
  196. fi
  197. exit $RESULT
  198. fi