launcher.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. verify_response_template = {testsubject_path}/res/verify_response_template
  26. federation.verifycerts = False
  27. [db]
  28. db.file = :memory:
  29. [general]
  30. server.name = test.local
  31. terms.path = {terms_path}
  32. [email]
  33. email.tlsmode = 0
  34. email.template = {testsubject_path}/res/verification_template.eml
  35. email.invite.subject = %(sender_display_name)s has invited you to chat
  36. email.smtphost = localhost
  37. email.from = Sydent Validation <noreply@localhost>
  38. email.smtpport = 9925
  39. email.subject = Your Validation Token
  40. email.invite_template = {testsubject_path}/res/invite_template.eml
  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