- name: Test podman_container
  block:
    - name: Delete all container leftovers from tests
      containers.podman.podman_container:
        name: "{{ item }}"
        state: absent
      loop:
        - "alpine:3.7"
        - "container"
        - "container1"
        - "container2"
        - "testidem-pod"

    - name: Test no image with default action
      containers.podman.podman_container:
        name: container
      ignore_errors: true
      register: no_image

    - name: Test no image with state 'started'
      containers.podman.podman_container:
        name: container
        state: created
      ignore_errors: true
      register: no_image1

    - name: Test no image with state 'present'
      containers.podman.podman_container:
        name: container
        state: present
      ignore_errors: true
      register: no_image2

    - name: Check no image
      assert:
        that:
          - no_image is failed
          - no_image1 is failed
          - no_image2 is failed
          - no_image.msg == "Cannot start container when image is not specified!"
          - no_image1.msg == "State 'created' required image to be configured!"
          - no_image2.msg == "State 'present' required image to be configured!"
        fail_msg: No image test failed!
        success_msg: No image test passed!

    - name: Ensure image doesn't exist
      containers.podman.podman_image:
        name: alpine:3.7
        state: absent

    - name: Check pulling image
      containers.podman.podman_container:
        name: container
        image: alpine:3.7
        state: started
        command: sleep 1d
      register: image

    - name: Check using already pulled image
      containers.podman.podman_container:
        name: container2
        image: alpine:3.7
        state: started
        command: sleep 1d
      register: image2

    - name: Check output is correct
      assert:
        that:
          - image is changed
          - image.container is defined
          - image.container['State']['Running']
          - "'pulled image alpine:3.7' in image.actions"
          - "'started container' in image.actions"
          - image2 is changed
          - image2.container is defined
          - image2.container['State']['Running']
          - "'pulled image alpine:3.7' not in image2.actions"
          - "'started container2' in image2.actions"
        fail_msg: Pulling image test failed!
        success_msg: Pulling image test passed!

    - name: Check failed image pull
      containers.podman.podman_container:
        name: container
        image: ineverneverneverexist
        state: started
        command: sleep 1d
      register: imagefail
      ignore_errors: true

    - name: Check output is correct
      assert:
        that:
          - imagefail is failed
          - imagefail.msg == "Can't pull image ineverneverneverexist"

    - name: Force container recreate
      containers.podman.podman_container:
        name: container
        image: alpine
        state: started
        command: sleep 1d
        recreate: true
      register: recreated

    - name: Check output is correct
      assert:
        that:
          - recreated is changed
          - recreated.container is defined
          - recreated.container['State']['Running']|bool
          - "'recreated container' in recreated.actions"
        fail_msg: Force recreate test failed!
        success_msg: Force recreate test passed!

    - name: Start container
      containers.podman.podman_container:
        name: container
        state: started

    - name: Present container
      containers.podman.podman_container:
        name: container
        image: alpine
        state: present
        command: sleep 1d
      register: start_present

    - name: Check output is correct
      assert:
        that:
          - start_present.container['State']['Running']

    - name: Stop container
      containers.podman.podman_container:
        name: container
        state: stopped
      register: stopped

    - name: Stop the same container again (idempotency)
      containers.podman.podman_container:
        name: container
        state: stopped
      register: stopped_again

    - name: Check output is correct
      assert:
        that:
          - stopped is changed
          - stopped.container is defined
          - not stopped.container['State']['Running']
          - "'stopped container' in stopped.actions"
          - stopped_again is not changed
          - stopped_again.container is defined
          - not stopped_again.container['State']['Running']
          - stopped_again.actions == []
        fail_msg: Stopping container test failed!
        success_msg: Stopping container test passed!

    - name: Delete stopped container
      containers.podman.podman_container:
        name: container
        state: absent
      register: deleted

    - name: Delete again container (idempotency)
      containers.podman.podman_container:
        name: container
        state: absent
      register: deleted_again

    - name: Check output is correct
      assert:
        that:
          - deleted is changed
          - deleted.container is defined
          - deleted.container == {}
          - "'deleted container' in deleted.actions"
          - deleted_again is not changed
          - deleted_again.container is defined
          - deleted_again.container == {}
          - deleted_again.actions == []
        fail_msg: Deleting stopped container test failed!
        success_msg: Deleting stopped container test passed!

    - name: Create container in 'stopped' state
      containers.podman.podman_container:
        name: container
        image: alpine:3.7
        state: stopped
        command: sleep 1d
      register: created

    - name: Check output is correct
      assert:
        that:
          - created is changed
          - created.container is defined
          - created.container != {}
          - not created.container['State']['Running']
          - "'created container' in created.actions"
        fail_msg: "Creating stopped container test failed!"
        success_msg: "Creating stopped container test passed!"

    - name: Delete created container
      containers.podman.podman_container:
        name: container
        state: absent

    - name: Create container in 'created' state
      containers.podman.podman_container:
        name: container
        image: alpine:3.7
        state: created
        command: sleep 1d
      register: created

    - name: Check output is correct
      assert:
        that:
          - created is changed
          - created.container is defined
          - created.container != {}
          - not created.container['State']['Running']
          - "'created container' in created.actions"
        fail_msg: "Creating stopped container test failed!"
        success_msg: "Creating stopped container test passed!"

    - name: Force container recreate
      containers.podman.podman_container:
        name: container
        image: alpine
        state: created
        command: sleep 1d
        recreate: true
      register: recreated

    - name: Check output is correct
      assert:
        that:
          - recreated is changed
          - recreated.container is defined
          - not recreated.container['State']['Running']
          - "'recreated container' in recreated.actions"
        fail_msg: Force recreate test failed!

    - name: Restart container
      containers.podman.podman_container:
        name: container
        restart: true
      register: restarted

    - name: Check output is correct
      assert:
        that:
          - restarted is changed
          - restarted.container is defined
          - restarted.container['State']['Running']
          - "'restarted container' in restarted.actions"
        fail_msg: Restart container test failed!

    - name: Restart running container
      containers.podman.podman_container:
        name: container
        restart: true
      register: restarted

    - name: Check output is correct
      assert:
        that:
          - restarted is changed
          - restarted.container is defined
          - restarted.container['State']['Running']
          - "'restarted container' in restarted.actions"
        fail_msg: Restart running container test failed!

    - name: Delete created container
      containers.podman.podman_container:
        name: container
        state: absent

    - name: Start container that was deleted
      containers.podman.podman_container:
        name: container
        image: alpine:3.7
        state: started
        command: sleep 1d
      register: started

    - name: Check output is correct
      assert:
        that:
          - started is changed
          - started.container is defined
          - started.container['State']['Running']
          - "'pulled image alpine:3.7' not in started.actions"

    - name: Delete started container
      containers.podman.podman_container:
        name: container
        state: absent
      register: deleted

    - name: Delete again container (idempotency)
      containers.podman.podman_container:
        name: container
        state: absent
      register: deleted_again

    - name: Check output is correct
      assert:
        that:
          - deleted is changed
          - deleted.container is defined
          - deleted.container == {}
          - "'deleted container' in deleted.actions"
          - deleted_again is not changed
          - deleted_again.container is defined
          - deleted_again.container == {}
          - deleted_again.actions == []
        fail_msg: Deleting started container test failed!
        success_msg: Deleting started container test passed!

    - name: Recreate container with parameters
      containers.podman.podman_container:
        name: container
        image: docker.io/alpine:3.7
        state: started
        command: sleep 1d
        recreate: true
        etc_hosts:
          host1: 127.0.0.1
          host2: 127.0.0.1
        annotation:
          this: "annotation_value"
        dns:
          - 1.1.1.1
          - 8.8.4.4
        dns_search: example.com
        cap_add:
          - SYS_TIME
          - NET_ADMIN
        publish:
          - "9000:80"
          - "9001:8000"
        workdir: "/bin"
        env:
          FOO: bar=1
          BAR: foo
          TEST: 1
          BOOL: false
        label:
          somelabel: labelvalue
          otheralbe: othervalue
        volumes:
          - /tmp:/data
        mounts:
          - type=devpts,destination=/dev/pts
      register: test

    - name: Check output is correct
      assert:
        that:
          - test is changed
          - test.container is defined
          - test.container != {}
          - test.container['State']['Running']
          # test capabilities
          - "'CAP_SYS_TIME' in test.container['BoundingCaps']"
          - "'CAP_NET_ADMIN' in test.container['BoundingCaps']"
          # test annotations
          - test.container['Config']['Annotations']['this'] is defined
          - test.container['Config']['Annotations']['this'] == "annotation_value"
          # test DNS
          - >-
            (test.container['HostConfig']['Dns'] is defined and
            test.container['HostConfig']['Dns'] == ['1.1.1.1', '8.8.4.4']) or
            (test.container['HostConfig']['DNS'] is defined and
            test.container['HostConfig']['DNS'] == ['1.1.1.1', '8.8.4.4'])
          # test ports
          - test.container['NetworkSettings']['Ports']|length == 2
          # test working dir
          - test.container['Config']['WorkingDir'] == "/bin"
          # test dns search
          - >-
            (test.container['HostConfig']['DnsSearch'] is defined and
            test.container['HostConfig']['DnsSearch'] == ['example.com']) or
            (test.container['HostConfig']['DNSSearch'] is defined and
            test.container['HostConfig']['DNSSearch'] == ['example.com'])
          # test environment variables
          - "'FOO=bar=1' in test.container['Config']['Env']"
          - "'BAR=foo' in test.container['Config']['Env']"
          - "'TEST=1' in test.container['Config']['Env']"
          - "'BOOL=False' in test.container['Config']['Env']"
          # test labels
          - test.container['Config']['Labels'] | length == 2
          - test.container['Config']['Labels']['somelabel'] == "labelvalue"
          - test.container['Config']['Labels']['otheralbe'] == "othervalue"
          # test mounts
          - test.container['Mounts'][0]['Type'] is defined and test.container['Mounts'][0]['Type'] == 'bind'
          - >-
            test.container['Mounts'][0]['Source'] is defined and test.container['Mounts'][0]['Source'] == 'devpts' or
            test.container['Mounts'][1]['Source'] is defined and test.container['Mounts'][1]['Source'] == 'devpts'
          - >-
            test.container['Mounts'][0]['Destination'] is defined and test.container['Mounts'][0]['Destination'] == '/dev/pts' or
            test.container['Mounts'][1]['Destination'] is defined and test.container['Mounts'][1]['Destination'] == '/dev/pts'
          # test volumes
          # test volumes
          - >-
            (test.container['Mounts'][0]['Destination'] is defined and
            '/data' in test.container['Mounts'] | map(attribute='Destination') | list)
          - >-
            (test.container['Mounts'][1]['Source'] is defined and
            '/tmp' in test.container['Mounts'] | map(attribute='Source') | list)
        fail_msg: Parameters container test failed!
        success_msg: Parameters container test passed!

    - name: Check basic idempotency of running container
      containers.podman.podman_container:
        name: testidem
        image: docker.io/alpine
        state: started
        command: sleep 20m

    - name: Check basic idempotency of running container - run it again
      containers.podman.podman_container:
        name: testidem
        image: alpine:latest
        state: started
        command: sleep 20m
      register: idem

    - name: Check that nothing was changed
      assert:
        that:
          - not idem.changed

    - name: Check force restart option - run again and force restart
      containers.podman.podman_container:
        name: testidem
        image: alpine:latest
        state: started
        command: sleep 20m
        force_restart: true
      register: idem_r

    - name: Check that task was changed
      assert:
        that:
          - idem_r is changed

    - name: Check removing force_restart option
      containers.podman.podman_container:
        name: testidem
        image: alpine:latest
        state: started
        command: sleep 20m
      register: idem_r1

    - name: Check that task was not changed
      assert:
        that:
          - idem_r1 is not changed

    - name: Run changed container (with tty enabled)
      containers.podman.podman_container:
        name: testidem
        image: alpine
        state: started
        command: sleep 20m
        tty: true
      register: idem1

    - name: Check that container is recreated when changed
      assert:
        that:
          - idem1 is changed

    - name: Run changed container without specifying an option, use defaults
      containers.podman.podman_container:
        name: testidem
        image: alpine
        state: started
        command: sleep 20m
      register: idem2

    - name: Check that container is recreated when changed to default value
      assert:
        that:
          - idem2 is changed

    - name: Remove container
      containers.podman.podman_container:
        name: testidem
        state: absent
      register: remove

    - name: Check podman_actions
      assert:
        that:
          - "'podman rm -f testidem' in remove.podman_actions"

    # - name: Create a pod
    #   shell: podman pod create --name testidempod

    - name: Check basic idempotency of pod container
      containers.podman.podman_container:
        name: testidem-pod
        image: docker.io/alpine
        state: started
        command: sleep 20m
        pod: "new:testidempod"

    - name: Check basic idempotency of pod container - run it again
      containers.podman.podman_container:
        name: testidem-pod
        image: alpine:latest
        state: started
        command: sleep 20m
        pod: testidempod
      register: idem3

    - name: Check that nothing was changed in pod containers
      assert:
        that:
          - not idem3.changed

    - name: Run changed pod container (with tty enabled)
      containers.podman.podman_container:
        name: testidem-pod
        image: alpine
        state: started
        command: sleep 20m
        tty: true
        pod: testidempod
      register: idem4

    - name: Check that container is recreated when changed
      assert:
        that:
          - idem4 is changed
          - idem4.podman_systemd.keys() | list | length > 0
          - idem4.podman_systemd.values() | list | length > 0

    - name: Run container with systemd generation parameters
      containers.podman.podman_container:
        name: container1
        image: alpine
        state: started
        command: sleep 20m
        generate_systemd:
          path: /tmp/
          restart_policy: always
          time: 120
          no_header: true
          names: true
          pod_prefix: whocares
          separator: zzzz
          container_prefix: contain
      register: system1

    - name: Check service file presents
      stat:
        path: /tmp/containzzzzcontainer1.service
      register: service_file

    - name: Check that container has correct systemd output
      assert:
        that:
          - system1.podman_systemd.keys() | list | first == 'containzzzzcontainer1'
          - system1.podman_systemd.values() | list | length > 0
          - service_file.stat.exists | bool
          - "'stop -t 120 container1' in system1.podman_systemd.values() | list | first"
          - "'Restart=always' in system1.podman_systemd.values() | list | first"
          - "'autogenerated by Podman' not in system1.podman_systemd.values() | list | first"

  always:

    - name: Remove container
      containers.podman.podman_container:
        name: testidem-pod
        state: absent

    - name: Delete all container leftovers from tests
      containers.podman.podman_container:
        name: "{{ item }}"
        state: absent
      loop:
        - "alpine:3.7"
        - "container"
        - "container1"
        - "container2"
        - "testidem-pod"

    - name: Remove pod
      shell: podman pod rm -f testidempod
      ignore_errors: true
