#  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.
---

# Testcase OMNIA_1.1_IB_TC_007
# Execute infiniband.yml with both valid Global and valid interface configs in ib_config.yml
- name: OMNIA_1.1_IB_TC_007
  hosts: infiniband
  gather_facts: false
  tags: VERIFY_OMNIA_01
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
     - name: Executing network_ib role
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ template_value }}"
        job_template_name: "{{ job_name }}"
        playbook_path: "{{ ib_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"
     
     - block:
        - name: Validate default flow
          assert:
            that:
              - ib_success_msg in job_status.status
            success_msg: "{{ success_message }}"
            fail_msg: "{{ fail_case }}"
          changed_when: false
   
     - name: Backup of ib_vars.yml
       copy:
         src: "{{ ib_vars_dir }}"
         dest: "{{ ib_vars_backup_dir }}"
         mode: "{{ file_perm }}"

     - name: Set MTU of port {{ port_num }}
       lineinfile:
        dest: "{{ ib_vars_dir }}"
        insertbefore: "{{ search_line }}"
        line: "{{ line_to_add }}"

     - name: Execute network_ib role after setting MTU
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ template_value }}"
        job_template_name: "{{ job_name }}"
        playbook_path: "{{ ib_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"

     - name: Get MTU of port {{ port_num }}
       command: ansible-playbook -i "{{ inventory_dir }}" "{{ ib_mtu_path }}" --tags 'mtu'
       register: mtuvalue
       changed_when: false

     - block:
        - name: Validate MTU
          assert:
            that:
              - 'var_check in mtuvalue.stdout'
            success_msg: "{{ success_message }}"
            fail_msg: "{{ fail_case }}"
            
# Testcase OMNIA_1.1_IB_TC_005
# set save_changes_to_startup to false
- name: OMNIA_1.1_IB_TC_005
  hosts: infiniband
  gather_facts: false
  tags: TC_005
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:        
     - name: Reload IB switch 
       command: ansible-playbook -i "{{ inventory_dir }}" "{{ ib_mtu_path }}" --tags 'reboot'
       changed_when: false

     - name: Pausing for IB switch to come up
       pause:
         minutes: "{{ time_to_pause }}"
   
     - name: Get MTU of port {{ port_num }}
       command: ansible-playbook -i "{{ inventory_dir }}" "{{ ib_mtu_path }}" --tags 'mtu'
       register: mtuvalue
       changed_when: false
          
     - block:
        - name: Validate that MTU is changed
          assert:
            that:
              - 'var_check not in mtuvalue.stdout'
            success_msg: "{{ success_message }}"
            fail_msg: "{{ fail_case }}"
            
# Testcase OMNIA_1.1_IB_TC_006
# set save_changes_to_startup to True            
- name: OMNIA_1.1_IB_TC_006
  hosts: infiniband
  gather_facts: false
  tags: TC_006
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:        
     - name: Set save_changes_to_startup to True
       ansible.builtin.replace:
         dest: "{{ ib_vars_dir }}"
         regexp: 'save_changes_to_startup: false'
         replace: 'save_changes_to_startup: True'
                          
     - name: Execute network_ib role as port {{ port_num }} has mtu set in ib_Vars
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ template_value }}"
        job_template_name: "{{ job_name }}"
        playbook_path: "{{ ib_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"
       
     - name: Reload IB switch 
       command: ansible-playbook -i "{{ inventory_dir }}" "{{ ib_mtu_path }}" --tags 'reboot'
       changed_when: false
       
     - name: Pausing for IB switch to come up
       pause:
         minutes: "{{ time_to_pause }}"
  
     - name: Get MTU of port {{ port_num }}
       command: ansible-playbook -i "{{ inventory_dir }}" "{{ ib_mtu_path }}" --tags 'mtu'
       register: mtuvalue
       changed_when: false
       
     - block:
        - name: Validate that MTU is not changed
          assert:
            that:
              - 'var_check in mtuvalue.stdout'
            success_msg: "{{ success_message }}"
            fail_msg: "{{ fail_case }}"
       
