complement.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. For help on arguments to 'go test', run 'go help testflag'.
  50. EOF
  51. }
  52. # parse our arguments
  53. skip_docker_build=""
  54. skip_complement_run=""
  55. while [ $# -ge 1 ]; do
  56. arg=$1
  57. case "$arg" in
  58. "-h")
  59. usage
  60. exit 1
  61. ;;
  62. "-f"|"--fast")
  63. skip_docker_build=1
  64. ;;
  65. "--build-only")
  66. skip_complement_run=1
  67. ;;
  68. *)
  69. # unknown arg: presumably an argument to gotest. break the loop.
  70. break
  71. esac
  72. shift
  73. done
  74. # enable buildkit for the docker builds
  75. export DOCKER_BUILDKIT=1
  76. # Change to the repository root
  77. cd "$(dirname $0)/.."
  78. # Check for a user-specified Complement checkout
  79. if [[ -z "$COMPLEMENT_DIR" ]]; then
  80. COMPLEMENT_REF=${COMPLEMENT_REF:-main}
  81. echo "COMPLEMENT_DIR not set. Fetching Complement checkout from ${COMPLEMENT_REF}..."
  82. wget -Nq https://github.com/matrix-org/complement/archive/${COMPLEMENT_REF}.tar.gz
  83. tar -xzf ${COMPLEMENT_REF}.tar.gz
  84. COMPLEMENT_DIR=complement-${COMPLEMENT_REF}
  85. echo "Checkout available at 'complement-${COMPLEMENT_REF}'"
  86. fi
  87. if [ -z "$skip_docker_build" ]; then
  88. # Build the base Synapse image from the local checkout
  89. echo_if_github "::group::Build Docker image: matrixdotorg/synapse"
  90. docker build -t matrixdotorg/synapse \
  91. --build-arg TEST_ONLY_SKIP_DEP_HASH_VERIFICATION \
  92. --build-arg TEST_ONLY_IGNORE_POETRY_LOCKFILE \
  93. -f "docker/Dockerfile" .
  94. echo_if_github "::endgroup::"
  95. # Build the workers docker image (from the base Synapse image we just built).
  96. echo_if_github "::group::Build Docker image: matrixdotorg/synapse-workers"
  97. docker build -t matrixdotorg/synapse-workers -f "docker/Dockerfile-workers" .
  98. echo_if_github "::endgroup::"
  99. # Build the unified Complement image (from the worker Synapse image we just built).
  100. echo_if_github "::group::Build Docker image: complement/Dockerfile"
  101. docker build -t complement-synapse \
  102. -f "docker/complement/Dockerfile" "docker/complement"
  103. echo_if_github "::endgroup::"
  104. fi
  105. if [ -n "$skip_complement_run" ]; then
  106. echo "Skipping Complement run as requested."
  107. exit
  108. fi
  109. export COMPLEMENT_BASE_IMAGE=complement-synapse
  110. extra_test_args=()
  111. test_tags="synapse_blacklist,msc2716,msc3030,msc3787"
  112. # All environment variables starting with PASS_ will be shared.
  113. # (The prefix is stripped off before reaching the container.)
  114. export COMPLEMENT_SHARE_ENV_PREFIX=PASS_
  115. # It takes longer than 10m to run the whole suite.
  116. extra_test_args+=("-timeout=60m")
  117. if [[ -n "$WORKERS" ]]; then
  118. # Use workers.
  119. export PASS_SYNAPSE_COMPLEMENT_USE_WORKERS=true
  120. # Workers can only use Postgres as a database.
  121. export PASS_SYNAPSE_COMPLEMENT_DATABASE=postgres
  122. # And provide some more configuration to complement.
  123. # It can take quite a while to spin up a worker-mode Synapse for the first
  124. # time (the main problem is that we start 14 python processes for each test,
  125. # and complement likes to do two of them in parallel).
  126. export COMPLEMENT_SPAWN_HS_TIMEOUT_SECS=120
  127. else
  128. export PASS_SYNAPSE_COMPLEMENT_USE_WORKERS=
  129. if [[ -n "$POSTGRES" ]]; then
  130. export PASS_SYNAPSE_COMPLEMENT_DATABASE=postgres
  131. else
  132. export PASS_SYNAPSE_COMPLEMENT_DATABASE=sqlite
  133. fi
  134. # We only test faster room joins on monoliths, because they are purposefully
  135. # being developed without worker support to start with.
  136. test_tags="$test_tags,faster_joins"
  137. fi
  138. if [[ -n "$SYNAPSE_TEST_LOG_LEVEL" ]]; then
  139. # Set the log level to what is desired
  140. export PASS_SYNAPSE_LOG_LEVEL="$SYNAPSE_TEST_LOG_LEVEL"
  141. # Allow logging sensitive things (currently SQL queries & parameters).
  142. # (This won't have any effect if we're not logging at DEBUG level overall.)
  143. # Since this is just a test suite, this is fine and won't reveal anyone's
  144. # personal information
  145. export PASS_SYNAPSE_LOG_SENSITIVE=1
  146. fi
  147. # Run the tests!
  148. echo "Images built; running complement"
  149. cd "$COMPLEMENT_DIR"
  150. go test -v -tags $test_tags -count=1 "${extra_test_args[@]}" "$@" ./tests/...