-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathtest_config.py
More file actions
349 lines (323 loc) · 13.1 KB
/
Copy pathtest_config.py
File metadata and controls
349 lines (323 loc) · 13.1 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright 2009-2014 Justin Riley
#
# This file is part of StarCluster.
#
# StarCluster is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# StarCluster is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with StarCluster. If not, see <http://www.gnu.org/licenses/>.
import os
import copy
import tempfile
import logging
logging.disable(logging.WARN)
from starcluster import exception
from starcluster import tests
from starcluster import static
from starcluster import config
from starcluster import utils
class TestStarClusterConfig(tests.StarClusterTest):
def test_valid_config_template(self):
self.config
def test_config_dne(self):
tmp_file = tempfile.NamedTemporaryFile()
non_existent_file = tmp_file.name
tmp_file.close()
assert not os.path.exists(non_existent_file)
try:
config.StarClusterConfig(non_existent_file, cache=True).load()
except exception.ConfigNotFound:
pass
else:
raise Exception('config loaded non-existent config file %s' %
non_existent_file)
def test_get_cluster(self):
try:
self.config.get_cluster_template('no_such_cluster')
except exception.ClusterTemplateDoesNotExist:
pass
else:
raise Exception('config returned non-existent cluster')
def test_int_required(self):
cases = [{'c1_size': '-s'}, {'c1_size': 2.5}, {'v1_partition': 'asdf'},
{'v1_partition': 0.33}]
for case in cases:
try:
self.get_custom_config(**case)
except exception.ConfigError:
pass
else:
raise Exception('config is not enforcing ints correctly')
def test_bool_required(self):
cases = [{'enable_experimental': 2}]
for case in cases:
try:
self.get_custom_config(**case)
except exception.ConfigError:
pass
else:
raise Exception("config is not enforcing strs correctly")
def test_missing_required(self):
cfg = self.config._config
section_copy = copy.deepcopy(cfg._sections)
for setting in static.CLUSTER_SETTINGS:
if not static.CLUSTER_SETTINGS[setting][1]:
continue
del cfg._sections['cluster c1'][setting]
try:
self.config.load()
except exception.ConfigError:
pass
else:
raise Exception(
"config is not enforcing required setting '%s'" % setting)
cfg._sections = copy.deepcopy(section_copy)
def test_volumes(self):
c1 = self.config.get_cluster_template('c1')
vols = c1.volumes
assert len(vols) == 3
assert 'v1' in vols
v1 = vols['v1']
assert 'volume_id' in v1 and v1['volume_id'] == 'vol-c999999'
assert 'device' in v1 and v1['device'] == '/dev/sdj'
assert 'partition' in v1 and v1['partition'] == '/dev/sdj1'
assert 'mount_path' in v1 and v1['mount_path'] == '/volume1'
assert 'v2' in vols
v2 = vols['v2']
assert 'volume_id' in v2 and v2['volume_id'] == 'vol-c888888'
assert 'device' in v2 and v2['device'] == '/dev/sdk'
assert 'partition' in v2 and v2['partition'] == '/dev/sdk1'
assert 'mount_path' in v2 and v2['mount_path'] == '/volume2'
assert 'v3' in vols
v3 = vols['v3']
assert 'volume_id' in v3 and v3['volume_id'] == 'vol-c777777'
assert 'device' in v3 and v3['device'] == '/dev/sdl'
assert 'partition' in v3 and v3['partition'] == '/dev/sdl1'
assert 'mount_path' in v3 and v3['mount_path'] == '/volume3'
def test_volume_not_defined(self):
try:
self.get_custom_config(**{'c1_vols': 'v1,v2,v2323'})
except exception.ConfigError:
pass
else:
raise Exception(
'config allows non-existent volumes to be specified')
def test_clusters(self):
assert 'c1' in self.config.clusters
assert 'c2' in self.config.clusters
assert 'c3' in self.config.clusters
def test_extends(self):
c1 = self.config.clusters.get('c1')
c2 = self.config.clusters.get('c2')
c3 = self.config.clusters.get('c3')
c2_settings = ['__name__', 'extends', 'keyname', 'key_location',
'cluster_size', 'node_instance_type',
'master_instance_type', 'volumes']
c3_settings = ['__name__', 'extends', 'keyname', 'key_location',
'cluster_size', 'volumes']
for key in c1:
if key in c2 and key not in c2_settings:
assert c2[key] == c1[key]
else:
# below only true for default test config
# not required in general
assert c2[key] != c1[key]
for key in c2:
if key in c3 and key not in c3_settings:
assert c3[key] == c2[key]
else:
# below only true for default test config
# not required in general
assert c3[key] != c2[key]
def test_order_invariance(self):
"""
Loads all cluster sections in the test config in all possible orders
(i.e. c1,c2,c3, c3,c1,c2, etc.) and test that the results are the same
"""
cfg = self.config
orig = cfg.clusters
cfg.clusters = None
sections = cfg._get_sections('cluster')
for perm in utils.permute(sections):
new = cfg._load_cluster_sections(perm)
assert new == orig
def test_plugins(self):
c1 = self.config.get_cluster_template('c1')
plugs = c1.plugins
assert len(plugs) == 4
# test that order is preserved
p1, p2, p3, p4 = plugs
p1_name = p1.__name__
p1_class = utils.get_fq_class_name(p1)
p2_name = p2.__name__
p2_class = utils.get_fq_class_name(p2)
p3_name = p3.__name__
p3_class = utils.get_fq_class_name(p3)
p4_name = p4.__name__
p4_class = utils.get_fq_class_name(p4)
assert p1_name == 'p1'
assert p1_class == 'starcluster.tests.mytestplugin.SetupClass'
assert p1.my_arg == '23'
assert p1.my_other_arg == 'skidoo'
assert p2_name == 'p2'
setup_class2 = 'starcluster.tests.mytestplugin.SetupClass2'
assert p2_class == setup_class2
assert p2.my_arg == 'hello'
assert p2.my_other_arg == 'world'
assert p3_name == 'p3'
setup_class3 = 'starcluster.tests.mytestplugin.SetupClass3'
assert p3_class == setup_class3
assert p3.my_arg == 'bon'
assert p3.my_other_arg == 'jour'
assert p3.my_other_other_arg == 'monsignour'
# Test that p4 inherits from p3
assert p4_name == 'p4'
assert p4_class == setup_class3
assert p4.my_arg == 'bon'
assert p4.my_other_arg == 'jour'
# And test that the p4 argument overides the p3 default
assert p4.my_other_other_arg == 'oui'
def test_plugin_not_defined(self):
try:
self.get_custom_config(**{'c1_plugs': 'p1,p2,p233'})
except exception.ConfigError:
pass
else:
raise Exception(
'config allows non-existent plugins to be specified')
def test_keypairs(self):
kpairs = self.config.keys
assert len(kpairs) == 3
k1 = kpairs.get('k1')
k2 = kpairs.get('k2')
k3 = kpairs.get('k3')
dcfg = tests.templates.config.default_config
k1_location = os.path.expanduser(dcfg['k1_location'])
k2_location = dcfg['k2_location']
k3_location = dcfg['k3_location']
assert k1 and k1['key_location'] == k1_location
assert k2 and k2['key_location'] == k2_location
assert k3 and k3['key_location'] == k3_location
def test_keypair_not_defined(self):
try:
self.get_custom_config(**{'c1_keyname': 'k2323'})
except exception.ConfigError:
pass
else:
raise Exception(
'config allows non-existent keypairs to be specified')
def test_invalid_config(self):
"""
Test that reading a non-INI formatted file raises an exception
"""
tmp_file = tempfile.NamedTemporaryFile()
tmp_file.write(
"<html>random garbage file with no section headings</html>")
tmp_file.flush()
try:
config.StarClusterConfig(tmp_file.name, cache=True).load()
except exception.ConfigHasNoSections:
pass
else:
raise Exception("config allows non-INI formatted files")
def test_empty_config(self):
"""
Test that reading an empty config generates no errors and that aws
credentials can be read from the environment.
"""
aws_key = 'testkey'
aws_secret_key = 'testsecret'
os.environ['AWS_ACCESS_KEY_ID'] = aws_key
os.environ['AWS_SECRET_ACCESS_KEY'] = aws_secret_key
tmp_file = tempfile.NamedTemporaryFile()
cfg = config.StarClusterConfig(tmp_file.name, cache=True).load()
assert cfg.aws['aws_access_key_id'] == aws_key
assert cfg.aws['aws_secret_access_key'] == aws_secret_key
del os.environ['AWS_ACCESS_KEY_ID']
del os.environ['AWS_SECRET_ACCESS_KEY']
def test_cyclical_extends(self):
"""
Test that cyclical extends in the config raises an exception
"""
try:
self.get_custom_config(**{'c2_extends': 'c3',
'c3_extends': 'c2'})
self.get_custom_config(**{'c2_extends': 'c3',
'c3_extends': 'c4',
'c4_extends': 'c2'})
except exception.ConfigError:
pass
else:
raise Exception('config allows cyclical extends graph')
def test_choices(self):
"""
Test that config enforces a value to be one of a list of choices if
specified
"""
try:
self.get_custom_config(**{'c1_shell': 'blahblah'})
except exception.ConfigError:
pass
else:
raise Exception('config not enforcing choices for setting')
def test_multiple_instance_types(self):
"""
Test that config properly handles multiple instance types syntax
(within node_instance_type setting)
"""
invalid_cases = [
{'c1_node_type': 'c1.xlarge:ami-asdffdas'},
{'c1_node_type': 'c1.xlarge:3'},
{'c1_node_type': 'c1.xlarge:ami-asdffdas:3'},
{'c1_node_type': 'c1.xlarge:asdf:asdf:asdf,m1.small'},
{'c1_node_type': 'c1.asdf:4, m1.small'},
{'c1_node_type': 'c1.xlarge: 0, m1.small'},
{'c1_node_type': 'c1.xlarge:-1, m1.small'}]
for case in invalid_cases:
try:
self.get_custom_config(**case)
except exception.ConfigError:
pass
else:
raise Exception(('config allows invalid multiple instance ' +
'type syntax: %s') % case)
valid_cases = [
{'c1_node_type': 'c1.xlarge:3, m1.small'},
{'c1_node_type': 'c1.xlarge:ami-asdfasdf:3, m1.small'},
{'c1_node_type': 'c1.xlarge:ami-asdfasdf:3, m1.large, m1.small'},
{'c1_node_type': 'm1.large, c1.xlarge:ami-asdfasdf:3, m1.large, ' +
'm1.small'},
{'c1_node_type': 'c1.xlarge:ami-asdfasdf:2, m1.large:2, m1.small'},
]
for case in valid_cases:
try:
self.get_custom_config(**case)
except exception.ConfigError:
raise Exception(('config rejects valid multiple instance ' +
'type syntax: %s') % case)
def test_inline_comments(self):
"""
Test that config ignores inline comments.
"""
invalid_case = {'c1_node_type': 'c1.xlarge:3, m1.small# some comment'}
try:
self.get_custom_config(**invalid_case)
except exception.ConfigError:
pass
else:
raise Exception(('config incorrectly ignores line with non-inline '
'comment pound sign: %s') % invalid_case)
valid_case = {'c1_node_type': 'c1.xlarge:3, m1.small # some #comment '}
try:
self.get_custom_config(**valid_case)
except exception.ConfigError:
raise Exception(('config does not ignore inline '
'comment: %s') % valid_case)