---
# Ensure that the role runs with parameters from files inputs to files outputs
#
# [Configuration]
# 2 files input (imfile) -> 2 files output (omfile)
# Note: one files input (files_input2) missing input_log_path to be skipped.
#
# [Test scenario]
# 0. Run logging role.
# 1. Check rsyslog.conf file size.
#    If logging role is executed, the file size is about 100 bytes.
#    If not executed, the default rsyslog.conf size is larger than 3000 bytes.
#    Thus, assert the size is less than 1000.
# 2. Check file count in /etc/rsyslog.d.
#    If logging role is executed, 8 config files are generated.
#    Other package could add rsyslog config files such as 21-cloudinit.conf to
#    /etc/rsyslog.d and the number varies per RHEL version.
#    Thus, assert the the count is greater than or equal to 8.
# 3. Check systemctl status of rsyslog as well as error or specific message in
#    the output.
# 4. Check a files output config file exists which verifies the generated
#    filename is correct.
# 5. Check a files_input2 input config file does not exist since it does not
#    have input_log_path in the config.
# 6. Check 2 input config files exist.
# 7. Check 2 input config files contains the configured input_log_path.
#
- name: "Ensure that the role runs with parameters from two files inputs
  to two files outputs"
  hosts: all
  vars:
    __test_inputfiles_dir0: /var/log/inputdirectory0
    __test_inputfiles_dir1: /var/log/inputdirectory1
    __test_inputfiles_conf0: /etc/rsyslog.d/90-input-files-files_input0.conf
    __test_inputfiles_conf1: /etc/rsyslog.d/90-input-files-php:.conf
    __test_inputfiles_conf2: /etc/rsyslog.d/90-input-files-files_input2.conf
    __test_outputfiles_conf: /etc/rsyslog.d/30-output-files-files_output0.conf
    __default_system_log: /var/log/messages
    __test_outputfiles_module_conf: /etc/rsyslog.d/10-output-files-modules.conf
    __test_template: RSYSLOG_TraditionalFileFormat

  tasks:
    # TEST CASE 0
    - name: "TEST CASE 0; Ensure that the role runs with parameters
      from two files inputs to two files outputs"
      vars:
        logging_files_template_format: traditional
        logging_outputs:
          - name: files_output0
            type: files
            severity: info
            exclude:
              - authpriv.none
              - auth.none
              - cron.none
              - mail.none
            path: "{{ __default_system_log }}"
          - name: files_output1
            type: files
            facility: authpriv,auth
            path: /var/log/secure
        logging_inputs:
          - name: files_input0
            type: files
            input_log_path: "{{ __test_inputfiles_dir0 }}/*.log"
            severity: err
            facility: local6
          - name: "php:"
            type: files
            input_log_path: "{{ __test_inputfiles_dir1 }}/*.log"
          - name: files_input2
            type: files
        logging_flows:
          - name: flow_0
            inputs: [files_input0, "php:"]
            outputs: [files_output0, files_output1]
      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: 8
        __conf_size: less
        __conf_files:
          - "{{ __test_inputfiles_conf0 }}"
          - "{{ __test_inputfiles_conf1 }}"
          - "{{ __test_outputfiles_conf }}"
        __check_systemctl_status: true
      include_tasks: tasks/check_daemon_config_files.yml

    - name: "Check the module param template is set to {{ __test_template }}"
      command: >-
        grep '{{ __test_template }}' '{{ __test_outputfiles_module_conf }}'
      changed_when: false

    - name: Check if the input files config does not exist
      stat:
        path: "{{ __test_inputfiles_conf2 }}"
      register: __result
      failed_when: __result.stat.exists

    - name: Grep output to messages line
      command: >-
        /bin/grep {{ __default_system_log }} {{ __test_outputfiles_conf }}
      changed_when: false

    - name: Check if the input files config exists 0
      stat:
        path: "{{ __test_inputfiles_conf0 }}"
      register: __result
      failed_when: not __result.stat.exists

    - name: Check if the input files config exists 1
      stat:
        path: "{{ __test_inputfiles_conf1 }}"
      register: __result
      failed_when: not __result.stat.exists

    - name: Check the filter 0
      lineinfile:
        path: "{{ __test_inputfiles_conf0 }}"
        line: "  {{ item }}"
        state: present
      check_mode: true
      register: _result
      failed_when: _result.changed
      loop:
        - type="imfile"
        - file="/var/log/inputdirectory0/*.log"
        - tag="files_input0"
        - severity="err"
        - facility="local6"

    - name: Check the filter 1
      lineinfile:
        path: "{{ __test_inputfiles_conf1 }}"
        line: "  {{ item }}"
        state: present
      check_mode: true
      register: _result
      failed_when: _result.changed
      loop:
        - type="imfile"
        - file="/var/log/inputdirectory1/*.log"
        - tag="php:"

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

    - name: END TEST CASE 0; Clean up the deployed config
      vars:
        logging_purge_confs: true
        logging_inputs: []
        logging_outputs: []
        logging_flows: []
      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: Check ports managed by firewall and selinux
      include_tasks: tasks/check_firewall_selinux.yml
