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

---
# Testacase OMNIA_1.1_EF_TC_007
# Execute ethernet.yml with both valid Global and interface configs in ethernet_config.yml
- name: OMNIA_1.1_EF_TC_007
  hosts: ethernet
  gather_facts: false
  tags: TC_007
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Back up of ethernet_config.yml
      copy:
        src: "{{ ethernet_config_dir }}"
        dest: "{{ ethernet_config_backup_dir }}"
        mode: "{{ file_perm }}"
      tags: TC_007

    - name: Executing ethernet role with default ethernet_config
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ eth_template_value }}"
       job_template_name: "{{ eth_job_name }}"
       playbook_path: "{{ eth_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"      

    - block:
       - name: Validate default flow
         assert:
           that:
             - ethernet_success_msg in job_status.status
           success_msg: "{{ success_message }}"
           fail_msg: "{{ fail_case }}"
         changed_when: false

    - name: Set MTU of port {{ port_num }}
      lineinfile:
       dest: "{{ ethernet_config_dir }}"
       insertbefore: "{{ search_line }}"
       line: "{{ add_mtu_line }}"
    
    - name: Executing ethernet role with default ethernet_config
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ eth_template_value }}"
       job_template_name: "{{ eth_job_name }}"
       playbook_path: "{{ eth_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"
      
    - name: Getting MTU of ethernet {{ validation_port }}
      command: ansible-playbook -i "{{ inventory_dir }}" "{{ get_mtu_dir }}" --tags 'mtu'
      changed_when: false
      register: mtu_out
      tags: TC_007,TC_002
       
    - name: Validate role exec output pre and post MTU addition
      assert:
        that:          
          - validate_mtu_line in mtu_out.stdout
        success_msg: "{{ success_message }}"
        fail_msg: "{{ fail_case }}"
      changed_when: false
      failed_when: false
      tags: TC_007

# Testacase OMNIA_1.1_EF_TC_005
# Execute ethernet.yml with save_config set to False
- name: OMNIA_1.1_EF_TC_005
  hosts: ethernet
  gather_facts: false
  tags: TC_005
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Reload switch
      command: ansible-playbook -i "{{ inventory_dir }}" "{{ get_mtu_dir }}" --tags 'reload'
      changed_when: false
    
    - name: Pausing for switch to come up
      pause:
        minutes: "{{ time_to_pause }}"
        
    - name: Getting MTU of ethernet {{ validation_port }}
      command: ansible-playbook -i "{{ inventory_dir }}" "{{ get_mtu_dir }}" --tags 'mtu'
      changed_when: false
      register: mtu_out
      
    - block:
       - name: Validate that MTU is changed
         assert:
           that:
             - validate_mtu_line not in mtu_out.stdout
           success_msg: "{{ success_message }}"
           fail_msg: "{{ fail_case }}"
         changed_when: false
         failed_when: false
                
# Testacase OMNIA_1.1_EF_TC_006
# Execute ethernet.yml with save_config set to True
- name: OMNIA_1.1_EF_TC_006
  hosts: ethernet
  gather_facts: false
  tags: TC_006
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Set save_changes_to_startup to True in ethernet_vars
      ansible.builtin.replace:
        dest: "{{ ethernet_config_dir }}"
        regexp: 'save_changes_to_startup: false'
        replace: 'save_changes_to_startup: True'
        
    - name: Execute network_ethernet role as port 4 has mtu set in ethernet_vars
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ eth_template_value }}"
       job_template_name: "{{ eth_job_name }}"
       playbook_path: "{{ eth_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"
      
    - name: Reload switch
      command: ansible-playbook -i "{{ inventory_dir }}" "{{ get_mtu_dir }}" --tags 'reload'
      changed_when: false
    
    - name: Pausing for switch to come up
      pause:
        minutes: "{{ time_to_pause }}"
        
    - name: Getting MTU of ethernet {{ validation_port }}
      command: ansible-playbook -i "{{ inventory_dir }}" "{{ get_mtu_dir }}" --tags 'mtu'
      changed_when: false
      register: mtu_out
    
    - block:
       - name: Validate that MTU is changed
         assert:
           that:
             - validate_mtu_line in mtu_out.stdout
           success_msg: "{{ success_message }}"
           fail_msg: "{{ fail_case }}"
         changed_when: false

