mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-08 22:30:04 -07:00
ini_file - add feature 'section_has_values' (#7505)
* insert new code * add changelog * add argument_spec * sanity check * docstring version_added * version-added-must-be-major-or-minor * Update plugins/modules/ini_file.py Co-authored-by: Felix Fontein <felix@fontein.de> * check for default value `None` * typo in example * add integration test and rename option * add license * update "version added" in docstring * insert new code * remove whitespace * update examples * support exclusive, allow_no_value, multiple values in section_has_values * prefer Todd's variable naming in loops * resolve number clash in file names * pass sanity test validate-modules * Documentation updates --------- Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Todd Lewis <todd_lewis@unc.edu>
This commit is contained in:
parent
865de5baa0
commit
be4d5b7dc4
4 changed files with 476 additions and 7 deletions
|
@ -16,7 +16,6 @@
|
|||
|
||||
- name: include tasks
|
||||
block:
|
||||
|
||||
- name: include tasks to perform basic tests
|
||||
include_tasks: tests/00-basic.yml
|
||||
|
||||
|
@ -50,3 +49,6 @@
|
|||
|
||||
- name: include tasks to test optional spaces in section headings
|
||||
include_tasks: tests/07-section_name_spaces.yml
|
||||
|
||||
- name: include tasks to test section_has_values
|
||||
include_tasks: tests/08-section.yml
|
||||
|
|
341
tests/integration/targets/ini_file/tasks/tests/08-section.yml
Normal file
341
tests/integration/targets/ini_file/tasks/tests/08-section.yml
Normal file
|
@ -0,0 +1,341 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
## testing section selection
|
||||
|
||||
- name: test-section 1 - Create starting ini file
|
||||
copy:
|
||||
content: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = pineapple juice
|
||||
|
||||
dest: "{{ output_file }}"
|
||||
|
||||
- name: test-section 1 - Modify starting ini file
|
||||
ini_file:
|
||||
dest: "{{ output_file }}"
|
||||
section: drinks
|
||||
option: car
|
||||
value: volvo
|
||||
state: present
|
||||
register: result1
|
||||
|
||||
- name: test-section 1 - Read modified file
|
||||
slurp:
|
||||
src: "{{ output_file }}"
|
||||
register: output_content
|
||||
|
||||
- name: test-section 1 - Create expected result
|
||||
set_fact:
|
||||
expected1: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
car = volvo
|
||||
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = pineapple juice
|
||||
output1: "{{ output_content.content | b64decode }}"
|
||||
|
||||
- name: test-section 1 - Option was added to first section
|
||||
assert:
|
||||
that:
|
||||
- result1 is changed
|
||||
- result1.msg == 'option added'
|
||||
- output1 == expected1
|
||||
|
||||
# ----------------
|
||||
|
||||
- name: test-section 2 - Create starting ini file
|
||||
copy:
|
||||
content: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = pineapple juice
|
||||
|
||||
dest: "{{ output_file }}"
|
||||
|
||||
- name: test-section 2 - Modify starting ini file
|
||||
ini_file:
|
||||
dest: "{{ output_file }}"
|
||||
section: drinks
|
||||
section_has_values:
|
||||
- option: beverage
|
||||
value: pineapple juice
|
||||
option: car
|
||||
value: volvo
|
||||
state: present
|
||||
register: result1
|
||||
|
||||
- name: test-section 2 - Read modified file
|
||||
slurp:
|
||||
src: "{{ output_file }}"
|
||||
register: output_content
|
||||
|
||||
- name: test-section 2 - Create expected result
|
||||
set_fact:
|
||||
expected1: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = pineapple juice
|
||||
car = volvo
|
||||
output1: "{{ output_content.content | b64decode }}"
|
||||
|
||||
- name: test-section 2 - Option added to second section specified with section_has_values
|
||||
assert:
|
||||
that:
|
||||
- result1 is changed
|
||||
- result1.msg == 'option added'
|
||||
- output1 == expected1
|
||||
|
||||
# ----------------
|
||||
|
||||
- name: test-section 3 - Create starting ini file
|
||||
copy:
|
||||
content: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = pineapple juice
|
||||
|
||||
dest: "{{ output_file }}"
|
||||
|
||||
- name: test-section 3 - Modify starting ini file
|
||||
ini_file:
|
||||
dest: "{{ output_file }}"
|
||||
section: drinks
|
||||
section_has_values:
|
||||
- option: beverage
|
||||
value: pineapple juice
|
||||
option: fav
|
||||
value: lemonade
|
||||
state: absent
|
||||
register: result1
|
||||
|
||||
- name: test-section 3 - Read modified file
|
||||
slurp:
|
||||
src: "{{ output_file }}"
|
||||
register: output_content
|
||||
|
||||
- name: test-section 3 - Create expected result
|
||||
set_fact:
|
||||
expected1: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
beverage = pineapple juice
|
||||
output1: "{{ output_content.content | b64decode }}"
|
||||
|
||||
- name: test-section 3 - Option was removed from specified section
|
||||
assert:
|
||||
that:
|
||||
- result1 is changed
|
||||
- result1.msg == 'option changed'
|
||||
- output1 == expected1
|
||||
|
||||
# ----------------
|
||||
|
||||
- name: test-section 4 - Create starting ini file
|
||||
copy:
|
||||
content: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = pineapple juice
|
||||
|
||||
dest: "{{ output_file }}"
|
||||
|
||||
- name: test-section 4 - Modify starting ini file
|
||||
ini_file:
|
||||
dest: "{{ output_file }}"
|
||||
section: drinks
|
||||
section_has_values:
|
||||
- option: beverage
|
||||
value: alligator slime
|
||||
option: fav
|
||||
value: tea
|
||||
state: present
|
||||
register: result1
|
||||
|
||||
- name: test-section 4 - Read modified file
|
||||
slurp:
|
||||
src: "{{ output_file }}"
|
||||
register: output_content
|
||||
|
||||
- name: test-section 4 - Create expected result
|
||||
set_fact:
|
||||
expected1: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = pineapple juice
|
||||
[drinks]
|
||||
beverage = alligator slime
|
||||
fav = tea
|
||||
output1: "{{ output_content.content | b64decode }}"
|
||||
|
||||
- name: test-section 4 - New section created, including required values
|
||||
assert:
|
||||
that:
|
||||
- result1 is changed
|
||||
- result1.msg == 'section and option added'
|
||||
- output1 == expected1
|
||||
|
||||
# ----------------
|
||||
|
||||
- name: test-section 5 - Modify test-section 4 result file
|
||||
ini_file:
|
||||
dest: "{{ output_file }}"
|
||||
section: drinks
|
||||
section_has_values:
|
||||
- option: fav
|
||||
value: lemonade
|
||||
- option: beverage
|
||||
value: pineapple juice
|
||||
state: absent
|
||||
register: result1
|
||||
|
||||
- name: test-section 5 - Read modified file
|
||||
slurp:
|
||||
src: "{{ output_file }}"
|
||||
register: output_content
|
||||
|
||||
- name: test-section 5 - Create expected result
|
||||
set_fact:
|
||||
expected1: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
beverage = alligator slime
|
||||
fav = tea
|
||||
output1: "{{ output_content.content | b64decode }}"
|
||||
|
||||
- name: test-section 5 - Section removed as specified
|
||||
assert:
|
||||
that:
|
||||
- result1 is changed
|
||||
- result1.msg == 'section removed'
|
||||
- output1 == expected1
|
||||
|
||||
# ----------------
|
||||
|
||||
- name: test-section 6 - Modify test-section 5 result file with multiple values
|
||||
ini_file:
|
||||
dest: "{{ output_file }}"
|
||||
section: drinks
|
||||
section_has_values:
|
||||
- option: fav
|
||||
values:
|
||||
- cherry
|
||||
- lemon
|
||||
- vanilla
|
||||
- option: beverage
|
||||
value: pineapple juice
|
||||
state: present
|
||||
option: fav
|
||||
values:
|
||||
- vanilla
|
||||
- grape
|
||||
exclusive: false
|
||||
register: result1
|
||||
|
||||
- name: test-section 6 - Read modified file
|
||||
slurp:
|
||||
src: "{{ output_file }}"
|
||||
register: output_content
|
||||
|
||||
- name: test-section 6 - Create expected result
|
||||
set_fact:
|
||||
expected1: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
beverage = alligator slime
|
||||
fav = tea
|
||||
[drinks]
|
||||
beverage = pineapple juice
|
||||
fav = vanilla
|
||||
fav = grape
|
||||
fav = cherry
|
||||
fav = lemon
|
||||
output1: "{{ output_content.content | b64decode }}"
|
||||
|
||||
- name: test-section 6 - New section added
|
||||
assert:
|
||||
that:
|
||||
- result1 is changed
|
||||
- result1.msg == 'section and option added'
|
||||
- output1 == expected1
|
||||
|
||||
# ----------------
|
||||
|
||||
- name: test-section 7 - Modify test-section 6 result file with exclusive value
|
||||
ini_file:
|
||||
dest: "{{ output_file }}"
|
||||
section: drinks
|
||||
section_has_values:
|
||||
- option: fav
|
||||
value: vanilla
|
||||
state: present
|
||||
option: fav
|
||||
value: cherry
|
||||
exclusive: true
|
||||
register: result1
|
||||
|
||||
- name: test-section 7 - Read modified file
|
||||
slurp:
|
||||
src: "{{ output_file }}"
|
||||
register: output_content
|
||||
|
||||
- name: test-section 7 - Create expected result
|
||||
set_fact:
|
||||
expected1: |
|
||||
[drinks]
|
||||
fav = lemonade
|
||||
beverage = orange juice
|
||||
|
||||
[drinks]
|
||||
beverage = alligator slime
|
||||
fav = tea
|
||||
[drinks]
|
||||
beverage = pineapple juice
|
||||
fav = cherry
|
||||
output1: "{{ output_content.content | b64decode }}"
|
||||
|
||||
- name: test-section 7 - Option changed
|
||||
assert:
|
||||
that:
|
||||
- result1 is changed
|
||||
- result1.msg == 'option changed'
|
||||
- output1 == expected1
|
Loading…
Add table
Add a link
Reference in a new issue