-
Notifications
You must be signed in to change notification settings - Fork 36
/
test_config.py
283 lines (242 loc) · 9.89 KB
/
test_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from __future__ import unicode_literals
import mock
import os
import sys
from tempfile import NamedTemporaryFile
import pexpect
import pytest
import six
from dallinger.config import Configuration
from dallinger.config import get_config, LOCAL_CONFIG
@pytest.mark.usefixtures("experiment_dir")
class TestConfiguration(object):
def test_register_new_variable(self):
config = Configuration()
config.register("num_participants", int)
config.extend({"num_participants": 1})
config.ready = True
assert config.get("num_participants", 1)
def test_register_duplicate_variable_raises(self):
config = Configuration()
config.register("num_participants", int)
with pytest.raises(KeyError):
config.register("num_participants", int)
def test_register_unknown_type_raises(self):
config = Configuration()
with pytest.raises(TypeError):
config.register("num_participants", object)
def test_type_mismatch_on_assignment(self):
config = get_config()
with pytest.raises(TypeError):
config["base_payment"] = 12
def test_type_mismatch_on_extend(self):
config = Configuration()
config.register("num_participants", int)
with pytest.raises(TypeError):
config.extend({"num_participants": 1.0})
def test_type_mismatch_with_cast_types(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
config.extend({"num_participants": 1.0}, cast_types=True)
assert config.get("num_participants", 1) == 1
def test_type_cast_types_failure_raises(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
with pytest.raises(TypeError):
config.extend({"num_participants": "A NUMBER"}, cast_types=True)
def test_get_before_ready_is_not_possible(self):
config = Configuration()
config.register("num_participants", int)
config.extend({"num_participants": 1})
with pytest.raises(RuntimeError):
config.get("num_participants", 1)
def test_layering_of_configs(self):
config = Configuration()
config.register("num_participants", int)
config.extend({"num_participants": 1})
config.ready = True
assert config.get("num_participants", 1) == 1
config.extend({"num_participants": 2})
assert config.get("num_participants", 1) == 2
def test_setting_unknown_key_is_ignored(self):
config = Configuration()
config.ready = True
config.extend({"num_participants": 1})
config.get("num_participants", None)
def test_setting_by_set(self):
config = Configuration()
config.ready = True
config.set("mode", "live")
def test_setting_by_assignment(self):
config = Configuration()
config.ready = True
config["mode"] = "live"
def test_get_without_default_raises(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
with pytest.raises(KeyError):
config.get("num_participants")
def test_get_has_default_value(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
assert config.get("num_participants", 10) == 10
def test_get_strips_strings(self):
config = Configuration()
config.register("test_string", six.text_type)
config.ready = True
config.extend({"test_string": " something "})
assert config.get("test_string") == "something"
def test_dict_access(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
config.extend({"num_participants": 1})
assert config["num_participants"] == 1
def test_attribute_access(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
config.extend({"num_participants": 1})
assert config.num_participants == 1
def test_attribute_setting(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
config.num_participants = 1
assert config.num_participants == 1
def test_strict_extending_blocks_unknown_keys(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
with pytest.raises(KeyError):
config.extend({"unknown_key": 1}, strict=True)
def test_setting_values_supports_synonyms(self):
config = Configuration()
config.register("num_participants", int, synonyms={"n"})
config.ready = True
config.extend({"n": 1})
assert config.get("num_participants") == 1
def test_loading_keys_from_config_file(self):
config = Configuration()
config.register("mode", six.text_type)
config.register("num_participants", int, synonyms={"n"})
config.register("deploy_worldwide", bool, synonyms={"worldwide"})
mode_with_trailing_whitespace = "live "
contents = """
[Example Section]
mode = {}
num_participants = 10
worldwide = false
""".format(
mode_with_trailing_whitespace
)
with NamedTemporaryFile() as configfile:
configfile.write(contents.encode("utf-8"))
configfile.flush()
config.load_from_file(configfile.name)
config.ready = True
assert config.get("mode") == "live" # whitespace stripped
assert config.get("num_participants") == 10
assert config.get("deploy_worldwide") is False
def test_loading_keys_from_environment_variables(self):
config = Configuration()
config.register("num_participants", int, synonyms={"n"})
os.environ["num_participants"] = "1"
try:
config.load_from_environment()
finally:
del os.environ["num_participants"]
config.ready = True
assert config.get("num_participants") == 1
@pytest.mark.slow
def test_experiment_defined_parameters(self):
try:
python = pexpect.spawn("python", encoding="utf-8")
python.read_nonblocking(10000)
python.setecho(False)
python.sendline("from dallinger.experiment_server import experiment_server")
python.sendline("config = experiment_server._config()")
python.sendline("print(config.types)")
if six.PY3:
python.expect_exact("custom_parameter': <class 'int'>")
else:
python.expect_exact("custom_parameter': <type 'int'>")
finally:
python.sendcontrol("d")
python.read()
def test_reload_config(self):
# replicate the experiment API runner config loading
config = get_config()
config.register_extra_parameters()
config.load_from_file(LOCAL_CONFIG)
# Failse with _reset()
config.clear()
config.register_extra_parameters()
config.load_from_file(LOCAL_CONFIG)
def test_custom_experiment_module_set_and_retained(self):
config = get_config()
with mock.patch.dict("sys.modules", dallinger_experiment=None):
config.register_extra_parameters()
assert sys.modules["dallinger_experiment"] is not None
exp_module = mock.Mock()
with mock.patch.dict("sys.modules", dallinger_experiment=exp_module):
config.clear()
config.register_extra_parameters()
assert sys.modules["dallinger_experiment"] is exp_module
def test_local_base_url(self):
from dallinger.utils import get_base_url
config = get_config()
config.ready = True
config.set("host", "localhost")
config.set("base_port", 5000)
config.set("num_dynos_web", 1)
assert get_base_url() == "http://localhost:5000"
def test_remote_base_url(self):
from dallinger.utils import get_base_url
config = get_config()
config.ready = True
config.set("host", "https://dlgr-bogus.herokuapp.com")
config.set("num_dynos_web", 1)
assert get_base_url() == "https://dlgr-bogus.herokuapp.com"
def test_remote_base_url_always_ssl(self):
from dallinger.utils import get_base_url
config = get_config()
config.ready = True
config.set("host", "http://dlgr-bogus.herokuapp.com")
config.set("num_dynos_web", 1)
assert get_base_url() == "https://dlgr-bogus.herokuapp.com"
def test_write_omits_sensitive_keys_if_filter_sensitive(self, in_tempdir):
config = get_config()
config.set("aws_region", "some region")
config.set("aws_secret_access_key", "foo")
config.ready = True
config.write(filter_sensitive=True)
with open(LOCAL_CONFIG) as txt:
contents = txt.read()
assert "aws_region" in contents
assert "aws_secret_access_key" not in contents
def test_write_includes_all_keys_if_filter_sensitive_false(self, in_tempdir):
config = get_config()
config.set("aws_region", "some region")
config.set("aws_secret_access_key", "foo")
config.ready = True
config.write(filter_sensitive=False)
with open(LOCAL_CONFIG) as txt:
contents = txt.read()
assert "aws_region" in contents
assert "aws_secret_access_key" in contents
def test_write_accepts_alternate_directory(self):
import tempfile
target = os.path.join(tempfile.mkdtemp(), "custom")
os.mkdir(target)
config = get_config()
config.set("aws_region", "some region")
config.ready = True
config.write(directory=target)
with open(os.path.join(target, LOCAL_CONFIG)) as txt:
contents = txt.read()
assert "aws_region" in contents