# Testcase OMNIA_1.1_EF_TC_010
# Execute ethernet.yml with invalid Global and correct interface configs in ethernet_config.yml
- name: OMNIA_1.1_EF_TC_010
  hosts: ethernet
  gather_facts: false
  tags: TC_010
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Making interface config invalid
      lineinfile:
        path: "{{ ethernet_config_dir }}"
        insertafter: 'os10_config:'
        line: 'gibberish inserted'
      tags: TC_007

    - name: Executing ethernet role with invalid global config
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ eth_template_value }}"
       job_template_name: "{{ eth_job_name }}"
       playbook_path: "{{ eth_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"

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

# Testcase OMNIA_1.1_EF_TC_009
# Validation of ethernet default configuration
- name: OMNIA_1.1_EF_TC_009
  hosts: ethernet
  gather_facts: false
  tags: VERIFY_OMNIA_01
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml    
  tasks:
    - name: Executing ethernet role
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ eth_template_value }}"
       job_template_name: "{{ eth_job_name }}"
       playbook_path: "{{ eth_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}" 
    
    - block:
       - name: Validate default flow
         assert:
           that:
             - ethernet_success_msg in job_status.status
           success_msg: "{{ success_message }}"
           fail_msg: "{{ fail_case }}"
         changed_when: false
         
# Testcase OMNIA_1.1_EF_TC_011
# Execute ethernet.yml with valid Global  and incorrect interface configs in ethernet_config.yml 
- name: OMNIA_1.1_EF_TC_011
  hosts: ethernet
  gather_facts: false
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Making interface config invalid
      lineinfile:
        path: "{{ ethernet_config_dir }}"
        insertafter: 'os10_interface:'
        line: 'gibberish inserted'
        
    - name: Executing ethernet role with invalid interface config
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ eth_template_value }}"
       job_template_name: "{{ eth_job_name }}"
       playbook_path: "{{ eth_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"

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


# Testcase OMNIA_1.1_EF_TC_008
# Execute ethernet.yml with only Global and no interface configs in ethernet_config.yml 
- name: OMNIA_1.1_EF_TC_008
  hosts: ethernet
  gather_facts: false
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Retrieving ethernet_config backup
      copy:
        src: "{{ ethernet_config_backup_dir }}"
        dest: "{{ ethernet_config_dir }}"
        mode: "{{ file_perm }}"
      tags: TC_008
    
    - name: Removing interface config from ethernet_config
      ansible.builtin.command: sed -i '22,117d' "{{ ethernet_config_dir }}"
      args:
       warn: no
      changed_when: false
      tags: TC_008
      
    - name: Executing ethernet role with no interface config
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ eth_template_value }}"
       job_template_name: "{{ eth_job_name }}"
       playbook_path: "{{ eth_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"
      
    - block:
       - name: Validate default flow
         assert:
           that:
             - ethernet_success_msg in job_status.status
           success_msg: "{{ success_message }}"
           fail_msg: "{{ fail_case }}"
         changed_when: false

    - name: Restoring original ethernt_config
      copy:
        src: "{{ ethernet_config_backup_dir }}"
        dest: "{{ ethernet_config_dir }}"
        mode: "{{ file_perm }}"
      tags: TC_008
      
    - name: Set save_changes_to_startup to True in ethernet_vars
      ansible.builtin.replace:
        dest: "{{ ethernet_config_dir }}"
        regexp: 'save_changes_to_startup: false'
        replace: 'save_changes_to_startup: True'
        
    - name: Execute network_ethernet role as port 4 has mtu set in ethernet_vars
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ eth_template_value }}"
       job_template_name: "{{ eth_job_name }}"
       playbook_path: "{{ eth_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"
      
    - name: Set save_changes_to_startup to False in ethernet_vars
      ansible.builtin.replace:
        dest: "{{ ethernet_config_dir }}"
        regexp: 'save_changes_to_startup: True'
        replace: 'save_changes_to_startup: False'