---
# Test to ensure the logging role successfully runs with
# logging_purge_confs and logging_reset_confs set to true.
#
- name: Test purge and reset functionality
  hosts: all
  vars:
    __test_default_files_conf: /etc/rsyslog.d/30-output-files-default_files.conf
  tasks:
    - name: Ensure rsyslog is installed
      package:
        name: rsyslog
        state: present

    - name: Create some unowned config files
      copy:
        dest: "{{ item }}"
        content: "# this is file {{ item }}"
        mode: "0444"
      loop:
        - "/etc/rsyslog.d/unowned1.conf"
        - "/etc/rsyslog.d/unowned2.conf"

    - name: Modify /etc/rsyslog.conf
      lineinfile:
        path: /etc/rsyslog.conf
        insertafter: EOF
        state: present
        line: "# this is a modification"

    - name: Configure with basics + purge
      vars:
        logging_purge_confs: true
        logging_inputs:
          - name: basic_input
            type: basics
      include_role:
        name: linux-system-roles.logging
        public: true

    # notify Restart rsyslogd is executed at the end of this test task.
    # thus we have to force to invoke handlers
    - name: "Force all notified handlers to run at this point,
      not waiting for normal sync points"
      meta: flush_handlers

    - name: Ensure config file size and counts
      vars:
        __conf_count: 5
        __conf_size: less
        __conf_files:
          - "{{ __test_default_files_conf }}"
        __check_systemctl_status: true
      include_tasks: tasks/check_daemon_config_files.yml

    - name: Ensure previous test config files are removed
      stat:
        path: "{{ item }}"
      register: __result
      failed_when: __result.stat.exists
      loop:
        - "/etc/rsyslog.d/unowned1.conf"
        - "/etc/rsyslog.d/unowned2.conf"

    - name: Check1 rsyslog config files
      command: ls /etc/rsyslog.d
      changed_when: false

    - name: Check ports managed by firewall and selinux
      include_tasks: tasks/check_firewall_selinux.yml

    - name: Configure with basics + purge again - check for idempotency
      vars:
        logging_purge_confs: true
        logging_inputs:
          - name: basic_input
            type: basics
      include_role:
        name: linux-system-roles.logging
        public: true

    - name: Check11 rsyslog config files
      command: ls /etc/rsyslog.d
      changed_when: false

    - name: Check ports managed by firewall and selinux
      include_tasks: tasks/check_firewall_selinux.yml

    - name: Run again with purge to erase all config
      vars:
        logging_purge_confs: true
        logging_enabled: false
        logging_inputs: []
        logging_outputs: []
        logging_flows: []
      include_role:
        name: linux-system-roles.logging
        public: true

    - name: Ensure config file size and counts
      vars:
        __conf_count: 0
        __conf_size: more
        __check_systemctl_status: true
      include_tasks: tasks/check_daemon_config_files.yml

    - name: Ensure test config files are removed
      stat:
        path: "{{ item }}"
      register: __result
      failed_when: __result.stat.exists
      loop:
        - "{{ __test_default_files_conf }}"

    - name: Check2 rsyslog config files
      command: ls /etc/rsyslog.d
      changed_when: false

    - name: Check ports managed by firewall and selinux
      include_tasks: tasks/check_firewall_selinux.yml

    - name: Run again with purge and reset to test idempotency
      vars:
        logging_purge_confs: true
        logging_enabled: false
        logging_inputs: []
        logging_outputs: []
        logging_flows: []
      include_role:
        name: linux-system-roles.logging
        public: true

    - name: Check3 rsyslog config files
      command: ls /etc/rsyslog.d
      changed_when: false

    - name: Check ports managed by firewall and selinux
      include_tasks: tasks/check_firewall_selinux.yml
