Browse Source

Merge "fix: Always check for exit_code 0 in CLI tests"

Zuul 5 months ago
parent
commit
49abff1be3
1 changed files with 89 additions and 130 deletions
  1. 89
    130
      tests/unit/test_cli.py

+ 89
- 130
tests/unit/test_cli.py View File

@@ -37,6 +37,9 @@ class BaseCLIActionTest(object):
37 37
     repo URL and as many other tests that are required that use a local repo
38 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 45
     # TODO(felipemonteiro): Need tests that validate repository overrides. Also
@@ -48,6 +51,8 @@ class BaseCLIActionTest(object):
48 51
 
49 52
         # Pin so we know that airship-seaworthy is a valid site.
50 53
         cls.site_name = "airship-seaworthy"
54
+        cls.site_type = "foundry"
55
+
51 56
         cls.repo_rev = '6b183e148b9bb7ba6f75c98dd13451088255c60b'
52 57
         cls.repo_name = "airship-treasuremap"
53 58
         repo_url = "https://github.com/openstack/%s.git" % cls.repo_name
@@ -156,6 +161,21 @@ class TestSiteCliActions(BaseCLIActionTest):
156 161
 
157 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 179
     def test_collect_using_remote_repo_url(self):
160 180
         """Validates collect action using a remote URL."""
161 181
         # Scenario:
@@ -164,20 +184,9 @@ class TestSiteCliActions(BaseCLIActionTest):
164 184
         # 2) Collect into save location (should clone repo automatically)
165 185
         # 3) Check that expected file name is there
166 186
 
167
-        save_location = tempfile.mkdtemp()
168 187
         repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
169 188
                                                            self.repo_rev)
170
-        result = self.runner.invoke(
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)
189
+        self._validate_collect_site_action(repo_url)
181 190
 
182 191
     def test_collect_using_remote_repo_url_ending_with_dot_git(self):
183 192
         """Validates collect action using a remote URL ending in .git."""
@@ -187,18 +196,9 @@ class TestSiteCliActions(BaseCLIActionTest):
187 196
         # 2) Collect into save location (should clone repo automatically)
188 197
         # 3) Check that expected file name is there
189 198
 
190
-        save_location = tempfile.mkdtemp()
191 199
         repo_url = 'https://github.com/openstack/%s@%s.git' % (self.repo_name,
192 200
                                                                self.repo_rev)
193
-        result = self.runner.invoke(
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)
201
+        self._validate_collect_site_action(repo_url)
202 202
 
203 203
     def test_collect_using_local_path(self):
204 204
         """Validates collect action using a path to a local repo."""
@@ -208,20 +208,33 @@ class TestSiteCliActions(BaseCLIActionTest):
208 208
         # 2) Collect into save location (should skip clone repo)
209 209
         # 3) Check that expected file name is there
210 210
 
211
-        save_location = tempfile.mkdtemp()
212 211
         repo_path = self.treasuremap_path
212
+        self._validate_collect_site_action(repo_path)
213 213
 
214
-        result = self.runner.invoke(
215
-            cli.site,
216
-            ['-r', repo_path, 'collect', self.site_name, '-s', save_location])
214
+    ### Lint tests ###
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 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 239
     def test_lint_site_using_remote_repo_url_with_exclude(self):
227 240
         """Validates site lint action using remote repo URL."""
@@ -232,22 +245,7 @@ class TestSiteCliActions(BaseCLIActionTest):
232 245
 
233 246
         repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
234 247
                                                            self.repo_rev)
235
-
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
248
+        self._test_lint_site_action(repo_url, exclude=True)
251 249
 
252 250
     def test_lint_site_using_local_path_with_exclude(self):
253 251
         """Validates site lint action using local repo path."""
@@ -257,21 +255,7 @@ class TestSiteCliActions(BaseCLIActionTest):
257 255
         # 2) Lint site with exclude flags (should skip clone repo)
258 256
 
259 257
         repo_path = self.treasuremap_path
260
-        lint_command = ['-r', repo_path, 'lint', self.site_name]
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
258
+        self._test_lint_site_action(repo_path, exclude=True)
275 259
 
276 260
     def test_lint_site_using_local_path_with_warn(self):
277 261
         """Validates site lint action using local repo path."""
@@ -281,23 +265,19 @@ class TestSiteCliActions(BaseCLIActionTest):
281 265
         # 2) Lint site with warn flags (should skip clone repo)
282 266
 
283 267
         repo_path = self.treasuremap_path
284
-        lint_command = ['-r', repo_path, 'lint', self.site_name]
285
-        warn_lint_command = [
286
-            '-w', errorcodes.SCHEMA_STORAGE_POLICY_MISMATCH_FLAG, '-w',
287
-            errorcodes.SECRET_NOT_ENCRYPTED_POLICY
288
-        ]
268
+        self._test_lint_site_action(repo_path, exclude=False)
289 269
 
290
-        with mock.patch('pegleg.engine.site.util.deckhand') as mock_deckhand:
291
-            mock_deckhand.deckhand_render.return_value = ([], [])
292
-            result = self.runner.invoke(cli.site,
293
-                                        lint_command + warn_lint_command)
270
+    ### List tests ###
294 271
 
