Provide stderr in exception when check_parser fails

For negative tests that are asserting an argparse failure
it would be useful to assert the specific reason for the
failure in the test rather than just getting an exception,
especially to avoid false positives in the tests when what
is being tested and failing isn't the actual expected reason
for the failure.

This wraps the check_parser code that parses the args and
mocks sys.stderr so we can trap that output and put it in the
exception message that gets raised to the test.

As a result, we can tighten up a test that was passing before
for the wrong reason [1].

[1] https://review.opendev.org/#/c/673725/12/openstackclient/tests/unit/compute/v2/test_server.py@605

Change-Id: I0f1dc1215bdfb3eba98ccaf66a0041d220b93812
This commit is contained in:
Matt Riedemann 2019-09-18 11:58:12 -04:00
parent 874a726f52
commit 5b3a827a1f
2 changed files with 10 additions and 5 deletions

View File

@ -626,7 +626,8 @@ class TestServerVolumeV279(TestServerVolume):
ex = self.assertRaises(utils.ParserException,
self.check_parser,
self.cmd, arglist, verifylist)
self.assertIn('Argument parse failed', str(ex))
self.assertIn('argument --disable-delete-on-termination: not allowed '
'with argument --enable-delete-on-termination', str(ex))
class TestServerAddNetwork(TestServer):

View File

@ -17,6 +17,7 @@
import os
import fixtures
from six.moves import StringIO
import testtools
from cliff import columns as cliff_columns
@ -72,10 +73,13 @@ class TestCommand(TestCase):
def check_parser(self, cmd, args, verify_args):
cmd_parser = cmd.get_parser('check_parser')
try:
parsed_args = cmd_parser.parse_args(args)
except SystemExit:
raise ParserException("Argument parse failed")
stderr = StringIO()
with fixtures.MonkeyPatch('sys.stderr', stderr):
try:
parsed_args = cmd_parser.parse_args(args)
except SystemExit:
raise ParserException("Argument parse failed: %s" %
stderr.getvalue())
for av in verify_args:
attr, value = av
if attr: