#  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_DIO_US_CDIP_TC_001
# Execute provison role in management station with os installed centos 7
- name: OMNIA_DIO_US_CDIP_TC_001
  hosts: localhost
  connection: local
  vars_files:
    - test_vars/test_provision_vars.yml
    - ../roles/provision/vars/main.yml
  tasks:
    - name: Delete the cobbler container if exits
      docker_container:
        name: "{{ docker_container_name }}"
        state: absent
      tags: TC_001

    - name: Delete docker image if exists
      docker_image:
        name: "{{ docker_image_name }}"
        tag: "{{ docker_image_tag }}"
        state: absent
      tags: TC_001

    - block:
        - name: Call common role
          include_role:
            name: ../roles/common
          vars:
            input_config_filename: "{{ test_input_config_filename }}"

        - name: Call provision role
          include_role:
            name: ../roles/provision
            tasks_from: "{{ item }}"
          with_items:
           - "{{ cobbler_image_files }}"
      tags: TC_001

    - name: Inspect cobbler docker image
      docker_image_info:
        name: "{{ docker_image_name }}"
      register: cobbler_image_status
      tags: TC_001,VERIFY_001

    - name: Validate cobbler docker image
      assert:
        that:
          - cobbler_image_status.images
        fail_msg: "{{ cobbler_img_fail_msg }}"
        success_msg: "{{ cobbler_img_success_msg }}"
      tags: TC_001,VERIFY_001

    - name: Inspect cobbler container
      docker_container_info:
        name: "{{ docker_container_name }}"
      register: cobbler_cnt_status
      tags: TC_001,VERIFY_001

    - name: Validate cobbler docker container
      assert:
        that:
          - cobbler_cnt_status.exists
        fail_msg: "{{ cobbler_cnt_fail_msg }}"
        success_msg: "{{ cobbler_cnt_success_msg }}"
      tags: TC_001,VERIFY_001

# Testcase OMNIA_DIO_US_CDIP_TC_002
# Execute provison role in management station where cobbler container and image already created
- name: OMNIA_DIO_US_CDIP_TC_002
  hosts: localhost
  connection: local
  vars_files:
    - test_vars/test_provision_vars.yml
    - ../roles/provision/vars/main.yml
  tasks:
    - block:
        - name: Call common role
          include_role:
            name: ../roles/common
          vars:
            input_config_filename: "{{ test_input_config_filename }}"

        - name: Call provision role
          include_role:
            name: ../roles/provision
      tags: TC_002

    - name: Inspect cobbler docker image
      docker_image_info:
        name: "{{ docker_image_name }}"
      register: cobbler_image_status
      tags: TC_002,VERIFY_002

    - name: Validate cobbler docker image
      assert:
        that:
          - cobbler_image_status.images
        fail_msg: "{{ cobbler_img_fail_msg }}"
        success_msg: "{{ cobbler_img_success_msg }}"
      tags: TC_002,VERIFY_002

    - name: Inspect cobbler container
      docker_container_info:
        name: "{{ docker_container_name }}"
      register: cobbler_cnt_status
      tags: TC_002,VERIFY_002

    - name: Validate cobbler docker container
      assert:
        that:
          - cobbler_cnt_status.exists
        fail_msg: "{{ cobbler_cnt_fail_msg }}"
        success_msg: "{{ cobbler_cnt_success_msg }}"
      tags: TC_002,VERIFY_002

# Testcase OMNIA_DIO_US_CDIP_TC_003
# Execute provison role in management station where docker service not running
- name: OMNIA_DIO_US_CDIP_TC_003
  hosts: localhost
  connection: local
  vars_files:
    - test_vars/test_provision_vars.yml
    - ../roles/provision/vars/main.yml
  tasks:
    - name: Delete the cobbler container if exits
      docker_container:
        name: "{{ docker_container_name }}"
        state: absent
      tags: TC_003

    - name: Delete docker image if exists
      docker_image:
        name: "{{ docker_image_name }}"
        tag: "{{ docker_image_tag }}"
        state: absent
      tags: TC_003

    - name: Stop docker service
      service:
        name: docker
        state: stopped
      tags: TC_003

    - block:
        - name: Call common role
          include_role:
            name: ../roles/common
          vars:
            input_config_filename: "{{ test_input_config_filename }}"

        - name: Call provision role
          include_role:
            name: ../roles/provision

        - name: Docker service stopped usecase success message
          debug:
            msg: "{{ docker_check_success_msg }}"

      rescue:
        - name: Docker service stopped usecase fail message
          fail:
            msg: "{{ docker_check_fail_msg }}"

      always:
        - name: Start docker service
          service:
            name: docker
            state: started
      tags: TC_003
