run.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. ODBC_REQUEST='no'
  37. while [ "$#" -gt 0 ]; do
  38. case $1 in
  39. -h|--help)
  40. help
  41. exit 0
  42. ;;
  43. --app)
  44. WHICH_APP="${2%/}"
  45. shift 2
  46. ;;
  47. --only-up)
  48. ONLY_UP='yes'
  49. shift 1
  50. ;;
  51. --keep-up)
  52. KEEP_UP='yes'
  53. shift 1
  54. ;;
  55. --attach)
  56. ATTACH='yes'
  57. shift 1
  58. ;;
  59. --stop)
  60. STOP='yes'
  61. shift 1
  62. ;;
  63. --console)
  64. CONSOLE='yes'
  65. shift 1
  66. ;;
  67. --ci)
  68. IS_CI='yes'
  69. shift 1
  70. ;;
  71. --)
  72. shift 1
  73. REBAR3CT="$*"
  74. shift $#
  75. ;;
  76. *)
  77. echo "unknown option $1"
  78. exit 1
  79. ;;
  80. esac
  81. done
  82. if [ "${WHICH_APP}" = 'novalue' ]; then
  83. echo "must provide --app arg"
  84. help
  85. exit 1
  86. fi
  87. if [ ! -d "${WHICH_APP}" ]; then
  88. echo "must provide an existing path for --app arg"
  89. help
  90. exit 1
  91. fi
  92. if [[ "${WHICH_APP}" == lib-ee* && (-z "${PROFILE+x}" || "${PROFILE}" != emqx-enterprise) ]]; then
  93. echo 'You are trying to run an enterprise test case without the emqx-enterprise profile.'
  94. echo 'This will most likely not work.'
  95. echo ''
  96. echo 'Run "export PROFILE=emqx-enterprise" and "make" to fix this'
  97. exit 1
  98. fi
  99. ERLANG_CONTAINER='erlang'
  100. DOCKER_CT_ENVS_FILE="${WHICH_APP}/docker-ct"
  101. if [ -z "${PROFILE+x}" ]; then
  102. case "${WHICH_APP}" in
  103. apps/emqx)
  104. export PROFILE='emqx-enterprise'
  105. ;;
  106. apps/emqx_bridge)
  107. export PROFILE='emqx-enterprise'
  108. ;;
  109. # emqx_connector test suite is using kafka bridge which is only available in emqx-enterprise
  110. apps/emqx_connector)
  111. export PROFILE='emqx-enterprise'
  112. ;;
  113. apps/emqx_dashboard)
  114. export PROFILE='emqx-enterprise'
  115. ;;
  116. lib-ee*)
  117. ## ensure enterprise profile when testing lib-ee applications
  118. export PROFILE='emqx-enterprise'
  119. ;;
  120. apps/*)
  121. if [[ -f "${WHICH_APP}/BSL.txt" ]]; then
  122. export PROFILE='emqx-enterprise'
  123. else
  124. export PROFILE='emqx'
  125. fi
  126. ;;
  127. *)
  128. export PROFILE="${PROFILE:-emqx}"
  129. ;;
  130. esac
  131. fi
  132. if [ -f "$DOCKER_CT_ENVS_FILE" ]; then
  133. # shellcheck disable=SC2002
  134. CT_DEPS="$(cat "$DOCKER_CT_ENVS_FILE" | xargs)"
  135. fi
  136. CT_DEPS="${ERLANG_CONTAINER} ${CT_DEPS:-}"
  137. FILES=( )
  138. for dep in ${CT_DEPS}; do
  139. case "${dep}" in
  140. erlang)
  141. FILES+=( '.ci/docker-compose-file/docker-compose.yaml' )
  142. ;;
  143. toxiproxy)
  144. FILES+=( '.ci/docker-compose-file/docker-compose-toxiproxy.yaml' )
  145. ;;
  146. influxdb)
  147. FILES+=( '.ci/docker-compose-file/docker-compose-influxdb-tcp.yaml'
  148. '.ci/docker-compose-file/docker-compose-influxdb-tls.yaml' )
  149. ;;
  150. mongo)
  151. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-single-tcp.yaml'
  152. '.ci/docker-compose-file/docker-compose-mongo-single-tls.yaml' )
  153. ;;
  154. mongo_rs_sharded)
  155. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-replicaset-tcp.yaml'
  156. '.ci/docker-compose-file/docker-compose-mongo-sharded-tcp.yaml' )
  157. ;;
  158. redis)
  159. FILES+=( '.ci/docker-compose-file/docker-compose-redis-single-tcp.yaml'
  160. '.ci/docker-compose-file/docker-compose-redis-single-tls.yaml'
  161. '.ci/docker-compose-file/docker-compose-redis-sentinel-tcp.yaml'
  162. '.ci/docker-compose-file/docker-compose-redis-sentinel-tls.yaml' )
  163. ;;
  164. redis_cluster)
  165. FILES+=( '.ci/docker-compose-file/docker-compose-redis-cluster-tcp.yaml'
  166. '.ci/docker-compose-file/docker-compose-redis-cluster-tls.yaml' )
  167. ;;
  168. mysql)
  169. FILES+=( '.ci/docker-compose-file/docker-compose-mysql-tcp.yaml'
  170. '.ci/docker-compose-file/docker-compose-mysql-tls.yaml' )
  171. ;;
  172. pgsql)
  173. FILES+=( '.ci/docker-compose-file/docker-compose-pgsql-tcp.yaml'
  174. '.ci/docker-compose-file/docker-compose-pgsql-tls.yaml' )
  175. ;;
  176. kafka)
  177. FILES+=( '.ci/docker-compose-file/docker-compose-kafka.yaml' )
  178. ;;
  179. tdengine)
  180. FILES+=( '.ci/docker-compose-file/docker-compose-tdengine-restful.yaml' )
  181. ;;
  182. clickhouse)
  183. FILES+=( '.ci/docker-compose-file/docker-compose-clickhouse.yaml' )
  184. ;;
  185. dynamo)
  186. FILES+=( '.ci/docker-compose-file/docker-compose-dynamo.yaml' )
  187. ;;
  188. rocketmq)
  189. FILES+=( '.ci/docker-compose-file/docker-compose-rocketmq.yaml' )
  190. ;;
  191. cassandra)
  192. FILES+=( '.ci/docker-compose-file/docker-compose-cassandra.yaml' )
  193. ;;
  194. sqlserver)
  195. ODBC_REQUEST='yes'
  196. FILES+=( '.ci/docker-compose-file/docker-compose-sqlserver.yaml' )
  197. ;;
  198. opents)
  199. FILES+=( '.ci/docker-compose-file/docker-compose-opents.yaml' )
  200. ;;
  201. pulsar)
  202. FILES+=( '.ci/docker-compose-file/docker-compose-pulsar.yaml' )
  203. ;;
  204. oracle)
  205. FILES+=( '.ci/docker-compose-file/docker-compose-oracle.yaml' )
  206. ;;
  207. iotdb)
  208. FILES+=( '.ci/docker-compose-file/docker-compose-iotdb.yaml' )
  209. ;;
  210. rabbitmq)
  211. FILES+=( '.ci/docker-compose-file/docker-compose-rabbitmq.yaml' )
  212. ;;
  213. minio)
  214. FILES+=( '.ci/docker-compose-file/docker-compose-minio-tcp.yaml'
  215. '.ci/docker-compose-file/docker-compose-minio-tls.yaml' )
  216. ;;
  217. gcp_emulator)
  218. FILES+=( '.ci/docker-compose-file/docker-compose-gcp-emulator.yaml' )
  219. ;;
  220. hstreamdb)
  221. FILES+=( '.ci/docker-compose-file/docker-compose-hstreamdb.yaml' )
  222. ;;
  223. kinesis)
  224. FILES+=( '.ci/docker-compose-file/docker-compose-kinesis.yaml' )
  225. ;;
  226. greptimedb)
  227. FILES+=( '.ci/docker-compose-file/docker-compose-greptimedb.yaml' )
  228. ;;
  229. ldap)
  230. FILES+=( '.ci/docker-compose-file/docker-compose-ldap.yaml' )
  231. ;;
  232. *)
  233. echo "unknown_ct_dependency $dep"
  234. exit 1
  235. ;;
  236. esac
  237. done
  238. if [ "$ODBC_REQUEST" = 'yes' ]; then
  239. INSTALL_ODBC="./scripts/install-msodbc-driver.sh"
  240. else
  241. INSTALL_ODBC="echo 'msodbc driver not requested'"
  242. fi
  243. F_OPTIONS=""
  244. for file in "${FILES[@]}"; do
  245. F_OPTIONS="$F_OPTIONS -f $file"
  246. done
  247. DOCKER_USER="$(id -u)"
  248. export DOCKER_USER
  249. TTY=''
  250. if [[ -t 1 ]]; then
  251. TTY='-t'
  252. fi
  253. # ensure directory with secrets is created by current user before running compose
  254. mkdir -p /tmp/emqx-ci/emqx-shared-secret
  255. if [ "$STOP" = 'no' ]; then
  256. # some left-over log file has to be deleted before a new docker-compose up
  257. rm -f '.ci/docker-compose-file/redis/*.log'
  258. set +e
  259. # shellcheck disable=2086 # no quotes for F_OPTIONS
  260. $DC $F_OPTIONS up -d --build --remove-orphans
  261. RESULT=$?
  262. if [ $RESULT -ne 0 ]; then
  263. mkdir -p _build/test/logs
  264. LOG='_build/test/logs/docker-compose.log'
  265. echo "Dumping docker-compose log to $LOG"
  266. # shellcheck disable=2086 # no quotes for F_OPTIONS
  267. $DC $F_OPTIONS logs --no-color --timestamps > "$LOG"
  268. exit 1
  269. fi
  270. set -e
  271. fi
  272. if [ "$DOCKER_USER" != "root" ]; then
  273. # the user must exist inside the container for `whoami` to work
  274. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c \
  275. "useradd --uid $DOCKER_USER -M -d / emqx && \
  276. mkdir -p /.cache /.hex /.mix && \
  277. chown $DOCKER_USER /.cache /.hex /.mix && \
  278. openssl rand -base64 -hex 16 > /.erlang.cookie && \
  279. chown $DOCKER_USER /.erlang.cookie && \
  280. chmod 0400 /.erlang.cookie && \
  281. chown -R $DOCKER_USER /var/lib/secret && \
  282. $INSTALL_ODBC" || true
  283. fi
  284. if [ "$ONLY_UP" = 'yes' ]; then
  285. exit 0
  286. fi
  287. set +e
  288. if [ "$STOP" = 'yes' ]; then
  289. # shellcheck disable=2086 # no quotes for F_OPTIONS
  290. $DC $F_OPTIONS down --remove-orphans
  291. elif [ "$ATTACH" = 'yes' ]; then
  292. docker exec -it "$ERLANG_CONTAINER" bash
  293. elif [ "$CONSOLE" = 'yes' ]; then
  294. docker exec -e PROFILE="$PROFILE" -i $TTY "$ERLANG_CONTAINER" bash -c "make run"
  295. else
  296. if [ -z "${REBAR3CT:-}" ]; then
  297. docker exec -e IS_CI="$IS_CI" \
  298. -e PROFILE="$PROFILE" \
  299. -e SUITEGROUP="${SUITEGROUP:-}" \
  300. -e ENABLE_COVER_COMPILE="${ENABLE_COVER_COMPILE:-}" \
  301. -e CT_COVER_EXPORT_PREFIX="${CT_COVER_EXPORT_PREFIX:-}" \
  302. -i $TTY "$ERLANG_CONTAINER" \
  303. bash -c "BUILD_WITHOUT_QUIC=1 make ${WHICH_APP}-ct"
  304. else
  305. # this is an ad-hoc run
  306. docker exec -e IS_CI="$IS_CI" \
  307. -e PROFILE="$PROFILE" \
  308. -i $TTY "$ERLANG_CONTAINER" \
  309. bash -c "./rebar3 ct $REBAR3CT"
  310. fi
  311. RESULT=$?
  312. if [ "$RESULT" -ne 0 ]; then
  313. LOG='_build/test/logs/docker-compose.log'
  314. echo "Dumping docker-compose log to $LOG"
  315. # shellcheck disable=2086 # no quotes for F_OPTIONS
  316. $DC $F_OPTIONS logs --no-color --timestamps > "$LOG"
  317. fi
  318. if [ "$KEEP_UP" != 'yes' ]; then
  319. # shellcheck disable=2086 # no quotes for F_OPTIONS
  320. $DC $F_OPTIONS down
  321. fi
  322. exit "$RESULT"
  323. fi