Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f5952d2

Browse files
committed
Add tests for synapse.config.__main__
Few basic tests to parse a config file when no arguments and config key read success/fail. Refactor test_load.py a bit to provide a common test case class for the config tests using a generated config file to parse. Signed-off-by: Jason Robinson <[email protected]>
1 parent d53d65e commit f5952d2

File tree

4 files changed

+117
-54
lines changed

4 files changed

+117
-54
lines changed

synapse/config/__main__.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
from synapse.config._base import ConfigError
15+
import sys
1616

17-
if __name__ == "__main__":
18-
import sys
17+
from synapse.config._base import ConfigError
18+
from synapse.config.homeserver import HomeServerConfig
1919

20-
from synapse.config.homeserver import HomeServerConfig
2120

22-
action = sys.argv[1] if len(sys.argv) > 1 and sys.argv[1] == "read" else None
23-
# If we're reading a key in the config file, then `sys.argv[1]` will be `read` and `sys.argv[2]`
21+
def main(args):
22+
action = args[1] if len(args) > 1 and args[1] == "read" else None
23+
# If we're reading a key in the config file, then `args[1]` will be `read` and `args[2]`
2424
# will be the key to read.
2525
# We'll want to rework this code if we want to support more actions than just `read`.
26-
load_config_args = sys.argv[3:] if action else sys.argv[1:]
26+
load_config_args = args[3:] if action else args[1:]
2727

2828
try:
2929
config = HomeServerConfig.load_config("", load_config_args)
@@ -34,7 +34,7 @@
3434
print("Config parses OK!")
3535

3636
if action == "read":
37-
key = sys.argv[2]
37+
key = args[2]
3838
key_parts = key.split(".")
3939

4040
value = config
@@ -44,9 +44,12 @@
4444
key_parts.pop(0)
4545

4646
print(f"\n{key}: {value}")
47-
sys.exit(0)
4847
except AttributeError:
4948
print(
5049
f"\nNo '{key}' key could be found in the provided configuration file."
5150
)
5251
sys.exit(1)
52+
53+
54+
if __name__ == "__main__":
55+
main(sys.argv)

tests/config/test___main__.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 The Matrix.org Foundation C.I.C.
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+
from synapse.config.__main__ import main
15+
16+
from tests.config.utils import ConfigFileTestCase
17+
18+
19+
class ConfigMainFileTestCase(ConfigFileTestCase):
20+
def test_executes_without_an_action(self):
21+
self.generate_config()
22+
main(["", "-c", self.config_file])
23+
24+
def test_read__error_if_key_not_found(self):
25+
self.generate_config()
26+
with self.assertRaises(SystemExit):
27+
main(["", "read", "foo.bar.hello", "-c", self.config_file])
28+
29+
def test_read__passes_if_key_found(self):
30+
self.generate_config()
31+
main(["", "read", "server.server_name", "-c", self.config_file])

tests/config/test_load.py

+16-45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright 2016 OpenMarket Ltd
2+
# Copyright 2021 The Matrix.org Foundation C.I.C.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -14,40 +15,38 @@
1415
import os.path
1516
import shutil
1617
import tempfile
17-
from contextlib import redirect_stdout
18-
from io import StringIO
1918

2019
import yaml
2120

2221
from synapse.config import ConfigError
2322
from synapse.config.homeserver import HomeServerConfig
2423

25-
from tests import unittest
24+
from tests.config.utils import ConfigFileTestCase
2625

2726

28-
class ConfigLoadingTestCase(unittest.TestCase):
27+
class ConfigLoadingFileTestCase(ConfigFileTestCase):
2928
def setUp(self):
3029
self.dir = tempfile.mkdtemp()
31-
self.file = os.path.join(self.dir, "homeserver.yaml")
30+
self.config_file = os.path.join(self.dir, "homeserver.yaml")
3231

3332
def tearDown(self):
3433
shutil.rmtree(self.dir)
3534

3635
def test_load_fails_if_server_name_missing(self):
3736
self.generate_config_and_remove_lines_containing("server_name")
3837
with self.assertRaises(ConfigError):
39-
HomeServerConfig.load_config("", ["-c", self.file])
38+
HomeServerConfig.load_config("", ["-c", self.config_file])
4039
with self.assertRaises(ConfigError):
41-
HomeServerConfig.load_or_generate_config("", ["-c", self.file])
40+
HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
4241

4342
def test_generates_and_loads_macaroon_secret_key(self):
4443
self.generate_config()
4544

46-
with open(self.file) as f:
45+
with open(self.config_file) as f:
4746
raw = yaml.safe_load(f)
4847
self.assertIn("macaroon_secret_key", raw)
4948

