---
# 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
    __test_outputfiles1_conf: /etc/rsyslog.d/30-output-files-files_output1.conf
    __test_outputfiles2_conf: /etc/rsyslog.d/30-output-files-files_output2.conf
    __default_system_log: /var/log/messages
    __test_outputfiles_module_conf: /etc/rsyslog.d/10-output-files-modules.conf
    __test_template: RSYSLOG_TraditionalFileFormat

  tasks:
    - name: Create tempdir for tests
      tempfile:
        prefix: logging_
        suffix: _lsr
        state: directory
      register: __logging_tmpdir

  # 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
          - name: files_output2
            type: files
            severity: info
            path: "{{ __logging_tmpdir.path }}/test2/test.log"
            mode: "0660"
            owner: 0
            group: root
            dir_mode: "0770"
            dir_owner: root
            dir_group: 0
        logging_inputs:
          - name: files_input0
            type: files
            input_log_path: "{{ __test_inputfiles_dir0 }}/*.log"
            severity: err
            facility: local6
            reopen_on_truncate: true
          - name: "php:"
            type: files
            input_log_path: "{{ __test_inputfiles_dir1 }}/*.log"
            reopen_on_truncate: false
          - name: files_input2
            type: files
        logging_flows:
          - name: flow_0
            inputs: [files_input0, "php:"]
            outputs: [files_output0, files_output1]
      include_role:
        name: fedora.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: 9
        __conf_size: less
        __conf_files:
          - "{{ __test_inputfiles_conf0 }}"
          - "{{ __test_inputfiles_conf1 }}"
          - "{{ __test_outputfiles_conf }}"
          - "{{ __test_outputfiles1_conf }}"
          - "{{ __test_outputfiles2_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"
        - reopenOnTruncate="on"

    - 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:"
        - reopenOnTruncate="off"

    - name: Check files_output2
      lineinfile:
        path: "{{ __test_outputfiles2_conf }}"
        line: "    {{ item }}"
        state: present
      check_mode: true
      register: _result
      failed_when: _result.changed
      loop:
        - "$FileCreateMode 0660"
        - "$DirCreateMode 0770"
        - "$fileOwnerNum 0"
        - "$dirOwner root"
        - "$fileGroup root"
        - "$dirGroupNum 0"

    - 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: fedora.linux_system_roles.logging
        public: true

    - name: Remove tempdir
      file:
        path: "{{ __logging_tmpdir.path }}"
        state: absent

  # 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

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