#  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_006
# Test case to validate whether the proper error message is displayed when slurm and kubernetes tags are skipped
- name: OMNIA_CRM_US_AWXD_TC_006
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_006

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt ../appliance/test/{{ test_input_config_filename }} --vault-password-file {{ vault_filename }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_006

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_006

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          manager:
            hosts:
              {{ host1 }}

          compute:
            hosts:
              {{ host2 }}
      tags: TC_006

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_006

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_006

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --skip_tags slurm,kubernetes --monitor -f human
          changed_when: false
          register: command_output

      rescue:
        - name: Validate error message
          assert:
            that: "'FAILED!' in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_006

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host1 }} --monitor -f human
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host2 }} --monitor -f human
      changed_when: false
      tags: TC_006

    - name: Delete the inventory file
      ignore_errors: yes
      file:
        state: absent
        path: testinventory.yml
      tags: TC_006

    - name: Create inventory file if it doesn't exist
      ignore_errors: yes
      file:
        path: "testinventory.yml"
        state: touch
        mode: '{{ file_permission }}'
      tags: TC_006

# Testcase OMNIA_CRM_US_AWXD_TC_007
# Test case to validate whether the skip tags validation is passed when slurm tag is given
- name: OMNIA_CRM_US_AWXD_TC_007
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_007

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt ../appliance/test/{{ test_input_config_filename }} --vault-password-file {{ vault_filename }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_007

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_007

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          manager:
            hosts:
              {{ host1 }}

          compute:
            hosts:
              {{ host2 }}
      tags: TC_007

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_007

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_007

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --skip_tags slurm --monitor -f human
          changed_when: false
          register: command_output
      tags: TC_007

      rescue:
        - name: Validate success message
          assert:
            that: "'FAILED!' not in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_007

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host1 }} --monitor -f human
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host2 }} --monitor -f human
      changed_when: false
      tags: TC_007

    - name: Delete the inventory file
      ignore_errors: yes
      file:
        state: absent
        path: testinventory.yml
      tags: TC_007

    - name: Create inventory file if it doesn't exist
      ignore_errors: yes
      file:
        path: "testinventory.yml"
        state: touch
        mode: '{{ file_permission }}'
      tags: TC_007

# Testcase OMNIA_CRM_US_AWXD_TC_008
# Test case to validate whether the skip tags validation is passed when kubernetes tag is given
- name: OMNIA_CRM_US_AWXD_TC_008
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_008

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt ../appliance/test/{{ test_input_config_filename }} --vault-password-file {{ vault_filename }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_008

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_008

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          manager:
            hosts:
              {{ host1 }}

          compute:
            hosts:
              {{ host2 }}
      tags: TC_008

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_008

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_008

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --skip_tags kubernetes --monitor -f human
          changed_when: false
          register: command_output

      rescue:
        - name: Validate success message
          assert:
            that: "'FAILED!' not in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_008

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host1 }} --monitor -f human
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host2 }} --monitor -f human
      changed_when: false
      tags: TC_008

    - name: Delete the inventory file
      ignore_errors: yes
      file:
        state: absent
        path: testinventory.yml
      tags: TC_008

    - name: Create inventory file if it doesn't exist
      ignore_errors: yes
      file:
        path: "testinventory.yml"
        state: touch
        mode: '{{ file_permission }}'
      tags: TC_008

# Testcase OMNIA_CRM_US_AWXD_TC_009
# Test case to validate whether the proper error message is displayed when no host is added to manager group
- name: OMNIA_CRM_US_AWXD_TC_009
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_009

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

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_009

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          manager:
            hosts:

          compute:
            hosts:
              {{ host2 }}
      tags: TC_009

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_009

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_009

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --monitor -f human
          changed_when: false
          register: command_output

      rescue:
        - name: Validate error message
          assert:
            that: "'FAILED!' in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_009

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host2 }} --monitor -f human
      changed_when: false
      tags: TC_009

    - name: Delete the inventory file
      ignore_errors: yes
      file:
        state: absent
        path: testinventory.yml
      tags: TC_009

    - name: Create inventory file if it doesn't exist
      ignore_errors: yes
      file:
        path: "testinventory.yml"
        state: touch
        mode: '{{ file_permission }}'
      tags: TC_009

# Testcase OMNIA_CRM_US_AWXD_TC_010
# Test case to verify whether the manger group validation is passed when single host is present
- name: OMNIA_CRM_US_AWXD_TC_010
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_010

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

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_010

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          manager:
            hosts:
              {{ host1 }}

          compute:
            hosts:
              {{ host2 }}
      tags: TC_010

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_010

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_010

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --monitor -f human
          changed_when: false
          register: command_output
      tags: TC_010

      rescue:
        - name: Validate success message
          assert:
            that: "'FAILED!' not in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_010

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host1 }} --monitor -f human
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host2 }} --monitor -f human
      changed_when: false
      tags: TC_010

    - name: Delete the inventory file
      ignore_errors: yes
      file:
        state: absent
        path: testinventory.yml
      tags: TC_010

    - name: Create inventory file if it doesn't exist
      ignore_errors: yes
      file:
        path: "testinventory.yml"
        state: touch
        mode: '{{ file_permission }}'
      tags: TC_010

