author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Fri, 04 Nov 2022 22:14:14 +0100 | |
branch | wxPython4 |
changeset 3660 | 0e41dbe6ddcd |
parent 3634 | db3ea47b0d0b |
child 3718 | 7841b651d601 |
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 |
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
|
25 |
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
|
26 |
|
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 |
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
|
28 |
|
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 |
server = 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
|
30 |
server.set_endpoint("opc.tcp://127.0.0.1:4840/freeopcua/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
|
31 |
|
3631
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
32 |
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
|
33 |
server.load_certificate("my_cert.der") |
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
34 |
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
|
35 |
|
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
|
36 |
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
|
37 |
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
|
38 |
|
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 |
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
|
40 |
|
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 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
|
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 |
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
|
47 |
|
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 |
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
|
49 |
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
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
|
3634
db3ea47b0d0b
Tests: fix OPC-UA test when run from repo.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3631
diff
changeset
|
58 |
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
|
59 |
mkdir $PROJECT_FILES_DIR |
db3ea47b0d0b
Tests: fix OPC-UA test when run from repo.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3631
diff
changeset
|
60 |
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
|
61 |
|
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
|
62 |
# 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
|
63 |
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
|
64 |
--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
|
65 |
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
|
66 |
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
|
67 |
# 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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
) & |
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 |
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
|
77 |
|
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 |
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
|
79 |
res=110 # default to ETIMEDOUT |
3631
176e1f218c61
Tests: add opc-ua encrypted test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
sleep 1 |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
89 |
fi |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
90 |
done |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
91 |
|
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
|
92 |
# 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
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
|
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 |
exit $res |