---
- hosts: localhost
  connection: local
  gather_facts: False

  vars_files:
    - test_vars.yml

  tasks:
    - block:
        - block:

            - name: Record test instance/run ID
              set_fact:
                run_id: "{{ lookup('env','RUN_ID') }}"

            - name: Construct testname appended with runID
              set_fact:
                test_name: "{{ test_name }}-{{ run_id }}"

          when: lookup('env','RUN_ID')
        
        ## RECORD START-OF-TEST IN e2e RESULT CR
        - include_tasks: /e2e-tests/hack/update_e2e_result_resource.yml
          vars:
            status: 'SOT'

        - block:

            - name: Getting the application pod name
              shell: kubectl get pod -n {{ namespace }} -l {{ app_label }} -o jsonpath={.items[0].metadata.name}
              register: pod_name
    
            - name: Replacing the placeholder for pod-name
              replace:
                path: "{{ busybox_liveness }}"
                regexp: "pod-name"
                replace: "{{ pod_name.stdout }}"   
    
            - name: Replacing the placeholder for namespace
              replace:
                path: "{{ busybox_liveness }}"
                regexp: "app-namespace"
                replace: "{{ namespace }}"   
    
            - name: Replacing the placeholder for liveness-retry-count
              replace:
                path: "{{ busybox_liveness }}"
                regexp: "liveness-retry-count"
                replace: "{{ liveness_retry }}"   
    
            - name: Replacing the placeholder for liveness-timeout
              replace:
                path: "{{ busybox_liveness }}"
                regexp: "liveness-timeout-seconds"
                replace: "{{ liveness_timeout }}"   
    
            - name: Creating busybox-liveness job
              shell: kubectl create -f {{ busybox_liveness }} 
    
            - name: Verifying whether liveness pod is started successfully  
              shell: kubectl get pod -n {{ namespace }} -l liveness=busybox-liveness -o jsonpath={.items[0].status.phase} 
              register: pod_status
              until: "'Running' in pod_status.stdout"
              delay: 5 
              retries: 40
    
            - set_fact:
                flag: "Pass"
          
          when: "'deprovision' not in action"

        - block: 
            - name: Getting the busybox liveness job
              shell: kubectl get job -l liveness=busybox-liveness -n {{ namespace }} -o jsonpath='{.items[0].metadata.name}'
              register: liveness_job

            - name: Deleting busybox liveness job
              shell: kubectl delete job {{ liveness_job.stdout }} -n {{ namespace }}

            - set_fact:
                flag: "Pass"

          when: "'deprovision' is in action"

      rescue:
        - set_fact:
            flag: "Fail"

      always:

        ## RECORD END-OF-TEST IN e2e RESULT CR
        - include_tasks: /e2e-tests/hack/update_e2e_result_resource.yml
          vars:
            status: 'EOT'
        