#  Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
---         
# Test case to verify the prerequisites are installed and execute the AWX deployment
- name: OMNIA_1.1_AWX_TC_001    
  hosts: localhost
  connection: local
  vars_files:
   - test_vars/test_inventory_vars.yml
  tasks:
     
   - name: Check login_vars file is encrypted
     command: cat "{{ login_vars_path }}"
     changed_when: false
     register: config_content
     tags: always
      
   - name: Decrpyt login_vars.yml
     command: >-
       ansible-vault decrypt {{ login_vars_path }}
       --vault-password-file {{ login_vars_vault_path }}
     changed_when: false
     when: "'$ANSIBLE_VAULT;' in config_content.stdout"
     tags: always

   - name: Include variable file login_vars.yml
     include_vars: "{{ login_vars_path }}"
     tags: always
       
   - name: Encypt login file
     command: >-
       ansible-vault encrypt {{ login_vars_path }}
       --vault-password-file {{ login_vars_vault_path }}
     changed_when: false
     tags: always
                     
   - name: Execute awx command
     command: "kubectl get pods -n {{ awx_namespace }}"
     changed_when: true
     register: k8s_pods
     run_once: true
     ignore_errors: true
     tags: TC_001,VERIFY_OMNIA_01    
     
   - name: Validate awx operator containers
     assert:
      that:  
       -  k8s_pods.stdout | regex_search("{{ item }}")
      fail_msg: "{{ awx_fail_msg }}"
      success_msg: "{{ awx_success_msg }}"
     loop: 
       - "awx-([A-Za-z0-9]{10})-([A-Za-z0-9]{5})"
       - "awx-operator-([A-Za-z0-9]{10})-([A-Za-z0-9]{5})"
       - "awx-postgres-([A-Za-z0-9]{1})"
     run_once: true
     tags: TC_001,VERIFY_OMNIA_01       
            
# Test case to verify inventory groups are present in AWX UI  (idrac, ethernet, inifiniband, rbod)  
- name: OMNIA_1.1_AWX_TC_003   
  hosts: localhost
  connection: local
  
  vars_files:
   - test_vars/test_inventory_vars.yml
  tasks:                 

   - name: Execute get pods command
     command: "kubectl get pods -n {{ awx_namespace }}"
     changed_when: true
     register: k8s_pods
     run_once: true
     ignore_errors: true
     tags: TC_003     
     
   - name: Get awx pod 
     set_fact:
      awx_pods: "{{ item | regex_search(awx_pod_regex) | trim  }}"
      idrac_status: true
     with_items: 
       - "{{ k8s_pods.stdout_lines }}"
     run_once: true
     when: item | regex_search(awx_pod_item_regex)
     tags: TC_003

   - name: Get awx cluster ip
     shell: "kubectl get svc awx-ui -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
     register: awx_cluster_ip
     changed_when: false
     ignore_errors: true
     tags: TC_003

   - name: Get AWX admin password
     shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
     register: awx_admin_password
     changed_when: false
     ignore_errors: true
     tags: TC_003
          
   - name: Execute awx get inventory hosts command
     command: "awx --conf.host http://{{ awx_cluster_ip.stdout }}:8052 --conf.username admin --conf.password {{ awx_admin_password.stdout }} --conf.insecure hosts list --inventory {{ item }} -f human --filter 'name'"
     register: idrac_hosts
     with_items:
      - "idrac_inventory"
      - "infiniband_inventory"
      - "ethernet_inventory"
      - "powervault_me4_inventory"
     run_once: true
     changed_when: false
     tags: TC_003        
       
   - name: Verify  inventory are present in AWX UI  
     assert:
      that: 
       - item.stdout_lines[0] | regex_search("name")
      fail_msg: "{{ item.item }} - {{ inventory_fail_msg }}"
      success_msg: "{{ item.item }} - {{ inventory_success_msg }}"
     with_items:
      - "{{ idrac_hosts.results }}"
     changed_when: false
     tags: TC_003
              
