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

  vars_files:
    - test_vars.yml
    - /mnt/parameters.yml

  tasks:
    - block:

          ## Generating the testname for service failure chaos test
        - include_tasks: /e2e-tests/hack/create_testname.yml
    
          ## Record SOT (start of test) in e2e result e2e-cr (e2e-custom-resource)
        - include_tasks: /e2e-tests/hack/update_e2e_result_resource.yml
          vars:
            status: 'SOT'

        - name: Identify the data consistency util to be invoked
          template:
            src: data_persistence.j2
            dest: data_persistence.yml

        - include_vars:
            file: data_persistence.yml
            
        - name: Record the data consistency util path
          set_fact:
            data_consistency_util_path: "{{ consistencyutil }}"
          when: data_persistence != ''
      
        - name: Get the application pod name
          shell: > 
            kubectl get pods -n {{ namespace }} -l {{ label }} --no-headers
            -o custom-columns=:.metadata.name
          args:
            executable: /bin/bash
          register: app_pod_name      

        - name: Create some test data
          include: "{{ data_consistency_util_path }}"
          vars:
            status: 'LOAD'
            ns: "{{ namespace }}"
            pod_name: "{{ app_pod_name.stdout }}"
          when: data_persistence != ''

        - include_tasks: /e2e-tests/chaoslib/service_failure/service_chaos.yml
          vars:
            app_ns: "{{ namespace }}"
            app_label: "{{ label }}"
            action: "svc_stop"
            app_pod: "{{ app_pod_name.stdout }}"

        - include_tasks: /e2e-tests/chaoslib/service_failure/service_chaos.yml
          vars:
            app_ns: "{{ namespace }}"
            app_label: "{{ label }}"
            action: "svc_start"
            app_pod: "{{ app_pod_name.stdout }}"

        - name: Verify application data persistence
          include: "{{ data_consistency_util_path }}"
          vars:
            status: 'VERIFY'
            ns: "{{ namespace }}"
            pod_name: "{{ new_app_pod }}"
          when: data_persistence != ''
 
        - set_fact:
            flag: "Pass"

      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'