run.sh 6.1 KB

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