# Test case to validate ip of idrac     
- name: OMNIA_1.1_AWX_TC_004    
  hosts: localhost
  connection: local
  
  vars_files:
   - test_vars/test_inventory_vars.yml
   - ../input_params/base_vars.yml  
  tasks:       
      
   - name: Execute get pods command
     command: "kubectl get pods -n {{ awx_namespace }}"
     changed_when: true
     register: k8s_pods
     run_once: true
     ignore_errors: true
     tags: TC_004     
     
   - name: Get awx pod 
     set_fact:
      awx_pods: "{{ item | regex_search(awx_pod_regex) | trim  }}"
     with_items: 
       - "{{ k8s_pods.stdout_lines }}"
     run_once: true
     when: item | regex_search(awx_pod_item_regex)
     changed_when: false
     tags: TC_004

   - name: Get awx cluster ip
     shell: "kubectl get svc awx-ui -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
     register: awx_cluster_ip
     changed_when: false
     tags: TC_004

   - name: Get AWX admin password
     shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
     register: awx_admin_password
     changed_when: false
     ignore_errors: true
     tags: TC_004
          
   - name: Execute awx get inventory hosts command
     command: "awx --conf.host http://{{ awx_cluster_ip.stdout }}:8052 --conf.username admin --conf.password {{ awx_admin_password.stdout }} --conf.insecure hosts list --inventory {{ idrac_inventory_name }} -f human --filter 'name'"
     changed_when: true
     register: idrac_hosts
     run_once: true
     tags: TC_004     
     
   - name: List of iDRAC host
     include_tasks: "{{ validation_script_path }}"
     with_items:
      - "{{ idrac_hosts.stdout_lines[2:] }}"
     when: idrac_hosts.stdout_lines | length > 2
     ignore_errors: true
     tags: TC_004
     
   - name: Empty iDRAC hosts
     debug:
      msg: "{{ empty_host_err }}"
     when: idrac_hosts.stdout_lines | length < 3
     failed_when: false
     tags: TC_004     

# Test case to validate ip of infiniband
- name: OMNIA_1.1_AWX_TC_005    
  hosts: localhost
  connection: local
  
  vars_files:
   - test_vars/test_inventory_vars.yml
   - ../input_params/base_vars.yml  
  tasks:                 

   - name: Execute get pods command
     command: "kubectl get pods -n {{ awx_namespace }}"
     changed_when: true
     register: k8s_pods
     run_once: true
     ignore_errors: true
     tags: TC_005     
     
   - name: Get awx pod 
     set_fact:
      awx_pods: "{{ item | regex_search(awx_pod_regex) | trim  }}"
     with_items: 
       - "{{ k8s_pods.stdout_lines }}"
     run_once: true
     when: item | regex_search(awx_pod_item_regex)
     failed_when: false
     tags: TC_005

   - name: Get awx cluster ip
     shell: "kubectl get svc awx-ui -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
     register: awx_cluster_ip
     changed_when: false
     tags: TC_005

   - name: Get AWX admin password
     shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
     register: awx_admin_password
     changed_when: false
     ignore_errors: true
     tags: TC_005
          
   - name: Execute awx get inventory hosts command
     command: "awx --conf.host http://{{ awx_cluster_ip.stdout }}:8052 --conf.username admin --conf.password {{ awx_admin_password.stdout }} --conf.insecure hosts list --inventory {{ ib_inventory_name }} -f human --filter 'name'"
     changed_when: true
     register: infiniband_hosts
     run_once: true
     ignore_errors: true
     tags: TC_005     
     
   - name: List of infiniband hosts
     include_tasks: "{{ validation_script_path }}"  
     with_items:
      - "{{ infiniband_hosts.stdout_lines[2:] }}"
     when: infiniband_hosts.stdout_lines | length > 2
     ignore_errors: true
     tags: TC_005
     
   - name: Empty infiniband hosts
     debug:
      msg: "{{ empty_host_err }}"
     when: infiniband_hosts.stdout_lines | length < 3
     failed_when: false
     tags: TC_005 

# Test case to validate ip of ethernet
- name: OMNIA_1.1_AWX_TC_006    
  hosts: localhost
  connection: local
  
  vars_files:
   - test_vars/test_inventory_vars.yml
   - ../input_params/base_vars.yml  
  tasks:                 

   - name: Execute get pods command
     command: "kubectl get pods -n {{ awx_namespace }}"
     changed_when: true
     register: k8s_pods
     run_once: true
     ignore_errors: true
     tags: TC_006     
     
   - name: Get awx pod 
     set_fact:
      awx_pods: "{{ item | regex_search(awx_pod_regex) | trim  }}"
     with_items: 
       - "{{ k8s_pods.stdout_lines }}"
     run_once: true
     when: item | regex_search(awx_pod_item_regex)
     failed_when: false
     tags: TC_006

   - name: Get awx cluster ip
     shell: "kubectl get svc awx-ui -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
     register: awx_cluster_ip
     changed_when: false
     tags: TC_006

   - name: Get AWX admin password
     shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
     register: awx_admin_password
     changed_when: false
     ignore_errors: true
     tags: TC_006
          
   - name: Execute awx get inventory hosts command
     command: "awx --conf.host http://{{ awx_cluster_ip.stdout }}:8052 --conf.username admin --conf.password {{ awx_admin_password.stdout }} --conf.insecure hosts list --inventory {{ ethernet_inventory_name }} -f human --filter 'name'"
     changed_when: true
     register: ethernet_hosts
     run_once: true
     ignore_errors: true
     tags: TC_006     
     
   - name: List of ethernet hosts   
     include_tasks: "{{ validation_script_path }}" 
     with_items:
      - "{{ ethernet_hosts.stdout_lines[2:] }}"
     when: ethernet_hosts.stdout_lines | length > 2
     ignore_errors: true
     tags: TC_006
     
   - name: Empty ethernet hosts
     debug:
      msg: "{{ empty_host_err }}"
     when: ethernet_hosts.stdout_lines | length < 3
     failed_when: false
     tags: TC_006
      
