launcher.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. """
  42. class MatrixIsTestLauncher:
  43. def __init__(self, with_terms):
  44. self.with_terms = with_terms
  45. def launch(self):
  46. sydent_path = os.path.abspath(
  47. os.path.join(
  48. os.path.dirname(__file__),
  49. "..",
  50. )
  51. )
  52. testsubject_path = os.path.join(
  53. sydent_path,
  54. "matrix_is_test",
  55. )
  56. terms_path = (
  57. os.path.join(testsubject_path, "terms.yaml") if self.with_terms else ""
  58. )
  59. port = 8099 if self.with_terms else 8098
  60. self.tmpdir = tempfile.mkdtemp(prefix="sydenttest")
  61. with open(os.path.join(self.tmpdir, "sydent.conf"), "w") as cfgfp:
  62. cfgfp.write(
  63. CFG_TEMPLATE.format(
  64. testsubject_path=testsubject_path,
  65. terms_path=terms_path,
  66. port=port,
  67. )
  68. )
  69. newEnv = os.environ.copy()
  70. newEnv.update(
  71. {
  72. "PYTHONPATH": sydent_path,
  73. }
  74. )
  75. stderr_fp = open(os.path.join(testsubject_path, "sydent.stderr"), "w")
  76. pybin = os.getenv("SYDENT_PYTHON", "python")
  77. self.process = Popen(
  78. args=[pybin, "-m", "sydent.sydent"],
  79. cwd=self.tmpdir,
  80. env=newEnv,
  81. stderr=stderr_fp,
  82. )
  83. # XXX: wait for startup in a sensible way
  84. time.sleep(2)
  85. self._baseUrl = "http://localhost:%d" % (port,)
  86. def tearDown(self):
  87. print("Stopping sydent...")
  88. self.process.terminate()
  89. shutil.rmtree(self.tmpdir)
  90. def get_base_url(self):
  91. return self._baseUrl