~hloeung/mojo/force-destroy-model

« back to all changes in this revision

Viewing changes to mojo/tests/test_manifest.py

  • Committer: mergebot at canonical
  • Author(s): "Stuart Bishop"
  • Date: 2020-04-22 13:04:02 UTC
  • mfrom: (545.1.6 blacken)
  • Revision ID: mergebot@juju-139df4-prod-is-toolbox-0.canonical.com-20200422130402-55dc5d92dj2lx14h
Reformat code with black

Reviewed-on: https://code.launchpad.net/~stub/mojo/blacken/+merge/382726
Reviewed-by: Tom Haddon <tom.haddon@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
    from unittest import mock
16
16
 
17
17
 
18
 
test_manifests_for_includes = [{
19
 
    'name': "An include at the end of a file",
20
 
    'expected_phases': ['collect', 'repo', 'deploy', 'script', 'verify'],
21
 
    'manifests': {
22
 
        'manifest': """
23
 
collect
24
 
repo
25
 
deploy config=services
26
 
include config=other-manifest
27
 
""",
28
 
        'other-manifest': """
29
 
script config=post-deploy
30
 
verify
31
 
""",
32
 
        },
33
 
    }, {
34
 
    'name': "An include in the middle of a file",
35
 
    'expected_phases': ['collect', 'repo', 'deploy', 'script', 'verify'],
36
 
    'manifests': {
37
 
        'manifest': """
38
 
collect
39
 
repo
40
 
deploy config=services
41
 
include config=other-manifest
42
 
verify
43
 
""",
44
 
        'other-manifest': """
45
 
script config=post-deploy
46
 
""",
47
 
        }
48
 
    }, {
49
 
    'name': "Nested include files",
50
 
    'expected_phases': [
51
 
        'collect',  # manifest
52
 
        'repo',
53
 
        'deploy',   # deploy-manifest
54
 
        'verify',   # verify-manifest
55
 
        'script',
56
 
        'verify',
57
 
        'deploy',   # deploy-manifest
58
 
        'script',   # manifest
 
18
test_manifests_for_includes = [
 
19
    {
 
20
        "name": "An include at the end of a file",
 
21
        "expected_phases": ["collect", "repo", "deploy", "script", "verify"],
 
22
        "manifests": {
 
23
            "manifest": """
 
24
collect
 
25
repo
 
26
deploy config=services
 
27
include config=other-manifest
 
28
""",
 
29
            "other-manifest": """
 
30
script config=post-deploy
 
31
verify
 
32
""",
 
33
        },
 
34
    },
 
35
    {
 
36
        "name": "An include in the middle of a file",
 
37
        "expected_phases": ["collect", "repo", "deploy", "script", "verify"],
 
38
        "manifests": {
 
39
            "manifest": """
 
40
collect
 
41
repo
 
42
deploy config=services
 
43
include config=other-manifest
 
44
verify
 
45
""",
 
46
            "other-manifest": """
 
47
script config=post-deploy
 
48
""",
 
49
        },
 
50
    },
 
51
    {
 
52
        "name": "Nested include files",
 
53
        "expected_phases": [
 
54
            "collect",  # manifest
 
55
            "repo",
 
56
            "deploy",  # deploy-manifest
 
57
            "verify",  # verify-manifest
 
58
            "script",
 
59
            "verify",
 
60
            "deploy",  # deploy-manifest
 
61
            "script",  # manifest
59
62
        ],
60
 
    'manifests': {
61
 
        'manifest': """
 
63
        "manifests": {
 
64
            "manifest": """
62
65
collect
63
66
repo
64
67
include config=deploy-manifest
65
68
script
66
69
""",
67
 
        'deploy-manifest': """
 
70
            "deploy-manifest": """
68
71
deploy config=services
69
72
include config=verify-manifest
70
73
deploy config=somethingelse
71
74
""",
72
 
        'verify-manifest': """
 
75
            "verify-manifest": """
73
76
verify
74
77
script config=other-test
75
78
verify config=verify-nagios
76
79
""",
77
80
        },
78
 
    }, {
79
 
    'name': "Nested include files in subdir",
80
 
    'expected_phases': [
81
 
        'collect',  # manifest
82
 
        'repo',
83
 
        'deploy',   # deploy-manifest
84
 
        'verify',   # verify-manifest
85
 
        'script',
86
 
        'verify',
87
 
        'deploy',   # deploy-manifest
88
 
        'script',   # manifest
 
81
    },
 
82
    {
 
83
        "name": "Nested include files in subdir",
 
84
        "expected_phases": [
 
85
            "collect",  # manifest
 
86
            "repo",
 
87
            "deploy",  # deploy-manifest
 
88
            "verify",  # verify-manifest
 
89
            "script",
 
90
            "verify",
 
91
            "deploy",  # deploy-manifest
 
92
            "script",  # manifest
89
93
        ],
90
 
    'manifests': {
91
 
        'manifest': """
 
94
        "manifests": {
 
95
            "manifest": """
92
96
collect
93
97
repo
94
98
include config=manifests/deploy-manifest
95
99
script
96
100
""",
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
101
105
""",
102
 
        'manifests/verify-manifest': """
 
106
            "manifests/verify-manifest": """
103
107
verify
104
108
script config=other-test
105
109
verify config=verify-nagios
106
110
""",
107
111
        },
108
 
    }]
 
112
    },
 
113
]
109
114
 
110
 
test_manifests_for_missing_include = [{
111
 
    'name': "Include file doesn't exist",
112
 
    'manifests': {
113
 
        'manifest': """
 
115
test_manifests_for_missing_include = [
 
116
    {
 
117
        "name": "Include file doesn't exist",
 
118
        "manifests": {
 
119
            "manifest": """
114
120
collect
115
121
repo
116
122
deploy config=services
117
123
include config=non-existent-manifest
118
124
""",
119
 
        'other-manifest': """
 
125
            "other-manifest": """
120
126
script config=post-deploy
121
127
verify
122
128
""",
123
129
        },
124
 
    }]
 
130
    }
 
131
]
125
132
 
