From 88c9bfb125d503c052ac44363af160fd24ce107a Mon Sep 17 00:00:00 2001 From: Steve Heyman Date: Sat, 18 Apr 2015 11:04:12 -0500 Subject: [PATCH] Update stdout and stderr capture in functional tests Updated base code to correctly capture stderr and stdout for barbican CLI. Change-Id: I75b253a8618531218b0c17aeae45a826c12b47f9 --- functionaltests/cli/base.py | 25 ++++++++++++++++------- functionaltests/cli/v1/smoke/test_help.py | 6 +++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/functionaltests/cli/base.py b/functionaltests/cli/base.py index 45fb3773..2c2ffe25 100644 --- a/functionaltests/cli/base.py +++ b/functionaltests/cli/base.py @@ -15,6 +15,7 @@ limitations under the License. """ import exceptions as exc +import six from functionaltests.base import BaseTestCase from barbicanclient import barbican @@ -31,16 +32,26 @@ class CmdLineTestCase(BaseTestCase): def issue_barbican_command(self, argv): """ Issue the barbican command and return its output. + The barbican command sometimes raises SystemExit, but not always, so + we will handle either situation here. + + Also we will create new stdout/stderr streams for each command so that + any output from a previous command doesn't contaminate the new command. + :param argv: dict of keyword arguments to pass to the command. This does NOT include "barbican" - that's not needed. - :return: list of strings returned by the command, one list element - per line of output. This means the caller doesn't have to worry about - parsing newlines, etc. If there is a problem then this method - will return None + :return: Two strings - one the captured stdout and one the captured + stderr. """ - result = None + try: + self.cmdline_client.stdout = six.StringIO() + self.cmdline_client.stderr = six.StringIO() self.cmdline_client.run(argv) except exc.SystemExit: - result = self.cmdline_client.stdout.getvalue() - return result + pass + + outstr = self.cmdline_client.stdout.getvalue() + errstr = self.cmdline_client.stderr.getvalue() + + return outstr, errstr diff --git a/functionaltests/cli/v1/smoke/test_help.py b/functionaltests/cli/v1/smoke/test_help.py index fc9dc7d8..b7b7e211 100644 --- a/functionaltests/cli/v1/smoke/test_help.py +++ b/functionaltests/cli/v1/smoke/test_help.py @@ -33,6 +33,6 @@ class HelpTestCase(CmdLineTestCase): }) @testcase.attr('positive') def test_help(self, argv): - result = self.issue_barbican_command(argv) - self.assertIsNotNone(result, "{0} returned None".format(argv)) - self.assertGreater(len(result), 0, "{0} invalid length".format(argv)) + stdout, stderr = self.issue_barbican_command(argv) + self.assertIsNotNone(stdout, "{0} returned None".format(argv)) + self.assertGreater(len(stdout), 0, "{0} invalid length".format(argv))