Test
====

Tests in Running CANopen network
--------------------------------

Tests on Running CANopen network are implemented with two CANopenNode devices, by default running on Linux virtual CAN interface. One of them has gateway interface and communicates with program `cocomm`.

Tests are run in Linux command line with [Bash Automated Testing System - BATS](https://github.com/bats-core/bats-core), which is included in CANopenSocket as three git submodules.

Test results are available in [test_report.md](test_report.md).


### Prerequisites

 - Get CANopenSocket and all submodules as described in README.md
 - make `cocomm`, `canopend` and `basicDevice`


### Run tests automatically

    cd test/running_canopen$
    ./test_canopend.sh vcan0

This will:
1. start `vcan0` interface if not already existed.
2. run two CANopenNode Linux devices in background (`start_canopen.sh` script):
   - `CANopenNode/canopend` with nodeId=1 and gateway interface: `local-/tmp/CO_command_socket`
   - `examples/basicDevice/basicDevice` with nodeId=4
3. run tests and measure time
4. cleanup (`tmp/stop_canopen.sh` script, generated by `start_canopen.sh`).

If argument `vcan0` or similar is omitted, it is assumed, two CANopen devices are already running in system. In that case steps 1, 2 and 4 are skipped.


### Start two CANopen devices manually

`start_canopen.sh <CAN device>` script runs two CANopenNode Linux devices in background.

Possible configuration options can configure CANopenNode applications. To override default values run the commands from command line before running test scripts. Default configuration is:

    export co_device_1="../../CANopenNode/canopend"
    export co_device_4="../../examples/basicDevice/basicDevice"

It is possible to run two CANopen devices without `start_canopen.sh` script and then run the tests. Devices can be connected with on real CAN bus and second device can run on any target system. By default this script runs following commands and prepares `tmp/stop_canopen.sh` script for cleanup:

    sudo modprobe vcan
    sudo ip link add dev vcan0 type vcan
    sudo ip link set up vcan0

    mkdir tmp
    echo "-" > tmp/dev1_lss.persist
    echo "-" > tmp/dev1_od_comm.persist
    ../../CANopenNode/canopend vcan0 -i 1 -s "tmp/dev1_" -c "local-/tmp/CO_command_socket"

    echo "-" > tmp/dev4_lss.persist
    echo "-" > tmp/dev4_od_comm.persist
    echo "-" > tmp/dev4_od_app_auto.persist
    echo "-" > tmp/dev4_od_app.persist
    ../../examples/basicDevice/basicDevice vcan0 -i 4 -s "tmp/dev4_"

    # after finish with testing terminate the programs and `rm -r tmp`

There is also a helper script, which runs only canopend and candump on specified interface in terminal:

    cd tools
    ./run_canopend_candump.sh <can device>


### Run the tests manually

Before running the tests, specify cocomm location and CAN device, which will be used for monitoring CAN messages (candump). To override other configuration variables see file `config_bats.sh`. Default configuration is:

    export can_device=vcan0
    export cocomm="../../cocomm/cocomm"

Then simply run the separate tests from the same terminal:

    ./basic_NMT_SDO_heartbeat.bats
    ./heartbeat_consumer.bats
    ./SDO_transfer.bats

Open the `*.bats` script file in text editor and examine the contents. Script is quite straightforward, see [BATS usage](https://bats-core.readthedocs.io/en/latest/writing-tests.html) and [BATS assert](https://github.com/bats-core/bats-assert) for information on testing commands. Command line program `cocomm` is used for running each test. For `cocomm` example usage see [basicDevice/README.md](../examples/basicDevice/README.md). Program uses here one additional functionality - it prints dump of actual CAN messages, which occur with each `cocomm` command. So BATS can verify the complete result.
