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

# Testase OMNIA_1.1_MS_TC_001
# Test Case to validate the execution of control_plane.yml with valid inputs -- Default Test Case
- name: OMNIA_1.1_MS_TC_001
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: VERIFY_OMNIA_01
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
             
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
     
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
          tags: Execute_control_plane
       
    - block:
        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
          tags: Execute_Validation_Script

# Testcase OMNIA_1.1_MS_TC_002
# Test Case to validate the execution of control_place.yml with no input
- name: OMNIA_1.1_MS_TC_002
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_002

  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
        
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc02 }}"
      tags: Replace_input
     
    - block:    
        - name: Execute control_plane_common role
          include_role:
            name: ../roles/control_plane_common
          vars:
            base_vars_filename: ../input_params/base_vars.yml
      rescue:
        - name: Validate error
          assert:
            that: input_base_failure_msg in ansible_failed_result.msg
            success_msg: "{{ input_config_check_success_msg }}"
            fail_msg: "{{ input_config_check_fail_msg }}"
      tags: Execute_common_role
    
# Testcase OMNIA_1.1_MS_TC_003 and OMNIA_1.1_MS_TC_004
# Test Case to validate the execution of control_plane.yml with NFS share already present
- name: OMNIA_1.1_MS_TC_003
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_003,TC_004
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
        
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
    
    - name: Creating new control_plane.yml
      copy:
        dest: "../test_control_plane.yml"
        content: |
         - name: Executing omnia roles
           hosts: localhost
           connection: local
           roles:
              - control_plane_common
              - control_plane_repo
        mode: '0644'
      tags: Replace_control_plane
      
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook test_control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
          tags: Execute_control_plane
    
    - block:
        - name: Execute validation script
          include_tasks: "{{ control_plane_validation_script_path }}" 
          tags: Execute_Validation_Script
      
    - name: Delete newly created control_plane.yml
      file:
        state: absent
        path: ../test_control_plane.yml
      when: foo_stat.stat.exists
      tags: Delete_test_control_plane

# Testcase OMNIA_1.1_MS_TC_005
# Test Case to validate the execution of control_plane.yml after a successful run and validate k8s pods along with services after reboot.
- name: OMNIA_1.1_MS_TC_005
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_005
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
      
    - name: Check uptime
      command: uptime -p
      register: system_uptime
      changed_when: false
      tags: Check_Uptime
      
    - name: Extracting data from system_uptime
      set_fact:
        uptime_number: "{{ system_uptime.stdout.split()[1] }}"
        uptime_min: "{{ system_uptime.stdout.split()[2] }}"
    
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      when: uptime_number|int > 15
      tags: Replace_input
      
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
          when: uptime_number|int > 15
          tags: Execute_control_plane
          
        - name: Reboot system
          command: reboot
          when: uptime_number|int > 15
          tags: Reboot_System
    
    - block:
        - name: Wait for 30sec for kubectl to get things ready
          pause:
            seconds: 200
          when: (uptime_number| int <= 15) and (uptime_min == "minutes")

        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
          when: (uptime_number| int <= 15) and (uptime_min == "minutes")
          tags: Execute_Validation_Script

# Testcase OMNIA_1.1_MS_TC_006 and OMNIA_1.1_MS_TC_007
# Test Case to validate the execution of control_plane.yml and after a successful run the user deletes/stops all pods
- name: OMNIA_1.1_MS_TC_006
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_006,TC_007
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
        
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: "0644"
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
     
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
      tags: Execute_control_plane
      
    - name: Delete all containers
      command: kubectl delete --all namespaces
      changed_when: false
      tags: Delete_Pods
      
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
      tags: Execute_control_plane
      
    - block:
        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
      tags: Execute_Validation_Script

# Testcase OMNIA_1.1_MS_TC_008
# Test Case to validate the execution of control_plane.yml with infiniband=false, powervault=true and ethernet=true
- name: OMNIA_1.1_MS_TC_008
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_008
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
        
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
     
    - name: Editing base_vars.yml for powervault to true
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ powervault_false }}"
        replace: "{{ powervault_true }}"
      tags: Edit_base_vars
    
    - name: Editing base_vars.yml for ethernet to true
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ ethernet_false }}"
        replace: "{{ ethernet_true }}"
      tags: Edit_base_vars
    
    - name: Editing base_vars.yml for infiniband to false
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ infiniband_true }}"
        replace: "{{ infiniband_false }}"
      tags: Edit_base_vars
        
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
      tags: Execute_control_plane
       
    - block:
        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
      tags: Execute_Validation_Script

# Testcase OMNIA_1.1_MS_TC_009
# Test Case to validate the execution of control_plane.yml with infiniband=true, powervault=false and ethernet=true
- name: OMNIA_1.1_MS_TC_009
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_009
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
        
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
     
    - name: Editing base_vars.yml for powervault to false
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ powervault_true }}"
        replace: "{{ powervault_false }}"
      tags: Edit_base_vars
    
    - name: Editing base_vars.yml for ethernet to true
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ ethernet_false }}"
        replace: "{{ ethernet_true }}"
      tags: Edit_base_vars
    
    - name: Editing base_vars.yml for infiniband to true
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ infiniband_false }}"
        replace: "{{ infiniband_true }}"
      tags: Edit_base_vars
        
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
      tags: Execute_control_plane
       
    - block:
        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
      tags: Execute_Validation_Script

