Format list to str for get_reason message or skip

If the message in the validation log is a list, this patch
format it to a string, as expected, otherwise, if the message
another type, we just skip and move to default reason: Unknown

Change-Id: I7d434408ab0af67967e9c1407a8e286daee51769
This commit is contained in:
matbu 2022-11-23 10:09:50 +01:00 committed by mbu
parent 9a2bcee59f
commit ac2b6b7b7e
3 changed files with 104 additions and 2 deletions

View File

@ -131,6 +131,70 @@ FAILED_VALIDATIONS_LOGS_CONTENTS_LIST = [{
]
}]
FAILED_VALIDATIONS_LOGS_WRONG_MSG_LIST = [{
'stats': {
'undercloud': {
'changed': 0,
'failures': 1,
'ignored': 0,
'ok': 0,
'rescued': 0,
'skipped': 0,
'unreachable': 0
}
},
'validation_output': [
{
"task": {
"hosts": {
"localhost": {
"_ansible_no_log": False,
"action": "fail",
"changed": False,
"failed": True,
"failed_when_result": True,
"msg": ["Fake", "Failed"]
}
},
"name": "Verify Fake requirements",
"status": "FAILED"
}
}
]
}]
FAILED_VALIDATIONS_LOGS_WRONG_MSG_TYPE = [{
'stats': {
'undercloud': {
'changed': 0,
'failures': 1,
'ignored': 0,
'ok': 0,
'rescued': 0,
'skipped': 0,
'unreachable': 0
}
},
'validation_output': [
{
"task": {
"hosts": {
"localhost": {
"_ansible_no_log": False,
"action": "fail",
"changed": False,
"failed": True,
"failed_when_result": True,
"msg": True
}
},
"name": "Verify Fake requirements",
"status": "FAILED"
}
}
]
}]
VALIDATIONS_LOGS_CONTENTS_LIST = [{
'plays': [{
'play': {

View File

@ -309,3 +309,35 @@ class TestValidationLog(TestCase):
[
fakes.VALIDATIONS_LOGS_CONTENTS_LIST[0]
['validation_output'][0]['task']])
@mock.patch('json.load',
return_value=fakes.FAILED_VALIDATIONS_LOGS_CONTENTS_LIST[0])
@mock.patch('builtins.open')
def test_get_reason(self, mock_open, mock_json):
val = ValidationLog(
logfile='/tmp/123_foo_2020-03-30T13:17:22.447857Z.json')
get_reason = val.get_reason
fake_reason = 'localhost: {}\n'.format(
fakes.FAILED_VALIDATIONS_LOGS_CONTENTS_LIST[0]
['validation_output'][0]['task']['hosts']['localhost']['msg'])
self.assertEqual(get_reason, fake_reason)
@mock.patch('json.load',
return_value=fakes.FAILED_VALIDATIONS_LOGS_WRONG_MSG_LIST[0])
@mock.patch('builtins.open')
def test_get_reason_list_wrong_msg(self, mock_open, mock_json):
val = ValidationLog(
logfile='/tmp/123_foo_2020-03-30T13:17:22.447857Z.json')
get_reason = val.get_reason
fake_reason = 'localhost: FakeFailed\n'
self.assertEqual(get_reason, fake_reason)
@mock.patch('json.load',
return_value=fakes.FAILED_VALIDATIONS_LOGS_WRONG_MSG_TYPE[0])
@mock.patch('builtins.open')
def test_get_reason_list_wrong_type(self, mock_open, mock_json):
val = ValidationLog(
logfile='/tmp/123_foo_2020-03-30T13:17:22.447857Z.json')
get_reason = val.get_reason
fake_reason = 'Unknown'
self.assertEqual(get_reason, fake_reason)

View File

@ -299,8 +299,14 @@ class ValidationLog:
for h in v_output['task']['hosts']:
msg = v_output['task']['hosts'][h].get('msg',
'Unknown')
msg = msg[:50] + '\n' + msg[50:]
reason.append('{}: {}'.format(h, msg))
if isinstance(msg, list):
msg = ''.join(msg)
try:
msg = msg[:50] + '\n' + msg[50:]
reason.append('{}: {}'.format(h, msg))
except TypeError:
LOG.warning('Wrong failure message type. skipping...')
reason.append('Unknown')
if not self.content['validation_output']:
if self.get_unreachable_hosts:
reason.append('Unreachable')