test_generate.py 2.1 KB

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