---
# 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
    __default_system_log: /var/log/messages
  tasks:
    - name: Determine if system is ostree and set flag
      when: not __logging_is_ostree is defined
      block:
        - name: Check if system is ostree
          stat:
            path: /run/ostree-booted
          register: __ostree_booted_stat

        - name: Set flag to indicate system is ostree
          set_fact:
            __logging_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"

    - name: Ensure rsyslog is installed
      package:
        name: rsyslog
        state: present
        use: "{{ (__logging_is_ostree | d(false)) |
                 ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

    - 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: "{{ 'less' if __logging_is_ostree | d(false)
          else '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

    - name: Assert {{ __default_system_log }}
      include_tasks: tasks/assert_varlogmessages.yml
