# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Copyright: Contributors to the Ansible project

- name: Prep package_fact tests - Debian Family
  block:
    - name: install python apt bindings - python3
      package: name="python3-apt" state=present

    - name: Gather package facts
      package_facts:
        manager: apt

    - name: check for ansible_facts.packages exists
      assert:
        that: ansible_facts.packages is defined

    - name: Now try again but installing misleading rpm
      block:
        - name: install misleading rpm api
          package: name="python3-rpm" state=present

        - name: prep outputdir
          tempfile: path=~ state=directory
          register: tempdir

        - name: install misleading rpm 'binary' file
          file: dest="{{tempdir['path']}}/rpm" state=touch mode='0700'

        - name: Gather package facts, finding 'rpm' on debian fam (needed for latest version)
          package_facts:
          environment:
            PATH: "${PATH}:{{tempdir['path']}}"

        - name: check we got packages
          assert:
            that:
              - (ansible_facts.packages | length ) > 0

        - name: Same again but this time forcing rpm first
          package_facts:
             manager: ['rpm', 'apt']
          environment:
            PATH: "${PATH}:{{tempdir['path']}}"

        - name: check we got packages
          assert:
            that:
              - (ansible_facts.packages | length ) > 0

      always:
        - package: name="python3-rpm" state=absent
        - file: path="{{tempdir['path']}}/rpm" state=absent
        - file: path="{{tempdir['path']}}" state=absent


  when: ansible_os_family == "Debian"

- name: Run package_fact tests - Red Hat Family
  block:
    - name: Gather package facts
      package_facts:
        manager: rpm

    - name: check for ansible_facts.packages exists
      assert:
        that: ansible_facts.packages is defined
  when: (ansible_os_family == "RedHat")

- name: Same as those above, but based on pkg_mgr, tests aliases
  block:
    - name: Gather package facts
      package_facts:
        manager: '{{ ansible_facts["pkg_mgr"] }}'

    - name: check for ansible_facts.packages exists
      assert:
        that:
          - ansible_facts.packages is defined
          - ansible_facts.packages | length > 1
  when: ansible_facts['os_family'] in ["RedHat", "Debian"]

# Check that auto detection works also
- name: Gather package facts
  package_facts:
    manager: auto

- name: check for ansible_facts.packages exists
  assert:
    that: ansible_facts.packages is defined

- name: Run package_fact tests - FreeBSD
  block:
    - name: Gather package facts
      package_facts:
        manager: pkg

    - name: check for ansible_facts.packages exists
      assert:
        that: ansible_facts.packages is defined

    - name: check there is at least one package not flagged vital nor automatic
      command: pkg query -e "%a = 0 && %V = 0" %n
      register: not_vital_nor_automatic
      failed_when: not not_vital_nor_automatic.stdout

    - vars:
        pkg_name: "{{ not_vital_nor_automatic.stdout_lines[0].strip() }}"
      block:
        - name: check the selected package is not vital
          assert:
            that:
              - 'not ansible_facts.packages[pkg_name][0].vital'
              - 'not ansible_facts.packages[pkg_name][0].automatic'

        - name: flag the selected package as vital and automatic
          command: 'pkg set --yes -v 1 -A 1 {{ pkg_name }}'

        - name: Gather package facts (again)
          package_facts:

        - name: check the selected package is flagged vital and automatic
          assert:
            that:
              - 'ansible_facts.packages[pkg_name][0].vital|bool'
              - 'ansible_facts.packages[pkg_name][0].automatic|bool'
      always:
        - name: restore previous flags for the selected package
          command: 'pkg set --yes -v 0 -A 0 {{ pkg_name }}'
  when: ansible_os_family == "FreeBSD"
