1
0

test_generate.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright 2016 OpenMarket Ltd
  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.path
  15. import re
  16. import shutil
  17. import tempfile
  18. from contextlib import redirect_stdout
  19. from io import StringIO
  20. from synapse.config.homeserver import HomeServerConfig
  21. from tests import unittest
  22. class ConfigGenerationTestCase(unittest.TestCase):
  23. def setUp(self) -> None:
  24. self.dir = tempfile.mkdtemp()
  25. self.file = os.path.join(self.dir, "homeserver.yaml")
  26. def tearDown(self) -> None:
  27. shutil.rmtree(self.dir)
  28. def test_generate_config_generates_files(self) -> None:
  29. with redirect_stdout(StringIO()):
  30. HomeServerConfig.load_or_generate_config(
  31. "",
  32. [
  33. "--generate-config",
  34. "-c",
  35. self.file,
  36. "--report-stats=yes",
  37. "-H",
  38. "lemurs.win",
  39. ],
  40. )
  41. self.assertSetEqual(
  42. {"homeserver.yaml", "lemurs.win.log.config", "lemurs.win.signing.key"},
  43. set(os.listdir(self.dir)),
  44. )
  45. self.assert_log_filename_is(
  46. os.path.join(self.dir, "lemurs.win.log.config"),
  47. os.path.join(os.getcwd(), "homeserver.log"),
  48. )
  49. def assert_log_filename_is(self, log_config_file: str, expected: str) -> None:
  50. with open(log_config_file) as f:
  51. config = f.read()
  52. # find the 'filename' line
  53. matches = re.findall(r"^\s*filename:\s*(.*)$", config, re.M)
  54. self.assertEqual(1, len(matches))
  55. self.assertEqual(matches[0], expected)