tests/cli_tests/C_runtime.bash
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 16 Oct 2024 12:18:14 +0200
changeset 4030 45532de22b75
parent 3955 792ae5ff01ca
permissions -rw-r--r--
TESTS: Update Matiec revision in GH workflow
3955
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
#!/bin/bash
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
rm -f ./PLC_OK ./CLI_OK
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
# Run C runtime
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
$BEREMIZPATH/C_runtime/beremiz_runtime -v -t tcp -p 61131 -h localhost > >(
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
    echo "Start PLC stdout reader loop"
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
    while read line; do 
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
        # Wait for server to print modified value
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
        echo "PLC>> $line"
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
        if [[ "$line" == "C runtime OK #3" ]]; then
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
            echo "$line"
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
            touch ./PLC_OK
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
        fi
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
    done
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
    echo "End PLC stdout reader loop"
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
) &
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
PLC_PID=$!
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
# Start PLC with C runtime test
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
setsid $BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
     --project-home $BEREMIZPATH/tests/projects/c_runtime build transfer run > >(
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
echo "Start CLI loop"
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
while read line; do 
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
    # Wait for CLI to output expected PLC log message on stdout
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
    echo "CLI>> $line"
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
    if [[ $line =~ .*C\ runtime\ log\ OK\ #3$ ]]; then
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
        echo "$line"
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
        touch ./CLI_OK
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
    fi
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
done
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
echo "End CLI loop"
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
) &
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
CLI_PID=$!
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
echo all subprocess started, start polling results
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
res=110  # default to ETIMEDOUT
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
c=45
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
while ((c--)); do
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
    if [[ -a ./PLC_OK && -a ./CLI_OK ]]; then
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
        echo got results.
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
        res=0  # OK success
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
        break
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
    else
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
        echo waiting.... $c
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
        sleep 1
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
    fi
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
done
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
# Kill PLC and subprocess
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
echo will kill CLI:$CLI_PID and PLC:$PLC_PID
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
pkill -s $CLI_PID 
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
kill $PLC_PID
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
792ae5ff01ca Tests: Add minimal C runtime test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
exit $res