# Test case to validate ip of powervault      
- name: OMNIA_1.1_AWX_TC_007    
  hosts: localhost
  connection: local
  
  vars_files:
   - test_vars/test_inventory_vars.yml
   - ../input_params/base_vars.yml  
  tasks:                 

   - name: Execute get pods command
     command: "kubectl get pods -n {{ awx_namespace }}"
     changed_when: true
     register: k8s_pods
     run_once: true
     ignore_errors: true
     tags: TC_007     
     
   - name: Get awx pod 
     set_fact:
      awx_pods: "{{ item | regex_search(awx_pod_regex) | trim  }}"
      idrac_status: true
     with_items: 
       - "{{ k8s_pods.stdout_lines }}"
     run_once: true
     when: item | regex_search(awx_pod_item_regex)
     failed_when: false
     tags: TC_007

   - name: Get awx cluster ip
     shell: "kubectl get svc awx-ui -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
     register: awx_cluster_ip
     changed_when: false
     tags: TC_007

   - name: Get AWX admin password
     shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
     register: awx_admin_password
     changed_when: false
     ignore_errors: true
     tags: TC_007
          
   - name: Execute awx get inventory hosts command
     command: "awx --conf.host http://{{ awx_cluster_ip.stdout }}:8052 --conf.username admin --conf.password {{ awx_admin_password.stdout }} --conf.insecure hosts list --inventory {{ pv_inventory_name }} -f human --filter 'name'"
     changed_when: true
     register: powervault_hosts
     run_once: true
     ignore_errors: true
     tags: TC_007     
     
   - name: List of powervault hosts
     include_tasks: "{{ validation_script_path }}"  
     with_items:
      - "{{ powervault_hosts.stdout_lines[2:] }}"
     when: powervault_hosts.stdout_lines | length > 2
     ignore_errors: true
     tags: TC_007
     
   - name: Empty powervault hosts
     debug:
      msg: "{{ empty_host_err }}"
     when: powervault_hosts.stdout_lines | length < 3
     failed_when: false
     tags: TC_007

# Test case to verify omnia inventory groups (manager, compute, login, nfs)                        
- name: OMNIA_1.1_AWX_TC_008   
  hosts: localhost
  connection: local
  vars_files:
   - test_vars/test_inventory_vars.yml
   - ../roles/webui_awx/vars/main.yml
    
  tasks:                 

    - name: Get awx-service Cluster-IP
      command: "kubectl get svc awx-service -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
      register: awx_cluster_ip
      changed_when: false
      run_once: true
      ignore_errors: true
      tags: TC_008
    
    - name: Get AWX admin password
      shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
      register: awx_admin_password
      changed_when: false
      run_once: true
      ignore_errors: true
      tags: TC_008
         
    - name: Set IP and password
      set_fact:
        awx_ip: 'http://{{ awx_cluster_ip.stdout }}'
        admin_password: "{{ awx_admin_password.stdout }}"
      run_once: true
      failed_when: false
      tags: TC_008
       
    - name: Get omnia inventory groups
      awx.awx.tower_group:
        name: "{{ item.name }}"
        description: "{{ item.description }}"
        inventory: "node_inventory"
        state: present
      loop: "{{ group_names }}"
      register: awx_group
      run_once: true
      ignore_errors: true
      tags: TC_008
     
    - name: Verify omnia inventory groups
      assert:
       that: 
         - item.changed == false
         - item.item.name == "{{ manager_group }}" or 
           item.item.name =="{{ compute_group }}" or 
           item.item.name == "{{ login_group }}" or 
           item.item.name == "{{ nfs_group }}"
       fail_msg: "{{ item .item.name }}{{ group_fail_msg }}"
       success_msg: "{{ item .item.name }}{{ group_success_msg }}"
      with_items:
       - "{{ awx_group.results }}"
      failed_when: false
      tags: TC_008
      
