---
- name: Setup a vm with podman on it
  hosts: all
  tags:
    # this test does not work in container builds
    - tests::booted
  gather_facts: true
  pre_tasks:
    - name: Test not supported on EL7
      meta: end_host
      when:
        - ansible_distribution in ['RedHat', 'CentOS']
        - ansible_distribution_major_version | int < 8
  tasks:
    - name: Determine if system is ostree and set flag
      when: not __firewall_is_ostree is defined
      block:
        - name: Check if system is ostree
          stat:
            path: /run/ostree-booted
          register: __ostree_booted_stat

        - name: Set flag to indicate system is ostree
          set_fact:
            __firewall_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"

    - name: Install test dependencies
      package:
        name:
          - podman
          # for ping
          - iputils
        state: present
        use: "{{ (__firewall_is_ostree | d(false)) |
                ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

    - name: Run test
      script:
        cmd: files/test_ping.sh
        executable: /bin/bash
      register: test_results
      environment:
        TEST_DEBUG: "{{ lookup('env', 'TEST_DEBUG') }}"

    - name: Process test results
      vars:
        coherence_check: "{{ test_results.stdout_lines[0] }}"
        restart_check: "{{ test_results.stdout_lines[2] }}"
        reload_check: "{{ test_results.stdout_lines[1] }}"
      fail:
        msg: Either coherence check or benchmark failed
      when: >-
        coherence_check | int != 0
        or restart_check | int < reload_check | int
