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

# Testcase OMNIA_UKP_US_VFKP_TC_001
# Execute k8s_common role in manager & compute nodes with os installed centos 7.8
- name: OMNIA_UKP_US_VFKP_TC_001
  hosts: manager, compute
  vars_files:
    - test_vars/test_k8s_common_vars.yml
  tasks:
    - block:
        - name: Call common role
          include_role:
            name: ../roles/k8s_common
      tags: TC_001

    - name: Checking common packages installation status
      command: "'{{ item }}' --version"
      with_items:
        - "{{ common_packages }}"
      register: common_packages_status
      changed_when: false
      ignore_errors: True
      tags: TC_001, VERIFY_001

    - name: Checking K8s packages installation status
      command: "'{{ item }}' version"
      with_items:
        - "{{ k8_packages }}"
      register: k8s_packages_status
      changed_when: false
      ignore_errors: True
      tags: TC_001, VERIFY_001

    - name: Checking docker service status
      systemd:
        name: docker
      register: docker_service
      tags: TC_001, VERIFY_001

    - name: Checking K8s service status
      systemd:
        name: kubelet
      register: kubelet_service
      tags: TC_001, VERIFY_001

    - name: Validate common & K8s packages status
      assert:
        that:
          - "'command not found' not in {{ item }}"
        fail_msg: "{{ packages_status_fail_msg }}"
        success_msg: "{{ packages_status_success_msg }}"
      with_items:
        - "{{ common_packages_status.results }}"
        - "{{ k8s_packages_status.results }}"
      tags: TC_001, VERIFY_001

    - name: Validating docker service status
      assert:
        that:
          - docker_service.status.ActiveState == 'active'
        fail_msg: "{{ docker_service_fail_msg }}"
        success_msg: "{{ docker_service_success_msg }}"
      tags: TC_001, VERIFY_001

    - name: Validating K8s service status
      assert:
        that:
          - kubelet_service.status.ActiveState == 'active'
        fail_msg: "{{ kubelet_service_fail_msg }}"
        success_msg: "{{ kubelet_service_success_msg }}"
      tags: TC_001, VERIFY_001

# Testcase OMNIA_UKP_US_VFKP_TC_002
# Execute k8s_common role in manager & compute nodes with common and K8s packages already installed
- name: OMNIA_UKP_US_VFKP_TC_002
  hosts: manager, compute
  vars_files:
    - test_vars/test_k8s_common_vars.yml
  tasks:
    - block:
        - name: Call common role
          include_role:
            name: ../roles/k8s_common
      tags: TC_002, VERIFY_002

    - name: Checking common packages installation status
      command: "'{{ item }}' --version"
      with_items:
        - "{{ common_packages }}"
      register: common_packages_status
      changed_when: false
      ignore_errors: True
      tags: TC_002, VERIFY_002

    - name: Checking K8s packages installation status
      command: "'{{ item }}' version"
      with_items:
        - "{{ k8_packages }}"
      register: k8s_packages_status
      changed_when: false
      ignore_errors: True
      tags: TC_002, VERIFY_002

    - name: Checking docker service status
      systemd:
        name: docker
      register: docker_service
      tags: TC_002, VERIFY_002

    - name: Checking K8s service status
      systemd:
        name: kubelet
      register: kubelet_service
      tags: TC_002, VERIFY_002

    - name: Validate common & K8s packages status
      assert:
        that:
          - "'command not found' not in {{ item }}"
        fail_msg: "{{ packages_status_fail_msg }}"
        success_msg: "{{ packages_status_success_msg }}"
        quiet: true
      with_items:
        - "{{ common_packages_status.results }}"
        - "{{ k8s_packages_status.results }}"
      tags: TC_002, VERIFY_002

    - name: Validating docker service status
      assert:
        that:
          - docker_service.status.ActiveState == 'active'
        fail_msg: "{{ docker_service_fail_msg }}"
        success_msg: "{{ docker_service_success_msg }}"
      tags: TC_002, VERIFY_002

    - name: Validating K8s service status
      assert:
        that:
          - kubelet_service.status.ActiveState == 'active'
        fail_msg: "{{ kubelet_service_fail_msg }}"
        success_msg: "{{ kubelet_service_success_msg }}"
      tags: TC_002, VERIFY_002