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

[test coverage] lineinfile testing #74122

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions test/integration/targets/lineinfile/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,59 @@
- "result.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'"
- "result.state == 'file'"

- name: "create a file that does not yet exist with `create: yes` and produce diff"
lineinfile:
dest: "{{ output_dir }}/a/a.txt"
state: present
line: "First line"
create: yes
diff: yes
register: result1

- name: assert that a diff was returned
assert:
that:
- result1.diff | length > 0

- name: stat the new file
stat:
path: "{{ output_dir }}/a/a.txt"
register: result

- name: assert that the file exists
assert:
that:
- result.stat.exists

- block:
- name: "EXPECTED FAILURE - test source file does not exist w/o `create: yes`"
lineinfile:
path: "/some/where/that/doesnotexist.txt"
state: present
line: "Doesn't matter"
- fail:
msg: "Should not get here"
rescue:
- name: Validate failure
assert:
that:
- "'Destination /some/where/that/doesnotexist.txt does not exist !' in ansible_failed_result.msg"

- block:
- name: EXPECTED FAILURE - test invalid `validate` value
lineinfile:
path: "{{ output_dir }}/test.txt"
state: present
line: "Doesn't matter"
validate: '/some/path'
- fail:
msg: "Should not get here"
rescue:
- name: Validate failure
assert:
that:
- "'validate must contain %s: /some/path' in ansible_failed_result.msg"

- name: insert a line at the beginning of the file, and back it up
lineinfile:
dest: "{{ output_dir }}/test.txt"
Expand Down