126
 
test_manifests_for_invalid_phase = [{
127
 
    'name': "Invalid phase name",
128
 
    'manifests': {
129
 
        'manifest': """
 
133
test_manifests_for_invalid_phase = [
 
134
    {
 
135
        "name": "Invalid phase name",
 
136
        "manifests": {
 
137
            "manifest": """
130
138
explode
131
139
repo
132
140
deploy config=services
133
141
include config=other-manifest
134
142
""",
135
 
        'other-manifest': """
 
143
            "other-manifest": """
136
144
script config=post-deploy
137
145
verify
138
146
""",
139
147
        },
140
 
    }]
 
148
    }
 
149
]
141
150
 
142
 
test_manifests_for_invalid_arg = [{
143
 
    'name': "Invalid phase argument",
144
 
    'manifests': {
145
 
        'manifest': """
 
151
test_manifests_for_invalid_arg = [
 
152
    {
 
153
        "name": "Invalid phase argument",
 
154
        "manifests": {
 
155
            "manifest": """
146
156
repo
147
157
deploy notavalidarg
148
 
"""},
149
 
    }]
150
 
 
151
 
 
152
 
test_manifests_for_multiple_args = [{
153
 
    'name': "Multiple phase arguments",
154
 
    'manifests': {
155
 
        'manifest': """
 
158
"""
 
159
        },
 
160
    }
 
161
]
 
162
 
 
163
 
 
164
test_manifests_for_multiple_args = [
 
165
    {
 
166
        "name": "Multiple phase arguments",
 
167
        "manifests": {
 
168
            "manifest": """
156
169
repo
157
170
deploy config=foo config=bar
158
 
"""},
159
 
    }]
160
 
 
161
 
 
162
 
test_manifests_for_comment_and_include_ordering = [{
163
 
    'name': "Comment ordering w/r/t includes",
164
 
    'expected_phases': ['collect', 'comment', 'deploy', 'comment', 'verify'],
165
 
    'manifests': {
166
 
        'manifest': """
 
171
"""
 
172
        },
 
173
    }
 
174
]
 
175
 
 
176
 
 
177
test_manifests_for_comment_and_include_ordering = [
 
178
    {
 
179
        "name": "Comment ordering w/r/t includes",
 
180
        "expected_phases": ["collect", "comment", "deploy", "comment", "verify"],
 
181
        "manifests": {
 
182
            "manifest": """
167
183
collect
168
184
# Deploy the environment.
169
185
include config=manifest-deploy
170
186
# Verify the environment.
171
187
include config=manifest-verify
172
188
""",
173
 
        'manifest-deploy': """
 
189
            "manifest-deploy": """
174
190
deploy
175
191
""",
176
 
        'manifest-verify': """
 
192
            "manifest-verify": """
177
193
verify
178
194
""",
179
195
        },
180
 
    }]
 
196
    }
 
197
]
181
198
 
182
199
 
183
200
class ManifestTestCase(TestCase, MojoTestCaseMixin):
184
 
 
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"])
188
204
 
189
 
            manifest = Manifest(spec=spec, manifest_file='manifest',
190
 
                                stage='devel')
 
205
            manifest = Manifest(spec=spec, manifest_file="manifest", stage="devel")
191
206
 
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"],
 
210
                actual_phases,
195
211
                "The example '{}' expected the phases:\n{}\n "
196
 
                "but had\n{}\ninstead.".format(
197
 
                    example['name'], example['expected_phases'],
198
 
                    actual_phases))
 
212
                "but had\n{}\ninstead.".format(example["name"], example["expected_phases"], actual_phases),
 
213
            )
199
214
 
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"])
203
218
 
204
219
            with self.assertRaises(ConfigNotFoundException):
205
 
                Manifest(spec=spec, manifest_file='manifest',
206
 
                         stage='devel')
 
220
                Manifest(spec=spec, manifest_file="manifest", stage="devel")
207
221
 
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"])
211
225
 
212
226
            with self.assertRaises(InvalidManifestException):
213
 
                Manifest(spec=spec, manifest_file='manifest',
214
 
                         stage='devel')
 
227
                Manifest(spec=spec, manifest_file="manifest", stage="devel")
215
228
 
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"])
219
232
 
220
233
            with self.assertRaises(InvalidManifestException):
221
 
                Manifest(spec=spec, manifest_file='manifest',
222
 
                         stage='devel')
 
234
                Manifest(spec=spec, manifest_file="manifest", stage="devel")
223
235
 
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"])
227
239
 
228
240
            with self.assertRaises(InvalidManifestException):
229
 
                Manifest(spec=spec, manifest_file='manifest',
230
 
                         stage='devel')
 
241
                Manifest(spec=spec, manifest_file="manifest", stage="devel")
231
242
 
232
243
    def test_manifest_comment_parsing(self):
233
244
        manifest_text = """
243
254
verify
244
255
"""
245
256
        spec = self.create_spec(manifests=dict(manifest=manifest_text))
246
 
        manifest = Manifest(spec=spec, manifest_file='manifest',
247
 
                            stage='devel')
 
257
        manifest = Manifest(spec=spec, manifest_file="manifest", stage="devel")
248
258
 
249
259
        actual_phases = [phase.name for phase in manifest.phases]
250
 
        self.assertEqual([
251
 
            'collect',
252
 
            'repo',
253
 
            'comment',
254
 
            'deploy',
255
 
            'comment',
256
 
            'script',
257
 
            'comment',
258
 
            'verify',
259
 
            ], actual_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'])
 
260
        self.assertEqual(
 
261
            ["collect", "repo", "comment", "deploy", "comment", "script", "comment", "verify"], actual_phases
 
262
        )
 
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"])
264
266
 
265
267
    def test_manifest_comment_and_include_ordering(self):
266
268
 
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"])
269
271
 
270
 
            manifest = Manifest(spec=spec, manifest_file='manifest',
271
 
                                stage='devel')
 
272
            manifest = Manifest(spec=spec, manifest_file="manifest", stage="devel")
272
273
 
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"],
 
277
                actual_phases,
276
278
                "The example '{}' expected the phases:\n{}\n "
277
 
                "but had\n{}\ninstead.".format(
278
 
                    example['name'], example['expected_phases'],
279
 
                    actual_phases))
 
279
                "but had\n{}\ninstead.".format(example["name"], example["expected_phases"], actual_phases),
 
280
            )
280
281
 
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:",
295
296
            "  collect ",
296
297
            "",
297
298
            "[(Y)es] / (N)o / (S)top",
298
 
            ]
299
 
        manifest.run(project, workspace, stage='devel', interactive=True)
 
299
        ]
 
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:",
318
319
            "[(Y)es] / (N)o / (S)top",
319
320
            "Skipping...",
320
321
            "",
321
 
            ]
322
 
        manifest.run(project, workspace, stage='devel', interactive=True)
 
322
        ]
 
323
        manifest.run(project, workspace, stage="devel", interactive=True)
323
324
        self.assertEquals(len(_logging_info.call_args_list), len(expected_call_args_list))
324
325
        for i, call in enumerate(_logging_info.call_args_list):
325
326
            self.assertEquals(call, mock.call(expected_call_args_list[i]))