launcher.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. templates.path = {testsubject_path}/res
  32. brand.default = is-test
  33. ip.whitelist = 127.0.0.1
  34. [email]
  35. email.tlsmode = 0
  36. email.invite.subject = %(sender_display_name)s has invited you to chat
  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(object):
  43. def __init__(self, with_terms):
  44. self.with_terms = with_terms
  45. def launch(self):
  46. sydent_path = os.path.abspath(os.path.join(
  47. os.path.dirname(__file__), '..',
  48. ))
  49. testsubject_path = os.path.join(
  50. sydent_path, 'matrix_is_test',
  51. )
  52. terms_path = os.path.join(testsubject_path, 'terms.yaml') if self.with_terms else ''
  53. port = 8099 if self.with_terms else 8098
  54. self.tmpdir = tempfile.mkdtemp(prefix='sydenttest')
  55. with open(os.path.join(self.tmpdir, 'sydent.conf'), 'w') as cfgfp:
  56. cfgfp.write(CFG_TEMPLATE.format(
  57. testsubject_path=testsubject_path,
  58. terms_path=terms_path,
  59. port=port,
  60. ))
  61. newEnv = os.environ.copy()
  62. newEnv.update({
  63. 'PYTHONPATH': sydent_path,
  64. })
  65. stderr_fp = open(os.path.join(testsubject_path, 'sydent.stderr'), 'w')
  66. pybin = os.getenv('SYDENT_PYTHON', 'python')
  67. self.process = Popen(
  68. args=[pybin, '-m', 'sydent.sydent'],
  69. cwd=self.tmpdir,
  70. env=newEnv,
  71. stderr=stderr_fp,
  72. )
  73. # XXX: wait for startup in a sensible way
  74. time.sleep(2)
  75. self._baseUrl = 'http://localhost:%d' % (port,)
  76. def tearDown(self):
  77. print("Stopping sydent...")
  78. self.process.terminate()
  79. shutil.rmtree(self.tmpdir)
  80. def get_base_url(self):
  81. return self._baseUrl