---
# test code for ini_file plugins
# (c) 2017 Red Hat Inc.

# 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: record the output directory
  set_fact: output_file={{output_dir}}/foo.ini

- name: add "fav=lemonade" is in section "[drinks]" in specified file
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    option: fav
    value: lemonade
  register: result1

- name: verify ini_file 'changed' is true
  assert:
    that:
      - result1.changed == True
      - result1.msg == 'section and option added'

- name: set expected content and get current ini file content
  set_fact:
    expected1: |-

      [drinks]
      fav = lemonade
    content1: "{{ lookup('file', output_file) }}"

- name: Verify content of ini file is as expected
  assert:
    that:
      - content1 == expected1

- name: add "fav=lemonade" is in section "[drinks]" again
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    option: fav
    value: lemonade
  register: result2

- name: Ensure unchanged
  assert:
    that:
      - result2.changed == False
      - result2.msg == 'OK'

- name: Ensure "hate=coke" is in section "[drinks]"
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    option: hate
    value: coke
  register: result3

- name: set expected content and get current ini file content
  set_fact:
    expected3: |-

      [drinks]
      fav = lemonade
      hate = coke
    content3: "{{ lookup('file', output_file) }}"

- name: assert 'changed' is true and content is OK
  assert:
    that:
      - result3.changed == True
      - result3.msg == 'option added'
      - content3 == expected3

- name: Remove option "hate=coke"
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    option: hate
    state: absent
  register: result4

- name: get ini file content
  set_fact:
    content4: "{{ lookup('file', output_file) }}"

- name: assert changed and content is as expected
  assert:
    that:
      - result4.changed == True
      - result4.msg == 'option changed'
      - content4 == expected1

- name: remove section 'drinks'
  ini_file:
    path: "{{ output_file }}"
    section: drinks
    state: absent
  register: result5

- name: get current ini file content
  set_fact:
    content5: "{{ lookup('file', output_file) }}"

- name: assert changed
  assert:
    that:
      - result5.changed == True
      - result5.msg == 'section removed'
      - content5 == ""