# Test case to verify AWX configuration is done properly with job_templates, schedules in place      
- name: OMNIA_1.1_AWX_TC_009   
  hosts: localhost
  connection: local
  vars_files:
   - test_vars/test_inventory_vars.yml
   - ../roles/webui_awx/vars/main.yml
    
  tasks:                 

    - name: Get awx-service Cluster-IP
      command: "kubectl get svc awx-service -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
      register: awx_cluster_ip
      changed_when: false
      ignore_errors: true
      run_once: true
      tags: TC_009
    
    - name: Get AWX admin password
      shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
      register: awx_admin_password
      changed_when: false
      ignore_errors: true
      run_once: true
      tags: TC_009
           
    - name: Set IP and password
      set_fact:
        awx_ip: 'http://{{ awx_cluster_ip.stdout }}'
        admin_password: "{{ awx_admin_password.stdout }}"
      failed_when: false
      run_once: true
      tags: TC_009   
      
    - name: Get job template details
      awx.awx.tower_job_template:
        name: "{{ item.name }}"
        job_type: "run"
        organization: "{{ organization_name }}"
        inventory: "{{ item.inventory }}"
        project: "{{ project_name }}"
        playbook: "{{ item.playbook }}"
        credentials:
          - "{{ item.credential }}"
        state: present
        tower_config_file: "{{ tower_config_file_path }}"
      loop: "{{ job_template_details }}"
      register: job_template
      when: item.flag
      ignore_errors: true
      tags: TC_009  

    - name: Validate job template 
      assert:
       that: 
         - item.changed == false
       fail_msg: "{{ item.item.name }}{{ job_template_fail_msg }}"
       success_msg: " {{ item.item.name }} {{ job_template_success_msg }}"
      with_items:
        - "{{ job_template.results }}"
      failed_when: false
      when: item.item.flag
      tags: TC_009  
      
    - name: Build a schedule for job template
      awx.awx.tower_schedule:
        name: "{{ item.name }}"
        unified_job_template: "{{ item.template }}"
        rrule: "{{ schedule_rule }}"
        state: present
        tower_config_file: "{{ tower_config_file_path }}"
      register: schedule
      loop: "{{ scheduled_templates }}"
      failed_when: false
      run_once: true
      tags: TC_009
      
    - name: Validate schedule status
      assert:
       that: 
         - schedule.changed == false
       fail_msg: "{{ schedule_fail_msg }}"
       success_msg: "{{ schedule_success_msg }}"
      failed_when: false
      tags: TC_009 


# Test case to verify updation of new node in omnia inventory
- name: OMNIA_1.1_AWX_TC_010   
  hosts: localhost
  connection: local
  
  vars_files:
   - test_vars/test_inventory_vars.yml
   - ../input_params/base_vars.yml  
  tasks:                 

    - name: Execute get pods command
      command: "kubectl get pods -n {{ awx_namespace }}"
      changed_when: true
      register: k8s_pods
      run_once: true
      ignore_errors: true
      tags: TC_010
          
    - name: Get awx pod 
      set_fact:
       awx_pods: "{{ item | regex_search(awx_pod_regex) | trim  }}"
      with_items: 
        - "{{ k8s_pods.stdout_lines }}"
      run_once: true
      when: item | regex_search(awx_pod_item_regex)
      failed_when: false
      tags: TC_010

    - name: Get AWX admin password
      shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
      register: awx_admin_password
      changed_when: false
      ignore_errors: true
      tags: TC_010
          
    - name: Execute awx get inventory hosts command
      command: "awx --conf.host {{ awx_host }} --conf.username admin --conf.password {{ awx_admin_password.stdout }} --conf.insecure hosts list --inventory {{ node_inventory_name }} -f human --filter 'name'"
      changed_when: true
      register: node_hosts
      run_once: true
      failed_when: false
      tags: TC_010
         
    - name: Get node_inventory hosts
      command: ping -c1 {{ item }}
      delegate_to: localhost
      register: ping_result
      ignore_errors: yes
      changed_when: false
      with_items:
      - "{{ node_hosts.stdout_lines[2:] }}"
      when: node_hosts.stdout_lines | length > 2
      tags: TC_010
            
    - name: Verify updation of new node
      assert:
       that: 
         - "'100% packet loss' not in item.stdout"
       fail_msg: "{{ node_fail_msg }}"
       success_msg: "{{ node_success_msg }}"
      with_items:
       - "{{ ping_result.results }}"
      when: node_hosts.stdout_lines | length > 2
      failed_when: false
      tags: TC_010

    - name: Empty node hosts
      debug:
       msg: "{{ empty_host_err }}"
      when: node_hosts.stdout_lines | length < 3
      tags: TC_010
          
