From da20d6101d0fa245ca62e4eeaccb71f55a2d087d Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Thu, 22 Nov 2018 16:33:40 +0000 Subject: [PATCH] Add assertions to verify placement-manage error output The tests for placement-manage, in unit/cmd/test_manage were not verifying help message output when partial or incorrect commands were provided. This led to some confusion when trying to verify a fix related to different behaviors of argparse. Here, some verification is added. Because of differences between python 2 and 3, we need to look at stderr and stdout, respectively, to verify the output. Change-Id: I337a25cfdc23bc56fbf15f128a94d2b1b1d1b664 Related-Bug: #1804420 --- placement/tests/unit/cmd/test_manage.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/placement/tests/unit/cmd/test_manage.py b/placement/tests/unit/cmd/test_manage.py index 746b0b58e..5a96dad7a 100644 --- a/placement/tests/unit/cmd/test_manage.py +++ b/placement/tests/unit/cmd/test_manage.py @@ -30,7 +30,8 @@ class TestCommandParsers(testtools.TestCase): self.useFixture(conf_fixture) # Quiet output from argparse (used within oslo_config). # If you are debugging, commenting this out might be useful. - self.useFixture(output.CaptureOutput(do_stderr=True)) + self.output = self.useFixture( + output.CaptureOutput(do_stderr=True, do_stdout=True)) # We don't use a database, but we need to set the opt as # it's required for a valid config. conf_fixture.config(group="placement_database", connection='sqlite://') @@ -79,3 +80,24 @@ class TestCommandParsers(testtools.TestCase): def test_too_many_args(self): self.assertRaises(SystemExit, self.conf, ['version', '5'], default_config_files=[]) + self.output.stderr.seek(0) + self.assertIn("choose from 'db'", self.output.stderr.read()) + + def test_help_message(self): + """Test that help output for sub commands shows right commands.""" + # This is noisy because we have different 'help' behaviors in + # Python 2 and 3. + if six.PY2: + self.assertRaises(SystemExit, self.conf, ['db'], + default_config_files=[]) + else: + self.conf(['db'], default_config_files=[]) + self.conf.command.func() + + self.output.stdout.seek(0) + self.output.stderr.seek(0) + + if six.PY2: + self.assertIn('{sync,version}', self.output.stderr.read()) + else: + self.assertIn('{sync,version}', self.output.stdout.read())