1
0

complement.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #!/usr/bin/env bash
  2. # This script is designed for developers who want to test their code
  3. # against Complement.
  4. #
  5. # It makes a Synapse image which represents the current checkout,
  6. # builds a synapse-complement image on top, then runs tests with it.
  7. #
  8. # By default the script will fetch the latest Complement main branch and
  9. # run tests with that. This can be overridden to use a custom Complement
  10. # checkout by setting the COMPLEMENT_DIR environment variable to the
  11. # filepath of a local Complement checkout or by setting the COMPLEMENT_REF
  12. # environment variable to pull a different branch or commit.
  13. #
  14. # By default Synapse is run in monolith mode. This can be overridden by
  15. # setting the WORKERS environment variable.
  16. #
  17. # You can optionally give a "-f" argument (for "fast") before any to skip
  18. # rebuilding the docker images, if you just want to rerun the tests.
  19. #
  20. # Remaining commandline arguments are passed through to `go test`. For example,
  21. # you can supply a regular expression of test method names via the "-run"
  22. # argument:
  23. #
  24. # ./complement.sh -run "TestOutboundFederation(Profile|Send)"
  25. #
  26. # Specifying TEST_ONLY_SKIP_DEP_HASH_VERIFICATION=1 will cause `poetry export`
  27. # to not emit any hashes when building the Docker image. This then means that
  28. # you can use 'unverifiable' sources such as git repositories as dependencies.
  29. # Exit if a line returns a non-zero exit code
  30. set -e
  31. # Helper to emit annotations that collapse portions of the log in GitHub Actions
  32. echo_if_github() {
  33. if [[ -n "$GITHUB_WORKFLOW" ]]; then
  34. echo $*
  35. fi
  36. }
  37. # Helper to print out the usage instructions
  38. usage() {
  39. cat >&2 <<EOF
  40. Usage: $0 [-f] <go test arguments>...
  41. Run the complement test suite on Synapse.
  42. -f, --fast
  43. Skip rebuilding the docker images, and just use the most recent
  44. 'complement-synapse:latest' image.
  45. Conflicts with --build-only.
  46. --build-only
  47. Only build the Docker images. Don't actually run Complement.
  48. Conflicts with -f/--fast.
  49. -e, --editable
  50. Use an editable build of Synapse, rebuilding the image if necessary.
  51. This is suitable for use in development where a fast turn-around time
  52. is important.
  53. Not suitable for use in CI in case the editable environment is impure.
  54. --rebuild-editable
  55. Force a rebuild of the editable build of Synapse.
  56. This is occasionally useful if the built-in rebuild detection with
  57. --editable fails, e.g. when changing configure_workers_and_start.py.
  58. For help on arguments to 'go test', run 'go help testflag'.
  59. EOF
  60. }
  61. # parse our arguments
  62. skip_docker_build=""
  63. skip_complement_run=""
  64. while [ $# -ge 1 ]; do
  65. arg=$1
  66. case "$arg" in
  67. "-h")
  68. usage
  69. exit 1
  70. ;;
  71. "-f"|"--fast")
  72. skip_docker_build=1
  73. ;;
  74. "--build-only")
  75. skip_complement_run=1
  76. ;;
  77. "-e"|"--editable")
  78. use_editable_synapse=1
  79. ;;
  80. "--rebuild-editable")
  81. rebuild_editable_synapse=1
  82. ;;
  83. *)
  84. # unknown arg: presumably an argument to gotest. break the loop.
  85. break
  86. esac
  87. shift
  88. done
  89. # enable buildkit for the docker builds
  90. export DOCKER_BUILDKIT=1
  91. # Change to the repository root
  92. cd "$(dirname $0)/.."
  93. # Check for a user-specified Complement checkout
  94. if [[ -z "$COMPLEMENT_DIR" ]]; then
  95. COMPLEMENT_REF=${COMPLEMENT_REF:-main}
  96. echo "COMPLEMENT_DIR not set. Fetching Complement checkout from ${COMPLEMENT_REF}..."
  97. wget -Nq https://github.com/matrix-org/complement/archive/${COMPLEMENT_REF}.tar.gz
  98. tar -xzf ${COMPLEMENT_REF}.tar.gz
  99. COMPLEMENT_DIR=complement-${COMPLEMENT_REF}
  100. echo "Checkout available at 'complement-${COMPLEMENT_REF}'"
  101. fi
  102. if [ -n "$use_editable_synapse" ]; then
  103. if [[ -e synapse/synapse_rust.abi3.so ]]; then
  104. # In an editable install, back up the host's compiled Rust module to prevent
  105. # inconvenience; the container will overwrite the module with its own copy.
  106. mv -n synapse/synapse_rust.abi3.so synapse/synapse_rust.abi3.so~host
  107. # And restore it on exit:
  108. synapse_pkg=`realpath synapse`
  109. trap "mv -f '$synapse_pkg/synapse_rust.abi3.so~host' '$synapse_pkg/synapse_rust.abi3.so'" EXIT
  110. fi
  111. editable_mount="$(realpath .):/editable-src:z"
  112. if [ -n "$rebuild_editable_synapse" ]; then
  113. unset skip_docker_build
  114. elif docker inspect complement-synapse-editable &>/dev/null; then
  115. # complement-synapse-editable already exists: see if we can still use it:
  116. # - The Rust module must still be importable; it will fail to import if the Rust source has changed.
  117. # - The Poetry lock file must be the same (otherwise we assume dependencies have changed)
  118. # First set up the module in the right place for an editable installation.
  119. docker run --rm -v $editable_mount --entrypoint 'cp' complement-synapse-editable -- /synapse_rust.abi3.so.bak /editable-src/synapse/synapse_rust.abi3.so
  120. if (docker run --rm -v $editable_mount --entrypoint 'python' complement-synapse-editable -c 'import synapse.synapse_rust' \
  121. && docker run --rm -v $editable_mount --entrypoint 'diff' complement-synapse-editable --brief /editable-src/poetry.lock /poetry.lock.bak); then
  122. skip_docker_build=1
  123. else
  124. echo "Editable Synapse image is stale. Will rebuild."
  125. unset skip_docker_build
  126. fi
  127. fi
  128. fi
  129. if [ -z "$skip_docker_build" ]; then
  130. if [ -n "$use_editable_synapse" ]; then
  131. # Build a special image designed for use in development with editable
  132. # installs.
  133. docker build -t synapse-editable \
  134. -f "docker/editable.Dockerfile" .
  135. docker build -t synapse-workers-editable \
  136. --build-arg FROM=synapse-editable \
  137. -f "docker/Dockerfile-workers" .
  138. docker build -t complement-synapse-editable \
  139. --build-arg FROM=synapse-workers-editable \
  140. -f "docker/complement/Dockerfile" "docker/complement"
  141. # Prepare the Rust module
  142. docker run --rm -v $editable_mount --entrypoint 'cp' complement-synapse-editable -- /synapse_rust.abi3.so.bak /editable-src/synapse/synapse_rust.abi3.so
  143. else
  144. # Build the base Synapse image from the local checkout
  145. echo_if_github "::group::Build Docker image: matrixdotorg/synapse"
  146. docker build -t matrixdotorg/synapse \
  147. --build-arg TEST_ONLY_SKIP_DEP_HASH_VERIFICATION \
  148. --build-arg TEST_ONLY_IGNORE_POETRY_LOCKFILE \
  149. -f "docker/Dockerfile" .
  150. echo_if_github "::endgroup::"
  151. # Build the workers docker image (from the base Synapse image we just built).
  152. echo_if_github "::group::Build Docker image: matrixdotorg/synapse-workers"
  153. docker build -t matrixdotorg/synapse-workers -f "docker/Dockerfile-workers" .
  154. echo_if_github "::endgroup::"
  155. # Build the unified Complement image (from the worker Synapse image we just built).
  156. echo_if_github "::group::Build Docker image: complement/Dockerfile"
  157. docker build -t complement-synapse \
  158. -f "docker/complement/Dockerfile" "docker/complement"
  159. echo_if_github "::endgroup::"
  160. fi
  161. fi
  162. if [ -n "$skip_complement_run" ]; then
  163. echo "Skipping Complement run as requested."
  164. exit
  165. fi
  166. export COMPLEMENT_BASE_IMAGE=complement-synapse
  167. if [ -n "$use_editable_synapse" ]; then
  168. export COMPLEMENT_BASE_IMAGE=complement-synapse-editable
  169. export COMPLEMENT_HOST_MOUNTS="$editable_mount"
  170. fi
  171. extra_test_args=()
  172. test_tags="synapse_blacklist,msc3787,msc3874,msc3890,msc3391,msc3930,faster_joins"
  173. # All environment variables starting with PASS_ will be shared.
  174. # (The prefix is stripped off before reaching the container.)
  175. export COMPLEMENT_SHARE_ENV_PREFIX=PASS_
  176. # It takes longer than 10m to run the whole suite.
  177. extra_test_args+=("-timeout=60m")
  178. if [[ -n "$WORKERS" ]]; then
  179. # Use workers.
  180. export PASS_SYNAPSE_COMPLEMENT_USE_WORKERS=true
  181. # Pass through the workers defined. If none, it will be an empty string
  182. export PASS_SYNAPSE_WORKER_TYPES="$WORKER_TYPES"
  183. # Workers can only use Postgres as a database.
  184. export PASS_SYNAPSE_COMPLEMENT_DATABASE=postgres
  185. # And provide some more configuration to complement.
  186. # It can take quite a while to spin up a worker-mode Synapse for the first
  187. # time (the main problem is that we start 14 python processes for each test,
  188. # and complement likes to do two of them in parallel).
  189. export COMPLEMENT_SPAWN_HS_TIMEOUT_SECS=120
  190. else
  191. export PASS_SYNAPSE_COMPLEMENT_USE_WORKERS=
  192. if [[ -n "$POSTGRES" ]]; then
  193. export PASS_SYNAPSE_COMPLEMENT_DATABASE=postgres
  194. else
  195. export PASS_SYNAPSE_COMPLEMENT_DATABASE=sqlite
  196. fi
  197. # The tests for importing historical messages (MSC2716)
  198. # only pass with monoliths, currently.
  199. test_tags="$test_tags,msc2716"
  200. fi
  201. if [[ -n "$ASYNCIO_REACTOR" ]]; then
  202. # Enable the Twisted asyncio reactor
  203. export PASS_SYNAPSE_COMPLEMENT_USE_ASYNCIO_REACTOR=true
  204. fi
  205. if [[ -n "$SYNAPSE_TEST_LOG_LEVEL" ]]; then
  206. # Set the log level to what is desired
  207. export PASS_SYNAPSE_LOG_LEVEL="$SYNAPSE_TEST_LOG_LEVEL"
  208. # Allow logging sensitive things (currently SQL queries & parameters).
  209. # (This won't have any effect if we're not logging at DEBUG level overall.)
  210. # Since this is just a test suite, this is fine and won't reveal anyone's
  211. # personal information
  212. export PASS_SYNAPSE_LOG_SENSITIVE=1
  213. fi
  214. # Run the tests!
  215. echo "Images built; running complement"
  216. cd "$COMPLEMENT_DIR"
  217. go test -v -tags $test_tags -count=1 "${extra_test_args[@]}" "$@" ./tests/...