# Test code for the getent module.
# (c) 2017, James Tanner <tanner.jc@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- name: check for getent command
  shell: which getent
  failed_when: False
  register: getent_check
##
## getent
##
- block:
    - name: run getent with specified service
      getent:
          database: passwd
          key: root
          service: files
      register: getent_test0
      when: ansible_system != 'FreeBSD' and ansible_distribution != 'Alpine'

    - name: run getent w/o specified service (FreeBSD)
      getent:
          database: passwd
          key: root
      register: getent_test0
      when: ansible_system == 'FreeBSD' or ansible_distribution == 'Alpine'

    - debug: var=getent_test0

    - name: validate results
      assert:
          that:
              - 'getent_passwd is defined'
              - 'getent_passwd.root is defined'
              - 'getent_passwd.root|length == 6'

    - name: run getent with invalid split value
      getent:
        database: group
        split: ""
      register: getent_test1
      ignore_errors: True

    - name: validate results
      assert:
        that:
          - 'getent_test1 is failed'
          - 'getent_test1.msg is contains "Invalid split value. The value must be a non-empty string"'

  when: getent_check.rc == 0
