---
- block:

   - name: Create some test data in the mysql database
     shell: >
       kubectl exec {{ pod_name }} -n {{ ns }} 
       -- {{ item }}
     args:
       executable: /bin/bash
     register: result
     failed_when: "result.rc != 0"
     with_items:
       - mysql -u{{ dbuser }} -p{{ dbpassword }} -e 'create database {{ dbname }};'
       - mysql -u{{ dbuser }} -p{{ dbpassword }} -e 'create table ttbl (Data VARCHAR(20));' {{ dbname }}
       - mysql -u{{ dbuser }} -p{{ dbpassword }} -e 'insert into ttbl (Data) VALUES ("tdata");' {{ dbname }}

  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 if db is ready for connections
     shell: kubectl logs {{ newpod_name.stdout }} -n {{ ns }} | grep 'ready for connections'
     register: initcheck
     until: "'ready for connections' in initcheck.stdout"
     delay: 5
     retries: 180

   - name: Checking for the Corrupted tables
     shell: >
       kubectl exec {{ newpod_name.stdout }} -n {{ ns }}
       -- mysqlcheck -c {{ dbname }} -u{{ dbuser }} -p{{ dbpassword }}
     args:
       executable: /bin/bash
     register: status 
     failed_when: "'OK' not in status.stdout"

   - name: Verify mysql data persistence 
     shell: >
           kubectl exec {{ newpod_name.stdout }} -n {{ ns }}
           -- mysql -u{{ dbuser }} -p{{ dbpassword }} -e 'select * from ttbl' {{ dbname }};
     args:
       executable: /bin/bash
     register: result 
     failed_when: "'tdata' not in result.stdout"   

  when: status == "VERIFY"

- block:

   - name: Delete/drop MySQL database 
     shell: > 
       kubectl exec {{ pod_name }} -n {{ ns }}
       -- mysql -u{{ dbuser }} -p{{ dbpassword }} -e 'drop database {{ dbname }}'; 
     args:
       executable: /bin/bash
     register: status 

   - name: Verify successful db delete 
     shell: > 
           kubectl exec {{ pod_name }} -n {{ ns }}
           -- mysql -u{{ dbuser }} -p{{ dbpassword }} -e 'show databases';
     args:
       executable: /bin/bash
     register: result 
     failed_when: "dbname in result.stdout"   

  when: status == "DELETE"

