#  Copyright 2022 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.
---
- name: OMNIA_1.2_MS_TC_001
  hosts: localhost
  connection: local
  vars_files:
    - ../input_params/base_vars.yml
    - test_vars/test_acct_vars.yml

  gather_subset:
    - 'min'
  tags: VERIFY_OMNIA_02

  tasks:
    - name: Check OS Version
      assert:
        that:
          - 'ansible_distribution == "{{ os_name_leap }}"'
        success_msg: "{{ check_os_success_msg }}"
        fail_msg: "{{ check_os_fail_msg }}"
      tags: Check_OS

# OMNIA_1.2_acct_TC_001
# Test case to Verify the enable acct service

    - name: Enable the acct service
      shell: systemctl enable --now acct
      register: acct_enable
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Verify acct service whether enabled or not
      assert:
        that:
          - acct_enable.stderr | regex_search( "{{ acct_enabled }}")
        success_msg: "{{ acct_enable_success_msg }}"
        fail_msg: "{{ acct_disable_fail_msg }}"

# OMNIA_1.2_acct_TC_002
# Test case to Check and Start acct Service

    - name: Check the acct service status
      shell: systemctl status acct.service
      register: acct_status
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Verify acct service is started or not
      assert:
        that:
          - acct_status.stdout | regex_search( "{{ acct_active }}")
        success_msg: "{{ acct_service_success_msg }}"
        fail_msg: "{{ acct_service_fail_msg }}"

# OMNIA_1.2_acct_TC_003
# Test case to Verify the Package Installation

    - name: Check the acct Package Installation
      shell: rpm -qa | grep -i acct
      register: acct_package
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Verify acct Package whether installed or not
      assert:
        that:
          - acct_package.stdout | regex_search( "acct-6.6.4-2.25.x86_64")
        success_msg: "{{ acct_package_success_msg }}"
        fail_msg: "{{ acct_package_fail_msg }}"

# OMNIA_1.2_acct_TC_004
# Test case to Verify the disable acct service

    - name: Disable the acct service
      shell: systemctl disable acct.service
      register: acct_disable
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Enable the acct service
      shell: systemctl enable --now acct
      register: acct_enable
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Verify acct service whether disabled or not
      assert:
        that:
          - acct_disable.stderr | regex_search( "{{ acct_disabled }}")
        success_msg: "{{ acct_disable_success_msg }}"
        fail_msg: "{{ acct_disable_fail_msg }}"


# OMNIA_1.2_acct_TC_005
# Test case to Verify ac and sa version

    - name: Check the ac version
      shell: ac -V
      register: ac_version
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Check the as version
      shell: sa -V
      register: sa_version
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Verify ac version
      assert:
        that:
          - ac_version.stdout | regex_search( "{{ acct_ac_version }}")
        success_msg: "{{ acct_ac_version_success_msg }}"
        fail_msg: "{{ acct_ac_version_fail_msg }}"

    - name: Verify sa version
      assert:
        that:
          - sa_version.stdout | regex_search( "{{ acct_sa_version }}")
        success_msg: "{{ acct_sa_version_success_msg }}"
        fail_msg: "{{ acct_sa_version_fail_msg }}"

# OMNIA_1.2_acct_TC_006
# Test case to Check Package details of ac and sa utility

    - name: find the complete path of ac
      shell: which ac
      register: ac_path
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: check the package details of ac utility
      shell: rpm -qf {{ ac_path.stdout }}
      register: ac_utility
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Verify package details of ac utility
      assert:
        that:
          - ac_utility.stdout | regex_search( "{{ acct_ac_sa_utility }}")
        success_msg: "{{ acct_ac_utility_success_msg }}"
        fail_msg: "{{ acct_ac_utility_fail_msg }}"

    - name: find the complete path of sa
      shell: which sa
      register: sa_path
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: check the package details of sa utility
      shell: rpm -qf {{ sa_path.stdout }}
      register: sa_utility
      when:
        - 'ansible_distribution == "{{ os_name_leap }}"'

    - name: Verify package details of sa utility
      assert:
        that:
          - sa_utility.stdout | regex_search( "{{ acct_ac_sa_utility }}")
        success_msg: "{{ acct_sa_utility_success_msg }}"
        fail_msg: "{{ acct_sa_utility_fail_msg }}"