1
0

complement.sh 10 KB

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