mirror of
https://github.com/ansible/awx.git
synced 2026-03-03 09:48:51 -03:30
make fact saving code more robust to unexpected fact data
see: https://github.com/ansible/awx/issues/5935
This commit is contained in:
@@ -829,8 +829,10 @@ class Job(UnifiedJob, JobOptions, SurveyJobMixin, JobNotificationMixin, TaskMana
|
|||||||
continue
|
continue
|
||||||
host.ansible_facts = ansible_facts
|
host.ansible_facts = ansible_facts
|
||||||
host.ansible_facts_modified = now()
|
host.ansible_facts_modified = now()
|
||||||
ansible_local_system_id = ansible_facts.get('ansible_local', {}).get('insights', {}).get('system_id', None)
|
ansible_local = ansible_facts.get('ansible_local', {}).get('insights', {})
|
||||||
ansible_facts_system_id = ansible_facts.get('insights', {}).get('system_id', None)
|
ansible_facts = ansible_facts.get('insights', {})
|
||||||
|
ansible_local_system_id = ansible_local.get('system_id', None) if isinstance(ansible_local, dict) else None
|
||||||
|
ansible_facts_system_id = ansible_facts.get('system_id', None) if isinstance(ansible_facts, dict) else None
|
||||||
if ansible_local_system_id:
|
if ansible_local_system_id:
|
||||||
print("Setting local {}".format(ansible_local_system_id))
|
print("Setting local {}".format(ansible_local_system_id))
|
||||||
logger.debug("Insights system_id {} found for host <{}, {}> in"
|
logger.debug("Insights system_id {} found for host <{}, {}> in"
|
||||||
|
|||||||
@@ -89,6 +89,27 @@ def test_finish_job_fact_cache_with_existing_data(job, hosts, inventory, mocker,
|
|||||||
hosts[1].save.assert_called_once_with()
|
hosts[1].save.assert_called_once_with()
|
||||||
|
|
||||||
|
|
||||||
|
def test_finish_job_fact_cache_with_malformed_fact(job, hosts, inventory, mocker, tmpdir):
|
||||||
|
fact_cache = os.path.join(tmpdir, 'facts')
|
||||||
|
modified_times = {}
|
||||||
|
job.start_job_fact_cache(fact_cache, modified_times, 0)
|
||||||
|
|
||||||
|
for h in hosts:
|
||||||
|
h.save = mocker.Mock()
|
||||||
|
|
||||||
|
for h in hosts:
|
||||||
|
filepath = os.path.join(fact_cache, h.name)
|
||||||
|
with open(filepath, 'w') as f:
|
||||||
|
json.dump({'ansible_local': {'insights': 'this is an unexpected error from ansible'}}, f)
|
||||||
|
new_modification_time = time.time() + 3600
|
||||||
|
os.utime(filepath, (new_modification_time, new_modification_time))
|
||||||
|
|
||||||
|
job.finish_job_fact_cache(fact_cache, modified_times)
|
||||||
|
|
||||||
|
for h in hosts:
|
||||||
|
assert h.insights_system_id is None
|
||||||
|
|
||||||
|
|
||||||
def test_finish_job_fact_cache_with_bad_data(job, hosts, inventory, mocker, tmpdir):
|
def test_finish_job_fact_cache_with_bad_data(job, hosts, inventory, mocker, tmpdir):
|
||||||
fact_cache = os.path.join(tmpdir, 'facts')
|
fact_cache = os.path.join(tmpdir, 'facts')
|
||||||
modified_times = {}
|
modified_times = {}
|
||||||
|
|||||||
Reference in New Issue
Block a user