295
-        assert result.exit_code == 0, result.output
296
-        # A successful result (while setting lint checks to warns) should
297
-        # output warnings.
298
-        assert result.output
272
+    def _validate_list_site_action(self, repo_path_or_url):
273
+        mock_output = mock.Mock()
274
+        result = self.runner.invoke(
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 282
     def test_list_sites_using_remote_repo_url(self):
303 283
         """Validates list action using remote repo URL."""
@@ -308,12 +288,7 @@ class TestSiteCliActions(BaseCLIActionTest):
308 288
         repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
309 289
                                                            self.repo_rev)
310 290
 
311
-        # Mock out PrettyTable to determine output.
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'])
291
+        self._validate_list_site_action(repo_url)
317 292
 
318 293
     def test_list_sites_using_local_path(self):
319 294
         """Validates list action using local repo path."""
@@ -322,15 +297,19 @@ class TestSiteCliActions(BaseCLIActionTest):
322 297
         # 1) List sites (should skip clone repo)
323 298
 
324 299
         repo_path = self.treasuremap_path
300
+        self._validate_list_site_action(repo_path)
325 301
 
326
-        # Mock out PrettyTable to determine output.
327
-        with mock.patch('pegleg.engine.site.PrettyTable') as mock_writer:
328
-            result = self.runner.invoke(cli.site, ['-r', repo_path, 'list'])
302
+    ### Show tests ###
329 303
 
330
-        m_writer = mock_writer.return_value
331
-        m_writer.add_row.assert_called_with([self.site_name, 'foundry'])
304
+    def _validate_site_show_action(self, repo_path_or_url):
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 314
     def test_show_site_using_remote_repo_url(self):
336 315
         """Validates show action using remote repo URL."""
@@ -340,14 +319,7 @@ class TestSiteCliActions(BaseCLIActionTest):
340 319
 
341 320
         repo_url = 'https://github.com/openstack/%s@%s' % (self.repo_name,
342 321
                                                            self.repo_rev)
343
-
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])
322
+        self._validate_site_show_action(repo_url)
351 323
 
352 324
     def test_show_site_using_local_path(self):
353 325
         """Validates show action using local repo path."""
@@ -356,27 +328,12 @@ class TestSiteCliActions(BaseCLIActionTest):
356 328
         # 1) Show site (should skip clone repo)
357 329
 
358 330
         repo_path = self.treasuremap_path
359
-        with mock.patch('pegleg.engine.site.PrettyTable') as mock_writer:
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])
331
+        self._validate_site_show_action(repo_path)
366 332
 
367 333
     ### Render tests ###
368 334
 
369
-    def test_render_site_using_remote_repo_url(self):
370
-        """Validates render action using remote repo URL."""
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]
335
+    def _validate_render_site_action(self, repo_path_or_url):
336
+        render_command = ['-r', repo_path_or_url, 'render', self.site_name]
380 337
 
381 338
         with mock.patch('pegleg.engine.site.yaml') as mock_yaml:
382 339
             with mock.patch(
@@ -387,6 +344,17 @@ class TestSiteCliActions(BaseCLIActionTest):
387 344
         assert result.exit_code == 0
388 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 358
     def test_render_site_using_local_path(self):
391 359
         """Validates render action using local repo path."""
392 360
         # Scenario:
@@ -395,16 +363,7 @@ class TestSiteCliActions(BaseCLIActionTest):
395 363
         # 2) Render site (should skip clone repo)
396 364
 
397 365
         repo_path = self.treasuremap_path
398
-        render_command = ['-r', repo_path, 'render', self.site_name]
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()
366
+        self._validate_render_site_action(repo_path)
408 367
 
409 368
 
410 369
 class TestRepoCliActions(BaseCLIActionTest):
@@ -470,9 +429,9 @@ class TestTypeCliActions(BaseCLIActionTest):
470 429
         self.expected_types = ['foundry']
471 430
 
472 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 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 436
     def _validate_type_list_action(self, repo_path_or_url):
478 437
         mock_output = mock.Mock()
@@ -509,11 +468,11 @@ class TestSiteCliActionsWithSubdirectory(BaseCLIActionTest):
509 468
         self.expected_sites = ['demo', 'gate-multinode', 'dev', 'dev-proxy']
510 469
 
511 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 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 476
         mock_output = mock.Mock()
518 477
         result = self.runner.invoke(
519 478
             cli.site, ['-r', repo_path_or_url, 'list', '-o', mock_output])
@@ -534,7 +493,7 @@ class TestSiteCliActionsWithSubdirectory(BaseCLIActionTest):
534 493
         repo_url = 'https://github.com/openstack/%s/deployment_files@%s' % (
535 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 498
     def test_site_action_with_subpath_in_local_repo_path(self):
540 499
         """Validates list action with subpath in local repo path."""
@@ -550,4 +509,4 @@ class TestSiteCliActionsWithSubdirectory(BaseCLIActionTest):
550 509
         _repo_path = git.git_handler(repo_url, ref=repo_rev)
551 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)

Loading…
Cancel
Save