#  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_002
# Execute ib_facts.yml with valid IP and valid credentials
- name: OMNIA_1.1_IB_TC_002
  hosts: infiniband
  gather_facts: false
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml
    - ../input_params/base_vars.yml
  tasks: 
     - name: Execute ib_facts
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ fact_template_value }}"
        job_template_name: "{{ fact_job_name }}"
        playbook_path: "{{ ib_facts_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"
       
     - block:
        - name: Validate default flow with valid IP and valid credentials
          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_003
# Execute ib_facts.yml with Invalid IP in inventory group
- name: OMNIA_1.1_IB_TC_003
  hosts: infiniband
  gather_facts: false
  tags: TC_003
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml
    - ../input_params/base_vars.yml
  tasks:
     - name: setting ip
       set_fact:
         ib_host_name: "{{ random_ip }}"
    
     - name: Execute ib_facts
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ fact_template_value }}"
        job_template_name: "{{ fact_job_name }}"
        playbook_path: "{{ ib_facts_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"

     - block:
        - name: Validate invalid IP and valid credentials
          assert:
            that:
              - ib_fail_msg in job_status.status
            success_msg: "{{ success_message }}"
            fail_msg: "{{ fail_case }}"
          changed_when: false
          failed_when: false
      
# Testcase OMNIA_1.1_IB_TC_001
# Execute ib_facts.yml with no hosts in inventory
- name: OMNIA_1.1_IB_TC_001
  hosts: infiniband
  gather_facts: false
  tags: TC_001
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml
    - ../input_params/base_vars.yml
  tasks:
     - name: Execute ib_facts with no host details
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        template_name: "{{ fact_template_value }}"
        job_template_name: "{{ fact_job_name }}"
        playbook_path: "{{ ib_facts_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"
  
     - block:
        - name: Validate no hosts and valid credentials
          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_004
# Execute ib_facts.yml with valid IP and incorrect credentials
- name: OMNIA_1.1_IB_TC_004
  hosts: infiniband
  gather_facts: false
  tags: TC_004
  connection: local
  vars_files:
    - test_vars/test_infiniband_vars.yml
    - ../roles/webui_awx/vars/main.yml
    - ../input_params/base_vars.yml
  tasks:
     - name: Making infiniband_credentials invalid
       tower_credential:
         name: "infiniband_credential"
         credential_type: "Network"
         inputs:
           username: "{{ invalid_username }}"
         
     - name: Execute ib_facts
       vars:
        inventory_name: "{{ ib_inventory_name }}"
        host_name: "{{ ib_host_name }}"
        template_name: "{{ fact_template_value }}"
        job_template_name: "{{ fact_job_name }}"
        playbook_path: "{{ ib_facts_playbook_path }}"
        delete_status: true
       include_tasks: "{{ awx_script_path }}"

     - block:
        - name: Validate valid IP and invalid credentials
          assert:
            that:
              - ib_fail_msg in job_status.status
            success_msg: "{{ success_message }}"
            fail_msg: "{{ fail_case }}"
          changed_when: false
  
     - name: Set credentials back to default
       tower_credential:
         name: "infiniband_credential"
         credential_type: "Network"
         inputs:
           username: "{{ username }}"
           password: "{{ password }}"