run.sh 11 KB

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