---
- name: "Ensure inputs from the remote logging system passed to the local files outputs."
  hosts: all
  vars:
    __test_input_remote_tcp: >-
      /etc/rsyslog.d/11-input-remote-remote_tcp_input.conf
    __test_input_remote_udp: >-
      /etc/rsyslog.d/11-input-remote-remote_udp_input.conf
    __test_output_remote0: >-
      /etc/rsyslog.d/30-output-files-remote_files_output0.conf
    __test_output_remote1: >-
      /etc/rsyslog.d/30-output-files-remote_files_output1.conf
    __default_system_log: /var/log/messages
  tasks:
  # TEST CASE 0
    - name: "TEST CASE 0; Test configuration inputs from the remote rsyslog to the default local files outputs."
      vars:
        logging_outputs:
          - name: remote_files_output0
            type: remote_files
        logging_inputs:
          - name: remote_tcp_input
            type: remote
            tcp_ports: [514, 40001]
          - name: remote_udp_input
            type: remote
            udp_ports: [514, 40002]
        logging_flows:
          - name: flow_0
            inputs: [remote_udp_input, remote_tcp_input]
            outputs: [remote_files_output0]
      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_input_remote_tcp }}"
          - "{{ __test_input_remote_udp }}"
          - "{{ __test_output_remote0 }}"
        __check_systemctl_status: true
      include_tasks: tasks/check_daemon_config_files.yml

  # Checking 'error' in stdout from systemctl status is
  # for detecting the case in which rsyslog is running,
  # but some functionality is disabled due to some error,
  # e.g., error: 'tls.cacert' file couldn't be accessed.
    - name: Check rsyslog errors
      command: systemctl status rsyslog
      register: __result
      changed_when: false
      failed_when: "'error' in __result.stdout or 'a RainerScript command' in __result.stdout or __result is failed"
      when: __logging_is_booted

    - name: Install lsof
      package:
        name: lsof
        state: present
        use: "{{ (__logging_is_ostree | d(false)) | ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
      when: __logging_is_booted

    - name: Check lsof outputs for rsyslogd
      shell: |-
        set -euo pipefail
        lsof -i -nP | grep rsyslogd
      register: __result
      changed_when: false
      when: __logging_is_booted

    - name: Show lsof output
      debug:
        msg: "lsof returned {{ __result.stdout }}"
      when: __logging_is_booted

    - name: Check port 514 and 40001 are open for TCP
      shell: |-
        set -euo pipefail
        lsof -i -nP | grep rsyslogd | grep TCP | grep {{ item }}
      loop: [514, 40001]
      changed_when: false
      when: __logging_is_booted

    - name: Check port 514 and 40002 are open for UDP
      shell: |-
        set -euo pipefail
        lsof -i -nP | grep rsyslogd | grep UDP | grep {{ item }}
      loop: [514, 40002]
      changed_when: false
      when: __logging_is_booted

    - 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

  # 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

  # TEST CASE 1
    - name: "TEST CASE 1; Test configuration inputs from the remote rsyslog to the configured local files outputs."
      vars:
        logging_tcp_threads: 2
        logging_udp_threads: 2
        logging_server_queue_type: FixedArray
        logging_outputs:
          - name: remote_files_output0
            type: remote_files
            remote_log_path: >-
              /var/log/remote/%FROMHOST%/%PROGRAMNAME:::secpath-replace%.log
            comment: "This is a comment 0."
            severity: info
            exclude:
              - authpriv.none
            client_count: 20
            io_buffer_size: 8192
            async_writing: true
            action:
              reportsuspension: "on"
              reportsuspensioncontinuation: "off"
            queue:
              Size: 49999
          - name: remote_files_output1
            type: remote_files
            remote_sub_path: >-
              others/%FROMHOST%/%PROGRAMNAME:::secpath-replace%.log
            facility: authpriv
        logging_inputs:
          - name: remote_tcp_input
            type: remote
            tcp_ports: [514, 40001]
          - name: remote_udp_input
            type: remote
            udp_ports: [514, 40002]
        logging_flows:
          - name: flow_0
            inputs: [remote_udp_input, remote_tcp_input]
            outputs: [remote_files_output0, remote_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_input_remote_tcp }}"
          - "{{ __test_input_remote_udp }}"
          - "{{ __test_output_remote0 }}"
          - "{{ __test_output_remote1 }}"
        __check_systemctl_status: true
      include_tasks: tasks/check_daemon_config_files.yml

    - name: Check action parameters in remote0 output
      command: >-
        grep 'authpriv.none action.* action.reportsuspension="on"
        action.reportsuspensioncontinuation="off"' {{ __test_output_remote0 }}
      changed_when: false
    - name: Check queue parameters in remote0 output
      command: >-
        grep ' queue.Size="49999"' {{ __test_output_remote0 }}
      changed_when: false

    - name: Install lsof
      package:
        name: lsof
        state: present
        use: "{{ (__logging_is_ostree | d(false)) | ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
      when: __logging_is_booted

    - name: Check lsof outputs for rsyslogd
      shell: |-
        set -euo pipefail
        lsof -i -nP | grep rsyslogd
      register: __result
      changed_when: false
      when: __logging_is_booted

    - name: Show lsof output
      debug:
        msg: "lsof returned {{ __result.stdout }}"
      when: __logging_is_booted

    - name: Check port 514 and 40001 is open for TCP
      shell: |-
        set -euo pipefail
        lsof -i -nP | grep rsyslogd | grep TCP | grep {{ item }}
      loop: [514, 40001]
      changed_when: false
      when: __logging_is_booted

    - name: Check port 514 and 40002 is open for UDP
      shell: |-
        set -euo pipefail
        lsof -i -nP | grep rsyslogd | grep UDP | grep {{ item }}
      loop: [514, 40002]
      changed_when: false
      when: __logging_is_booted

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

    - name: END TEST CASE 1; 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

  # 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