# Testcase OMNIA_1.1_IB_TC_010
# Execute infiniband.yml with valid interface and incorrect Global configs in ib_config.yml
- name: OMNIA_1.1_IB_TC_010
  hosts: infiniband
  gather_facts: false
  tags: TC_010
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml    
  tasks:
     - name: Making gobal config incorrect
       lineinfile:
        dest: "{{ ib_vars_dir }}"
        insertafter: 'mellanox_switch_config:'
        line: "gibberish inserted"
        
     - name: Executing network_ib role
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ template_value }}"
        job_template_name: "{{ job_name }}"
        playbook_path: "{{ ib_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"

     - block:
        - name: Validate role exec output
          assert:
            that:
              - ib_fail_msg in job_status.status
            success_msg: "{{ success_message }}"
            fail_msg: "{{ fail_case }}"
      
# Testcase OMNIA_1.1_IB_TC_009
# Execute infiniband.yml with only interface and no Global configs in ib_config.yml
- name: OMNIA_1.1_IB_TC_009
  hosts: infiniband
  gather_facts: false
  tags: TC_009
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
     - name: Removing global config from ib_vars.yml
       lineinfile:
        dest: "{{ ib_vars_dir }}"
        state: absent
        regexp: "^gibberish inserted"
        
     - name: Executing network_ib role
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ template_value }}"
        job_template_name: "{{ job_name }}"
        playbook_path: "{{ ib_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"

     - name: Validate role exec output
       assert:
         that:
           - ib_success_msg in job_status.status
         success_msg: "{{ success_message }}"
         fail_msg: "{{ fail_case }}"
       changed_when: false
       
# Testcase OMNIA_1.1_IB_TC_011
# Execute infiniband.yml with valid Global and incorrect interface configs in ib_config.yml
- name: OMNIA_1.1_IB_TC_011
  hosts: infiniband
  gather_facts: false
  tags: TC_011
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
     - name: Make interface config incorrect
       lineinfile:
        dest: "{{ ib_vars_dir }}"
        insertafter: "{{ line_to_search }}"
        line: "gibberish inserted"
        
     - name: Executing network_ib role
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ template_value }}"
        job_template_name: "{{ job_name }}"
        playbook_path: "{{ ib_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"
 
     - name: Validate role exec output
       assert:
         that:
           - ib_fail_msg in job_status.status
         success_msg: "{{ success_message }}"
         fail_msg: "{{ fail_case }}"
      
# Testcase OMNIA_1.1_IB_TC_008
# Execute infiniband.yml with only Global and no interface configs in ib_config.yml
- name: OMNIA_1.1_IB_TC_008
  hosts: infiniband
  gather_facts: false
  tags: TC_008
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
     - name: Removing interface config
       ansible.builtin.command: sed -i '49,196d' "{{ ib_vars_dir }}"
       args:
        warn: no
       changed_when: false
     
     - name: Executing network_ib role
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ template_value }}"
        job_template_name: "{{ job_name }}"
        playbook_path: "{{ ib_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"

     - name: Validate role exec output
       assert:
         that:
           - ib_success_msg in job_status.status
         success_msg: "{{ success_message }}"
         fail_msg: "{{ fail_case }}"
       changed_when: false

     - name: Restore orginal ib_vars file
       copy:
         src: "{{ ib_vars_backup_dir }}"
         dest: "{{ ib_vars_dir }}"
         mode: "{{ file_perm }}"
         
     - name: Set save_changes_to_startup to True
       ansible.builtin.replace:
         dest: "{{ ib_vars_dir }}"
         regexp: 'save_changes_to_startup: false'
         replace: 'save_changes_to_startup: True'
                          
     - name: Execute network_ib role to set default IB config as is
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ template_value }}"
        job_template_name: "{{ job_name }}"
        playbook_path: "{{ ib_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"
       
     - name: Set save_changes_to_startup back to false
       ansible.builtin.replace:
         dest: "{{ ib_vars_dir }}"
         regexp: 'save_changes_to_startup: True'
         replace: 'save_changes_to_startup: false'