From 5b3a827a1ff80e4b51c7ede44b84bf640d5b6380 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Wed, 18 Sep 2019 11:58:12 -0400 Subject: [PATCH] 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 --- openstackclient/tests/unit/compute/v2/test_server.py | 3 ++- openstackclient/tests/unit/utils.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index c70d6d72d0..c2bac2771f 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -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): diff --git a/openstackclient/tests/unit/utils.py b/openstackclient/tests/unit/utils.py index c15d8bbfe2..8df81a50eb 100644 --- a/openstackclient/tests/unit/utils.py +++ b/openstackclient/tests/unit/utils.py @@ -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: