summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/blockinfile/tasks
diff options
context:
space:
mode:
Diffstat (limited to 'test/integration/targets/blockinfile/tasks')
-rw-r--r--test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml52
-rw-r--r--test/integration/targets/blockinfile/tasks/block_without_trailing_newline.yml30
-rw-r--r--test/integration/targets/blockinfile/tasks/create_file.yml32
-rw-r--r--test/integration/targets/blockinfile/tasks/diff.yml18
-rw-r--r--test/integration/targets/blockinfile/tasks/file_without_trailing_newline.yml36
-rw-r--r--test/integration/targets/blockinfile/tasks/insertafter.yml37
-rw-r--r--test/integration/targets/blockinfile/tasks/insertbefore.yml39
-rw-r--r--test/integration/targets/blockinfile/tasks/main.yml41
-rw-r--r--test/integration/targets/blockinfile/tasks/multiline_search.yml70
-rw-r--r--test/integration/targets/blockinfile/tasks/preserve_line_endings.yml24
-rw-r--r--test/integration/targets/blockinfile/tasks/validate.yml28
11 files changed, 407 insertions, 0 deletions
diff --git a/test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml b/test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml
new file mode 100644
index 0000000..c610905
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml
@@ -0,0 +1,52 @@
+- name: copy the sshd_config to the test dir
+ copy:
+ src: sshd_config
+ dest: "{{ remote_tmp_dir_test }}"
+
+- name: insert/update "Match User" configuration block in sshd_config
+ blockinfile:
+ path: "{{ remote_tmp_dir_test }}/sshd_config"
+ block: |
+ Match User ansible-agent
+ PasswordAuthentication no
+ backup: yes
+ register: blockinfile_test0
+
+- name: ensure we have a bcackup file
+ assert:
+ that:
+ - "'backup_file' in blockinfile_test0"
+
+- name: check content
+ shell: 'grep -c -e "Match User ansible-agent" -e "PasswordAuthentication no" {{ remote_tmp_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: "{{ remote_tmp_dir_test }}/sshd_config"
+ block: |
+ Match User ansible-agent
+ PasswordAuthentication no
+ register: blockinfile_test1
+
+- name: validate idempotence results
+ assert:
+ that:
+ - 'not blockinfile_test1.changed'
diff --git a/test/integration/targets/blockinfile/tasks/block_without_trailing_newline.yml b/test/integration/targets/blockinfile/tasks/block_without_trailing_newline.yml
new file mode 100644
index 0000000..466e86c
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/block_without_trailing_newline.yml
@@ -0,0 +1,30 @@
+- name: Add block without trailing line separator
+ blockinfile:
+ path: "{{ remote_tmp_dir_test }}/chomped_block_test.txt"
+ create: yes
+ content: |-
+ one
+ two
+ three
+ register: chomptest1
+
+- name: Add block without trailing line separator again
+ blockinfile:
+ path: "{{ remote_tmp_dir_test }}/chomped_block_test.txt"
+ content: |-
+ one
+ two
+ three
+ register: chomptest2
+
+- name: Check output file
+ stat:
+ path: "{{ remote_tmp_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'
diff --git a/test/integration/targets/blockinfile/tasks/create_file.yml b/test/integration/targets/blockinfile/tasks/create_file.yml
new file mode 100644
index 0000000..c8ded30
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/create_file.yml
@@ -0,0 +1,32 @@
+- name: Create a file with blockinfile
+ blockinfile:
+ path: "{{ remote_tmp_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: "{{ remote_tmp_dir_test }}/empty.txt"
+ block: |
+ Hey
+ there
+ state: absent
+ create: yes
+ register: empty_test_2
+
+- stat:
+ path: "{{ remote_tmp_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
diff --git a/test/integration/targets/blockinfile/tasks/diff.yml b/test/integration/targets/blockinfile/tasks/diff.yml
new file mode 100644
index 0000000..56ef08d
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/diff.yml
@@ -0,0 +1,18 @@
+- name: Create a test file
+ copy:
+ content: diff test
+ dest: "{{ remote_tmp_dir_test }}/diff.txt"
+
+- name: Add block to file with diff
+ blockinfile:
+ path: "{{ remote_tmp_dir_test }}/diff.txt"
+ block: |
+ line 1
+ line 2
+ register: difftest
+ diff: yes
+
+- name: Ensure diff was shown
+ assert:
+ that:
+ - difftest.diff | length > 0
diff --git a/test/integration/targets/blockinfile/tasks/file_without_trailing_newline.yml b/test/integration/targets/blockinfile/tasks/file_without_trailing_newline.yml
new file mode 100644
index 0000000..797ffc5
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/file_without_trailing_newline.yml
@@ -0,0 +1,36 @@
+- name: Create file without trailing newline
+ copy:
+ content: '# File with no newline'
+ dest: "{{ remote_tmp_dir_test }}/no_newline_at_end.txt"
+ register: no_newline
+
+
+- name: Add block to file that does not have a newline at the end
+ blockinfile:
+ path: "{{ remote_tmp_dir_test }}/no_newline_at_end.txt"
+ content: |
+ one
+ two
+ three
+ register: no_newline_test1
+
+- name: Add block to file that does not have a newline at the end again
+ blockinfile:
+ path: "{{ remote_tmp_dir_test }}/no_newline_at_end.txt"
+ content: |
+ one
+ two
+ three
+ register: no_newline_test2
+
+- name: Stat the file
+ stat:
+ path: "{{ remote_tmp_dir_test }}/no_newline_at_end.txt"
+ register: no_newline_file
+
+- name: Ensure block was correctly written to file with no newline at end
+ assert:
+ that:
+ - no_newline_test1 is changed
+ - no_newline_test2 is not changed
+ - no_newline_file.stat.checksum == 'dab16f864025e59125e74d1498ffb2bb048224e6'
diff --git a/test/integration/targets/blockinfile/tasks/insertafter.yml b/test/integration/targets/blockinfile/tasks/insertafter.yml
new file mode 100644
index 0000000..a4cdd5f
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/insertafter.yml
@@ -0,0 +1,37 @@
+- name: Create insertafter test file
+ copy:
+ dest: "{{ remote_tmp_dir }}/after.txt"
+ content: |
+ line1
+ line2
+ line3
+
+- name: Add block using insertafter
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/after.txt"
+ insertafter: line2
+ block: |
+ block1
+ block2
+ register: after1
+
+- name: Add block using insertafter again
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/after.txt"
+ insertafter: line2
+ block: |
+ block1
+ block2
+ register: after2
+
+- name: Stat the after.txt file
+ stat:
+ path: "{{ remote_tmp_dir }}/after.txt"
+ register: after_file
+
+- name: Ensure insertafter worked correctly
+ assert:
+ that:
+ - after1 is changed
+ - after2 is not changed
+ - after_file.stat.checksum == 'a8adeb971358230a28ce554f3b8fdd1ef65fdf1c'
diff --git a/test/integration/targets/blockinfile/tasks/insertbefore.yml b/test/integration/targets/blockinfile/tasks/insertbefore.yml
new file mode 100644
index 0000000..03e51c9
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/insertbefore.yml
@@ -0,0 +1,39 @@
+- name: Create insertbefore test file
+ copy:
+ dest: "{{ remote_tmp_dir }}/before.txt"
+ content: |
+ line1
+ line2
+ line3
+
+- name: Add block using insertbefore
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/before.txt"
+ insertbefore: line2
+ block: |
+ block1
+ block2
+ register: after1
+
+- name: Add block using insertbefore again
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/before.txt"
+ insertbefore: line2
+ block: |
+ block1
+ block2
+ register: after2
+
+- name: Stat the before.txt file
+ stat:
+ path: "{{ remote_tmp_dir }}/before.txt"
+ register: after_file
+
+- command: cat {{ remote_tmp_dir }}/before.txt
+
+- name: Ensure insertbefore worked correctly
+ assert:
+ that:
+ - after1 is changed
+ - after2 is not changed
+ - after_file.stat.checksum == '16681d1d7f29d173243bb951d6afb9c0824d7bf4'
diff --git a/test/integration/targets/blockinfile/tasks/main.yml b/test/integration/targets/blockinfile/tasks/main.yml
new file mode 100644
index 0000000..054e554
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/main.yml
@@ -0,0 +1,41 @@
+# 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:
+ remote_tmp_dir_test: "{{ remote_tmp_dir }}/test_blockinfile"
+
+- name: make sure our testing sub-directory does not exist
+ file:
+ path: "{{ remote_tmp_dir_test }}"
+ state: absent
+
+- name: create our testing sub-directory
+ file:
+ path: "{{ remote_tmp_dir_test }}"
+ state: directory
+
+- import_tasks: add_block_to_existing_file.yml
+- import_tasks: create_file.yml
+- import_tasks: preserve_line_endings.yml
+- import_tasks: block_without_trailing_newline.yml
+- import_tasks: file_without_trailing_newline.yml
+- import_tasks: diff.yml
+- import_tasks: validate.yml
+- import_tasks: insertafter.yml
+- import_tasks: insertbefore.yml
+- import_tasks: multiline_search.yml
diff --git a/test/integration/targets/blockinfile/tasks/multiline_search.yml b/test/integration/targets/blockinfile/tasks/multiline_search.yml
new file mode 100644
index 0000000..8eb94f5
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/multiline_search.yml
@@ -0,0 +1,70 @@
+- name: Create multiline_search test file
+ copy:
+ dest: "{{ remote_tmp_dir }}/listener.ora"
+ content: |
+ LISTENER=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=IPC)(KEY=LISTENER)))) # line added by Agent
+ ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER=ON # line added by Agent
+
+ SID_LIST_LISTENER_DG =
+ (SID_LIST =
+ (SID_DESC =
+ (GLOBAL_DBNAME = DB01_DG)
+ (ORACLE_HOME = /u01/app/oracle/product/12.1.0.1/db_1)
+ (SID_NAME = DB011)
+ )
+ )
+
+ SID_LIST_LISTENER =
+ (SID_LIST =
+ (SID_DESC =
+ (GLOBAL_DBNAME = DB02)
+ (ORACLE_HOME = /u01/app/oracle/product/12.1.0.1/db_1)
+ (SID_NAME = DB021)
+ )
+ )
+
+- name: Set fact listener_line
+ set_fact:
+ listener_line: |
+ (SID_DESC =
+ (GLOBAL_DBNAME = DB03
+ (ORACLE_HOME = /u01/app/oracle/product/12.1.0.1/db_1)
+ (SID_NAME = DB031)
+ )
+
+- name: Add block using multiline_search enabled
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/listener.ora"
+ block: "{{ listener_line }}"
+ insertafter: '(?m)SID_LIST_LISTENER_DG =\n.*\(SID_LIST ='
+ marker: " <!-- {mark} ANSIBLE MANAGED BLOCK 1-->"
+ register: multiline_search1
+
+- name: Add block using multiline_search enabled again
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/listener.ora"
+ block: "{{ listener_line }}"
+ insertafter: '(?m)SID_LIST_LISTENER_DG =\n.*\(SID_LIST ='
+ marker: " <!-- {mark} ANSIBLE MANAGED BLOCK 1-->"
+ register: multiline_search2
+
+- name: Try to add block using without multiline flag in regex should add block add end of file
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/listener.ora"
+ block: "{{ listener_line }}"
+ insertafter: 'SID_LIST_LISTENER_DG =\n.*\(SID_LIST ='
+ marker: " <!-- {mark} ANSIBLE MANAGED BLOCK 2-->"
+ register: multiline_search3
+
+- name: Stat the listener.ora file
+ stat:
+ path: "{{ remote_tmp_dir }}/listener.ora"
+ register: listener_ora_file
+
+- name: Ensure insertafter worked correctly
+ assert:
+ that:
+ - multiline_search1 is changed
+ - multiline_search2 is not changed
+ - multiline_search3 is changed
+ - listener_ora_file.stat.checksum == '5a8010ac4a2fad7c822e6aeb276931657cee75c0'
diff --git a/test/integration/targets/blockinfile/tasks/preserve_line_endings.yml b/test/integration/targets/blockinfile/tasks/preserve_line_endings.yml
new file mode 100644
index 0000000..0528c3b
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/preserve_line_endings.yml
@@ -0,0 +1,24 @@
+- name: create line_endings_test.txt in the test dir
+ copy:
+ dest: "{{ remote_tmp_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: "{{ remote_tmp_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.$" {{ remote_tmp_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"'
diff --git a/test/integration/targets/blockinfile/tasks/validate.yml b/test/integration/targets/blockinfile/tasks/validate.yml
new file mode 100644
index 0000000..aa7aa63
--- /dev/null
+++ b/test/integration/targets/blockinfile/tasks/validate.yml
@@ -0,0 +1,28 @@
+- name: EXPECTED FAILURE test improper validate
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/validate.txt"
+ block: |
+ line1
+ line2
+ create: yes
+ validate: grep
+ ignore_errors: yes
+
+- name: EXPECTED FAILURE test failure to validate
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/validate.txt"
+ block: |
+ line1
+ line2
+ create: yes
+ validate: grep line47 %s
+ ignore_errors: yes
+
+- name: Test proper validate
+ blockinfile:
+ path: "{{ remote_tmp_dir }}/validate.txt"
+ block: |
+ line1
+ line2
+ create: yes
+ validate: grep line1 %s