# Test case to verify AWX configuration is done properly with all items in place      
- name: OMNIA_1.1_AWX_TC_011   
  hosts: localhost
  connection: local
  vars_files:
   - test_vars/test_inventory_vars.yml
   - ../roles/webui_awx/vars/main.yml
    
  tasks:                 

    - name: Get awx-service Cluster-IP
      command: "kubectl get svc awx-service -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
      register: awx_cluster_ip
      changed_when: false
      ignore_errors: true
      run_once: true
      tags: TC_011
    
    - name: Get AWX admin password
      shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
      register: awx_admin_password
      changed_when: false
      ignore_errors: true
      run_once: true
      tags: TC_011
           
    - name: Set IP and password
      set_fact:
        awx_ip: 'http://{{ awx_cluster_ip.stdout }}'
        admin_password: "{{ awx_admin_password.stdout }}"
      run_once: true
      tags: TC_011

    - name: Get organization details
      awx.awx.tower_organization:
        name: "{{ organization_name }}"
        description: "{{ org_description }}"
        state: present
      register: organization
      ignore_errors: true
      run_once: true
      tags: TC_011
      
    - name: Validate an organization
      assert:
       that: 
         - organization.changed == false
       fail_msg: "{{ organization_fail_msg }}"
       success_msg: "{{ organization_success_msg }}"
      failed_when: false
      tags: TC_011
             
    - name: Get tower inventory details
      awx.awx.tower_inventory:
        name: "{{ item.name }}"
        description: "{{ item.description }}"
        organization: "{{ organization_name }}"
        state: present
      loop: "{{ inventory_names }}"  
      register: inventory 
      when: item.flag
      ignore_errors: true
      run_once: true
      tags: TC_011 

    - name: Validate inventory status
      assert:
       that: 
         - item.changed == false
       fail_msg: "{{ inventory_fail_msg }}"
       success_msg: "{{ inventory_success_msg }}"
      with_items:
       - "{{ inventory.results }}"
      failed_when: false
      tags: TC_011   
      
    - name: Get job template details
      awx.awx.tower_job_template:
        name: "{{ item.name }}"
        job_type: "run"
        organization: "{{ organization_name }}"
        inventory: "{{ item.inventory }}"
        project: "{{ project_name }}"
        playbook: "{{ item.playbook }}"
        credentials:
          - "{{ item.credential }}"
        state: present
      loop: "{{ job_template_details }}"
      register: job_template
      when: item.flag
      ignore_errors: true
      run_once: true
      tags: TC_011  

    - name: Validate job template 
      assert:
       that: 
         - item.changed == false
       fail_msg: "{{ item.item.name }} {{ job_template_fail_msg }}"
       success_msg: "{{ item.item.name }} {{ job_template_success_msg }}"
      with_items:
        - "{{ job_template.results }}"
      failed_when: false
      when: item.item.flag
      tags: TC_011 
      
    - name: Get project details
      awx.awx.tower_project:
        name: "{{ project_name }}"
        description: "{{ project_description }}"
        organization: "{{ organization_name }}"
        state: present
      register: project
      ignore_errors: true
      run_once: true
      tags: TC_011 
            
    - name: Verify project 
      assert:
       that: 
         - project.changed == false
       fail_msg: "{{ project_fail_msg }}"
       success_msg: "{{ project_success_msg }}"
      failed_when: false
      tags: TC_011 
      
    - name: Build a schedule for job template
      awx.awx.tower_schedule:
        name: "{{ item.name }}"
        unified_job_template: "{{ item.template }}"
        rrule: "{{ schedule_rule }}"
        state: present
      register: schedule
      loop: "{{ scheduled_templates }}"
      failed_when: false
      run_once: true
      tags: TC_011
      
    - name: Validate schedule status
      assert:
       that: 
         - schedule.changed == false
       fail_msg: "{{ schedule_fail_msg }}"
       success_msg: "{{ schedule_success_msg }}"
      failed_when: false
      tags: TC_011