50-
config = HomeServerConfig.load_config("", ["-c", self.file])
49+
config = HomeServerConfig.load_config("", ["-c", self.config_file])
5150
self.assertTrue(
5251
hasattr(config.key, "macaroon_secret_key"),
5352
"Want config to have attr macaroon_secret_key",
@@ -58,7 +57,7 @@ def test_generates_and_loads_macaroon_secret_key(self):
5857
"was: %r" % (config.key.macaroon_secret_key,)
5958
)
6059

61-
config = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
60+
config = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
6261
self.assertTrue(
6362
hasattr(config.key, "macaroon_secret_key"),
6463
"Want config to have attr macaroon_secret_key",
@@ -71,9 +70,9 @@ def test_generates_and_loads_macaroon_secret_key(self):
7170

7271
def test_load_succeeds_if_macaroon_secret_key_missing(self):
7372
self.generate_config_and_remove_lines_containing("macaroon")
74-
config1 = HomeServerConfig.load_config("", ["-c", self.file])
75-
config2 = HomeServerConfig.load_config("", ["-c", self.file])
76-
config3 = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
73+
config1 = HomeServerConfig.load_config("", ["-c", self.config_file])
74+
config2 = HomeServerConfig.load_config("", ["-c", self.config_file])
75+
config3 = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
7776
self.assertEqual(
7877
config1.key.macaroon_secret_key, config2.key.macaroon_secret_key
7978
)
@@ -87,15 +86,15 @@ def test_disable_registration(self):
8786
["enable_registration: true", "disable_registration: true"]
8887
)
8988
# Check that disable_registration clobbers enable_registration.
90-
config = HomeServerConfig.load_config("", ["-c", self.file])
89+
config = HomeServerConfig.load_config("", ["-c", self.config_file])
9190
self.assertFalse(config.registration.enable_registration)
9291

93-
config = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
92+
config = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
9493
self.assertFalse(config.registration.enable_registration)
9594

9695
# Check that either config value is clobbered by the command line.
9796
config = HomeServerConfig.load_or_generate_config(
98-
"", ["-c", self.file, "--enable-registration"]
97+
"", ["-c", self.config_file, "--enable-registration"]
9998
)
10099
self.assertTrue(config.registration.enable_registration)
101100

@@ -104,33 +103,5 @@ def test_stats_enabled(self):
104103
self.add_lines_to_config(["enable_metrics: true"])
105104

106105
# The default Metrics Flags are off by default.
107-
config = HomeServerConfig.load_config("", ["-c", self.file])
106+
config = HomeServerConfig.load_config("", ["-c", self.config_file])
108107
self.assertFalse(config.metrics.metrics_flags.known_servers)
109-
110-
def generate_config(self):
111-
with redirect_stdout(StringIO()):
112-
HomeServerConfig.load_or_generate_config(
113-
"",
114-
[
115-
"--generate-config",
116-
"-c",
117-
self.file,
118-
"--report-stats=yes",
119-
"-H",
120-
"lemurs.win",
121-
],
122-
)
123-
124-
def generate_config_and_remove_lines_containing(self, needle):
125-
self.generate_config()
126-
127-
with open(self.file) as f:
128-
contents = f.readlines()
129-
contents = [line for line in contents if needle not in line]
130-
with open(self.file, "w") as f:
131-
f.write("".join(contents))
132-
133-
def add_lines_to_config(self, lines):
134-
with open(self.file, "a") as f:
135-
for line in lines:
136-
f.write(line + "\n")

tests/config/utils.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2021 The Matrix.org Foundation C.I.C.
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
15+
import shutil
16+
import tempfile
17+
import unittest
18+
from contextlib import redirect_stdout
19+
from io import StringIO
20+
21+
from synapse.config.homeserver import HomeServerConfig
22+
23+
24+
class ConfigFileTestCase(unittest.TestCase):
25+
def setUp(self):
26+
self.dir = tempfile.mkdtemp()
27+
self.config_file = os.path.join(self.dir, "homeserver.yaml")
28+
29+
def tearDown(self):
30+
shutil.rmtree(self.dir)
31+
32+
def generate_config(self):
33+
with redirect_stdout(StringIO()):
34+
HomeServerConfig.load_or_generate_config(
35+
"",
36+
[
37+
"--generate-config",
38+
"-c",
39+
self.config_file,
40+
"--report-stats=yes",
41+
"-H",
42+
"lemurs.win",
43+
],
44+
)
45+
46+
def generate_config_and_remove_lines_containing(self, needle):
47+
self.generate_config()
48+
49+
with open(self.config_file) as f:
50+
contents = f.readlines()
51+
contents = [line for line in contents if needle not in line]
52+
with open(self.config_file, "w") as f:
53+
f.write("".join(contents))
54+
55+
def add_lines_to_config(self, lines):
56+
with open(self.config_file, "a") as f:
57+
for line in lines:
58+
f.write(line + "\n")

0 commit comments

Comments
 (0)