From 7b8b37d9a8e46b3cda101f3c5691c870b9de46da Mon Sep 17 00:00:00 2001 From: Peter Braun Date: Thu, 27 Feb 2025 13:43:59 +0100 Subject: [PATCH] fix: audit record name should not be the hostname (#15864) * fix: audit record name should not be the hostname * fix: update tests --- awx/main/tasks/host_indirect.py | 4 +++- .../projects/host_query/extensions/audit/event_query.yml | 2 +- .../data/projects/host_query/plugins/modules/example.py | 1 + awx/main/tests/functional/tasks/test_host_indirect.py | 5 +++-- awx/main/tests/live/tests/test_indirect_host_counting.py | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/awx/main/tasks/host_indirect.py b/awx/main/tasks/host_indirect.py index d7e9edffaf32..26a75f1850e2 100644 --- a/awx/main/tasks/host_indirect.py +++ b/awx/main/tasks/host_indirect.py @@ -82,6 +82,8 @@ def build_indirect_host_data(job: Job, job_event_queries: dict[str, dict[str, st # Obtain the record based on the hashable canonical_facts now determined facts = data.get('facts') + name = data.get('name') + if hashable_facts in results: audit_record = results[hashable_facts] else: @@ -90,7 +92,7 @@ def build_indirect_host_data(job: Job, job_event_queries: dict[str, dict[str, st facts=facts, job=job, organization=job.organization, - name=event.host_name, + name=name, ) results[hashable_facts] = audit_record diff --git a/awx/main/tests/data/projects/host_query/extensions/audit/event_query.yml b/awx/main/tests/data/projects/host_query/extensions/audit/event_query.yml index fb27540cec47..a10586b90e22 100644 --- a/awx/main/tests/data/projects/host_query/extensions/audit/event_query.yml +++ b/awx/main/tests/data/projects/host_query/extensions/audit/event_query.yml @@ -1,4 +1,4 @@ --- demo.query.example: query: >- - {canonical_facts: {host_name: .direct_host_name}, facts: {device_type: .device_type}} + {name: .name, canonical_facts: {host_name: .direct_host_name}, facts: {device_type: .device_type}} diff --git a/awx/main/tests/data/projects/host_query/plugins/modules/example.py b/awx/main/tests/data/projects/host_query/plugins/modules/example.py index fb1eb2314e6b..c1427c1d52bd 100644 --- a/awx/main/tests/data/projects/host_query/plugins/modules/example.py +++ b/awx/main/tests/data/projects/host_query/plugins/modules/example.py @@ -62,6 +62,7 @@ def run_module(): result['direct_host_name'] = module.params['host_name'] result['nested_host_name'] = {'host_name': module.params['host_name']} + result['name'] = 'vm-foo' # non-cononical facts result['device_type'] = 'Fake Host' diff --git a/awx/main/tests/functional/tasks/test_host_indirect.py b/awx/main/tests/functional/tasks/test_host_indirect.py index bf9d3b5c325d..51cc5e8241a4 100644 --- a/awx/main/tests/functional/tasks/test_host_indirect.py +++ b/awx/main/tests/functional/tasks/test_host_indirect.py @@ -17,7 +17,7 @@ """These are unit tests, similar to test_indirect_host_counting in the live tests""" -TEST_JQ = "{canonical_facts: {host_name: .direct_host_name}, facts: {another_host_name: .direct_host_name}}" +TEST_JQ = "{name: .name, canonical_facts: {host_name: .direct_host_name}, facts: {another_host_name: .direct_host_name}}" @pytest.fixture @@ -30,7 +30,7 @@ def bare_job(job_factory): def create_registered_event(job, task_name='demo.query.example'): - return job.job_events.create(event_data={'resolved_action': task_name, 'res': {'direct_host_name': 'foo_host'}}) + return job.job_events.create(event_data={'resolved_action': task_name, 'res': {'direct_host_name': 'foo_host', 'name': 'vm-foo'}}) @pytest.fixture @@ -107,6 +107,7 @@ def test_save_indirect_host_entries(job_with_counted_event, event_query): assert host_audit.canonical_facts == {'host_name': 'foo_host'} assert host_audit.facts == {'another_host_name': 'foo_host'} assert host_audit.organization == job_with_counted_event.organization + assert host_audit.name == 'vm-foo' @pytest.mark.django_db diff --git a/awx/main/tests/live/tests/test_indirect_host_counting.py b/awx/main/tests/live/tests/test_indirect_host_counting.py index 7c86eb8d313b..ce202f02aee6 100644 --- a/awx/main/tests/live/tests/test_indirect_host_counting.py +++ b/awx/main/tests/live/tests/test_indirect_host_counting.py @@ -14,7 +14,7 @@ def test_indirect_host_counting(live_tmp_folder, run_job_from_playbook): # Data matches to awx/main/tests/data/projects/host_query/extensions/audit/event_query.yml # this just does things in-line to be a more localized test for the immediate testing - module_jq_str = '{canonical_facts: {host_name: .direct_host_name}, facts: {device_type: .device_type}}' + module_jq_str = '{name: .name, canonical_facts: {host_name: .direct_host_name}, facts: {device_type: .device_type}}' event_query = {'demo.query.example': {'query': module_jq_str}} # Run the task logic directly with local data