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

- name: Verify
  hosts: all
  gather_facts: false
  become: true
  vars:
    interfaces:
      - DR_CAN1
      - DR_CAN2
      - DR_CAN3
      - DR_CAN4
      - BDY_CAN1
      - BDY_CAN2
      - DIAG_CAN
      - AI_CAN
      - LUX_CAN
      - EMU_BDY_CAN2
      - EMU_DR_CAN3
      - EMU_DR_CAN4
  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: check if the can configuration files exist
    stat:
      path: /home/robot/ecu_tester.sh
    register: can_files
    failed_when: can_files.stat.exists
