#  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
  gather_facts: true
  vars_files:
    - ../input_params/base_vars.yml
    - test_vars/test_psacct_vars.yml

  gather_subset:
    - 'min'
  tags: VERIFY_OMNIA_02

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

# OMNIA_1.2_psacct_TC_001
# Test case to Verify the enable psacct service

    - name: Enable the psacct service
        shell: systemctl enable --now psacct
      register: psacct_enable
      when:
        - 'ansible_distribution == "{{ os_name_rocky }}"'

    - name: Verify psacct service whether enabled or not
      assert:
        that:
          - psacct_enable.stderr | regex_search( "{{ psacct_enabled }}")
        success_msg: "{{ psacct_enable_success_msg }}"
        fail_msg: "{{ psacct_disable_fail_msg }}"

# OMNIA_1.2_psacct_TC_002
# Test case to Check and Start psacct Service

    - name: Check the psacct service status
      shell: systemctl status psacct.service
      register: psacct_status
      when:
        - 'ansible_distribution == "{{ os_name_rocky }}"'

    - name: Verify psacct service is started or not
      assert:
        that:
          - psacct_status.stdout | regex_search( "{{ psacct_active }}")
        success_msg: "{{ psacct_service_success_msg }}"
        fail_msg: "{{ psacct_service_fail_msg }}"

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

    - name: Check the psacct Package Installation
      shell: rpm -qa | grep -i psacct
      register: psacct_package
      when:
        - 'ansible_distribution == "{{ os_name_rocky }}"'

    - name: Verify psacct Package whether installed or not
      assert:
        that:
          - psacct_package.stdout | regex_search( "psacct-6.6.3-4.el8.x86_64")
        success_msg: "{{ psacct_package_success_msg }}"
        fail_msg: "{{ psacct_package_fail_msg }}"

# OMNIA_1.2_psacct_TC_004
# Test case to Verify the disable psacct service

    - name: Disable the psacct service
      shell: systemctl disable psacct.service
      register: psacct_disable
      when:
        - 'ansible_distribution == "{{ os_name_rocky }}"'

    - name: Enable the psacct service
      shell: systemctl enable --now psacct
      register: psacct_enable
      when:
        - 'ansible_distribution == "{{ os_name_rocky }}"'

    - name: Verify psacct service whether disabled or not
      assert:
        that:
          - psacct_disable.stderr | regex_search( "{{ psacct_disabled }}")
        success_msg: "{{ psacct_disable_success_msg }}"
        fail_msg: "{{ psacct_disable_fail_msg }}"


# OMNIA_1.2_psacct_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_rocky }}"'

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

    - name: Verify ac version
      assert:
        that:
          - ac_version.stdout | regex_search( "{{ psacct_ac_version }}")
        success_msg: "{{ psacct_ac_version_success_msg }}"
        fail_msg: "{{ psacct_ac_version_fail_msg }}"

    - name: Verify sa version
      assert:
        that:
          - sa_version.stdout | regex_search( "{{ psacct_sa_version }}")
        success_msg: "{{ psacct_sa_version_success_msg }}"
        fail_msg: "{{ psacct_sa_version_fail_msg }}"

# OMNIA_1.2_psacct_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_rocky }}"'

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

    - name: Verify package details of ac utility
      assert:
        that:
          - ac_utility.stdout | regex_search( "{{ psacct_ac_sa_utility }}")
        success_msg: "{{ psacct_ac_utility_success_msg }}"
        fail_msg: "{{ psacct_ac_utility_fail_msg }}"

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

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

    - name: Verify package details of sa utility
      assert:
        that:
          - sa_utility.stdout | regex_search( "{{ psacct_ac_sa_utility }}")
        success_msg: "{{ psacct_sa_utility_success_msg }}"
        fail_msg: "{{ psacct_sa_utility_fail_msg }}"