---
# to run configurator only if necessary
- hosts: pfservers
  name: Run Venom tests
  become: True
  collections:
    - inverse_inc.packetfence

  vars:
    test_suites:
      - configurator_inline_l2

  tasks:
    - name: Check configurator state
      uri:
        url: "https://{{ packetfence_install__mgmt_interface['ip'] }}:1443/api/v1/configurator/config/interfaces"
        method: GET
        follow_redirects: safe
        validate_certs: no
        return_content: yes
        status_code:
          - 200 # configurator enabled
          - 401 # configurator disabled
      register: configurator_state_register

    - name: Run Venom testsuites
      command: '{{ venom_dir }}/venom-wrapper.sh {{ venom_dir }}/test_suites/{{ item }}'
      args:
        chdir: '{{ venom_dir }}'
      loop: '{{ test_suites }}'
      vars:
        venom_wrapper_args:
          # temp, overcome Venom issue:
          # - when running test suite one by one, previous logs and results are overriden
          # Venom will create one directory per test suite to store results
          VENOM_COMMON_FLAGS: '--output-dir={{ venom_dir }}/results/{{ item }}'

      # add inventory__group_environment to Ansible environment
      # useful to make env vars available for Venom
      environment: '{{ inventory__group_environment | d({})
                       | combine(venom_wrapper_args | d({})) }}'

      # only run when configurator is enabled
      when: configurator_state_register['status'] == 200


# other test suites
- hosts: pfservers
  name: Run Venom tests
  become: True
  collections:
    - inverse_inc.packetfence

  vars:
    test_suites:
      - global_config
      - inline_l2_and_radius


  tasks:
    - name: Run Venom testsuites
      command: '{{ venom_dir }}/venom-wrapper.sh {{ venom_dir }}/test_suites/{{ item }}'
      args:
        chdir: '{{ venom_dir }}'
      loop: '{{ test_suites }}'
      vars:
        venom_wrapper_args:
          # temp, overcome Venom issue:
          # - when running test suite one by one, previous logs and results are overriden
          # Venom will create one directory per test suite to store results
          VENOM_COMMON_FLAGS: '--output-dir={{ venom_dir }}/results/{{ item }}'

      # add inventory__group_environment to Ansible environment
      # useful to make env vars available for Venom
      environment: '{{ inventory__group_environment | d({})
                       | combine(venom_wrapper_args | d({})) }}'



