summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-10-27 04:07:31 +0000
committerGerrit Code Review <review@openstack.org>2018-10-27 04:07:31 +0000
commit49abff1be3fa73cda725773425323f6d6627d155 (patch)
tree70c940f8dfad1d2a8ca34760a61a4950b25c3b37
parentc498bfee813d8660954006946b4778bfce509d25 (diff)
parent58b393322bc8fed8773cc2f929d5f610157c9a0b (diff)
Merge "fix: Always check for exit_code 0 in CLI tests"
-rw-r--r--tests/unit/test_cli.py219
1 files changed, 89 insertions, 130 deletions
diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py
index ad4c34c..79a6ff6 100644
--- a/tests/unit/test_cli.py
+++ b/tests/unit/test_cli.py
@@ -37,6 +37,9 @@ class BaseCLIActionTest(object):
37 repo URL and as many other tests that are required that use a local repo 37 repo URL and as many other tests that are required that use a local repo
38 path for runtime optimization. 38 path for runtime optimization.
39 39
40 All tests should validate that the ``exit_code`` from the CLI is 0 (for
41 positive tests).
42
40 """ 43 """
41 44
42 # TODO(felipemonteiro): Need tests that validate repository overrides. Also 45 # TODO(felipemonteiro): Need tests that validate repository overrides. Also
@@ -48,6 +51,8 @@ class BaseCLIActionTest(object):
48 51
49 # Pin so we know that airship-seaworthy is a valid site. 52 # Pin so we know that airship-seaworthy is a valid site.
50 cls.site_name = "airship-seaworthy" 53 cls.site_name = "airship-seaworthy"
54 cls.site_type = "foundry"
55
51 cls.repo_rev = '6b183e148b9bb7ba6f75c98dd13451088255c60b' 56 cls.repo_rev = '6b183e148b9bb7ba6f75c98dd13451088255c60b'
52 cls.repo_name = "airship-treasuremap" 57 cls.repo_name = "airship-treasuremap"
53 repo_url = "https://github.com/openstack/%s.git" % cls.repo_name 58 repo_url = "https://github.com/openstack/%s.git" % cls.repo_name
@@ -156,6 +161,21 @@ class TestSiteCliActions(BaseCLIActionTest):
156 161
157 ### Collect tests ### 162 ### Collect tests ###
158 163
164 def _validate_collect_site_action(self, repo_path_or_url):
165 save_location = tempfile.mkdtemp()
166 result = self.runner.invoke(cli.site, [
167 '-r', repo_path_or_url, 'collect', self.site_name, '-s',
168 save_location
169 ])
170
171 collected_files = os.listdir(save_location)
172
173 assert result.exit_code == 0, result.output
174 assert len(collected_files) == 1
175 # Validates that site manifests collected from cloned repositories
176 # are written out to sensibly named files like airship-treasuremap.yaml
177 assert collected_files[0] == ("%s.yaml" % self.repo_name)
178
159 def test_collect_using_remote_repo_url(self): 179 def test_collect_using_remote_repo_url(self):
160 """Validates collect action using a remote URL.""" 180 """Validates collect action using a remote URL."""
161 # Scenario: 181 # Scenario:
@@ -164,20 +184,9 @@ class TestSiteCliActions(BaseCLIActionTest):
164 # 2) Collect into save location (should clone repo automatically) 184 # 2) Collect into save location (should clone repo automatically)
165 # 3) Check that expected file name is there 185 # 3) Check that expected file name is there
166 186
167 save_location = tempfile.mkdtemp()
168 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name, 187 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
169 self.repo_rev) 188 self.repo_rev)
170 result = self.runner.invoke( 189 self._validate_collect_site_action(repo_url)
171 cli.site,
172 ['-r', repo_url, 'collect', self.site_name, '-s', save_location])
173
174 collected_files = os.listdir(save_location)
175
176 assert result.exit_code == 0, result.output
177 assert len(collected_files) == 1
178 # Validates that site manifests collected from cloned repositories
179 # are written out to sensibly named files like airship-treasuremap.yaml
180 assert collected_files[0] == ("%s.yaml" % self.repo_name)
181 190
182 def test_collect_using_remote_repo_url_ending_with_dot_git(self): 191 def test_collect_using_remote_repo_url_ending_with_dot_git(self):
183 """Validates collect action using a remote URL ending in .git.""" 192 """Validates collect action using a remote URL ending in .git."""
@@ -187,18 +196,9 @@ class TestSiteCliActions(BaseCLIActionTest):
187 # 2) Collect into save location (should clone repo automatically) 196 # 2) Collect into save location (should clone repo automatically)
188 # 3) Check that expected file name is there 197 # 3) Check that expected file name is there
189 198
190 save_location = tempfile.mkdtemp()
191 repo_url = 'https://github.com/openstack/%s@%s.git' % (self.repo_name, 199 repo_url = 'https://github.com/openstack/%s@%s.git' % (self.repo_name,
192 self.repo_rev) 200 self.repo_rev)
193 result = self.runner.invoke( 201 self._validate_collect_site_action(repo_url)
194 cli.site,
195 ['-r', repo_url, 'collect', self.site_name, '-s', save_location])
196
197 collected_files = os.listdir(save_location)
198
199 assert result.exit_code == 0
200 assert len(collected_files) == 1
201 assert collected_files[0] == ("%s.yaml" % self.repo_name)
202 202
203 def test_collect_using_local_path(self): 203 def test_collect_using_local_path(self):
204 """Validates collect action using a path to a local repo.""" 204 """Validates collect action using a path to a local repo."""
@@ -208,20 +208,33 @@ class TestSiteCliActions(BaseCLIActionTest):
208 # 2) Collect into save location (should skip clone repo) 208 # 2) Collect into save location (should skip clone repo)
209 # 3) Check that expected file name is there 209 # 3) Check that expected file name is there
210 210
211 save_location = tempfile.mkdtemp()
212 repo_path = self.treasuremap_path 211 repo_path = self.treasuremap_path
212 self._validate_collect_site_action(repo_path)
213 213
214 result = self.runner.invoke( 214 ### Lint tests ###
215 cli.site,
216 ['-r', repo_path, 'collect', self.site_name, '-s', save_location])
217 215
218 collected_files = os.listdir(save_location) 216 def _test_lint_site_action(self, repo_path_or_url, exclude=True):
217 flag = '-x' if exclude else '-w'
218
219 lint_command = ['-r', repo_path_or_url, 'lint', self.site_name]
220 exclude_lint_command = [
221 flag, errorcodes.SCHEMA_STORAGE_POLICY_MISMATCH_FLAG, flag,
222 errorcodes.SECRET_NOT_ENCRYPTED_POLICY
223 ]
224
225 with mock.patch('pegleg.engine.site.util.deckhand') as mock_deckhand:
226 mock_deckhand.deckhand_render.return_value = ([], [])
227 result = self.runner.invoke(cli.site,
228 lint_command + exclude_lint_command)
219 229
220 assert result.exit_code == 0, result.output 230 assert result.exit_code == 0, result.output
221 assert len(collected_files) == 1
222 assert collected_files[0] == ("%s.yaml" % self.repo_name)
223 231
224 ### Lint tests ### 232 if exclude:
233 # A successful result (while setting lint checks to exclude) should
234 # output nothing.
235 assert not result.output
236 else:
237 assert result.output
225 238
226 def test_lint_site_using_remote_repo_url_with_exclude(self): 239 def test_lint_site_using_remote_repo_url_with_exclude(self):
227 """Validates site lint action using remote repo URL.""" 240 """Validates site lint action using remote repo URL."""
@@ -232,22 +245,7 @@ class TestSiteCliActions(BaseCLIActionTest):
232 245
233 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name, 246 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
234 self.repo_rev) 247 self.repo_rev)
235 248 self._test_lint_site_action(repo_url, exclude=True)
236 lint_command = ['-r', repo_url, 'lint', self.site_name]
237 exclude_lint_command = [
238 '-x', errorcodes.SCHEMA_STORAGE_POLICY_MISMATCH_FLAG, '-x',
239 errorcodes.SECRET_NOT_ENCRYPTED_POLICY
240 ]
241
242 with mock.patch('pegleg.engine.site.util.deckhand') as mock_deckhand:
243 mock_deckhand.deckhand_render.return_value = ([], [])
244 result = self.runner.invoke(cli.site,
245 lint_command + exclude_lint_command)
246
247 assert result.exit_code == 0, result.output
248 # A successful result (while setting lint checks to exclude) should
249 # output nothing.
250 assert not result.output
251 249
252 def test_lint_site_using_local_path_with_exclude(self): 250 def test_lint_site_using_local_path_with_exclude(self):
253 """Validates site lint action using local repo path.""" 251 """Validates site lint action using local repo path."""
@@ -257,21 +255,7 @@ class TestSiteCliActions(BaseCLIActionTest):
257 # 2) Lint site with exclude flags (should skip clone repo) 255 # 2) Lint site with exclude flags (should skip clone repo)
258 256
259 repo_path = self.treasuremap_path 257 repo_path = self.treasuremap_path
260 lint_command = ['-r', repo_path, 'lint', self.site_name] 258 self._test_lint_site_action(repo_path, exclude=True)
261 exclude_lint_command = [
262 '-x', errorcodes.SCHEMA_STORAGE_POLICY_MISMATCH_FLAG, '-x',
263 errorcodes.SECRET_NOT_ENCRYPTED_POLICY
264 ]
265
266 with mock.patch('pegleg.engine.site.util.deckhand') as mock_deckhand:
267 mock_deckhand.deckhand_render.return_value = ([], [])
268 result = self.runner.invoke(cli.site,
269 lint_command + exclude_lint_command)
270
271 assert result.exit_code == 0, result.output
272 # A successful result (while setting lint checks to exclude) should
273 # output nothing.
274 assert not result.output
275 259
276 def test_lint_site_using_local_path_with_warn(self): 260 def test_lint_site_using_local_path_with_warn(self):
277 """Validates site lint action using local repo path.""" 261 """Validates site lint action using local repo path."""
@@ -281,23 +265,19 @@ class TestSiteCliActions(BaseCLIActionTest):
281 # 2) Lint site with warn flags (should skip clone repo) 265 # 2) Lint site with warn flags (should skip clone repo)
282 266
283 repo_path = self.treasuremap_path 267 repo_path = self.treasuremap_path
284 lint_command = ['-r', repo_path, 'lint', self.site_name] 268 self._test_lint_site_action(repo_path, exclude=False)
285 warn_lint_command = [
286 '-w', errorcodes.SCHEMA_STORAGE_POLICY_MISMATCH_FLAG, '-w',
287 errorcodes.SECRET_NOT_ENCRYPTED_POLICY
288 ]
289 269
290 with mock.patch('pegleg.engine.site.util.deckhand') as mock_deckhand: 270 ### List tests ###
291 mock_deckhand.deckhand_render.return_value = ([], [])
292 result = self.runner.invoke(cli.site,
293 lint_command + warn_lint_command)
294 271
295 assert result.exit_code == 0, result.output 272 def _validate_list_site_action(self, repo_path_or_url):
296 # A successful result (while setting lint checks to warns) should 273 mock_output = mock.Mock()
297 # output warnings. 274 result = self.runner.invoke(
298 assert result.output 275 cli.site, ['-r', repo_path_or_url, 'list', '-o', mock_output])
299 276
300 ### List tests ### 277 assert result.exit_code == 0, result.output
278 table_output = mock_output.write.mock_calls[0][1][0]
279 assert self.site_name in table_output
280 assert self.site_type in table_output
301 281
302 def test_list_sites_using_remote_repo_url(self): 282 def test_list_sites_using_remote_repo_url(self):
303 """Validates list action using remote repo URL.""" 283 """Validates list action using remote repo URL."""
@@ -308,12 +288,7 @@ class TestSiteCliActions(BaseCLIActionTest):
308 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name, 288 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
309 self.repo_rev) 289 self.repo_rev)
310 290
311 # Mock out PrettyTable to determine output. 291 self._validate_list_site_action(repo_url)
312 with mock.patch('pegleg.engine.site.PrettyTable') as mock_writer:
313 result = self.runner.invoke(cli.site, ['-r', repo_url, 'list'])
314
315 m_writer = mock_writer.return_value
316 m_writer.add_row.assert_called_with([self.site_name, 'foundry'])
317 292
318 def test_list_sites_using_local_path(self): 293 def test_list_sites_using_local_path(self):
319 """Validates list action using local repo path.""" 294 """Validates list action using local repo path."""
@@ -322,15 +297,19 @@ class TestSiteCliActions(BaseCLIActionTest):
322 # 1) List sites (should skip clone repo) 297 # 1) List sites (should skip clone repo)
323 298
324 repo_path = self.treasuremap_path 299 repo_path = self.treasuremap_path
300 self._validate_list_site_action(repo_path)
325 301
326 # Mock out PrettyTable to determine output. 302 ### Show tests ###
327 with mock.patch('pegleg.engine.site.PrettyTable') as mock_writer:
328 result = self.runner.invoke(cli.site, ['-r', repo_path, 'list'])
329 303
330 m_writer = mock_writer.return_value 304 def _validate_site_show_action(self, repo_path_or_url):
331 m_writer.add_row.assert_called_with([self.site_name, 'foundry']) 305 mock_output = mock.Mock()
306 result = self.runner.invoke(cli.site, [
307 '-r', repo_path_or_url, 'show', self.site_name, '-o', mock_output
308 ])
332 309
333 ### Show tests ### 310 assert result.exit_code == 0, result.output
311 table_output = mock_output.write.mock_calls[0][1][0]
312 assert self.site_name in table_output
334 313
335 def test_show_site_using_remote_repo_url(self): 314 def test_show_site_using_remote_repo_url(self):
336 """Validates show action using remote repo URL.""" 315 """Validates show action using remote repo URL."""
@@ -340,14 +319,7 @@ class TestSiteCliActions(BaseCLIActionTest):
340 319
341 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name, 320 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
342 self.repo_rev) 321 self.repo_rev)
343 322 self._validate_site_show_action(repo_url)
344 with mock.patch('pegleg.engine.site.PrettyTable') as mock_writer:
345 result = self.runner.invoke(
346 cli.site, ['-r', repo_url, 'show', self.site_name])
347
348 m_writer = mock_writer.return_value
349 m_writer.add_row.assert_called_with(
350 ['', self.site_name, 'foundry', mock.ANY])
351 323
352 def test_show_site_using_local_path(self): 324 def test_show_site_using_local_path(self):
353 """Validates show action using local repo path.""" 325 """Validates show action using local repo path."""
@@ -356,27 +328,12 @@ class TestSiteCliActions(BaseCLIActionTest):
356 # 1) Show site (should skip clone repo) 328 # 1) Show site (should skip clone repo)
357 329
358 repo_path = self.treasuremap_path 330 repo_path = self.treasuremap_path
359 with mock.patch('pegleg.engine.site.PrettyTable') as mock_writer: 331 self._validate_site_show_action(repo_path)
360 result = self.runner.invoke(
361 cli.site, ['-r', repo_path, 'show', self.site_name])
362
363 m_writer = mock_writer.return_value
364 m_writer.add_row.assert_called_with(
365 ['', self.site_name, 'foundry', mock.ANY])
366 332
367 ### Render tests ### 333 ### Render tests ###
368 334
369 def test_render_site_using_remote_repo_url(self): 335 def _validate_render_site_action(self, repo_path_or_url):
370 """Validates render action using remote repo URL.""" 336 render_command = ['-r', repo_path_or_url, 'render', self.site_name]
371 # Scenario:
372 #
373 # 1) Mock out Deckhand render (so we can ignore P005 issues)
374 # 2) Render site (should clone repo automatically)
375
376 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
377 self.repo_rev)
378
379 render_command = ['-r', repo_url, 'render', self.site_name]
380 337
381 with mock.patch('pegleg.engine.site.yaml') as mock_yaml: 338 with mock.patch('pegleg.engine.site.yaml') as mock_yaml:
382 with mock.patch( 339 with mock.patch(
@@ -387,6 +344,17 @@ class TestSiteCliActions(BaseCLIActionTest):
387 assert result.exit_code == 0 344 assert result.exit_code == 0
388 mock_yaml.dump_all.assert_called_once() 345 mock_yaml.dump_all.assert_called_once()
389 346
347 def test_render_site_using_remote_repo_url(self):
348 """Validates render action using remote repo URL."""
349 # Scenario:
350 #
351 # 1) Mock out Deckhand render (so we can ignore P005 issues)
352 # 2) Render site (should clone repo automatically)
353
354 repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
355 self.repo_rev)
356 self._validate_render_site_action(repo_url)
357
390 def test_render_site_using_local_path(self): 358 def test_render_site_using_local_path(self):
391 """Validates render action using local repo path.""" 359 """Validates render action using local repo path."""
392 # Scenario: 360 # Scenario:
@@ -395,16 +363,7 @@ class TestSiteCliActions(BaseCLIActionTest):
395 # 2) Render site (should skip clone repo) 363 # 2) Render site (should skip clone repo)
396 364
397 repo_path = self.treasuremap_path 365 repo_path = self.treasuremap_path
398 render_command = ['-r', repo_path, 'render', self.site_name] 366 self._validate_render_site_action(repo_path)
399
400 with mock.patch('pegleg.engine.site.yaml') as mock_yaml:
401 with mock.patch(
402 'pegleg.engine.site.util.deckhand') as mock_deckhand:
403 mock_deckhand.deckhand_render.return_value = ([], [])
404 result = self.runner.invoke(cli.site, render_command)
405
406 assert result.exit_code == 0
407 mock_yaml.dump_all.assert_called_once()
408 367
409 368
410class TestRepoCliActions(BaseCLIActionTest): 369class TestRepoCliActions(BaseCLIActionTest):
@@ -470,9 +429,9 @@ class TestTypeCliActions(BaseCLIActionTest):
470 self.expected_types = ['foundry'] 429 self.expected_types = ['foundry']
471 430
472 def _assert_table_has_expected_sites(self, mock_output): 431 def _assert_table_has_expected_sites(self, mock_output):
473 output_table = mock_output.write.mock_calls[0][1][0] 432 table_output = mock_output.write.mock_calls[0][1][0]
474 for expected_type in self.expected_types: 433 for expected_type in self.expected_types:
475 assert expected_type in output_table 434 assert expected_type in table_output
476 435
477 def _validate_type_list_action(self, repo_path_or_url): 436 def _validate_type_list_action(self, repo_path_or_url):
478 mock_output = mock.Mock() 437 mock_output = mock.Mock()
@@ -509,11 +468,11 @@ class TestSiteCliActionsWithSubdirectory(BaseCLIActionTest):
509 self.expected_sites = ['demo', 'gate-multinode', 'dev', 'dev-proxy'] 468 self.expected_sites = ['demo', 'gate-multinode', 'dev', 'dev-proxy']
510 469
511 def _assert_table_has_expected_sites(self, mock_output): 470 def _assert_table_has_expected_sites(self, mock_output):
512 output_table = mock_output.write.mock_calls[0][1][0] 471 table_output = mock_output.write.mock_calls[0][1][0]
513 for expected_site in self.expected_sites: 472 for expected_site in self.expected_sites:
514 assert expected_site in output_table 473 assert expected_site in table_output
515 474
516 def _validate_site_action(self, repo_path_or_url): 475 def _validate_list_site_action(self, repo_path_or_url):
517 mock_output = mock.Mock() 476 mock_output = mock.Mock()
518 result = self.runner.invoke( 477 result = self.runner.invoke(
519 cli.site, ['-r', repo_path_or_url, 'list', '-o', mock_output]) 478 cli.site, ['-r', repo_path_or_url, 'list', '-o', mock_output])
@@ -534,7 +493,7 @@ class TestSiteCliActionsWithSubdirectory(BaseCLIActionTest):
534 repo_url = 'https://github.com/openstack/%s/deployment_files@%s' % ( 493 repo_url = 'https://github.com/openstack/%s/deployment_files@%s' % (
535 repo_name, repo_rev) 494 repo_name, repo_rev)
536 495
537 self._validate_site_action(repo_url) 496 self._validate_list_site_action(repo_url)
538 497
539 def test_site_action_with_subpath_in_local_repo_path(self): 498 def test_site_action_with_subpath_in_local_repo_path(self):
540 """Validates list action with subpath in local repo path.""" 499 """Validates list action with subpath in local repo path."""
@@ -550,4 +509,4 @@ class TestSiteCliActionsWithSubdirectory(BaseCLIActionTest):
550 _repo_path = git.git_handler(repo_url, ref=repo_rev) 509 _repo_path = git.git_handler(repo_url, ref=repo_rev)
551 repo_path = os.path.join(_repo_path, 'deployment_files') 510 repo_path = os.path.join(_repo_path, 'deployment_files')
552 511
553 self._validate_site_action(repo_path) 512 self._validate_list_site_action(repo_path)