---
- name: Create directory for user build images
  file:
    path: /tmp/usr_img
    state: directory

- block:

    - name: Copy Dockerfile to container build directory
      copy:
        src: "{{ item }}"
        dest: "/tmp/usr_img/{{ item }}"
        mode: 755
      loop:
        - Dockerfile
        - testinstall.sh

    - name: Build test docker image for regular user
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ runlabel_image }}"
        path: /tmp/usr_img
        build:
          format: docker
          extra_args: --cgroup-manager=cgroupfs

    - name: Run container label install
      containers.podman.podman_runlabel:
        image: "{{ runlabel_image }}"
        label: install
      register: install_runlabel

    - name: Run container label run
      containers.podman.podman_runlabel:
        image: "{{ runlabel_image }}"
        label: run

    - name: Check file for run exists
      stat:
        path: /tmp/testedrunfortests
      register: testedrunfortests

    - name: Make sure files exist
      assert:
        that:
          - testedrunfortests.stat.exists

    - name: Make sure install label exited with 128
      assert:
        that: install_runlabel.stdout == 'Installed.'
  always:

    - name: Remove the directory
      file:
        path: /tmp/usr_img
        state: absent

    - name: Remove the image
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ runlabel_image }}"
        state: absent
