Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/improve tests #9859

Merged
merged 2 commits into from
Mar 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/integration/targets/callback/filter_plugins/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.module_utils.six import string_types


def callback_results_extractor(outputs_results):
results = []
for result in outputs_results:
differences = []
expected_output = result['test']['expected_output']
stdout_lines = result['stdout_lines']
for i in range(max(len(expected_output), len(stdout_lines))):
line = "line_%s" % (i + 1)
test_line = stdout_lines[i] if i < len(stdout_lines) else None
expected_lines = expected_output[i] if i < len(expected_output) else None
if not isinstance(expected_lines, string_types) and expected_lines is not None:
if test_line not in expected_lines:
differences.append({
'line': {
'expected_one_of': expected_lines,
'got': test_line,
}
})
else:
if test_line != expected_lines:
differences.append({
'line': {
'expected': expected_lines,
'got': test_line,
}
})
results.append({
'name': result['test']['name'],
'output': {
'differences': differences,
'expected': expected_output,
'got': stdout_lines,
},
})
return results


class FilterModule:
''' Jinja2 compat filters '''

def filters(self):
return {
'callback_results_extractor': callback_results_extractor,
}
35 changes: 1 addition & 34 deletions tests/integration/targets/callback/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -50,44 +50,11 @@
- name: Assert test output equals expected output
assert:
that: result.output.differences | length == 0
loop: "{{ results }}"
loop: "{{ outputs.results | callback_results_extractor }}"
loop_control:
loop_var: result
label: "{{ result.name }}"
register: assertions
vars:
results: >-
{%- set results = [] -%}
{%- for result in outputs.results -%}
{%- set differences = [] -%}
{%- for i in range([result.test.expected_output | count, result.stdout_lines | count] | max) -%}
{%- set line = "line_%s" | format(i+1) -%}
{%- set test_line = result.stdout_lines[i] | default(none) -%}
{%- set expected_lines = result.test.expected_output[i] | default(none) -%}
{%- if expected_lines is not string and expected_lines is not none -%}
{%- if test_line not in expected_lines -%}
{{- differences.append({
line: {
'expected_one_of': expected_lines,
'got': test_line }}) -}}
{%- endif -%}
{%- else -%}
{%- if expected_lines != test_line -%}
{{- differences.append({
line: {
'expected': expected_lines,
'got': test_line }}) -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{- results.append({
'name': result.test.name,
'output': {
'differences': differences,
'expected': result.test.expected_output,
'got': result.stdout_lines }}) -}}
{%- endfor -%}
{{- results -}}

always:
- name: Remove temporary playbooks
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
that:
- result is changed
- result.binding_rule.AuthMethod == 'test'
- result.binding.Description == 'test-binding: my description'
- "result.binding_rule.Description == 'test-binding: my description'"
- result.operation == 'create'

- name: Update a binding rule
@@ -46,7 +46,7 @@
- assert:
that:
- result is changed
- result.binding.Description == 'test-binding: my description'
- "result.binding_rule.Description == 'test-binding: my description'"
- result.operation == 'update'

- name: Update a binding rule (noop)
@@ -58,7 +58,7 @@
- assert:
that:
- result is not changed
- result.binding.Description == 'test-binding: my description'
- "result.binding_rule.Description == 'test-binding: my description'"
- result.operation is not defined

- name: Delete a binding rule
4 changes: 2 additions & 2 deletions tests/integration/targets/consul/tasks/consul_token.yml
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
- assert:
that:
- simple_create_result is changed
- simple_create_result.token.AccessorID
- simple_create_result.token.AccessorID is truthy
- simple_create_result.operation == 'create'

- name: Create token
@@ -84,5 +84,5 @@
- assert:
that:
- result is changed
- not result.token
- result.token is falsy
- result.operation == 'remove'
3 changes: 2 additions & 1 deletion tests/integration/targets/deploy_helper/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -123,11 +123,12 @@
register: releases_path
- stat: path={{ deploy_helper.shared_path }}
register: shared_path
when: deploy_helper.shared_path is truthy
- name: Assert State=present with shared_path set to False
assert:
that:
- "releases_path.stat.exists"
- "not shared_path.stat.exists"
- "deploy_helper.shared_path is falsy or not shared_path.stat.exists"

# Setup older releases for tests
- file: path={{ deploy_helper.releases_path }}/{{ item }} state=directory
4 changes: 2 additions & 2 deletions tests/integration/targets/filter_accumulate/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -30,6 +30,6 @@
assert:
that:
- integer_result is failed
- integer_result.msg is match('Invalid value type.*')
- integer_result.msg is search('Invalid value type')
- non_uniform_list_result is failed
- non_uniform_list_result.msg is match('Unexpected templating type error.*can only concatenate str.*')
- non_uniform_list_result.msg is search('can only concatenate str')
4 changes: 2 additions & 2 deletions tests/integration/targets/filter_counter/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
assert:
that:
- res is failed
- res.msg is match('Argument for community.general.counter must be a sequence')
- res.msg is search('Argument for community.general.counter must be a sequence')

- name: test fail element not hashable
debug:
@@ -38,4 +38,4 @@
assert:
that:
- res is failed
- res.msg is match('community.general.counter needs a sequence with hashable elements')
- res.msg is search('community.general.counter needs a sequence with hashable elements')
2 changes: 1 addition & 1 deletion tests/integration/targets/filter_from_csv/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -51,4 +51,4 @@
assert:
that:
- _invalid_csv_strict_true is failed
- _invalid_csv_strict_true.msg is match('Unable to process file:.*')
- _invalid_csv_strict_true.msg is search('Unable to process file:.*')
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@

- assert:
that:
- result.msg == 'Input is not a sequence'
- result.msg is search('Input is not a sequence')

- name: 'Test error: list element not a mapping'
set_fact:
@@ -26,7 +26,7 @@

- assert:
that:
- "result.msg == 'Sequence element #0 is not a mapping'"
- "result.msg is search('Sequence element #0 is not a mapping')"

- name: 'Test error: list element does not have attribute'
set_fact:
@@ -36,7 +36,7 @@

- assert:
that:
- "result.msg == 'Attribute not contained in element #1 of sequence'"
- "result.msg is search('Attribute not contained in element #1 of sequence')"

- name: 'Test error: attribute collision'
set_fact:
@@ -46,4 +46,4 @@

- assert:
that:
- result.msg == "Multiple sequence entries have attribute value 'a'" or result.msg == "Multiple sequence entries have attribute value u'a'"
- result.msg is search("Multiple sequence entries have attribute value u?'a'")
6 changes: 3 additions & 3 deletions tests/integration/targets/filter_hashids/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@

- name: Register result of invalid salt
debug:
var: "invalid_input | community.general.hashids_encode(salt=10)"
var: "single_int | community.general.hashids_encode(salt=10)"
register: invalid_salt_message
ignore_errors: true

@@ -42,7 +42,7 @@

- name: Register result of invalid alphabet
debug:
var: "invalid_input | community.general.hashids_encode(alphabet='abc')"
var: "single_int | community.general.hashids_encode(alphabet='abc')"
register: invalid_alphabet_message
ignore_errors: true

@@ -53,7 +53,7 @@

- name: Register result of invalid min_length
debug:
var: "invalid_input | community.general.hashids_encode(min_length='foo')"
var: "single_int | community.general.hashids_encode(min_length='foo')"
register: invalid_min_length_message
ignore_errors: true

8 changes: 4 additions & 4 deletions tests/integration/targets/filter_keep_keys/tasks/tests.yml
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
- name: Debug ansible_version
ansible.builtin.debug:
var: ansible_version
when: not quite_test | d(true) | bool
when: not (quiet_test | default(true) | bool)
tags: ansible_version

- name: Tests
@@ -19,13 +19,13 @@
fail_msg: |
[ERR] result:
{{ result | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
quiet: "{{ quiet_test | default(true) | bool }}"
loop: "{{ tests | subelements('group') }}"
loop_control:
loop_var: i
label: "{{ i.1.mp | d('default') }}: {{ i.1.tt }}"
label: "{{ i.1.mp | default('default') }}: {{ i.1.tt }}"
vars:
input: "{{ i.0.input }}"
target: "{{ i.1.tt }}"
mp: "{{ i.1.mp | d('default') }}"
mp: "{{ i.1.mp | default('default') }}"
result: "{{ lookup('template', i.0.template) }}"
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
{{ my_list|to_nice_yaml|indent(2) }}
my_list|difference(result101):
{{ my_list|difference(result101)|to_nice_yaml|indent(2) }}
when: debug_test|d(false)|bool
when: debug_test|default(false)|bool
- name: Merge 2 lists by attribute name. list_merge='keep'. assert
assert:
that: my_list | difference(result101) | length == 0
@@ -35,7 +35,7 @@
{{ my_list|to_nice_yaml|indent(2) }}
my_list|difference(result102):
{{ my_list|difference(result102)|to_nice_yaml|indent(2) }}
when: debug_test|d(false)|bool
when: debug_test|default(false)|bool
- name: Merge 2 lists by attribute name. list_merge='append'. assert
assert:
that: my_list | difference(result102) | length == 0
@@ -54,7 +54,7 @@
{{ my_list|to_nice_yaml|indent(2) }}
my_list|difference(result103):
{{ my_list|difference(result103)|to_nice_yaml|indent(2) }}
when: debug_test|d(false)|bool
when: debug_test|default(false)|bool
- name: Merge 2 lists by attribute name. list_merge='prepend'. assert
assert:
that: my_list | difference(result103) | length == 0
@@ -73,7 +73,7 @@
{{ my_list|to_nice_yaml|indent(2) }}
my_list|difference(result104):
{{ my_list|difference(result104)|to_nice_yaml|indent(2) }}
when: debug_test|d(false)|bool
when: debug_test|default(false)|bool
- name: Merge 2 lists by attribute name. list_merge='append_rp'. assert
assert:
that: my_list | difference(result104) | length == 0
@@ -92,7 +92,7 @@
{{ my_list|to_nice_yaml|indent(2) }}
my_list|difference(result105):
{{ my_list|difference(result105)|to_nice_yaml|indent(2) }}
when: debug_test|d(false)|bool
when: debug_test|default(false)|bool
- name: Merge 2 lists by attribute name. list_merge='prepend_rp'. assert
assert:
that: my_list | difference(result105) | length == 0
@@ -115,7 +115,7 @@
{{ my_list|to_nice_yaml|indent(2) }}
my_list|difference(result200):
{{ my_list|difference(result200)|to_nice_yaml|indent(2) }}
when: debug_test|d(false)|bool
when: debug_test|default(false)|bool
- name: Merge by name. recursive=True list_merge='append_rp'. assert
assert:
that: my_list | difference(result200) | length == 0
@@ -136,7 +136,7 @@
{{ my_list|to_nice_yaml|indent(2) }}
my_list|difference(result201):
{{ my_list|difference(result201)|to_nice_yaml|indent(2) }}
when: debug_test|d(false)|bool
when: debug_test|default(false)|bool
- name: Merge by name. recursive=False list_merge='append_rp'. assert
assert:
that: my_list | difference(result201) | length == 0
Loading