15
15
from unittest import mock
18
test_manifests_for_includes = [{
19
'name': "An include at the end of a file",
20
'expected_phases': ['collect', 'repo', 'deploy', 'script', 'verify'],
25
deploy config=services
26
include config=other-manifest
29
script config=post-deploy
34
'name': "An include in the middle of a file",
35
'expected_phases': ['collect', 'repo', 'deploy', 'script', 'verify'],
40
deploy config=services
41
include config=other-manifest
45
script config=post-deploy
49
'name': "Nested include files",
53
'deploy', # deploy-manifest
54
'verify', # verify-manifest
57
'deploy', # deploy-manifest
18
test_manifests_for_includes = [
20
"name": "An include at the end of a file",
21
"expected_phases": ["collect", "repo", "deploy", "script", "verify"],
26
deploy config=services
27
include config=other-manifest
30
script config=post-deploy
36
"name": "An include in the middle of a file",
37
"expected_phases": ["collect", "repo", "deploy", "script", "verify"],
42
deploy config=services
43
include config=other-manifest
47
script config=post-deploy
52
"name": "Nested include files",
56
"deploy", # deploy-manifest
57
"verify", # verify-manifest
60
"deploy", # deploy-manifest
64
67
include config=deploy-manifest
67
'deploy-manifest': """
70
"deploy-manifest": """
68
71
deploy config=services
69
72
include config=verify-manifest
70
73
deploy config=somethingelse
72
'verify-manifest': """
75
"verify-manifest": """
74
77
script config=other-test
75
78
verify config=verify-nagios
79
'name': "Nested include files in subdir",
83
'deploy', # deploy-manifest
84
'verify', # verify-manifest
87
'deploy', # deploy-manifest
83
"name": "Nested include files in subdir",
87
"deploy", # deploy-manifest
88
"verify", # verify-manifest
91
"deploy", # deploy-manifest
94
98
include config=manifests/deploy-manifest
97
'manifests/deploy-manifest': """
101
"manifests/deploy-manifest": """
98
102
deploy config=services
99
103
include config=manifests/verify-manifest
100
104
deploy config=somethingelse
102
'manifests/verify-manifest': """
106
"manifests/verify-manifest": """
104
108
script config=other-test
105
109
verify config=verify-nagios
110
test_manifests_for_missing_include = [{
111
'name': "Include file doesn't exist",
115
test_manifests_for_missing_include = [
117
"name": "Include file doesn't exist",
116
122
deploy config=services
117
123
include config=non-existent-manifest
119
'other-manifest': """
125
"other-manifest": """
120
126
script config=post-deploy
126
test_manifests_for_invalid_phase = [{
127
'name': "Invalid phase name",
133
test_manifests_for_invalid_phase = [
135
"name": "Invalid phase name",
132
140
deploy config=services
133
141
include config=other-manifest
135
'other-manifest': """
143
"other-manifest": """
136
144
script config=post-deploy
142
test_manifests_for_invalid_arg = [{
143
'name': "Invalid phase argument",
151
test_manifests_for_invalid_arg = [
153
"name": "Invalid phase argument",
147
157
deploy notavalidarg
152
test_manifests_for_multiple_args = [{
153
'name': "Multiple phase arguments",
164
test_manifests_for_multiple_args = [
166
"name": "Multiple phase arguments",
157
170
deploy config=foo config=bar
162
test_manifests_for_comment_and_include_ordering = [{
163
'name': "Comment ordering w/r/t includes",
164
'expected_phases': ['collect', 'comment', 'deploy', 'comment', 'verify'],
177
test_manifests_for_comment_and_include_ordering = [
179
"name": "Comment ordering w/r/t includes",
180
"expected_phases": ["collect", "comment", "deploy", "comment", "verify"],
168
184
# Deploy the environment.
169
185
include config=manifest-deploy
170
186
# Verify the environment.
171
187
include config=manifest-verify
173
'manifest-deploy': """
189
"manifest-deploy": """
176
'manifest-verify': """
192
"manifest-verify": """
183
200
class ManifestTestCase(TestCase, MojoTestCaseMixin):
185
201
def test_include_sets_correct_phases(self):
186
202
for example in test_manifests_for_includes:
187
spec = self.create_spec(manifests=example['manifests'])
203
spec = self.create_spec(manifests=example["manifests"])
189
manifest = Manifest(spec=spec, manifest_file='manifest',
205
manifest = Manifest(spec=spec, manifest_file="manifest", stage="devel")
192
207
actual_phases = [phase.name for phase in manifest.phases]
193
208
self.assertEqual(
194
example['expected_phases'], actual_phases,
209
example["expected_phases"],
195
211
"The example '{}' expected the phases:\n{}\n "
196
"but had\n{}\ninstead.".format(
197
example['name'], example['expected_phases'],
212
"but had\n{}\ninstead.".format(example["name"], example["expected_phases"], actual_phases),
200
215
def test_failing_manifests_missing_include(self):
201
216
for example in test_manifests_for_missing_include:
202
spec = self.create_spec(manifests=example['manifests'])
217
spec = self.create_spec(manifests=example["manifests"])
204
219
with self.assertRaises(ConfigNotFoundException):
205
Manifest(spec=spec, manifest_file='manifest',
220
Manifest(spec=spec, manifest_file="manifest", stage="devel")
208
222
def test_failing_manifests_invalid_phase(self):
209
223
for example in test_manifests_for_invalid_phase:
210
spec = self.create_spec(manifests=example['manifests'])
224
spec = self.create_spec(manifests=example["manifests"])
212
226
with self.assertRaises(InvalidManifestException):
213
Manifest(spec=spec, manifest_file='manifest',
227
Manifest(spec=spec, manifest_file="manifest", stage="devel")
216
229
def test_failing_manifests_invalid_arg(self):
217
230
for example in test_manifests_for_invalid_arg:
218
spec = self.create_spec(manifests=example['manifests'])
231
spec = self.create_spec(manifests=example["manifests"])
220
233
with self.assertRaises(InvalidManifestException):
221
Manifest(spec=spec, manifest_file='manifest',
234
Manifest(spec=spec, manifest_file="manifest", stage="devel")
224
236
def test_failing_manifests_multiple_args(self):
225
237
for example in test_manifests_for_multiple_args:
226
spec = self.create_spec(manifests=example['manifests'])
238
spec = self.create_spec(manifests=example["manifests"])
228
240
with self.assertRaises(InvalidManifestException):
229
Manifest(spec=spec, manifest_file='manifest',
241
Manifest(spec=spec, manifest_file="manifest", stage="devel")
232
243
def test_manifest_comment_parsing(self):
233
244
manifest_text = """
245
256
spec = self.create_spec(manifests=dict(manifest=manifest_text))
246
manifest = Manifest(spec=spec, manifest_file='manifest',
257
manifest = Manifest(spec=spec, manifest_file="manifest", stage="devel")
249
259
actual_phases = [phase.name for phase in manifest.phases]
260
self.assertEqual("Here we go", manifest.phases[2].options['text'])
261
self.assertEqual("Done\nwith a multiline comment.",
262
manifest.phases[4].options['text'])
263
self.assertEqual("Verify", manifest.phases[6].options['text'])
261
["collect", "repo", "comment", "deploy", "comment", "script", "comment", "verify"], actual_phases
263
self.assertEqual("Here we go", manifest.phases[2].options["text"])
264
self.assertEqual("Done\nwith a multiline comment.", manifest.phases[4].options["text"])
265
self.assertEqual("Verify", manifest.phases[6].options["text"])
265
267
def test_manifest_comment_and_include_ordering(self):
267
269
for example in test_manifests_for_comment_and_include_ordering:
268
spec = self.create_spec(manifests=example['manifests'])
270
spec = self.create_spec(manifests=example["manifests"])
270
manifest = Manifest(spec=spec, manifest_file='manifest',
272
manifest = Manifest(spec=spec, manifest_file="manifest", stage="devel")
273
274
actual_phases = [phase.name for phase in manifest.phases]
274
275
self.assertEqual(
275
example['expected_phases'], actual_phases,
276
example["expected_phases"],
276
278
"The example '{}' expected the phases:\n{}\n "
277
"but had\n{}\ninstead.".format(
278
example['name'], example['expected_phases'],
279
"but had\n{}\ninstead.".format(example["name"], example["expected_phases"], actual_phases),
281
282
@mock.patch("mojo.manifest.input")
282
@mock.patch('logging.info')
283
@mock.patch("logging.info")
283
284
def test_manifest_interactive(self, _logging_info, _mock_raw_input):
284
285
manifest_list = ["collect", "deploy config=services"]
285
286
spec = self.create_spec(manifests=dict(manifest="\n".join(manifest_list)))
286
manifest = Manifest(spec=spec, manifest_file='manifest', stage='devel')
287
project = Project('unused', 'trusty', '/srv/mojo')
287
manifest = Manifest(spec=spec, manifest_file="manifest", stage="devel")
288
project = Project("unused", "trusty", "/srv/mojo")
288
289
workspace = self.create_workspace()
289
290
# Test with Stop as the directive first. We expect it to prompt for
290
291
# the first phase, and then stop.
291
_mock_raw_input.return_value = 's'
292
_mock_raw_input.return_value = "s"
292
293
expected_call_args_list = [
293
294
"Running 'mojo run -m manifest'",
294
295
"Interactive mode... about to run:",
297
298
"[(Y)es] / (N)o / (S)top",
299
manifest.run(project, workspace, stage='devel', interactive=True)
300
manifest.run(project, workspace, stage="devel", interactive=True)
300
301
self.assertEquals(len(_logging_info.call_args_list), len(expected_call_args_list))
301
302
for i, call in enumerate(_logging_info.call_args_list):
302
303
self.assertEquals(call, mock.call(expected_call_args_list[i]))
303
304
_logging_info.reset_mock()
304
305
# And now test with No as the directive. We expect it to prompt for
305
306
# each phase, but not execute.
306
_mock_raw_input.return_value = 'n'
307
_mock_raw_input.return_value = "n"
307
308
expected_call_args_list = [
308
309
"Running 'mojo run -m manifest'",
309
310
"Interactive mode... about to run:",