---
- block:
    - name: Create a project
      community.okd.k8s:
        name: "{{ playbook_namespace }}"
        kind: Project
        api_version: project.openshift.io/v1

    - name: incredibly simple ConfigMap
      community.okd.k8s:
        definition:
          apiVersion: v1
          kind: ConfigMap
          metadata:
            name: hello
            namespace: "{{ playbook_namespace }}"
        validate:
          fail_on_error: yes
      register: k8s_with_validate

    - name: assert that k8s_with_validate succeeds
      assert:
        that:
          - k8s_with_validate is successful

    - name: extra property does not fail without strict
      community.okd.k8s:
        src: "files/kuard-extra-property.yml"
        namespace: "{{ playbook_namespace }}"
        validate:
          fail_on_error: yes
          strict: no

    - name: extra property fails with strict
      community.okd.k8s:
        src: "files/kuard-extra-property.yml"
        namespace: "{{ playbook_namespace }}"
        validate:
          fail_on_error: yes
          strict: yes
      ignore_errors: yes
      register: extra_property

    - name: check that extra property fails with strict
      assert:
        that:
          - extra_property is failed

    - name: invalid type fails at validation stage
      community.okd.k8s:
        src: "files/kuard-invalid-type.yml"
        namespace: "{{ playbook_namespace }}"
        validate:
          fail_on_error: yes
          strict: no
      ignore_errors: yes
      register: invalid_type

    - name: check that invalid type fails
      assert:
        that:
          - invalid_type is failed

    - name: invalid type fails with warnings when fail_on_error is False
      community.okd.k8s:
        src: "files/kuard-invalid-type.yml"
        namespace: "{{ playbook_namespace }}"
        validate:
          fail_on_error: no
          strict: no
      ignore_errors: yes
      register: invalid_type_no_fail

    - name: check that invalid type fails
      assert:
        that:
          - invalid_type_no_fail is failed

    - name: setup custom resource definition
      community.okd.k8s:
        src: "files/setup-crd.yml"

    - name: wait a few seconds
      pause:
        seconds: 5

    - name: add custom resource definition
      community.okd.k8s:
        src: "files/crd-resource.yml"
        namespace: "{{ playbook_namespace }}"
        validate:
          fail_on_error: yes
          strict: yes
      register: unknown_kind

    - name: check that unknown kind warns
      assert:
        that:
          - unknown_kind is successful

  always:
    - name: remove custom resource
      community.okd.k8s:
        definition: "{{ lookup('file', 'files/crd-resource.yml') }}"
        namespace: "{{ playbook_namespace }}"
        state: absent
      ignore_errors: yes

    - name: remove custom resource definitions
      community.okd.k8s:
        definition: "{{ lookup('file', 'files/setup-crd.yml') }}"
        state: absent

    - name: Delete namespace
      community.okd.k8s:
        state: absent
        definition:
          - kind: Project
            apiVersion: project.openshift.io/v1
            metadata:
              name: "{{ playbook_namespace }}"
      ignore_errors: yes
