Skip to content

Commit

Permalink
test: sort files to compare
Browse files Browse the repository at this point in the history
On some platforms the parameters are stored in a `dict`, and the ordering
of the elements is not guaranteed to be stable.  So when comparing
the expected results to the actual file, sort both.

Signed-off-by: Rich Megginson <[email protected]>
  • Loading branch information
richm committed Jul 31, 2024
1 parent b8af0cd commit 4e37d29
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 79 deletions.
17 changes: 17 additions & 0 deletions tests/tasks/compare_file_to_content.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
# input
# * expected_content - the expected contents
# * compare_file - the actual contents
# NOTE: Have to sort because order of dict is not guaranteed to
# be stable on all supported platforms
- name: Get file to compare
slurp:
path: "{{ compare_file }}"
register: __actual_content

- name: Compare expected to actual
assert:
that: __expected == __actual
vars:
__expected: "{{ expected_content.split('\n') | sort | select | list }}"
__actual: "{{ (__actual_content.content | b64decode).split('\n') | sort | select | list }}"
121 changes: 42 additions & 79 deletions tests/tests_basics_forwards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@
grep '{{ __test_template_trad }}' '{{ __test_forward_module_conf }}'
changed_when: false
- name: Generate a file to check severity_and_facility
copy:
dest: /tmp/__testfile__
content: |
- name: Check severity_and_facility
include_tasks: tasks/compare_file_to_content.yml
vars:
compare_file: "{{ __test_forward_conf_s_f }}"
expected_content: |
#
# Ansible managed
#
Expand All @@ -166,16 +167,12 @@
queue.saveonshutdown="on"
)
}
mode: '0600'
- name: Check severity_and_facility
command: diff -B /tmp/__testfile__ '{{ __test_forward_conf_s_f }}'
changed_when: false

- name: Generate a file to check facility_only
copy:
dest: /tmp/__testfile__
content: |
- name: Check facility_only
include_tasks: tasks/compare_file_to_content.yml
vars:
compare_file: "{{ __test_forward_conf_f }}"
expected_content: |
#
# Ansible managed
#
Expand All @@ -189,16 +186,12 @@
Template="{{ __test_template }}"
)
}
mode: '0600'

- name: Check facility_only
command: diff -B /tmp/__testfile__ '{{ __test_forward_conf_f }}'
changed_when: false
- name: Generate a file to check severity_only
copy:
dest: /tmp/__testfile__
content: |
- name: Check severity_only
include_tasks: tasks/compare_file_to_content.yml
vars:
compare_file: "{{ __test_forward_conf_s }}"
expected_content: |
#
# Ansible managed
#
Expand All @@ -212,16 +205,12 @@
Template="{{ __test_template }}"
)
}
mode: '0600'
- name: Check severity_only
command: diff -B /tmp/__testfile__ '{{ __test_forward_conf_s }}'
changed_when: false

- name: Generate a file to check no_severity_and_facility
copy:
dest: /tmp/__testfile__
content: |
- name: Check no_severity_and_facility
include_tasks: tasks/compare_file_to_content.yml
vars:
compare_file: "{{ __test_forward_conf_no }}"
expected_content: |
#
# Ansible managed
#
Expand All @@ -235,16 +224,12 @@
Template="{{ __test_template }}"
)
}
mode: '0600'

- name: Check no_severity_and_facility
command: diff -B /tmp/__testfile__ '{{ __test_forward_conf_no }}'
changed_when: false
- name: Generate a file to check no_severity_and_facility_udp
copy:
dest: /tmp/__testfile__
content: |
- name: Check no_severity_and_facility_udp
include_tasks: tasks/compare_file_to_content.yml
vars:
compare_file: "{{ __test_forward_conf_no_udp }}"
expected_content: |
#
# Ansible managed
#
Expand All @@ -258,17 +243,13 @@
Template="{{ __test_template }}"
)
}
mode: '0600'

- name: Check no_severity_and_facility_udp
command: diff -B /tmp/__testfile__ '{{ __test_forward_conf_no_udp }}'
changed_when: false
# yamllint disable rule:line-length
- name: Generate a file to check no_severity_and_facility_protocol_port
copy:
dest: /tmp/__testfile__
content: |
- name: Check no_severity_and_facility_protocol_port
include_tasks: tasks/compare_file_to_content.yml
vars:
compare_file: "{{ __test_forward_conf_no_p_p }}"
expected_content: |
#
# Ansible managed
#
Expand All @@ -280,27 +261,22 @@
Template="{{ __test_template }}"
)
}
mode: '0600'

- name: Check no_severity_and_facility_protocol_port
command: diff -B /tmp/__testfile__ '{{ __test_forward_conf_no_p_p }}'
changed_when: false
- name: Grep no_severity_and_facility_protocol_port_target
shell: |-
set -euo pipefail
/bin/grep '<action.*forwards_no_severity_and_facility_protocol_port_target>' /etc/rsyslog.d/30-output-forwards*.conf
register: __result
changed_when: false
failed_when: "__result is not failed"
failed_when: __result is not failed

- name: Grep no_name
shell: |-
set -euo pipefail
/bin/grep '<action.*forwards_no_name\.localdomain>' /etc/rsyslog.d/30-output-forwards*.conf
register: __result
changed_when: false
failed_when: "__result is not failed"
failed_when: __result is not failed

- name: Check output to messages line
command: >-
Expand Down Expand Up @@ -386,10 +362,11 @@
__check_systemctl_status: true
include_tasks: tasks/check_daemon_config_files.yml

- name: Generate a file to check severity_and_facility
copy:
dest: /tmp/__testfile__
content: |
- name: Check severity_and_facility
include_tasks: tasks/compare_file_to_content.yml
vars:
compare_file: "{{ __test_forward_conf_s_f }}"
expected_content: |
#
# Ansible managed
#
Expand All @@ -407,11 +384,6 @@
Template="{{ __test_template }}"
)
}
mode: '0600'

- name: Check severity_and_facility
command: diff -B /tmp/__testfile__ '{{ __test_forward_conf_s_f }}'
changed_when: false
- name: Check the fake ca cert is successfully copied
stat:
Expand Down Expand Up @@ -504,10 +476,11 @@
__check_systemctl_status: true
include_tasks: tasks/check_daemon_config_files.yml

- name: Generate a file to check severity_and_facility
copy:
dest: /tmp/__testfile__
content: |
- name: Check severity_and_facility
include_tasks: tasks/compare_file_to_content.yml
vars:
compare_file: "{{ __test_forward_conf_s_f }}"
expected_content: |
#
# Ansible managed
#
Expand All @@ -525,11 +498,6 @@
Template="{{ __test_template }}"
)
}
mode: '0600'

- name: Check severity_and_facility
command: diff -B /tmp/__testfile__ '{{ __test_forward_conf_s_f }}'
changed_when: false
- name: Check the fake key/certs are successfully copied
stat:
Expand Down Expand Up @@ -653,8 +621,3 @@
path: "{{ _tmpdir.path }}"
state: absent
delegate_to: localhost

- name: Remove test file
file:
path: /tmp/__testfile__
state: absent

0 comments on commit 4e37d29

Please sign in to comment.