# Testcase OMNIA_CRM_US_AWXD_TC_011
# Test case to validate whether the proper error message is displayed when no host is added to compute group
- name: OMNIA_CRM_US_AWXD_TC_011
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_011

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt ../appliance/test/{{ test_input_config_filename }} --vault-password-file {{ vault_filename }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_011

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_011

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          manager:
            hosts:
              {{ host3 }}

          compute:
            hosts:

      tags: TC_011

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_011

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_011

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --monitor -f human
          changed_when: false
          register: command_output

      rescue:
        - name: Validate error message
          assert:
            that: "'FAILED!' in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_011

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host3 }} --monitor -f human
      changed_when: false
      tags: TC_011

    - name: Delete the inventory file
      file:
        state: absent
        path: testinventory.yml
      ignore_errors: yes
      tags: TC_011

    - name: Create inventory file if it doesn't exist
      file:
        path: "testinventory.yml"
        mode: '{{ file_permission }}'
        state: touch
      ignore_errors: yes
      tags: TC_011

# Testcase OMNIA_CRM_US_AWXD_TC_012
# Test case to verify whether the compute group validation is passed when more than 1 host is present
- name: OMNIA_CRM_US_AWXD_TC_012
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_012

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

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_012

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          manager:
            hosts:
              {{ host1 }}

          compute:
            hosts:
              {{ host2 }}
            hosts:
              {{ host3 }}
      tags: TC_012

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_012

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_012

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --monitor -f human
          changed_when: false
          register: command_output

      rescue:
        - name: Validate success message
          assert:
            that: "'FAILED!' not in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_012

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host1 }} --monitor -f human
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host2 }} --monitor -f human
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host3 }} --monitor -f human
      changed_when: false
      tags: TC_012

    - name: Delete the inventory file
      ignore_errors: yes
      file:
        state: absent
        path: testinventory.yml
      tags: TC_012

    - name: Create inventory file if it doesn't exist
      ignore_errors: yes
      file:
        path: "testinventory.yml"
        state: touch
        mode: '{{ file_permission }}'
      tags: TC_012

# Testcase OMNIA_CRM_US_AWXD_TC_013
# Test case to validate the success meesage when a host is present in both manager and compute groups
- name: OMNIA_CRM_US_AWXD_TC_013
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_013

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt ../appliance/test/{{ test_input_config_filename }} --vault-password-file {{ vault_filename }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_013

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_013

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          manager:
            hosts:
              {{ host1 }}

          compute:
            hosts:
              {{ host1 }}
      tags: TC_013

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_013

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_013

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --monitor -f human
          changed_when: false
          register: command_output

      rescue:
        - name: Validate success message
          assert:
            that: "'FAILED!' not in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_013

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host1 }} --monitor -f human
      changed_when: false
      tags: TC_013

    - name: Delete the inventory file
      ignore_errors: yes
      file:
        state: absent
        path: testinventory.yml
      tags: TC_013

    - name: Create inventory file if it doesn't exist
      ignore_errors: yes
      file:
        path: "testinventory.yml"
        state: touch
        mode: '{{ file_permission }}'
      tags: TC_013

# Testcase OMNIA_CRM_US_AWXD_TC_014
# Test case to validate whether the proper error message is displayed when more than one host is added to manager group
- name: OMNIA_CRM_US_AWXD_TC_014
  hosts: localhost
  connection: local
  vars_files:
    - ../appliance/roles/web_ui/vars/main.yml
    - ../appliance/roles/common/vars/main.yml
    - test_vars/test_omnia_vars.yml
  tasks:
    - name: Check input config file is encrypted
      command: cat ../appliance/test/{{ test_input_config_filename }}
      changed_when: false
      register: config_content
      tags: TC_014

    - name: Decrpyt input_config.yml
      command: ansible-vault decrypt ../appliance/test/{{ test_input_config_filename }} --vault-password-file {{ vault_filename }}
      changed_when: false
      when: "'$ANSIBLE_VAULT;' in config_content.stdout"
      tags: TC_014

    - name: Include variable file input_config.yml
      include_vars: "../appliance/test/{{ test_input_config_filename }}"
      tags: TC_014

    - name: Creating inventory file with hosts associated to the groups
      copy:
        dest: "testinventory.yml"
        mode: '{{ file_permission }}'
        content: |
          ---
          compute:
            hosts:
              {{ host1 }}

          manager:
            hosts:
              {{ host2 }}
            hosts:
              {{ host3 }}

      tags: TC_014

    - name: Get present working directory
      command: >-
        pwd
      register: path
      changed_when: false
      tags: TC_014

    - name: Push the inventory to AWX
      shell: |
        set -o pipefail
        docker exec awx_task awx-manage inventory_import --inventory-name {{ omnia_inventory_name }} --source "{{ inventory_path }}/{{ path.stdout.split('/')[-2] }}/test/testinventory.yml"
      changed_when: false
      tags: TC_014

    - block:
        - name: Launch the job template
          command: >-
            awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
            job_templates launch "{{ omnia_template_name }}" --credentials "{{ credential_name }}" --monitor -f human
          changed_when: false
          register: command_output

      rescue:
        - name: Validate error message
          assert:
            that: "'FAILED!' in command_output.stdout"
            success_msg: "{{ test_case_success_msg }}"
            fail_msg: "{{ test_case_failure_msg }}"
      tags: TC_014

    - name: Delete the hosts
      command: >-
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host1 }} --monitor -f human
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host2 }} --monitor -f human
        awx --conf.host "{{ awx_ip }}" --conf.username "{{ awx_user }}" --conf.password "{{ awx_password }}" --conf.insecure
        hosts delete {{ host3 }} --monitor -f human
      changed_when: false
      tags: TC_014

    - name: Delete the inventory file
      ignore_errors: yes
      file:
        state: absent
        path: testinventory.yml
      tags: TC_014

    - name: Create inventory file if it doesn't exist
      ignore_errors: yes
      file:
        path: "testinventory.yml"
        state: touch
        mode: '{{ file_permission }}'
      tags: TC_014