---
- name: "Ensure that the role runs with parameters from ovirt to two elasticsearch outputs and local files output"
  hosts: all
  tags:
    - tests::uses_selinux
  vars:
    __test_ovirt_collectd_conf: >-
      /etc/rsyslog.d/90-input-ovirt-ovirt_collectd_input.conf
    __test_ovirt_engine_conf: >-
      /etc/rsyslog.d/90-input-ovirt-ovirt_engine_input.conf
    __test_ovirt_vdsm_conf: /etc/rsyslog.d/90-input-ovirt-ovirt_vdsm_input.conf
    __test_ovirt_bogus_conf: >-
      /etc/rsyslog.d/90-input-ovirt-ovirt_bogus_input.conf
    __test_es_conf: >-
      /etc/rsyslog.d/31-output-elasticsearch-elasticsearch_output.conf
    __test_es_ops_conf: >-
      /etc/rsyslog.d/31-output-elasticsearch-elasticsearch_output_ops.conf
    __test_collectd_name: ovirt_collectd_input
    __test_engine_name: ovirt_engine_input
    __test_vdsm_name: ovirt_vdsm_input
    __test_collectd_port: 44514
    __test_metrics_index: project.ovirt-metrics
    __test_logs_index: project.ovirt-logs
    __test_engine_input: /var/log/ovirt-engine/engine.log
    __test_vdsm_input: /var/log/vdsm/vdsm.log
    __default_system_log: /var/log/messages
  tasks:
    - name: TEST CASE 0; Ensure basic ovirt default configuration works
      vars:
        logging_elasticsearch_password: password0
        logging_outputs:
          - name: default_files
            type: files
          - name: elasticsearch_output
            type: elasticsearch
            server_host: logging-es
            server_port: 9200
            index_prefix: project.
            input_type: ovirt
            retryfailures: false
            ca_cert: "/etc/rsyslog.d/es-ca.crt"
            cert: "/etc/rsyslog.d/es-cert.pem"
            private_key: "/etc/rsyslog.d/es-key.pem"
            uid: testuser0
          - name: elasticsearch_output_ops
            type: elasticsearch
            server_host: logging-es-ops
            server_port: 9200
            index_prefix: .operations.
            input_type: ovirt
            retryfailures: false
            ca_cert: "/etc/rsyslog.d/es-ca.crt"
            cert: "/etc/rsyslog.d/es-cert.pem"
            private_key: "/etc/rsyslog.d/es-key.pem"
        logging_inputs:
          - name: basic_input
            type: basics
          - name: "{{ __test_collectd_name }}"
            type: ovirt
            subtype: collectd
          - name: "{{ __test_engine_name }}"
            type: ovirt
            subtype: engine
          - name: "{{ __test_vdsm_name }}"
            type: ovirt
            subtype: vdsm
          - name: ovirt_bogus_input
            type: ovirt
            subtype: bogus
        logging_flows:
          - name: flow_0
            inputs:
              - "{{ __test_collectd_name }}"
              - "{{ __test_engine_name }}"
              - "{{ __test_vdsm_name }}"
            outputs: [elasticsearch_output, elasticsearch_output_ops]
          - name: flow_1
            inputs: [basic_input]
            outputs: [default_files]
      include_role:
        name: fedora.linux_system_roles.logging

  # 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: 17
        __conf_size: less
        __conf_files:
          - "{{ __test_ovirt_collectd_conf }}"
          - "{{ __test_ovirt_engine_conf }}"
          - "{{ __test_ovirt_vdsm_conf }}"
        __check_systemctl_status: false
      include_tasks: tasks/check_daemon_config_files.yml

    - name: Check ovirt config files
      stat:
        path: "{{ item }}"
      register: __result
      loop:
        - "{{ __test_ovirt_collectd_conf }}"
        - "{{ __test_ovirt_engine_conf }}"
        - "{{ __test_ovirt_vdsm_conf }}"
      failed_when: not __result.stat.exists

    - name: Check bogus conf does not exist - {{ __test_ovirt_bogus_conf }}
      stat:
        path: "{{ __test_ovirt_bogus_conf }}"
      register: __result
      failed_when: __result.stat.exists

    - name: Check ovirt_collectd_port is {{ __test_collectd_port }}
      command: >-
        /bin/grep 'input(name="{{ __test_collectd_name }}" type="imtcp"
        port="{{ __test_collectd_port }}"' {{ __test_ovirt_collectd_conf }}
      changed_when: false

    - name: Check index_prefix is in {{ __test_ovirt_collectd_conf }}
      command: >-
        /bin/grep 'set $.index_prefix = "{{ __test_metrics_index }}"'
        {{ __test_ovirt_collectd_conf }}
      changed_when: false

    - name: Check input file is configured in {{ __test_ovirt_engine_conf }}
      command: >-
        /bin/grep 'input(type="imfile" file="{{ __test_engine_input }}"
        tag="{{ __test_engine_name }}"' {{ __test_ovirt_engine_conf }}
      changed_when: false

    - name: Check index_prefix is configured in {{ __test_ovirt_engine_conf }}
      command: >-
        /bin/grep 'set $.index_prefix = "{{ __test_logs_index }}"'
        {{ __test_ovirt_engine_conf }}
      changed_when: false

    - name: Check input file is configured in {{ __test_ovirt_vdsm_conf }}
      command: >-
        /bin/grep 'input(type="imfile" file="{{ __test_vdsm_input }}"
        tag="{{ __test_vdsm_name }}"' {{ __test_ovirt_vdsm_conf }}
      changed_when: false

    - name: Check index_prefix is configured in {{ __test_ovirt_vdsm_conf }}
      command: >-
        /bin/grep 'set $.index_prefix = "{{ __test_logs_index }}"'
        {{ __test_ovirt_vdsm_conf }}
      changed_when: false

    - name: Check server param in "{{ __test_es_conf }}"
      command: /bin/grep 'server="logging-es"' {{ __test_es_conf }}
      changed_when: false

    - name: Check server param in "{{ __test_es_ops_conf }}"
      command: /bin/grep 'server="logging-es-ops"' {{ __test_es_ops_conf }}
      changed_when: false

    - name: Check password is in "{{ __test_es_conf }}"
      command: /bin/grep 'pwd="password0"'  {{ __test_es_conf }}
      changed_when: false

    - name: Check pwd= is not in "{{ __test_es_ops_conf }}"
      command: /bin/grep "pwd="  {{ __test_es_ops_conf }}
      register: __result
      changed_when: false
      failed_when: __result.rc != 1

    - name: END TEST CASE 0; Ensure basic ovirt configuration works
      vars:
        logging_purge_confs: true
        logging_inputs: []
        logging_outputs: []
        logging_flows: []
      include_role:
        name: fedora.linux_system_roles.logging

  # 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: TEST CASE 1; Ensure advanced ovirt configuration works
      vars:
        logging_outputs:
          - name: default_files
            type: files
          - name: elasticsearch_output
            type: elasticsearch
            server_host: [logging-es0, logging-es1]
            server_port: 9200
            index_prefix: project.
            input_type: ovirt
            ca_cert: "/etc/rsyslog.d/es-ca.crt"
            cert: "/etc/rsyslog.d/es-cert.pem"
            private_key: "/etc/rsyslog.d/es-key.pem"
            uid: testuser0
          - name: elasticsearch_output_ops
            type: elasticsearch
            server_host:
              - logging-es-ops0
              - logging-es-ops1
            server_port: 9200
            index_prefix: .operations.
            input_type: ovirt
            retryfailures: false
            ca_cert: "/etc/rsyslog.d/es-ca.crt"
            cert: "/etc/rsyslog.d/es-cert.pem"
            private_key: "/etc/rsyslog.d/es-key.pem"
            dynSearchIndex: false
            bulkmode: false
            dynbulkid: false
            allowUnsignedCerts: true
            usehttps: false
        logging_inputs:
          - name: basic_input
            type: basics
          - name: "{{ __test_collectd_name }}"
            type: ovirt
            subtype: collectd
            ovirt_collectd_port: "{{ __test_collectd_port }}"
            ovirt_elasticsearch_index_prefix: "{{ __test_metrics_index }}"
            ovirt_env_name: test-engine
            ovirt_env_uuid: 111-222-333
          - name: "{{ __test_engine_name }}"
            type: ovirt
            subtype: engine
            ovirt_elasticsearch_index_prefix: "{{ __test_logs_index }}"
            ovirt_env_name: test-engine
            ovirt_env_uuid: 444-555-666
            ovirt_vds_cluster_name: test_vds_cluster
            ovirt_engine_fqdn: engine.ovirt.example.com
            ovirt_input_file: "{{ __test_engine_input }}"
          - name: "{{ __test_vdsm_name }}"
            type: ovirt
            subtype: vdsm
            ovirt_elasticsearch_index_prefix: "{{ __test_logs_index }}"
            ovirt_env_name: test-engine
            ovirt_env_uuid: 777-888-999
            ovirt_vds_cluster_name: test_vds_cluster
            ovirt_engine_fqdn: engine.ovirt.example.com
            ovirt_input_file: "{{ __test_vdsm_input }}"
          - name: ovirt_bogus_input
            type: ovirt
            subtype: bogus
        logging_flows:
          - name: flow_0
            inputs:
              - "{{ __test_collectd_name }}"
              - "{{ __test_engine_name }}"
              - "{{ __test_vdsm_name }}"
            outputs: [elasticsearch_output, elasticsearch_output_ops]
          - name: flow_1
            inputs: [basic_input]
            outputs: [default_files]
      include_role:
        name: fedora.linux_system_roles.logging

  # 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: 17
        __conf_size: less
        __conf_files:
          - "{{ __test_ovirt_collectd_conf }}"
          - "{{ __test_ovirt_engine_conf }}"
          - "{{ __test_ovirt_vdsm_conf }}"
        __check_systemctl_status: false
      include_tasks: tasks/check_daemon_config_files.yml

    - name: Check ovirt config files
      stat:
        path: "{{ item }}"
      register: __result
      loop:
        - "{{ __test_ovirt_collectd_conf }}"
        - "{{ __test_ovirt_engine_conf }}"
        - "{{ __test_ovirt_vdsm_conf }}"
      failed_when: not __result.stat.exists

    - name: Check bogus config does not exist -  {{ __test_ovirt_bogus_conf }}
      stat:
        path: "{{ __test_ovirt_bogus_conf }}"
      register: __result
      failed_when: __result.stat.exists

    - name: Check ovirt_collectd_port is "{{ __test_collectd_port }}"
      command: >-
        /bin/grep 'input(name="{{ __test_collectd_name }}" type="imtcp"
        port="{{ __test_collectd_port }}"' {{ __test_ovirt_collectd_conf }}
      changed_when: false

    - name: Check index_prefix is configured in {{ __test_ovirt_collectd_conf }}
      command: >-
        /bin/grep 'set $.index_prefix = "{{ __test_metrics_index }}"'
        {{ __test_ovirt_collectd_conf }}
      changed_when: false

    - name: Check input file is configured in {{ __test_ovirt_engine_conf }}
      command: >-
        /bin/grep 'input(type="imfile" file="{{ __test_engine_input }}"
        tag="{{ __test_engine_name }}"' {{ __test_ovirt_engine_conf }}
      changed_when: false

    - name: Check index_prefix is configured in {{ __test_ovirt_engine_conf }}
      command: >-
        /bin/grep 'set $.index_prefix = "{{ __test_logs_index }}"'
        {{ __test_ovirt_engine_conf }}
      changed_when: false

    - name: Check input file is configured in {{ __test_ovirt_vdsm_conf }}
      command: >-
        /bin/grep 'input(type="imfile" file="{{ __test_vdsm_input }}"
        tag="{{ __test_vdsm_name }}"' {{ __test_ovirt_vdsm_conf }}
      changed_when: false

    - name: Check index_prefix is configured in {{ __test_ovirt_vdsm_conf }}
      command: >-
        /bin/grep 'set $.index_prefix = "{{ __test_logs_index }}"'
        {{ __test_ovirt_vdsm_conf }}
      changed_when: false

    - name: Check default config params in "{{ __test_es_conf }}"
      command: /bin/grep {{ item }} {{ __test_es_conf }}
      loop:
        - "dynSearchIndex=.on."
        - "bulkmode=.on."
        - "dynbulkid=.on."
        - "allowUnsignedCerts=.off."
        - "usehttps=.on."
        - "retryfailures=.on."
      changed_when: false

    - name: Check modified config params in "{{ __test_es_ops_conf }}"
      command: /bin/grep {{ item }} {{ __test_es_ops_conf }}
      loop:
        - "dynSearchIndex=.off."
        - "bulkmode=.off."
        - "dynbulkid=.off."
        - "allowUnsignedCerts=.on."
        - "usehttps=.off."
        - "retryfailures=.off."
      changed_when: false

    - name: Check server param in "{{ __test_es_conf }}"
      command: >-
        /bin/grep "server=\[\"logging-es0\", \"logging-es1\"\]"
                  {{ __test_es_conf }}
      changed_when: false

    - name: Check server param in "{{ __test_es_ops_conf }}"
      command: >-
        /bin/grep "server=\[\"logging-es-ops0\", \"logging-es-ops1\"\]"
                  {{ __test_es_ops_conf }}
      changed_when: false

    - name: END TEST CASE 1; Ensure basic ovirt configuration works
      vars:
        logging_purge_confs: true
        logging_inputs: []
        logging_outputs: []
        logging_flows: []
      include_role:
        name: fedora.linux_system_roles.logging

  # 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

  # This tests results in expected error in /var/log/messages on EL 7
  # Errors are that /etc/rsyslog.d/es-ca.crt and /etc/rsyslog.d/es-cert.pem
  # do not exist.
    - name: Remove {{ __default_system_log }}
      file:
        path: "{{ __default_system_log }}"
        state: absent
