- name: Test podman play kube
  block:

    - name: Discover podman version
      shell: podman version | grep "^Version:" | awk {'print $2'}
      register: podman_v

    - name: Discover cgroups version
      shell: podman info | grep cgroupVersion | awk {'print $2'}
      register: cgroups

    - name: Set cgroups version
      set_fact:
        cgroups_version: "{{ cgroups.stdout }}"

    - name: Copy files to known place
      copy:
        src: "{{ item }}"
        dest: "/tmp/{{ item }}"
        remote_src: false
      loop:
        - play1.yaml
        - play3.yaml
        - play-root1.yaml
        - play-root3.yaml
        - envdata.yaml

    - name: Delete all pods leftovers from tests
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ item }}"
        state: absent
      loop:
        - web-deploy
        - web-deploy-pod
        - web-deploy-pod-0

    - name: Play kube file
      containers.podman.podman_play:
        executable: "{{ test_executable | default('podman') }}"
        kube_file: /tmp/play1.yaml
        state: started
      register: play1

    - name: Play same kube file again
      containers.podman.podman_play:
        executable: "{{ test_executable | default('podman') }}"
        kube_file: /tmp/play1.yaml
        state: started
      register: play2

    - name: Check info
      assert:
        that:
          - play1 is changed
          - play2 is not changed

    - name: Recreate play
      containers.podman.podman_play:
        executable: "{{ test_executable | default('podman') }}"
        kube_file: /tmp/play1.yaml
        state: started
        recreate: true
      register: play3

    - name: Check info
      assert:
        that:
          - play3 is changed

    - name: Check 'created' after 'started'
      containers.podman.podman_play:
        executable: "{{ test_executable | default('podman') }}"
        kube_file: /tmp/play1.yaml
        state: created
      register: play4

    - name: Check info
      assert:
        that:
          - play4 is not changed

    - name: Run with configmap
      containers.podman.podman_play:
        executable: "{{ test_executable | default('podman') }}"
        kube_file: /tmp/play3.yaml
        debug: true
        state: started
        recreate: true
        configmap:
          - /tmp/envdata.yaml
      register: play5

    - name: Check info
      assert:
        that:
          - play5 is changed

    - name: Check if pod is running well
      containers.podman.podman_pod_info:
        executable: "{{ test_executable | default('podman') }}"
        name: web-deploy-pod-0
      register: info1

    - name: Check pod info
      assert:
        that:
          - info1['pods'][0]['State'] == 'Running'

  always:

    - name: Delete all pods leftovers from tests
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ item }}"
        state: absent
      loop:
        - web-deploy
        - web-deploy-pod
        - web-deploy-pod-0
        - web-deploy-pod-1
        - web-deploy-pod-2

- name: Test idempotency for root pods
  include_tasks: root-play.yml
  vars:
    ansible_python_interpreter: "/usr/bin/python"
  args:
    apply:
      become: true
