-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AAP-37282 Add parse JQ data and test it for a
job
object in isolati…
…on (#15774) * Add jq dependency * Add file in progress * Add license for jq * Write test and get it passing
- Loading branch information
1 parent
1f50364
commit ee9fb42
Showing
7 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from types import SimpleNamespace | ||
import logging | ||
|
||
import jq | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def build_indirect_host_data(job, event_query: dict): | ||
results = [] | ||
compiled_jq_expressions = {} # Cache for compiled jq expressions | ||
facts_missing_logged = False | ||
for event in job.job_events.filter(task__in=event_query.keys()): | ||
if 'res' not in event.event_data: | ||
continue | ||
jq_str_for_event = event_query[event.task] | ||
if jq_str_for_event not in compiled_jq_expressions: | ||
compiled_jq_expressions[event.task] = jq.compile(jq_str_for_event) | ||
compiled_jq = compiled_jq_expressions[event.task] | ||
for data in compiled_jq.input(event.event_data['res']).all(): | ||
if not data.get('canonical_facts'): | ||
if not facts_missing_logged: | ||
logger.error(f'jq output missing canonical_facts for module {event.task} on event {event.id} using jq:{jq_str_for_event}') | ||
continue | ||
canonical_facts = data['canonical_facts'] | ||
facts = data.get('facts') | ||
results.append(SimpleNamespace(canonical_facts=canonical_facts, facts=facts)) | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
{ | ||
"demo.query.example": "" | ||
"demo.query.example": "{canonical_facts: {host_name: .direct_host_name}, facts: {device_type: .device_type}}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 18 additions & 1 deletion
19
awx/main/tests/live/tests/projects/test_indirect_host_counting.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
from awx.main.tasks.host_indirect import build_indirect_host_data | ||
from awx.main.models import Job | ||
|
||
|
||
def test_indirect_host_counting(live_tmp_folder, run_job_from_playbook): | ||
run_job_from_playbook('test_indirect_host_counting', 'run_task.yml', scm_url=f'file://{live_tmp_folder}/test_host_query') | ||
# TODO: add assertions that the host query data is populated | ||
job = Job.objects.filter(name__icontains='test_indirect_host_counting').order_by('-created').first() | ||
|
||
# Data matches to awx/main/tests/data/projects/host_query/meta/event_query.yml | ||
# this just does things in-line to be a more localized test for the immediate testing | ||
event_query = {'demo.query.example': '{canonical_facts: {host_name: .direct_host_name}, facts: {device_type: .device_type}}'} | ||
|
||
# Run the task logic directly with local data | ||
results = build_indirect_host_data(job, event_query) | ||
assert len(results) == 1 | ||
host_audit_entry = results[0] | ||
|
||
# Asserts on data that will match to the input jq string from above | ||
assert host_audit_entry.canonical_facts == {'host_name': 'foo_host_default'} | ||
assert host_audit_entry.facts == {'device_type': 'Fake Host'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2013, Michael Williamson | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters