run.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. if [[ "${WHICH_APP}" == lib-ee* && (-z "${PROFILE+x}" || "${PROFILE}" != emqx-enterprise) ]]; then
  77. echo 'You are trying to run an enterprise test case without the emqx-enterprise profile.'
  78. echo 'This will most likely not work.'
  79. echo ''
  80. echo 'Run "export PROFILE=emqx-enterprise" and "make" to fix this'
  81. exit 1
  82. fi
  83. ERLANG_CONTAINER='erlang'
  84. DOCKER_CT_ENVS_FILE="${WHICH_APP}/docker-ct"
  85. case "${WHICH_APP}" in
  86. lib-ee*)
  87. ## ensure enterprise profile when testing lib-ee applications
  88. export PROFILE='emqx-enterprise'
  89. ;;
  90. *)
  91. export PROFILE="${PROFILE:-emqx}"
  92. ;;
  93. esac
  94. if [ -f "$DOCKER_CT_ENVS_FILE" ]; then
  95. # shellcheck disable=SC2002
  96. CT_DEPS="$(cat "$DOCKER_CT_ENVS_FILE" | xargs)"
  97. fi
  98. CT_DEPS="${ERLANG_CONTAINER} ${CT_DEPS:-}"
  99. FILES=( )
  100. for dep in ${CT_DEPS}; do
  101. case "${dep}" in
  102. erlang)
  103. FILES+=( '.ci/docker-compose-file/docker-compose.yaml' )
  104. ;;
  105. toxiproxy)
  106. FILES+=( '.ci/docker-compose-file/docker-compose-toxiproxy.yaml' )
  107. ;;
  108. influxdb)
  109. FILES+=( '.ci/docker-compose-file/docker-compose-influxdb-tcp.yaml'
  110. '.ci/docker-compose-file/docker-compose-influxdb-tls.yaml' )
  111. ;;
  112. mongo)
  113. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-single-tcp.yaml'
  114. '.ci/docker-compose-file/docker-compose-mongo-single-tls.yaml' )
  115. ;;
  116. mongo_rs_sharded)
  117. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-replicaset-tcp.yaml'
  118. '.ci/docker-compose-file/docker-compose-mongo-sharded-tcp.yaml' )
  119. ;;
  120. redis)
  121. FILES+=( '.ci/docker-compose-file/docker-compose-redis-single-tcp.yaml'
  122. '.ci/docker-compose-file/docker-compose-redis-single-tls.yaml'
  123. '.ci/docker-compose-file/docker-compose-redis-sentinel-tcp.yaml'
  124. '.ci/docker-compose-file/docker-compose-redis-sentinel-tls.yaml' )
  125. ;;
  126. redis_cluster)
  127. FILES+=( '.ci/docker-compose-file/docker-compose-redis-cluster-tcp.yaml'
  128. '.ci/docker-compose-file/docker-compose-redis-cluster-tls.yaml' )
  129. ;;
  130. mysql)
  131. FILES+=( '.ci/docker-compose-file/docker-compose-mysql-tcp.yaml'
  132. '.ci/docker-compose-file/docker-compose-mysql-tls.yaml' )
  133. ;;
  134. pgsql)
  135. FILES+=( '.ci/docker-compose-file/docker-compose-pgsql-tcp.yaml'
  136. '.ci/docker-compose-file/docker-compose-pgsql-tls.yaml' )
  137. ;;
  138. kafka)
  139. # Kafka container generates root owned ssl files
  140. # the files are shared with EMQX (with a docker volume)
  141. NEED_ROOT=yes
  142. FILES+=( '.ci/docker-compose-file/docker-compose-kafka.yaml' )
  143. ;;
  144. tdengine)
  145. FILES+=( '.ci/docker-compose-file/docker-compose-tdengine-restful.yaml' )
  146. ;;
  147. *)
  148. echo "unknown_ct_dependency $dep"
  149. exit 1
  150. ;;
  151. esac
  152. done
  153. F_OPTIONS=""
  154. for file in "${FILES[@]}"; do
  155. F_OPTIONS="$F_OPTIONS -f $file"
  156. done
  157. ORIG_UID_GID="$UID:$UID"
  158. if [[ "${NEED_ROOT:-}" == 'yes' ]]; then
  159. export UID_GID='root:root'
  160. else
  161. # Passing $UID to docker-compose to be used in erlang container
  162. # as owner of the main process to avoid git repo permissions issue.
  163. # Permissions issue happens because we are mounting local filesystem
  164. # where files are owned by $UID to docker container where it's using
  165. # root (UID=0) by default, and git is not happy about it.
  166. export UID_GID="$ORIG_UID_GID"
  167. fi
  168. # /emqx is where the source dir is mounted to the Erlang container
  169. # in .ci/docker-compose-file/docker-compose.yaml
  170. TTY=''
  171. if [[ -t 1 ]]; then
  172. TTY='-t'
  173. fi
  174. function restore_ownership {
  175. if ! sudo chown -R "$ORIG_UID_GID" . >/dev/null 2>&1; then
  176. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "chown -R $ORIG_UID_GID /emqx" >/dev/null 2>&1 || true
  177. fi
  178. }
  179. restore_ownership
  180. trap restore_ownership EXIT
  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. # shellcheck disable=2086 # no quotes for F_OPTIONS
  185. docker-compose $F_OPTIONS up -d --build --remove-orphans
  186. fi
  187. echo "Fixing file owners and permissions for $UID_GID"
  188. # rebar and hex cache directory need to be writable by $UID
  189. 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"
  190. # need to initialize .erlang.cookie manually here because / is not writable by $UID
  191. 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"
  192. if [ "$ONLY_UP" = 'yes' ]; then
  193. exit 0
  194. fi
  195. set +e
  196. if [ "$STOP" = 'yes' ]; then
  197. # shellcheck disable=2086 # no quotes for F_OPTIONS
  198. docker-compose $F_OPTIONS down --remove-orphans
  199. elif [ "$ATTACH" = 'yes' ]; then
  200. docker exec -it "$ERLANG_CONTAINER" bash
  201. elif [ "$CONSOLE" = 'yes' ]; then
  202. docker exec -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "make run"
  203. else
  204. if [ -z "${REBAR3CT:-}" ]; then
  205. docker exec -e IS_CI="$IS_CI" -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "BUILD_WITHOUT_QUIC=1 make ${WHICH_APP}-ct"
  206. else
  207. docker exec -e IS_CI="$IS_CI" -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "./rebar3 ct $REBAR3CT"
  208. fi
  209. RESULT=$?
  210. restore_ownership
  211. if [ $RESULT -ne 0 ]; then
  212. LOG='_build/test/logs/docker-compose.log'
  213. echo "Dumping docker-compose log to $LOG"
  214. # shellcheck disable=2086 # no quotes for F_OPTIONS
  215. docker-compose $F_OPTIONS logs --no-color --timestamps > "$LOG"
  216. fi
  217. if [ "$KEEP_UP" != 'yes' ]; then
  218. # shellcheck disable=2086 # no quotes for F_OPTIONS
  219. docker-compose $F_OPTIONS down
  220. fi
  221. exit $RESULT
  222. fi