author | GP Orcullo <kinsamanka@gmail.com> |
Mon, 31 Oct 2022 14:48:25 +0800 | |
branch | python3 |
changeset 3768 | c032fff8af32 |
parent 3718 | 7841b651d601 |
child 3820 | 46f3ca3f0157 |
permissions | -rw-r--r-- |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
1 |
#!/bin/bash |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
2 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
3 |
rm -f ./SRVOK ./PLCOK |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
4 |
|
3631
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
5 |
|
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
6 |
yes "" | openssl req -x509 -newkey rsa:2048 -keyout my_private_key.pem -out my_cert.pem \ |
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
7 |
-days 355 -nodes -addext "subjectAltName = URI:urn:example.org:FreeOpcUa:python-opcua" |
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
8 |
openssl x509 -outform der -in my_cert.pem -out my_cert.der |
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
9 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
10 |
# Run server |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
11 |
$BEREMIZPYTHONPATH - > >( |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
12 |
echo "Start SRV loop" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
13 |
while read line; do |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
14 |
# Wait for server to print modified value |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
15 |
echo "SRV>> $line" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
16 |
if [[ "$line" == 3.4 ]]; then |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
17 |
echo "PLC could write value" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
18 |
touch ./SRVOK |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
19 |
fi |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
20 |
done |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
21 |
echo "End SRV loop" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
22 |
) << EOF & |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
23 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
24 |
import sys |
3718
7841b651d601
Tests: OPCUA tests can now use OPCUA_DEFAULT_HOST environment variable to force host to something else than "127.0.0.1" or "localhost"
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3634
diff
changeset
|
25 |
import os |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
26 |
import time |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
27 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
28 |
from opcua import ua, Server |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
29 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
30 |
server = Server() |
3718
7841b651d601
Tests: OPCUA tests can now use OPCUA_DEFAULT_HOST environment variable to force host to something else than "127.0.0.1" or "localhost"
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3634
diff
changeset
|
31 |
host = os.environ.get("OPCUA_DEFAULT_HOST", "127.0.0.1") |
7841b651d601
Tests: OPCUA tests can now use OPCUA_DEFAULT_HOST environment variable to force host to something else than "127.0.0.1" or "localhost"
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3634
diff
changeset
|
32 |
endpoint = "opc.tcp://"+host+":4840/freeopcua/server/" |
7841b651d601
Tests: OPCUA tests can now use OPCUA_DEFAULT_HOST environment variable to force host to something else than "127.0.0.1" or "localhost"
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3634
diff
changeset
|
33 |
server.set_endpoint(endpoint) |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
34 |
|
3631
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
35 |
server.set_security_policy([ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt]) |
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
36 |
server.load_certificate("my_cert.der") |
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
37 |
server.load_private_key("my_private_key.pem") |
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
38 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
39 |
uri = "http://beremiz.github.io" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
40 |
idx = server.register_namespace(uri) |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
41 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
42 |
objects = server.get_objects_node() |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
43 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
44 |
testobj = objects.add_object(idx, "TestObject") |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
45 |
testvarout = testobj.add_variable(idx, "TestOut", 1.2) |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
46 |
testvar = testobj.add_variable(idx, "TestIn", 5.6) |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
47 |
testvar.set_writable() |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
48 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
49 |
server.start() |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
50 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
51 |
try: |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
52 |
while True: |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
53 |
time.sleep(1) |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
54 |
print testvar.get_value() |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
55 |
sys.stdout.flush() |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
56 |
finally: |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
57 |
server.stop() |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
58 |
EOF |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
59 |
SERVER_PID=$! |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
60 |
|
3634
db3ea47b0d0b
Tests: fix OPC-UA test when run from repo.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3631
diff
changeset
|
61 |
PROJECT_FILES_DIR=$BEREMIZPATH/tests/projects/opcua_client_encrypted/project_files |
db3ea47b0d0b
Tests: fix OPC-UA test when run from repo.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3631
diff
changeset
|
62 |
mkdir $PROJECT_FILES_DIR |
db3ea47b0d0b
Tests: fix OPC-UA test when run from repo.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3631
diff
changeset
|
63 |
cp my_cert.der my_private_key.pem $PROJECT_FILES_DIR |
3631
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
64 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
65 |
# Start PLC with opcua test |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
66 |
setsid $BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \ |
3631
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
67 |
--project-home $BEREMIZPATH/tests/projects/opcua_client_encrypted build transfer run > >( |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
68 |
echo "Start PLC loop" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
69 |
while read line; do |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
70 |
# Wait for PLC runtime to output expected value on stdout |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
71 |
echo "PLC>> $line" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
72 |
if [[ "$line" == 1.2 ]]; then |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
73 |
echo "PLC could read value" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
74 |
touch ./PLCOK |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
75 |
fi |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
76 |
done |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
77 |
echo "End PLC loop" |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
78 |
) & |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
79 |
PLC_PID=$! |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
80 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
81 |
echo all subprocess started, start polling results |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
82 |
res=110 # default to ETIMEDOUT |
3631
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
83 |
c=45 |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
84 |
while ((c--)); do |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
85 |
if [[ -a ./SRVOK && -a ./PLCOK ]]; then |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
86 |
echo got results. |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
87 |
res=0 # OK success |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
88 |
break |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
89 |
else |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
90 |
echo waiting.... $c |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
91 |
sleep 1 |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
92 |
fi |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
93 |
done |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
94 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
95 |
# Kill PLC and subprocess |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
96 |
echo will kill PLC:$PLC_PID and SERVER:$SERVER_PID |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
97 |
pkill -s $PLC_PID |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
98 |
kill $SERVER_PID |
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
99 |
|
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
100 |
exit $res |