complement.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # A regular expression of test method names can be supplied as the first
  18. # argument to the script. Complement will then only run those tests. If
  19. # no regex is supplied, all tests are run. For example;
  20. #
  21. # ./complement.sh -run "TestOutboundFederation(Profile|Send)"
  22. #
  23. # Exit if a line returns a non-zero exit code
  24. set -e
  25. # Helper to emit annotations that collapse portions of the log in GitHub Actions
  26. echo_if_github() {
  27. if [[ -n "$GITHUB_WORKFLOW" ]]; then
  28. echo $*
  29. fi
  30. }
  31. # enable buildkit for the docker builds
  32. export DOCKER_BUILDKIT=1
  33. # Change to the repository root
  34. cd "$(dirname $0)/.."
  35. # Check for a user-specified Complement checkout
  36. if [[ -z "$COMPLEMENT_DIR" ]]; then
  37. COMPLEMENT_REF=${COMPLEMENT_REF:-main}
  38. echo "COMPLEMENT_DIR not set. Fetching Complement checkout from ${COMPLEMENT_REF}..."
  39. wget -Nq https://github.com/matrix-org/complement/archive/${COMPLEMENT_REF}.tar.gz
  40. tar -xzf ${COMPLEMENT_REF}.tar.gz
  41. COMPLEMENT_DIR=complement-${COMPLEMENT_REF}
  42. echo "Checkout available at 'complement-${COMPLEMENT_REF}'"
  43. fi
  44. # Build the base Synapse image from the local checkout
  45. echo_if_github "::group::Build Docker image: matrixdotorg/synapse"
  46. docker build -t matrixdotorg/synapse -f "docker/Dockerfile" .
  47. echo_if_github "::endgroup::"
  48. # Build the workers docker image (from the base Synapse image we just built).
  49. echo_if_github "::group::Build Docker image: matrixdotorg/synapse-workers"
  50. docker build -t matrixdotorg/synapse-workers -f "docker/Dockerfile-workers" .
  51. echo_if_github "::endgroup::"
  52. # Build the unified Complement image (from the worker Synapse image we just built).
  53. echo_if_github "::group::Build Docker image: complement/Dockerfile"
  54. docker build -t complement-synapse \
  55. -f "docker/complement/Dockerfile" "docker/complement"
  56. echo_if_github "::endgroup::"
  57. export COMPLEMENT_BASE_IMAGE=complement-synapse
  58. extra_test_args=()
  59. test_tags="synapse_blacklist,msc2716,msc3030,msc3787"
  60. # All environment variables starting with PASS_ will be shared.
  61. # (The prefix is stripped off before reaching the container.)
  62. export COMPLEMENT_SHARE_ENV_PREFIX=PASS_
  63. # It takes longer than 10m to run the whole suite.
  64. extra_test_args+=("-timeout=60m")
  65. if [[ -n "$WORKERS" ]]; then
  66. # Use workers.
  67. export PASS_SYNAPSE_COMPLEMENT_USE_WORKERS=true
  68. # Workers can only use Postgres as a database.
  69. export PASS_SYNAPSE_COMPLEMENT_DATABASE=postgres
  70. # And provide some more configuration to complement.
  71. # It can take quite a while to spin up a worker-mode Synapse for the first
  72. # time (the main problem is that we start 14 python processes for each test,
  73. # and complement likes to do two of them in parallel).
  74. export COMPLEMENT_SPAWN_HS_TIMEOUT_SECS=120
  75. else
  76. export PASS_SYNAPSE_COMPLEMENT_USE_WORKERS=
  77. if [[ -n "$POSTGRES" ]]; then
  78. export PASS_SYNAPSE_COMPLEMENT_DATABASE=postgres
  79. else
  80. export PASS_SYNAPSE_COMPLEMENT_DATABASE=sqlite
  81. fi
  82. # We only test faster room joins on monoliths, because they are purposefully
  83. # being developed without worker support to start with.
  84. test_tags="$test_tags,faster_joins"
  85. fi
  86. # Run the tests!
  87. echo "Images built; running complement"
  88. cd "$COMPLEMENT_DIR"
  89. go test -v -tags $test_tags -count=1 "${extra_test_args[@]}" "$@" ./tests/...