launcher.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import shutil
  16. import tempfile
  17. import time
  18. from subprocess import Popen
  19. CFG_TEMPLATE = """
  20. [http]
  21. clientapi.http.bind_address = localhost
  22. clientapi.http.port = {port}
  23. client_http_base = http://localhost:{port}
  24. federation.verifycerts = False
  25. [db]
  26. db.file = :memory:
  27. [general]
  28. server.name = test.local
  29. terms.path = {terms_path}
  30. templates.path = {testsubject_path}/res
  31. brand.default = is-test
  32. ip.whitelist = 127.0.0.1
  33. [email]
  34. email.tlsmode = 0
  35. email.invite.subject = %(sender_display_name)s has invited you to chat
  36. email.invite.subject_space = %(sender_display_name)s has invited you to a space
  37. email.smtphost = localhost
  38. email.from = Sydent Validation <noreply@localhost>
  39. email.smtpport = 9925
  40. email.subject = Your Validation Token
  41. email.ratelimit_sender.burst = 100000
  42. email.ratelimit_sender.rate_hz = 100000
  43. """
  44. class MatrixIsTestLauncher:
  45. def __init__(self, with_terms):
  46. self.with_terms = with_terms
  47. def launch(self):
  48. sydent_path = os.path.abspath(
  49. os.path.join(
  50. os.path.dirname(__file__),
  51. "..",
  52. )
  53. )
  54. testsubject_path = os.path.join(
  55. sydent_path,
  56. "matrix_is_test",
  57. )
  58. terms_path = (
  59. os.path.join(testsubject_path, "terms.yaml") if self.with_terms else ""
  60. )
  61. port = 8099 if self.with_terms else 8098
  62. self.tmpdir = tempfile.mkdtemp(prefix="sydenttest")
  63. with open(os.path.join(self.tmpdir, "sydent.conf"), "w") as cfgfp:
  64. cfgfp.write(
  65. CFG_TEMPLATE.format(
  66. testsubject_path=testsubject_path,
  67. terms_path=terms_path,
  68. port=port,
  69. )
  70. )
  71. newEnv = os.environ.copy()
  72. newEnv.update(
  73. {
  74. "PYTHONPATH": sydent_path,
  75. }
  76. )
  77. stderr_fp = open(os.path.join(testsubject_path, "sydent.stderr"), "w")
  78. pybin = os.getenv("SYDENT_PYTHON", "python")
  79. self.process = Popen(
  80. args=[pybin, "-m", "sydent.sydent"],
  81. cwd=self.tmpdir,
  82. env=newEnv,
  83. stderr=stderr_fp,
  84. )
  85. # XXX: wait for startup in a sensible way
  86. time.sleep(2)
  87. self._baseUrl = "http://localhost:%d" % (port,)
  88. def tearDown(self):
  89. print("Stopping sydent...")
  90. self.process.terminate()
  91. shutil.rmtree(self.tmpdir)
  92. def get_base_url(self):
  93. return self._baseUrl