# Testcase OMNIA_1.1_MS_TC_010
# Test Case to validate the execution of control_plane.yml with infiniband=true, powervault=true and ethernet=false
- name: OMNIA_1.1_MS_TC_010
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_010

  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
        
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
     
    - name: Editing base_vars.yml for powervault to true
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ powervault_false }}"
        replace: "{{ powervault_true }}"
      tags: Edit_base_vars
    
    - name: Editing base_vars.yml for ethernet to false
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ ethernet_true }}"
        replace: "{{ ethernet_false }}"
      tags: Edit_base_vars
    
    - name: Editing base_vars.yml for infiniband to true
      replace:
        path: ../input_params/base_vars.yml
        regexp: "{{ infiniband_false }}"
        replace: "{{ infiniband_true }}"
      tags: Edit_base_vars
        
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
      tags: Execute_control_plane
       
    - block:
        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
      tags: Execute_Validation_Script

# Testcase OMNIA_1.1_MS_TC_011
# Test Case to validate the execution of control_plane.yml with firmware update set to False
- name: OMNIA_1.1_MS_TC_011
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_011
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
      
    - name: Set firmware update to false
      replace:
        path: ../input_params/idrac_vars.yml
        regexp: "{{ fw_update_true }}"
        replace: "{{ fw_update_false }}"
      tags: Set_FW_Update
        
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
     
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
      tags: Execute_control_plane
       
    - block:
        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
      tags: Execute_Validation_Script
      
    - name: Check if firmware updates folder exists
      stat:
        path: /var/nfs_repo/dellupdates
      register: fw_update_dir
      tags: Set_FW_Update
      
    - name: Verify firmware updates were downloaded
      assert:
        that:
          - not fw_update_dir.stat.exists
        success_msg: "{{ fw_success_validation }}"
        fail_msg: "{{ fw_fail_validation }}"
      tags: Set_FW_Update
        
# Testcase OMNIA_1.1_MS_TC_012
# Test Case to validate the execution of control_plane.yml with firmware update set to true
- name: OMNIA_1.1_MS_TC_012
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml   
   
  gather_subset:
    - 'min'
  tags: TC_012
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
      
    - name: Set firmware update to true
      replace:
        path: ../input_params/idrac_vars.yml
        regexp: "{{ fw_update_false }}"
        replace: "{{ fw_update_true }}"
      tags: Set_FW_Update
        
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
     
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
      tags: Execute_control_plane
       
    - block:
        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
      tags: Execute_Validation_Script
      
    - name: Check if firmware updates folder exists
      stat:
        path: /var/nfs_repo/dellupdates
      register: fw_update_dir
      tags: Set_FW_Update
      
    - name: Verify firmware updates were downloaded
      assert:
        that:
          - fw_update_dir.stat.exists
        success_msg: "{{ fw_success_validation }}"
        fail_msg: "{{ fw_fail_validation }}"
      tags: Set_FW_Update

# Testcase OMNIA_1.1_MS_TC_013
# Test Case to validate the execution of control_plane.yml with docker login credential
- name: OMNIA_1.1_MS_TC_013
  hosts: localhost
  connection: local
  vars_files:
    - ../roles/control_plane_common/vars/main.yml  
    - test_vars/test_control_plane_vars.yml  
   
  gather_subset:
    - 'min'
  tags: TC_013
  
  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name }}"'
          - 'ansible_distribution_version == "{{ os_version }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS
    
    - name: Replace input parameters folder
      copy:
        src: "{{ item }}"
        dest: "{{ input_params_folder }}"
        force: yes
        mode: '0644'
      with_items:
        - "{{ input_files_tc01 }}"
      tags: Replace_input
      
    - name: Change docker params in omnia_config.yml
      replace:
        path: ../../omnia_config.yml
        regexp: "docker_username: .*$"
        replace: 'docker_username: "{{ docker_user }}"'
      tags: Set_Docker_Creds
    
    - name: Assert if the credentials are valid in test_control_plane_vars.yml
      assert:
        that:
          - 'docker_user != "User"'
          - 'docker_password != "Password"'
        success_msg: "{{ valid_docker_creds }}"
        fail_msg: "{{ invalid_docker_creds }}"
      tags: Set_Docker_Creds
    
    - name: Change docker params in omnia_config.yml
      replace:
        path: ../../omnia_config.yml
        regexp: "docker_password: .*$"
        replace: 'docker_password: "{{ docker_password }}"'
      tags: Set_Docker_Creds
    
    - block:    
        - name: Execute control_plane.yml playbook
          command: ansible-playbook control_plane.yml
          args:
            chdir: "{{ control_plane_dir }}"
          tags: Execute_control_plane
    
    - block:
        - name: Execute default validation script
          include_tasks: "{{ control_plane_validation_script_path }}"
          tags: Execute_Validation_Script
      
    - name: Fetch docker info
      shell: docker login & sleep 3
      register: new
      changed_when: false
      tags: Set_Docker_Creds

    - name: Assert that docker was used to pull images 
      assert:
        that:
          - "'Login did not succeed' in new.stderr"
        success_msg: "{{ docker_success_validation }}"
        fail_msg: "{{ docker_fail_validation }}"
      tags: Set_Docker_Creds