# 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_CRM_US_AWXD_TC_001
# Test case to verify the prerequisites are installed and execute the AWX deployment
- name: OMNIA_CRM_US_AWXD_TC_001
  hosts: localhost
  connection: local
  gather_subset:
    - 'min'
  vars_files:
    - ../roles/web_ui/vars/main.yml
    - test_vars/test_web_ui_vars.yml
  tasks:
    - name: Get the docker package facts
      package_facts:
        manager: auto
      tags: TC_001

    - name: Check if docker-ce is already installed
      debug:
        var: ansible_facts.packages['{{ package }}']
      tags: TC_001

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

        - name: Calling the web_ui role to be tested
          include_role:
            name: ../roles/web_ui
      tags: TC_001

    - name: Check that you can connect to github repo and it returns a status 200
      uri:
        url: "{{ awx_git_repo }}"
        status_code: "{{ return_status }}"
        return_content: true
      tags: TC_001

    - name: Check that you can can connect to AWX UI and it returns a status 200
      uri:
        url: "{{ awx_ip }}"
        status_code: "{{ return_status }}"
        return_content: true
      tags: TC_001

    - name: verify awx-server is listening on 8081
      wait_for:
        port: "{{ awx_listening_port }}"
        timeout: "{{ time }}"
      tags: TC_001

    - name: Get the containers count
      shell: |
        set -o pipefail
        docker ps -a | grep awx | wc -l
      register: containers_count
      changed_when: False
      tags: TC_001

    - name: Validate the containers count
      assert:
        that: containers_count.stdout | int >= actual_containers
        success_msg: "{{ awx_exists_msg }}"
        fail_msg: "{{ awx_not_exists_msg }}"
      tags: TC_001

# Testcase OMNIA_CRM_US_AWXD_TC_002
# Test case to verify regression testing
- name: OMNIA_CRM_US_AWXD_TC_002
  hosts: localhost
  connection: local
  gather_subset:
    - 'min'
  vars_files:
    - ../roles/web_ui/vars/main.yml
    - test_vars/test_web_ui_vars.yml
  tasks:
    - block:
        - name: Call common role
          include_role:
            name: ../roles/common
          vars:
            input_config_filename: "{{ test_input_config_filename }}"

        - name: Calling the web_ui role to be tested
          include_role:
            name: ../roles/web_ui
      tags: TC_002

    - name: Check that you can connect to github repo and it returns a status 200
      uri:
        url: "{{ awx_git_repo }}"
        status_code: "{{ return_status }}"
        return_content: true
      tags: TC_002

    - name: Check that you can can connect to AWX UI and it returns a status 200
      uri:
        url: "{{ awx_ip }}"
        status_code: "{{ return_status }}"
        return_content: true
      tags: TC_002

    - name: verify awx-server is listening on 80
      wait_for:
        port: "{{ awx_listening_port }}"
        timeout: "{{ time }}"
      tags: TC_002

    - name: Get the containers count
      shell: |
        set -o pipefail
        docker ps -a | grep awx | wc -l
      register: containers_count
      changed_when: False
      tags: TC_002

    - name: Validate the containers count
      assert:
        that: containers_count.stdout | int >= actual_containers
        success_msg: "{{ awx_exists_msg }}"
        fail_msg: "{{ awx_not_exists_msg }}"
      tags: TC_002

