run.sh 7.6 KB

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