---
- name: Test podman import
  block:
    - name: Start container
      containers.podman.podman_container:
        executable: "{{ test_executable | default('podman') }}"
        name: container
        image: alpine:3.7
        state: started
        command: sleep 1d

    - name: Export container
      containers.podman.podman_export:
        executable: "{{ test_executable | default('podman') }}"
        container: container
        dest: /tmp/container

    - name: Check file
      stat:
        path: /tmp/container
      register: img

    - name: Check it's saved
      assert:
        that:
          - img.stat.exists

    - name: Import container
      containers.podman.podman_import:
        executable: "{{ test_executable | default('podman') }}"
        src: /tmp/container
      register: test

    - name: Check it's imported
      assert:
        that:
          - test is success
          - test.image["Id"] != ''

    - name: Import container with commit message
      containers.podman.podman_import:
        executable: "{{ test_executable | default('podman') }}"
        src: /tmp/container
        commit_message: 'Test in CI'
      register: test1

    - name: Check it's imported with commit message
      assert:
        that:
          - test1.image.Comment == "Test in CI"

    - name: Import container with changes
      containers.podman.podman_import:
        executable: "{{ test_executable | default('podman') }}"
        src: /tmp/container
        change:
          - "User": "someuser"
          - "CMD": "/bin/nonsh"
      register: test2

    - name: Check it's imported with changes
      assert:
        that:
          - test2.image.User == 'someuser'
          - test2.image["Config"]["Cmd"][2] == "/bin/nonsh"

- name: Test podman volume import
  block:
    - name: Start container
      containers.podman.podman_container:
        executable: "{{ test_executable | default('podman') }}"
        name: container
        image: alpine:3.7
        state: started
        volume:
          - "volume:/test"
        command: touch /test/test_file

    - name: Export volume
      containers.podman.podman_export:
        executable: "{{ test_executable | default('podman') }}"
        volume: volume
        dest: /tmp/volume

    - name: Check file
      stat:
        path: /tmp/volume
      register: vlm

    - name: Check it's exported
      assert:
        that:
          - vlm.stat.exists

    - name: delete container
      containers.podman.podman_container:
        state: absent 
        name: container

    - name: delete volume
      containers.podman.podman_volume:
        state: absent 
        name: volume

    # podman needs a volume to exist before import
    - name: creating volume before importing
      containers.podman.podman_volume:
        name: volume
        state: present

    - name: Import volume
      containers.podman.podman_import:
        executable: "{{ test_executable | default('podman') }}"
        src: /tmp/volume
        volume: "volume"
      register: volume

    - name: Check it's imported
      assert:
        that:
          - volume is success

    - name: Check file is there
      containers.podman.podman_container:
        executable: "{{ test_executable | default('podman') }}"
        name: container
        image: alpine:3.7
        state: started
        volume:
          - "volume:/test"
        command: ls /test/test_file
      register: ls

    - name: Check it's imported
      assert:
        that:
          - ls is success

  always:
    - name: Remove container
      containers.podman.podman_container:
        executable: "{{ test_executable | default('podman') }}"
        name: container
        state: absent
    - name: Remove volume
      containers.podman.podman_volume:
        executable: "{{ test_executable | default('podman') }}"
        name: volume
        state: absent