# Testcase OMNIA_CRM_US_AWXD_TC_003
# Test case to validate the AWX configuration
- name: OMNIA_CRM_US_AWXD_TC_003
  hosts: localhost
  connection: local
  gather_subset:
    - 'min'
  vars_files:
    - ../roles/web_ui/vars/main.yml
    - ../roles/common/vars/main.yml
    - test_vars/test_web_ui_vars.yml
  tasks:

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

        - name: Calling the web_ui role to be tested
          include_role:
            name: ../roles/web_ui
      tags: TC_003

    - name: Get the package facts
      package_facts:
        manager: auto
      tags: TC_003

    - name: Check if ansible-tower-cli is already installed
      assert:
        that: "'{{ tower_cli_package_name }}' in ansible_facts.packages"
        success_msg: "{{ resource_exists_success_msg }}"
        fail_msg: "{{ resource_exists_fail_msg }}"
      tags: TC_003

    - name: Get the existing organizations
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
        organizations list -f human
      register: organizations_array
      changed_when: False
      tags: TC_003

    - name: Check for organization
      assert:
        that: organization_name in organizations_array.stdout
        success_msg: "{{ resource_exists_success_msg }}"
        fail_msg: "{{ resource_exists_fail_msg }}"
      tags: TC_003

    - name: Get the existing projects
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
        projects list -f human
      changed_when: False
      register: projects_array
      tags: TC_003

    - name: Check for project
      assert:
        that: project_name in projects_array.stdout
        success_msg: "{{ resource_exists_success_msg }}"
        fail_msg: "{{ resource_exists_fail_msg }}"
      tags: TC_003

    - name: Get the existing inventories
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
        inventory list -f human
      changed_when: False
      register: inventory_array
      tags: TC_003

    - name: Check for inventories
      assert:
        that: omnia_inventory_name in inventory_array.stdout
        success_msg: "{{ resource_exists_success_msg }}"
        fail_msg: "{{ resource_exists_fail_msg }}"
      tags: TC_003

    - name: Get the existing groups if omnia-inventory exists
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
        groups list --inventory "{{ omnia_inventory_name }}" -f human
      changed_when: False
      register: groups_array
      when: omnia_inventory_name in inventory_array.stdout
      tags: TC_003

    - name: Check for manager and compute groups
      assert:
        that: manager_group_name and compute_group_name in groups_array.stdout
        success_msg: "{{ resource_exists_success_msg }}"
        fail_msg: "{{ resource_exists_fail_msg }}"
      tags: TC_003

    - name: Get the existing credentials
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
        credentials list -f human
      changed_when: False
      register: credentials_array
      tags: TC_003

    - name: Check for "{{ credential_name }}"
      assert:
        that: credential_name in credentials_array.stdout
        success_msg: "{{ resource_exists_success_msg }}"
        fail_msg: "{{ resource_exists_fail_msg }}"
      tags: TC_003

    - name: Get the existing job templates
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
        job_templates list -f human
      changed_when: False
      register: templates_array
      tags: TC_003

    - name: Check for templates
      assert:
        that: omnia_template_name and inventory_template_name in templates_array.stdout
        success_msg: "{{ resource_exists_success_msg }}"
        fail_msg: "{{ resource_exists_fail_msg }}"
      tags: TC_003

    - name: Get the existing schedules for job templates
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ admin_password }}"
        schedules list -f human
      changed_when: False
      register: schedules_array
      tags: TC_003

    - name: Check for schedules to job template
      assert:
        that: schedule_name in schedules_array.stdout
        success_msg: "{{ resource_exists_success_msg }}"
        fail_msg: "{{ resource_exists_fail_msg }}"
      tags: TC_003

# Testcase OMNIA_CRM_US_AWXD_TC_004
# Execute common role in management station without internet connectivity
- name: OMNIA_CRM_US_AWXD_TC_004
  hosts: localhost
  connection: local
  gather_subset:
    - 'min'
  vars_files:
    - test_vars/test_common_vars.yml
    - ../roles/common/vars/main.yml
  tasks:
    - name: Down internet connectivity
      lineinfile:
        path: /etc/hosts
        line: "172.16.0.5 github.com"
        state: present
        backup: yes
      tags: TC_004

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

        - name: Calling the web_ui role to be tested
          include_role:
            name: ../roles/web_ui

      rescue:
        - name: Validate internet connectivity failure message
          assert:
            that: internet_status in internet_value.msg
            success_msg: "{{ internet_check_success_msg }}"
            fail_msg: "{{ internet_check_fail_msg }}"
      tags: TC_004

    - name: Up internet connectivity
      lineinfile:
        path: /etc/hosts
        line: "172.16.0.5 github.com"
        state: absent
      tags: TC_004

# Testcase OMNIA_CRM_US_AWXD_TC_005
# Execute web_ui role in management station and reboot the server
- name: OMNIA_CRM_US_AWXD_TC_005
  hosts: localhost
  connection: local
  vars_files:
    - test_vars/test_web_ui_vars.yml
  tasks:
    - name: Get last uptime of the server
      command: uptime -s
      register: uptime_status
      changed_when: false
      ignore_errors: yes
      tags: TC_005

    - name: Get current date
      command: date +"%Y-%m-%d %H"
      register: current_time
      changed_when: false
      ignore_errors: yes
      tags: TC_005

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

        - name: Calling the web_ui role to be tested
          include_role:
            name: ../roles/web_ui
      tags: TC_005

    - name: Reboot localhost
      command: reboot
      when: current_time.stdout not in uptime_status.stdout
      tags: TC_005

    - name: Inspect AWX web container
      docker_container_info:
        name: "{{ docker_container_name }}"
      register: awx_container_status
      tags: TC_005

    - name: Verify AWX container is running after reboot
      assert:
        that:
          - "'running' in awx_container_status.container.State.Status"