---
- name: Verify
  hosts: all
  gather_facts: false
  become: true
  vars:
    interfaces:
      - CAN1
      - CAN2
  tasks:
  - name: vcan module loaded
    shell: lsmod | grep vcan
    register: result
    changed_when: false
    failed_when: result.rc != 0

  - name: vcan module set to load on reboot
    shell: cat /etc/modules | grep vcan
    register: result
    changed_when: false
    failed_when: result.rc != 0

  - name: vcan up
    shell: "ip link show {{ item }}"
    register: result
    changed_when: false
    failed_when: result.rc != 0
    loop: "{{ interfaces }}"

  - name: reset candump log
    ansible.builtin.file:
      path: candump.log
      state: absent
    changed_when: false

  - name: background candump
    shell: "(candump {{ item }} >> candump.log 2>&1 &)"
    async: 10
    poll: 0
    changed_when: false
    loop: "{{ interfaces }}"

  - name: cansend
    shell: cansend {{ item }} 180#01000000
    changed_when: false
    loop: "{{ interfaces }}"

  - name: verify can message received
    shell: "cat candump.log|grep {{ item }}"
    register: result
    changed_when: false
    failed_when: result.rc != 0
    loop: "{{ interfaces }}"

  - name: verify CAN1 has default txqueuelen 4096
    shell: "cat /etc/network/interfaces.d/can_iface.cfg|grep 'CAN1 txqueuelen 4096'"
    register: result
    changed_when: false
    failed_when: result.rc != 0

  - name: verify CAN2 has non-default txqueuelen 1024
    shell: "cat /etc/network/interfaces.d/can_iface.cfg|grep 'CAN2 txqueuelen 1024'"
    register: result
    changed_when: false
    failed_when: result.rc != 0
