---
# This is an example playbook to execute Ansible tests.

- name: Verify
  hosts: all
  gather_facts: false
  vars:
    id_product:
      - 0e11
      - 9115
      - 9117
    id_vendor:
      - 1ab1
      - ffff
    symlink:
      - '"psu"'
      - psu1
      - psu2
      - psu3
      - psu4
      - psu5
  tasks:
    - name: Registor PSU Status
      ansible.builtin.stat:
        path: /etc/udev/rules.d/76-hil-psu.rules
      register: psu

    - name: Verify UDEV file exists
      assert:
        that:
          - "psu.stat.exists"
          - "psu.stat.isreg"
          - "psu.stat.path == '/etc/udev/rules.d/76-hil-psu.rules'"
          - "psu.stat.mode == '0644'"

    - name: Verify id_product
      shell: cat /etc/udev/rules.d/76-hil-psu.rules|grep {{ item }}
      register: result
      changed_when: false
      failed_when: result.rc != 0
      loop: "{{ id_product }}"

    - name: Verify id_vendor
      shell: cat /etc/udev/rules.d/76-hil-psu.rules|grep {{ item }}
      register: result
      changed_when: false
      failed_when: result.rc != 0
      loop: "{{ id_vendor }}"

    - name: Verify symlinks
      shell: cat /etc/udev/rules.d/76-hil-psu.rules|grep '{{ item }}'
      register: result
      changed_when: false
      failed_when: result.rc != 0
      loop: "{{ symlink }}"

    - name: Verify mode
      shell: cat /etc/udev/rules.d/76-hil-psu.rules|grep -c 'MODE="0666"'
      register: result
      changed_when: false
      failed_when: '"6" not in result.stdout'

    - name: Verify only 5 serial_numbers
      shell: cat /etc/udev/rules.d/76-hil-psu.rules|grep -c 'ATTRS{serial}'
      register: result
      changed_when: false
      failed_when: '"5" not in result.stdout'
