---
- block:

   - name: Create some test data in the busybox app
     shell: >
       kubectl exec {{ pod_name }} -n {{ ns }} 
       -- sh -c "{{ item }}"
     args:
       executable: /bin/bash
     register: result
     failed_when: "result.rc != 0"
     with_items:
       - "dd if=/dev/urandom of=/busybox/{{ testfile }} bs={{ blocksize }} count={{ blockcount }}"
       - "md5sum /busybox/{{ testfile }} > /busybox/{{ testfile }}-pre-chaos-md5"
       - "sync;sync;sync"

  when: status == "LOAD"

- block:

   - name: Kill the application pod
     shell: >
       kubectl delete pod {{ pod_name }} -n {{ ns }}
     args:
       executable: /bin/bash

   - name: Verify if the application pod is deleted
     shell: >
       kubectl get pods -n {{ ns }}
     args:
       executable: /bin/bash
     register: podstatus
     until: '"{{ pod_name }}" not in podstatus.stdout'
     retries: 2
     delay: 150

   - name: Obtain the newly created pod name for application
     shell: >
       kubectl get pods -n {{ ns }} -l {{ label }} -o jsonpath='{.items[].metadata.name}'
     args:
       executable: /bin/bash
     register: newpod_name

   - name: Checking application pod is in running state
     shell: kubectl get pods -n {{ ns }} -o jsonpath='{.items[?(@.metadata.name=="{{ newpod_name.stdout }}")].status.phase}'
     register: result
     until: "((result.stdout.split()|unique)|length) == 1 and 'Running' in result.stdout"
     delay: 2
     retries: 150

   - name: Get the container status of application.
     shell: >
        kubectl get pods -n {{ ns }} -o jsonpath='{.items[?(@.metadata.name=="{{ newpod_name.stdout }}")].status.containerStatuses[].state}' | grep running
     args:
       executable: /bin/bash
     register: containerStatus
     until: "'running' in containerStatus.stdout"
     delay: 2
     retries: 150

   - name: Check the md5sum of stored data file
     shell: >
       kubectl exec {{ newpod_name.stdout }} -n {{ ns }}
       -- sh -c "md5sum /busybox/{{ testfile }} > /busybox/{{ testfile }}-post-chaos-md5"
     args:
       executable: /bin/bash
     register: status 
     failed_when: "status.rc != 0"

   - name: Verify whether data is consistent
     shell: >
           kubectl exec {{ newpod_name.stdout }} -n {{ ns }}
           -- sh -c "diff /busybox/{{ testfile }}-pre-chaos-md5 /busybox/{{ testfile }}-post-chaos-md5"
     args:
       executable: /bin/bash
     register: result 
     failed_when: "result.rc != 0 or result.stdout != ''"   

  when: status == "VERIFY"

- block:

   - name: Obtain the current pod name for application
     shell: >
       kubectl get pods -n {{ ns }} -l {{ label }} -o jsonpath='{.items[].metadata.name}'
     args:
       executable: /bin/bash
     register: newpod_name

   - name: Delete/drop the files 
     shell: > 
       kubectl exec {{ newpod_name.stdout }} -n {{ ns }}
       -- sh -c "rm -f /busybox/{{ testfile }}*"
     args:
       executable: /bin/bash
     register: status 

   - name: Verify successful file delete 
     shell: > 
           kubectl exec {{ newpod_name.stdout }} -n {{ ns }}
           -- ls /busybox/
     args:
       executable: /bin/bash
     register: result 
     failed_when: "testfile in result.stdout"   

  when: status == "DELETE"

