1
1
# Copyright 2016 OpenMarket Ltd
2
+ # Copyright 2021 The Matrix.org Foundation C.I.C.
2
3
#
3
4
# Licensed under the Apache License, Version 2.0 (the "License");
4
5
# you may not use this file except in compliance with the License.
14
15
import os .path
15
16
import shutil
16
17
import tempfile
17
- from contextlib import redirect_stdout
18
- from io import StringIO
19
18
20
19
import yaml
21
20
22
21
from synapse .config import ConfigError
23
22
from synapse .config .homeserver import HomeServerConfig
24
23
25
- from tests import unittest
24
+ from tests . config . utils import ConfigFileTestCase
26
25
27
26
28
- class ConfigLoadingTestCase ( unittest . TestCase ):
27
+ class ConfigLoadingFileTestCase ( ConfigFileTestCase ):
29
28
def setUp (self ):
30
29
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" )
32
31
33
32
def tearDown (self ):
34
33
shutil .rmtree (self .dir )
35
34
36
35
def test_load_fails_if_server_name_missing (self ):
37
36
self .generate_config_and_remove_lines_containing ("server_name" )
38
37
with self .assertRaises (ConfigError ):
39
- HomeServerConfig .load_config ("" , ["-c" , self .file ])
38
+ HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
40
39
with self .assertRaises (ConfigError ):
41
- HomeServerConfig .load_or_generate_config ("" , ["-c" , self .file ])
40
+ HomeServerConfig .load_or_generate_config ("" , ["-c" , self .config_file ])
42
41
43
42
def test_generates_and_loads_macaroon_secret_key (self ):
44
43
self .generate_config ()
45
44
46
- with open (self .file ) as f :
45
+ with open (self .config_file ) as f :
47
46
raw = yaml .safe_load (f )
48
47
self .assertIn ("macaroon_secret_key" , raw )
49
48
50
- config = HomeServerConfig .load_config ("" , ["-c" , self .file ])
49
+ config = HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
51
50
self .assertTrue (
52
51
hasattr (config .key , "macaroon_secret_key" ),
53
52
"Want config to have attr macaroon_secret_key" ,
@@ -58,7 +57,7 @@ def test_generates_and_loads_macaroon_secret_key(self):
58
57
"was: %r" % (config .key .macaroon_secret_key ,)
59
58
)
60
59
61
- config = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .file ])
60
+ config = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .config_file ])
62
61
self .assertTrue (
63
62
hasattr (config .key , "macaroon_secret_key" ),
64
63
"Want config to have attr macaroon_secret_key" ,
@@ -71,9 +70,9 @@ def test_generates_and_loads_macaroon_secret_key(self):
71
70
72
71
def test_load_succeeds_if_macaroon_secret_key_missing (self ):
73
72
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 ])
77
76
self .assertEqual (
78
77
config1 .key .macaroon_secret_key , config2 .key .macaroon_secret_key
79
78
)
@@ -87,15 +86,15 @@ def test_disable_registration(self):
87
86
["enable_registration: true" , "disable_registration: true" ]
88
87
)
89
88
# Check that disable_registration clobbers enable_registration.
90
- config = HomeServerConfig .load_config ("" , ["-c" , self .file ])
89
+ config = HomeServerConfig .load_config ("" , ["-c" , self .config_file ])
91
90
self .assertFalse (config .registration .enable_registration )
92
91
93
- config = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .file ])
92
+ config = HomeServerConfig .load_or_generate_config ("" , ["-c" , self .config_file ])
94
93
self .assertFalse (config .registration .enable_registration )
95
94
96
95
# Check that either config value is clobbered by the command line.
97
96
config = HomeServerConfig .load_or_generate_config (
98
- "" , ["-c" , self .file , "--enable-registration" ]
97
+ "" , ["-c" , self .config_file , "--enable-registration" ]
99
98
)
100
99
self .assertTrue (config .registration .enable_registration )
101
100
@@ -104,33 +103,5 @@ def test_stats_enabled(self):
104
103
self .add_lines_to_config (["enable_metrics: true" ])
105
104
106
105
# 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 ])
108
107
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 " )
0 commit comments