ansible/test/integration/targets/blockinfile/tasks/main.yml
Sam Doran d7618712fd
blockinfile - fix line end regression (#69734)
PR #66461 introduced a regression that resulted in an in correct block in the file
if the block to be inserted did not end with a line separator. Fix this bug and add
tests to cover this scenario.
2020-05-27 16:54:38 -04:00

171 lines
4.7 KiB
YAML

# Test code for the blockinfile module.
# (c) 2017, James Tanner <tanner.jc@gmail.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- set_fact:
output_dir_test: "{{ output_dir }}/test_blockinfile"
- name: make sure our testing sub-directory does not exist
file:
path: "{{ output_dir_test }}"
state: absent
- name: create our testing sub-directory
file:
path: "{{ output_dir_test }}"
state: directory
##
## blockinfile
##
- name: copy the sshd_config to the test dir
copy:
src: sshd_config
dest: "{{ output_dir_test }}"
- name: insert/update "Match User" configuration block in sshd_config
blockinfile:
path: "{{ output_dir_test }}/sshd_config"
block: |
Match User ansible-agent
PasswordAuthentication no
register: blockinfile_test0
- name: check content
shell: 'grep -c -e "Match User ansible-agent" -e "PasswordAuthentication no" {{ output_dir_test }}/sshd_config'
register: blockinfile_test0_grep
- debug:
var: blockinfile_test0
verbosity: 1
- debug:
var: blockinfile_test0_grep
verbosity: 1
- name: validate first example results
assert:
that:
- 'blockinfile_test0.changed is defined'
- 'blockinfile_test0.msg is defined'
- 'blockinfile_test0.changed'
- 'blockinfile_test0.msg == "Block inserted"'
- 'blockinfile_test0_grep.stdout == "2"'
- name: check idemptotence
blockinfile:
path: "{{ output_dir_test }}/sshd_config"
block: |
Match User ansible-agent
PasswordAuthentication no
register: blockinfile_test1
- name: validate idempotence results
assert:
that:
- 'not blockinfile_test1.changed'
- name: Create a file with blockinfile
blockinfile:
path: "{{ output_dir_test }}/empty.txt"
block: |
Hey
there
state: present
create: yes
register: empty_test_1
- name: Run a task that results in an empty file
blockinfile:
path: "{{ output_dir_test }}/empty.txt"
block: |
Hey
there
state: absent
create: yes
register: empty_test_2
- stat:
path: "{{ output_dir_test }}/empty.txt"
register: empty_test_stat
- name: Ensure empty file was created
assert:
that:
- empty_test_1 is changed
- "'File created' in empty_test_1.msg"
- empty_test_2 is changed
- "'Block removed' in empty_test_2.msg"
- empty_test_stat.stat.size == 0
- name: create line_endings_test.txt in the test dir
copy:
dest: "{{ output_dir_test }}/line_endings_test.txt"
# generating the content like this instead of copying a fixture file
# prevents sanity checks from warning about mixed line endings
content: "unix\nunix\nunix\n\ndos\r\ndos\r\ndos\r\n\nunix\nunix\n# BEGIN ANSIBLE MANAGED BLOCK\ndos\r\n# END ANSIBLE MANAGED BLOCK\nunix\nunix\nunix\nunix\n"
- name: insert/update "dos" configuration block in line_endings_test.txt
blockinfile:
path: "{{ output_dir_test }}/line_endings_test.txt"
block: "dos\r\ndos\r\ndos\r\n"
register: blockinfile_test2
- name: check content
# using the more precise `grep -Pc "^dos\\r$" ...` fails on BSD/macOS
shell: 'grep -c "^dos.$" {{ output_dir_test }}/line_endings_test.txt'
register: blockinfile_test2_grep
- name: validate line_endings_test.txt results
assert:
that:
- 'blockinfile_test2 is changed'
- 'blockinfile_test2.msg == "Block inserted"'
- 'blockinfile_test2_grep.stdout == "6"'
- name: Add block without trailing line separator
blockinfile:
path: "{{ output_dir_test }}/chomped_block_test.txt"
create: yes
content: |-
one
two
three
register: chomptest1
- name: Add block without trailing line separator again
blockinfile:
path: "{{ output_dir_test }}/chomped_block_test.txt"
content: |-
one
two
three
register: chomptest2
- name: Check output file
stat:
path: "{{ output_dir_test }}/chomped_block_test.txt"
register: chomptest_file
- name: Ensure chomptest results are correct
assert:
that:
- chomptest1 is changed
- chomptest2 is not changed
- chomptest_file.stat.checksum == '50d49f528a5f7147c7029ed6220c326b1ee2c4ae'