1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/test/integration/roles/test_lineinfile/tasks/main.yml

296 lines
9.1 KiB
YAML

# test code for the lineinfile module
# (c) 2014, James Cammarata <jcammarata@ansible.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/>.
- name: deploy the test file for lineinfile
copy: src=test.txt dest={{output_dir}}/test.txt
register: result
- name: assert that the test file was deployed
assert:
that:
- "result.changed == true"
- "result.md5sum == '6be7fb7fa7fb758c80a6dc0722979c40'"
- "result.state == 'file'"
- name: insert a line at the beginning of the file, and back it up
lineinfile: dest={{output_dir}}/test.txt state=present line="New line at the beginning" insertbefore="BOF" backup=yes
register: result
- name: assert that the line was inserted at the head of the file
assert:
that:
- "result.changed == true"
- "result.msg == 'line added'"
- "result.backup != ''"
- name: stat the backup file
stat: path={{result.backup}}
register: result
- name: assert the backup file matches the previous md5
assert:
that:
- "result.stat.md5 == '6be7fb7fa7fb758c80a6dc0722979c40'"
- name: stat the test after the insert at the head
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after the insert at the head
assert:
that:
- "result.stat.md5 == '07c16434644a2a3cc1807c685917443a'"
- name: insert a line at the end of the file
lineinfile: dest={{output_dir}}/test.txt state=present line="New line at the end" insertafter="EOF"
register: result
- name: assert that the line was inserted at the end of the file
assert:
that:
- "result.changed == true"
- "result.msg == 'line added'"
- name: stat the test after the insert at the end
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after the insert at the end
assert:
that:
- "result.stat.md5 == 'da4c2150e5782fcede1840280ab87eff'"
- name: insert a line after the first line
lineinfile: dest={{output_dir}}/test.txt state=present line="New line after line 1" insertafter="^This is line 1$"
register: result
- name: assert that the line was inserted after the first line
assert:
that:
- "result.changed == true"
- "result.msg == 'line added'"
- name: stat the test after insert after the first line
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after the insert after the first line
assert:
that:
- "result.stat.md5 == '196722c8faaa28b960bee66fa4cce58c'"
- name: insert a line before the last line
lineinfile: dest={{output_dir}}/test.txt state=present line="New line after line 5" insertbefore="^This is line 5$"
register: result
- name: assert that the line was inserted before the last line
assert:
that:
- "result.changed == true"
- "result.msg == 'line added'"
- name: stat the test after the insert before the last line
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after the insert before the last line
assert:
that:
- "result.stat.md5 == 'd5955ee042139dfef16dbe3a7334475f'"
- name: replace a line with backrefs
lineinfile: dest={{output_dir}}/test.txt state=present line="This is line 3" backrefs=yes regexp="^(REF) .* \\1$"
register: result
- name: assert that the line with backrefs was changed
assert:
that:
- "result.changed == true"
- "result.msg == 'line replaced'"
- name: stat the test after the backref line was replaced
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after backref line was replaced
assert:
that:
- "result.stat.md5 == '0f585270054e17be242743dd31c6f593'"
- name: remove the middle line
lineinfile: dest={{output_dir}}/test.txt state=absent regexp="^This is line 3$"
register: result
- name: assert that the line was inserted at the head of the file
assert:
that:
- "result.changed == true"
- "result.msg == '1 line(s) removed'"
- name: stat the test after the middle line was removed
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after the middle line was removed
assert:
that:
- "result.stat.md5 == '661603660051991b79429c2dc68d9a67'"
- name: run a validation script that succeeds
lineinfile: dest={{output_dir}}/test.txt state=absent regexp="^This is line 5$" validate="/bin/true %s"
register: result
- name: assert that the file validated after removing a line
assert:
that:
- "result.changed == true"
- "result.msg == '1 line(s) removed'"
- name: stat the test after the validation succeeded
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after the validation succeeded
assert:
that:
- "result.stat.md5 == '9af984939bd859f7794661e501b4f1a4'"
- name: run a validation script that fails
lineinfile: dest={{output_dir}}/test.txt state=absent regexp="^This is line 1$" validate="/bin/false %s"
register: result
ignore_errors: yes
- name: assert that the validate failed
assert:
that:
- "result.failed == true"
- name: stat the test after the validation failed
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches the previous after the validation failed
assert:
that:
- "result.stat.md5 == '9af984939bd859f7794661e501b4f1a4'"
- name: use create=yes
lineinfile: dest={{output_dir}}/new_test.txt create=yes insertbefore=BOF state=present line="This is a new file"
register: result
- name: assert that the new file was created
assert:
that:
- "result.changed == true"
- "result.msg == 'line added'"
- name: validate that the newly created file exists
stat: path={{output_dir}}/new_test.txt
register: result
ignore_errors: yes
- name: assert the newly created test md5 matches
assert:
that:
- "result.stat.md5 == 'fef1d487711facfd7aa2c87d788c19d9'"
# Test EOF in cases where file has no newline at EOF
- name: testnoeof deploy the file for lineinfile
copy: src=testnoeof.txt dest={{output_dir}}/testnoeof.txt
register: result
- name: testnoeof insert a line at the end of the file
lineinfile: dest={{output_dir}}/testnoeof.txt state=present line="New line at the end" insertafter="EOF"
register: result
- name: testempty assert that the line was inserted at the end of the file
assert:
that:
- "result.changed == true"
- "result.msg == 'line added'"
- name: insert a multiple lines at the end of the file
lineinfile: dest={{output_dir}}/test.txt state=present line="This is a line\nwith \\\n character" insertafter="EOF"
register: result
- name: assert that the multiple lines was inserted
assert:
that:
- "result.changed == true"
- "result.msg == 'line added'"
- name: testnoeof stat the no newline EOF test after the insert at the end
stat: path={{output_dir}}/testnoeof.txt
register: result
- name: testnoeof assert test md5 matches after the insert at the end
assert:
that:
- "result.stat.md5 == 'f75c9d51f45afd7295000e63ce655220'"
# Test EOF with empty file to make sure no unneccessary newline is added
- name: testempty deploy the testempty file for lineinfile
copy: src=testempty.txt dest={{output_dir}}/testempty.txt
register: result
- name: testempty insert a line at the end of the file
lineinfile: dest={{output_dir}}/testempty.txt state=present line="New line at the end" insertafter="EOF"
register: result
- name: testempty assert that the line was inserted at the end of the file
assert:
that:
- "result.changed == true"
- "result.msg == 'line added'"
- name: testempty stat the test after the insert at the end
stat: path={{output_dir}}/testempty.txt
register: result
- name: testempty assert test md5 matches after the insert at the end
assert:
that:
- "result.stat.md5 == '357dcbee8dfb4436f63bab00a235c45a'"
- stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after insert the multiple lines
assert:
that:
- "result.stat.md5 == 'c2510d5bc8fdef8e752b8f8e74c784c2'"
- name: replace a line with backrefs included in the line
lineinfile: dest={{output_dir}}/test.txt state=present line="New \\1 created with the backref" backrefs=yes regexp="^This is (line 4)$"
register: result
- name: assert that the line with backrefs was changed
assert:
that:
- "result.changed == true"
- "result.msg == 'line replaced'"
- name: stat the test after the backref line was replaced
stat: path={{output_dir}}/test.txt
register: result
- name: assert test md5 matches after backref line was replaced
assert:
that:
- "result.stat.md5 == '65f955c2a9722fd43d07103d7756ff9b'"