Browse Source

Avoid None value when missing data in ansible log

When missing datas in ansible log for some reasons,
we need to avoid None value, and just ignore it.

Change-Id: Ic6e05bf65bf01eeafd5e9222f3410a4dc5a50c9e
changes/51/727951/2
Mathieu Bultel 2 years ago committed by Cédric Jeanneret (Tengu)
parent
commit
1477887091
  1. 25
      validations_libs/tests/fakes.py
  2. 27
      validations_libs/tests/test_validation_log.py
  3. 10
      validations_libs/validation_logs.py

25
validations_libs/tests/fakes.py

@ -33,6 +33,31 @@ GROUPS_LIST = [
('group3', 'Group3 description'),
]
BAD_VALIDATIONS_LOGS_CONTENTS_LIST = [{
'plays': [{
'play': {
'duration': {
'end': '2019-11-25T13:40:17.538611Z',
},
'host': 'undercloud',
'id': '008886df-d297-1eaa-2a74-000000000008',
'validation_id': '512e',
'validation_path':
'/usr/share/openstack-tripleo-validations/playbooks'
}}],
'stats': {
'undercloud': {
'changed': 0,
'failures': 0,
'ignored': 0,
'ok': 1,
'rescued': 0,
'skipped': 1
}
},
'validation_output': []
}]
VALIDATIONS_LOGS_CONTENTS_LIST = [{
'plays': [{
'play': {

27
validations_libs/tests/test_validation_log.py

@ -134,6 +134,15 @@ class TestValidationLog(TestCase):
unreachable = val.get_unreachable_hosts
self.assertEquals(unreachable, '')
@mock.patch('json.load',
return_value=fakes.BAD_VALIDATIONS_LOGS_CONTENTS_LIST[0])
@mock.patch('six.moves.builtins.open')
def test_get_unreachable_hosts_bad_data(self, mock_open, mock_json):
val = ValidationLog(
logfile='/tmp/123_foo_2020-03-30T13:17:22.447857Z.json')
unreachable = val.get_unreachable_hosts
self.assertEquals(unreachable, '')
@mock.patch('json.load',
return_value=fakes.VALIDATIONS_LOGS_CONTENTS_LIST[0])
@mock.patch('six.moves.builtins.open')
@ -143,6 +152,15 @@ class TestValidationLog(TestCase):
duration = val.get_duration
self.assertEquals(duration, '0:00:03.753')
@mock.patch('json.load',
return_value=fakes.BAD_VALIDATIONS_LOGS_CONTENTS_LIST[0])
@mock.patch('six.moves.builtins.open')
def test_get_duration_bad_data(self, mock_open, mock_json):
val = ValidationLog(
logfile='/tmp/123_foo_2020-03-30T13:17:22.447857Z.json')
duration = val.get_duration
self.assertEquals(duration, '')
@mock.patch('json.load',
return_value=fakes.VALIDATIONS_LOGS_CONTENTS_LIST[0])
@mock.patch('six.moves.builtins.open')
@ -152,6 +170,15 @@ class TestValidationLog(TestCase):
start_time = val.get_start_time
self.assertEquals(start_time, '2019-11-25T13:40:14.404623Z')
@mock.patch('json.load',
return_value=fakes.BAD_VALIDATIONS_LOGS_CONTENTS_LIST[0])
@mock.patch('six.moves.builtins.open')
def test_get_start_time_bad_data(self, mock_open, mock_json):
val = ValidationLog(
logfile='/tmp/123_foo_2020-03-30T13:17:22.447857Z.json')
start_time = val.get_start_time
self.assertEquals(start_time, '')
@mock.patch('six.moves.builtins.open')
def test_log_not_found(self, mock_open):
mock_open.side_effect = IOError()

10
validations_libs/validation_logs.py

@ -126,14 +126,16 @@ class ValidationLog(object):
@property
def get_duration(self):
"""Return duration of Ansible runtime"""
return ', '.join([play['play']['duration'].get('time_elapsed') for
play in self.content['plays']])
duration = [play['play']['duration'].get('time_elapsed') for
play in self.content['plays']]
return ', '.join(filter(None, duration))
@property
def get_start_time(self):
"""Return Ansible start time"""
return ', '.join([play['play']['duration'].get('start') for
play in self.content['plays']])
start_time = [play['play']['duration'].get('start') for
play in self.content['plays']]
return ', '.join(filter(None, start_time))
class ValidationLogs(object):

Loading…
Cancel
Save