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

# OMNIA_DIO_US_NDOD_TC_009
# Execute provison role in management station and  PXE boot one compute node
- name: OMNIA_DIO_US_NDOD_TC_009
  hosts: localhost
  connection: local
  gather_subset:
    - 'min'
  vars_files:
    - test_vars/test_provision_vars.yml
    - ../roles/common/vars/main.yml
  tasks:
    - name: Set ip address of the compute node
      set_fact:
        single_node_ip_address: "{{ groups[cobbler_groupname][0] }}"
      tags: TC_009,VERIFY_009

    - name: Delete inventory if exists
      file:
        path: inventory
        state: absent
      tags: TC_009,VERIFY_009

    - name: Check input config file is encrypted
      command: cat {{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_009,VERIFY_009

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt {{ test_input_config_filename }} --vault-password-file {{ vault_path }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_009,VERIFY_009

    - name: Include variable file input_config.yml
      include_vars: "{{ test_input_config_filename }}"
      tags: TC_009,VERIFY_009

    - name: Create inventory file
      lineinfile:
        path: inventory
        line: "{{ single_node_ip_address }} ansible_user=root ansible_password={{ provision_password }} ansible_ssh_common_args='-o StrictHostKeyChecking=no'"
        create: yes
        mode: '{{ file_permission }}'
      tags: TC_009,VERIFY_009

    - meta: refresh_inventory
      tags: TC_009,VERIFY_009

    - name: Validate authentication of username and password
      command: ansible {{ single_node_ip_address }} -m ping -i inventory
      register: validate_login
      changed_when: false
      ignore_errors: yes
      tags: TC_009,VERIFY_009

    - name: Validate the authentication output
      assert:
        that:
          - "'pong' in validate_login.stdout"
          - "'SUCCESS' in validate_login.stdout"
          - "'UNREACHABLE' not in validate_login.stdout"
        fail_msg: "{{ authentication_fail_msg }}"
        success_msg: "{{ authentication_success_msg }}"
      tags: TC_009,VERIFY_009

    - name: Check hostname
      command: ansible {{ single_node_ip_address }} -m shell -a hostname -i inventory
      register: validate_hostname
      changed_when: false
      ignore_errors: yes
      tags: TC_009,VERIFY_009

    - name: Validate the hostname
      assert:
        that: "'localhost' not in validate_hostname.stdout"
        fail_msg: "{{ hostname_fail_msg }}"
        success_msg: "{{ hostname_success_msg }}"
      tags: TC_009,VERIFY_009

    - name: Delete inventory if exists
      file:
        path: inventory
        state: absent
      tags: TC_009,VERIFY_009

# OMNIA_DIO_US_NDOD_TC_010
# Execute provison role in management station and PXE boot two compute node
- name: OMNIA_DIO_US_NDOD_TC_010
  hosts: localhost
  connection: local
  gather_subset:
    - 'min'
  vars_files:
    - test_vars/test_provision_vars.yml
    - ../roles/provision/vars/main.yml
  tasks:
    - name: Delete inventory if exists
      file:
        path: inventory
        state: absent
      tags: TC_010,VERIFY_010

    - name: Check input config file is encrypted
      command: cat {{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_010,VERIFY_010

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt {{ test_input_config_filename }} --vault-password-file {{ vault_path }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_010,VERIFY_010

    - name: Include variable file input_config.yml
      include_vars: "{{ test_input_config_filename }}"
      tags: TC_010,VERIFY_010

    - name: Create inventory file
      lineinfile:
        path: inventory
        line: "[nodes]"
        create: yes
        mode: '{{ file_permission }}'
      tags: TC_010,VERIFY_010

    - name: Edit inventory file
      lineinfile:
        path: inventory
        line: "{{ item }} ansible_user=root ansible_password={{ provision_password }} ansible_ssh_common_args='-o StrictHostKeyChecking=no'"
      with_items:
        - "{{ groups[cobbler_groupname] }}"
      tags: TC_010,VERIFY_010

    - meta: refresh_inventory
      tags: TC_010,VERIFY_010

    - name: Validate ip address is different for both servers
      assert:
        that: groups[cobbler_groupname][0] != groups[cobbler_groupname][1]
        fail_msg: "{{ ip_address_fail_msg }}"
        success_msg: "{{ ip_address_success_msg }}"
      delegate_to: localhost
      run_once: yes
      tags: TC_010,VERIFY_010

    - name: Check hostname of both servers
      command: ansible nodes -m shell -a hostname -i inventory
      register: node_hostname
      changed_when: false
      ignore_errors: yes
      tags: TC_010,VERIFY_010

    - name: Validate hostname is different for both servers
      assert:
        that:
          - node_hostname.stdout_lines[1] != node_hostname.stdout_lines[3]
          - "'localhost' not in node_hostname.stdout_lines[1]"
          - "'localhost' not in node_hostname.stdout_lines[3]"
        fail_msg: "{{ hostname_fail_msg }}"
        success_msg: "{{ hostname_success_msg }}"
      delegate_to: localhost
      run_once: yes
      tags: TC_010,VERIFY_010

    - name: Delete inventory if exists
      file:
        path: inventory
        state: absent
      delegate_to: localhost
      run_once: yes
      tags: TC_010,VERIFY_010

# OMNIA_DIO_US_NDOD_TC_011
# Validate passwordless ssh connection established or not with compute nodes
- name: OMNIA_DIO_US_NDOD_TC_011
  hosts: localhost
  gather_subset:
    - 'min'
  vars_files:
    - test_vars/test_provision_vars.yml
    - ../roles/provision/vars/main.yml
  tasks:
    - name: Validate authentication of username and password
      command: "ansible {{ cobbler_groupname }} -m ping -i {{ inventory_file }}"
      register: validate_login
      changed_when: false
      ignore_errors: yes
      tags: TC_011,VERIFY_011

    - name: Validate the passwordless SSH connection
      assert:
        that:
          - "'pong' in validate_login.stdout"
          - "'SUCCESS' in validate_login.stdout"
          - "'UNREACHABLE' not in validate_login.stdout"
        success_msg: "{{ authentication_success_msg }}"
        fail_msg: "{{ authentication_fail_msg }}"
      tags: TC_011,VERIFY_011

# OMNIA_DIO_US_NDOD_TC_012
# Execute provison role in management station and reboot compute node after os provision again
- name: OMNIA_DIO_US_NDOD_TC_012
  hosts: localhost
  connection: local
  gather_subset:
    - 'min'
  vars_files:
    - test_vars/test_provision_vars.yml
  tasks:
    - name: Set ip address of the compute node
      set_fact:
        single_node_ip_address: "{{ groups[cobbler_groupname][0] }}"
      tags: TC_012,VERIFY_012

    - name: Delete inventory if exists
      file:
        path: inventory
        state: absent
      tags: TC_012,VERIFY_012

    - name: Check input config file is encrypted
      command: cat {{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_012,VERIFY_012

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt {{ test_input_config_filename }} --vault-password-file {{ vault_path }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_012,VERIFY_012

    - name: Include variable file input_config.yml
      include_vars: "{{ test_input_config_filename }}"
      tags: TC_012,VERIFY_012

    - name: Create inventory file
      lineinfile:
        path: inventory
        line: "[nodes]"
        create: yes
        mode: '{{ file_permission }}'
      tags: TC_012,VERIFY_012

    - name: Edit inventory file
      lineinfile:
        path: inventory
        line: "{{ single_node_ip_address }} ansible_user=root ansible_password={{ provision_password }} ansible_ssh_common_args='-o StrictHostKeyChecking=no'"
      tags: TC_012,VERIFY_012

    - meta: refresh_inventory
      tags: TC_012,VERIFY_012

    - name: Reboot servers
      command: ansible nodes -m command -a reboot -i inventory
      ignore_errors: yes
      changed_when: true
      tags: TC_012,VERIFY_012

    - name: Wait for 10 minutes
      pause:
        minutes: 10
      tags: TC_012,VERIFY_012

    - name: Check ip address of servers
      command: ansible nodes -m command -a 'ip a' -i inventory
      ignore_errors: yes
      changed_when: false
      register: ip_address_after_reboot
      tags: TC_012,VERIFY_012

    - name: Validate ip address is same after reboot
      assert:
        that: "'{{ single_node_ip_address }}' in ip_address_after_reboot.stdout"
        fail_msg: "{{ ip_address_fail_msg }}"
        success_msg: "{{ ip_address_success_msg }}"
      tags: TC_012,VERIFY_012