run.sh 12 KB

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