# SPDX-License-Identifier: MIT
---
- name: Ensure that the role runs with default parameters
  hosts: all
  vars_files:
    - vars/test_vars.yml
  vars:
    podman_host_directories:
      "{{ __test_tmpdir.path ~ '/httpd1-create' }}":
        mode: "0777"
        owner: "{{ 3001 +
          podman_subuid_info[__podman_test_username]['start'] - 1 }}"
        group: "{{ 3001 +
          podman_subgid_info[__podman_test_username]['start'] - 1 }}"
    podman_run_as_user: root
    __podman_test_username: podman_basic_user
    test_names_users:
      - [httpd1, "{{ __podman_test_username }}", 3001]
      - [httpd2, root, 0]
      - [httpd3, root, 0]
    podman_create_host_directories: true
    podman_firewall:
      - port: 15001-15003/tcp
        state: enabled
    podman_selinux_ports:
      - ports: 15001-15003
        setype: http_port_t
    __podman_kube_specs:
      - state: started
        debug: true
        log_level: debug
        run_as_user: "{{ __podman_test_username }}"
        kube_file_content:
          apiVersion: v1
          kind: Pod
          metadata:
            labels:
              app: test
              io.containers.autoupdate: registry
            name: httpd1
          spec:
            containers:
              - name: httpd1
                image: "{{ test_image }}"
                command:
                  - /bin/busybox-extras
                  - httpd
                  - -f
                  - -p
                  - 80
                ports:
                  - containerPort: 80
                    hostPort: 15001
                volumeMounts:
                  - mountPath: /var/www:Z
                    name: www
                  - mountPath: /var/httpd-create:Z
                    name: create
                workingDir: /var/www
            volumes:
              - name: www
                hostPath:
                  path: "{{ __test_tmpdir.path ~ '/httpd1' }}"
              - name: create
                hostPath:
                  path: "{{ __test_tmpdir.path ~ '/httpd1-create' }}"
      - state: started
        debug: true
        log_level: debug
        kube_file_content:
          apiVersion: v1
          kind: Pod
          metadata:
            labels:
              app: test
              io.containers.autoupdate: registry
            name: httpd2
          spec:
            containers:
              - name: httpd2
                image: "{{ test_image }}"
                command:
                  - /bin/busybox-extras
                  - httpd
                  - -f
                  - -p
                  - 80
                ports:
                  - containerPort: 80
                    hostPort: 15002
                volumeMounts:
                  - mountPath: /var/www:Z
                    name: www
                  - mountPath: /var/httpd-create:Z
                    name: create
                workingDir: /var/www
            volumes:
              - name: www
                hostPath:
                  path: "{{ __test_tmpdir.path ~ '/httpd2' }}"
              - name: create
                hostPath:
                  path: "{{ __test_tmpdir.path ~ '/httpd2-create' }}"
    __podman_kube_file_content: |
      apiVersion: v1
      kind: Pod
      metadata:
        labels:
          app: test
          io.containers.autoupdate: registry
        name: httpd3
      spec:
        containers:
          - name: httpd3
            image: {{ test_image }}
            command:
              - /bin/busybox-extras
              - httpd
              - -f
              - -p
              - 80
            ports:
              - containerPort: 80
                hostPort: 15003
            volumeMounts:
              - mountPath: /var/www:Z
                name: www
              - mountPath: /var/httpd-create:Z
                name: create
            workingDir: /var/www
        volumes:
          - name: www
            hostPath:
              path: "{{ __test_tmpdir.path ~ '/httpd3' }}"
          - name: create
            hostPath:
              path: "{{ __test_tmpdir.path ~ '/httpd3-create' }}"
  tasks:
    - name: Create tmpdir for testing
      tempfile:
        state: directory
        prefix: lsr_
        suffix: _podman
      register: __test_tmpdir

    - name: Change tmpdir permissions
      file:
        path: "{{ __test_tmpdir.path }}"
        mode: "0777"

    - name: Run basic tests
      vars:
        __podman_use_kube_file:
          state: started
          kube_file_src: "{{ __kube_file_src.path }}"
      block:
        - name: Enable podman copr
          command: dnf copr enable rhcontainerbot/podman-next -y
          when: podman_use_copr | d(false)
          changed_when: true

        - name: Install podman from updates-testing
          command: dnf -y install podman
          when: podman_use_copr | d(false)
          changed_when: true

        - name: Podman version
          command: podman --version
          when: podman_use_copr | d(false)
          changed_when: false

        - name: Create user
          user:
            name: "{{ __podman_test_username }}"
            uid: 3001

        - name: Create tempfile for kube_src
          tempfile:
            state: file
            prefix: lsr_podman_
            suffix: .yml
          register: __kube_file_src
          delegate_to: localhost

        - name: Write kube_file_src
          copy:
            content: "{{ __podman_kube_file_content }}"
            dest: "{{ __kube_file_src.path }}"
            mode: "0600"
          delegate_to: localhost

        - name: Create host directories for data
          file:
            path: "{{ __test_tmpdir.path ~ '/' ~ item[0] }}"
            state: directory
            mode: "0755"
            owner: "{{ item[1] }}"
          loop: "{{ test_names_users }}"

        - name: Create data files
          copy:
            dest: "{{ __test_tmpdir.path ~ '/' ~ item[0] ~ '/index.txt' }}"
            content: "123"
            mode: "0644"
            owner: "{{ item[1] }}"
          loop: "{{ test_names_users }}"

        - name: See if not pulling images fails
          block:
            - name: Run role - do not pull images
              include_role:
                name: linux-system-roles.podman
              vars:
                podman_kube_specs:
                  - state: created
                    pull_image: false
                    activate_systemd_unit: false
                    kube_file_content: |
                      apiVersion: v1
                      kind: Pod
                      metadata:
                        labels:
                          app: test
                          io.containers.autoupdate: registry
                        name: nopull
                      spec:
                        containers:
                          - name: nopull
                            image: {{ test_image }}

            - name: Verify image not pulled
              assert:
                that: __podman_image_updated.results[0] is skipped

            - name: Run role - verify continue if pull fails
              include_role:
                name: linux-system-roles.podman
              vars:
                podman_kube_specs:
                  - state: created
                    continue_if_pull_fails: true
                    activate_systemd_unit: false
                    kube_file_content: |
                      apiVersion: v1
                      kind: Pod
                      metadata:
                        labels:
                          app: test
                          io.containers.autoupdate: registry
                        name: bogus
                      spec:
                        containers:
                          - name: bogus
                            image: >-
                              quay.io/linux-system-roles/this_is_a_bogus_image:latest

          rescue:
            - name: Verify image not pulled
              assert:
                that:
                  - not __podman_image_updated.results[0] is changed
                  - not __podman_image_updated.results[0] is skipped

          always:
            - name: Cleanup
              include_role:
                name: linux-system-roles.podman
              vars:
                podman_kube_specs:
                  - state: absent
                    kube_file_content: |
                      apiVersion: v1
                      kind: Pod
                      metadata:
                        labels:
                          app: test
                          io.containers.autoupdate: registry
                        name: {{ item }}
                      spec:
                        containers:
                          - name: {{ item }}
                            image: {{ test_image }}
              loop:
                - nopull
                - bogus

        - name: Run role
          include_role:
            name: linux-system-roles.podman
            public: true
          vars:
            podman_kube_specs: "{{ __podman_kube_specs |
              union([__podman_use_kube_file]) | list }}"
            podman_pull_retry: true

        - name: Check if pods are running
          command: podman pod inspect {{ item[0] }} --format {{ __fmt | quote }}
          changed_when: false
          register: __output
          failed_when: __output.stdout != 'Running'
          become: "{{ (item[1] != 'root') | ternary(true, omit) }}"
          become_user: "{{ (item[1] != 'root') | ternary(item[1], omit) }}"
          vars:
            __fmt: "{{ '{' ~ '{.State}' ~ '}' }}"
          loop: "{{ test_names_users }}"

        - name: Check Services
          shell: |
            set -euo pipefail
            systemctl --{{ __state }} list-units -a -l --plain | \
              grep -E '{{ __regex }} '
          changed_when: false
          become: "{{ (item[1] != 'root') | ternary(true, omit) }}"
          become_user: "{{ (item[1] != 'root') | ternary(item[1], omit) }}"
          environment:
            XDG_RUNTIME_DIR: /run/user/{{ item[2] }}
          loop: "{{ test_names_users }}"
          vars:
            __state: "{{ (item[1] != 'root') | ternary('user', 'system') }}"
            __regex: >-
              ^[  ]*podman-kube@.+-{{ item[0] }}[.]yml[.]service[ ]+loaded[
              ]+active

        - name: Check ports, data
          uri:
            url: http://localhost:{{ item }}/index.txt
            return_content: true
          register: __return
          failed_when: __return.content != "123"
          loop: [15001, 15002]

        - name: Check host directories
          command: ls -alrtF {{ __test_tmpdir.path ~ '/' ~ item[0] }}-create
          loop: "{{ test_names_users }}"
          changed_when: false

        - name: Run role again to test for idempotency
          include_role:
            name: linux-system-roles.podman
          vars:
            podman_kube_specs: "{{ __podman_kube_specs |
              union([__podman_use_kube_file]) | list }}"

        - name: Check if pods are running
          command: podman pod inspect {{ item[0] }} --format {{ __fmt | quote }}
          changed_when: false
          register: __output
          failed_when: __output.stdout != 'Running'
          become: "{{ (item[1] != 'root') | ternary(true, omit) }}"
          become_user: "{{ (item[1] != 'root') | ternary(item[1], omit) }}"
          vars:
            __fmt: "{{ '{' ~ '{.State}' ~ '}' }}"
          loop: "{{ test_names_users }}"

        - name: Check Services
          shell: |
            set -euo pipefail
            systemctl --{{ __state }} list-units -a -l --plain | \
              grep -E '{{ __regex }} '
          changed_when: false
          become: "{{ (item[1] != 'root') | ternary(true, omit) }}"
          become_user: "{{ (item[1] != 'root') | ternary(item[1], omit) }}"
          environment:
            XDG_RUNTIME_DIR: /run/user/{{ item[2] }}
          loop: "{{ test_names_users }}"
          vars:
            __state: "{{ (item[1] != 'root') | ternary('user', 'system') }}"
            __regex: >-
              ^[  ]*podman-kube@.+-{{ item[0] }}[.]yml[.]service[ ]+loaded[
              ]+active

        - name: Check ports, data
          uri:
            url: http://localhost:{{ item }}/index.txt
            return_content: true
          register: __return
          failed_when: __return.content != "123"
          loop: [15001, 15002, 15003]

        # - name: Check auto-update
        #   command: podman auto-update --dry-run
        #   changed_when: false

        - name: Remove pods and units
          include_role:
            name: linux-system-roles.podman
          vars:
            # noqa jinja[spacing]
            podman_kube_specs: "{{ __podman_kube_specs |
              union([__podman_use_kube_file]) |
              map('combine', {'state':'absent'}) | list }}"
            podman_create_host_directories: false
            podman_firewall: []
            podman_selinux_ports: []

        - name: Check that pods are not running
          command: podman pod exists {{ item[0] }}
          changed_when: false
          register: __output
          failed_when:
            - not __output is failed
            - item[1] == "root" or
              not __output.stderr is search(__err_pat)
          become: "{{ (item[1] != 'root') | ternary(true, omit) }}"
          become_user: "{{ (item[1] != 'root') | ternary(item[1], omit) }}"
          vars:
            __err_pat: "Error: creating events dirs"
          loop: "{{ test_names_users }}"

        - name: Check Services should be absent
          shell: |
            set -euo pipefail
            systemctl --{{ __state }} list-units -a -l --plain | \
              grep -E '{{ __regex }} '
          become: "{{ (item[1] != 'root') | ternary(true, omit) }}"
          become_user: "{{ (item[1] != 'root') | ternary(item[1], omit) }}"
          register: __output
          failed_when: not __output is failed
          changed_when: false
          environment:
            XDG_RUNTIME_DIR: /run/user/{{ item[2] }}
          loop: "{{ test_names_users }}"
          vars:
            __state: "{{ (item[1] != 'root') | ternary('user', 'system') }}"
            __regex: >-
              ^[  ]*podman-kube@.+-{{ item[0] }}[.]yml[.]service[ ]+loaded[
              ]+active

        - name: Ensure no linger
          stat:
            path: /var/lib/systemd/linger/{{ item[1] }}
          when: item[1] != "root"
          loop: "{{ test_names_users }}"
          register: __stat
          failed_when: __stat.stat.exists

        - name: Remove pods and units again - test idempotence
          include_role:
            name: linux-system-roles.podman
          vars:
            # noqa jinja[spacing]
            podman_kube_specs: "{{ __podman_kube_specs |
              union([__podman_use_kube_file]) |
              map('combine', {'state':'absent'}) | list }}"
            podman_create_host_directories: false
            podman_firewall: []
            podman_selinux_ports: []

      rescue:
        - name: Dump journal
          command: journalctl -ex
          changed_when: false
          failed_when: true

      always:
        # have to clean up storage.conf - otherwise, get this message:
        # A storage.conf file exists at /etc/containers/storage.conf
        # You should remove this file if you did not modify the configuration.
        - name: Clean up storage.conf
          file:
            path: /etc/containers/storage.conf
            state: absent
          tags:
            - tests::cleanup

        - name: Clean up host directories
          file:
            path: "{{ __test_tmpdir.path }}"
            state: absent
          tags:
            - tests::cleanup

        - name: Remove kube file src
          file:
            path: "{{ __kube_file_src.path }}"
            state: absent
          delegate_to: localhost
