Improve tests for warning messages

The functional test for allocation create did test that it displays
the warning message if the microversion is not high enough, but didn't
validate the returned json result.

This patch changes it to validate the actual result as well as the
warning message.

Change-Id: I8280075a020fb4a10b82b710d5e0ca997a3028fd
This commit is contained in:
Tetsuro Nakamura 2019-11-25 02:33:27 +00:00
parent e4ceef795a
commit d80651a389
2 changed files with 35 additions and 15 deletions

View File

@ -80,7 +80,8 @@ class BaseTestCase(base.BaseTestCase):
for name in RESET_LOGGING:
logging.getLogger(name).setLevel(logging.WARNING)
def openstack(self, cmd, may_fail=False, use_json=False):
def openstack(self, cmd, may_fail=False, use_json=False,
may_print_to_stderr=False):
to_exec = []
# Make all requests as a noauth admin user.
to_exec += [
@ -110,17 +111,30 @@ class BaseTestCase(base.BaseTestCase):
except SystemExit as exc:
return_code = exc.code
# We may have error/warning messages in stderr, so treat it
# separately from the stdout.
output = self.output.getvalue()
error = self.error.getvalue()
if return_code:
msg = 'Command: "%s"\noutput: %s' % (' '.join(to_exec),
self.error.getvalue())
msg = 'Command: "%s"\noutput: %s' % (' '.join(to_exec), error)
if not may_fail:
raise CommandException(msg, cmd=' '.join(to_exec))
output = self.output.getvalue() + self.error.getvalue()
if use_json and output:
return json.loads(output)
else:
return output
output = json.loads(output)
if may_print_to_stderr:
return output, error
if error:
msg = ('Test code error - The command did not fail but it '
'has a warning message. Set the "may_print_to_stderr" '
'argument to true to get and validate the message:\n'
'Command: "%s"\nstderr: %s') % (
' '.join(to_exec), error)
raise CommandException(msg, cmd=' '.join(to_exec))
return output
def rand_name(self, name='', prefix=None):
"""Generate a random name that includes a random number
@ -221,7 +235,7 @@ class BaseTestCase(base.BaseTestCase):
def resource_allocation_set(self, consumer_uuid, allocations,
project_id=None, user_id=None,
use_json=True):
use_json=True, may_print_to_stderr=False):
cmd = 'resource provider allocation set {allocs} {uuid}'.format(
uuid=consumer_uuid,
allocs=' '.join('--allocation {}'.format(a) for a in allocations)
@ -230,7 +244,8 @@ class BaseTestCase(base.BaseTestCase):
cmd += ' --project-id %s' % project_id
if user_id:
cmd += ' --user-id %s' % user_id
result = self.openstack(cmd, use_json=use_json)
result = self.openstack(cmd, use_json=use_json,
may_print_to_stderr=may_print_to_stderr)
def cleanup(uuid):
try:

View File

@ -54,17 +54,22 @@ class TestAllocation(base.BaseTestCase):
self.assertEqual(expected, retrieved_alloc)
# Test that specifying --project-id and --user-id before microversion
# 1.8 does not result in an error (they will be ignored). We have
# to specify use_json=False because there will be a warning in the
# output which can't be json-decoded.
output = self.resource_allocation_set(
# 1.8 does not result in an error but display a warning.
output, warning = self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid']),
'rp={},MEMORY_MB=512'.format(self.rp1['uuid'])],
project_id='fake-project', user_id='fake-user', use_json=False)
project_id='fake-project', user_id='fake-user',
may_print_to_stderr=True)
expected = [
{'resource_provider': self.rp1['uuid'],
'generation': 3,
'resources': {'VCPU': 2, 'MEMORY_MB': 512}}
]
self.assertEqual(expected, output)
self.assertIn(
'--project-id and --user-id options do not affect allocation for '
'--os-placement-api-version less than 1.8', output)
'--os-placement-api-version less than 1.8', warning)
def test_allocation_create_empty(self):
consumer_uuid = str(uuid.uuid4())