launcher.py 2.9 KB

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