Tests: Add WAMP automated test
authorEdouard Tisserant <edouard@beremiz.fr>
Fri, 28 Feb 2025 17:00:14 +0100 (4 weeks ago)
changeset 4126 80c52609d1ad
parent 4125 5f9e61421174
child 4127 7da45bd15fc8
Tests: Add WAMP automated test
tests/cli_tests/wamp_test.bash
tests/projects/wamp/.crossbar/config.json
tests/projects/wamp/README
tests/projects/wamp/beremiz.xml
tests/projects/wamp/project_files/wampconf.json
tests/projects/wamp/psk/management.json
tests/projects/wamp/psk/test_service_name.psk
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cli_tests/wamp_test.bash	Fri Feb 28 17:00:14 2025 +0100
@@ -0,0 +1,197 @@
+#!/bin/bash
+
+rm -f ./CLI_OK ./PLC_OK
+
+# Start runtime one first time to generate PSK
+$BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_service.py -s psk.txt -n test_wamp_ID -x 0 &
+PLC_PID=$!
+res=110  # default to ETIMEDOUT
+c=5
+while ((c--)); do
+    if [[ -a psk.txt ]]; then
+        echo got PSK.
+        res=0  # OK success
+        break
+    else
+        echo waiting PSK.... $c
+        sleep 1
+    fi
+done
+
+kill $PLC_PID
+
+if [ "$res" != "0" ] ; then
+    echo timeout generating PSK.
+    exit $res
+fi
+
+IFS=':' read -r wamp_ID wamp_secret < psk.txt
+
+# Start crossbar server
+mkdir -p .crossbar
+cat > .crossbar/config.json <<JsonEnd
+{
+    "version": 2,
+    "workers": [
+        {
+            "type": "router",
+            "id": "automation_router",
+            "realms": [
+                {
+                    "name": "Automation",
+                    "roles": [
+                        {
+                            "name": "authenticated",
+                            "permissions": [
+                                {
+                                    "uri": "",
+                                    "match": "prefix",
+                                    "allow": {
+                                        "call": true,
+                                        "register": true,
+                                        "publish": true,
+                                        "subscribe": true
+                                    },
+                                    "disclose": {
+                                        "caller": false,
+                                        "publisher": false
+                                    },
+                                    "cache": true
+                                }
+                            ]
+                        }
+                    ]
+                }
+            ],
+		    "transports": [
+                {
+                    "type": "web",
+                    "endpoint": {
+                        "type": "tcp",
+                        "port": 8888
+                    },
+                    "paths": {
+                        "ws": {
+                            "type": "websocket",
+                            "auth": {
+                                "wampcra": {
+                                    "type": "static",
+                                    "users": {
+                                        "${wamp_ID}": {
+                                            "secret": "${wamp_secret}",
+                                            "role": "authenticated"
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            ]
+        }
+    ]
+}
+JsonEnd
+crossbar start &> crossbar_log.txt &
+SERVER_PID=$!
+res=110  # default to ETIMEDOUT
+c=15
+while ((c--)); do
+    if [[ -a .crossbar/node.pid ]]; then
+        echo found crossbar pid
+        res=0  # OK success
+        break
+    else
+        echo wait for crossbar to start.... $c
+        sleep 1
+    fi
+done
+
+if [ "$res" != "0" ] ; then
+    kill $SERVER_PID
+    echo timeout starting crossbar.
+    exit $res
+fi
+
+# give more time to crossbar
+sleep 3
+
+# Prepare runtime Wamp config
+cat > wampconf.json <<JsonEnd
+{
+    "ID": "${wamp_ID}", 
+    "active": true, 
+    "protocolOptions": {
+        "autoPingInterval": 60, 
+        "autoPingTimeout": 20
+
+    }, 
+    "realm": "Automation", 
+    "url": "ws://127.0.0.1:8888/ws"
+}
+JsonEnd
+
+# Start Beremiz runtime again, with wamp enabled
+$BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_service.py -c wampconf.json -s psk.txt -n test_wamp_ID -x 0 &> >(
+    echo "Start PLC loop"
+    while read line; do 
+        # Wait for server to print modified value
+        echo "PLC>> $line"
+        if [[ "$line" == "PLCobject : PLC started" ]]; then
+            echo "PLC was programmed"
+            touch ./PLC_OK
+        fi
+    done
+    echo "End PLC loop"
+) &
+PLC_PID=$!
+
+echo wait for runtime to come up
+sleep 3
+
+# Prepare test project
+cp -a $BEREMIZPATH/tests/projects/wamp .
+# place PSK so that IDE already knows runtime
+mkdir -p wamp/psk
+cp psk.txt wamp/psk/${wamp_ID}.secret
+
+# TODO: patch project's URI to connect to $BEREMIZ_LOCAL_HOST
+#       used in tests instead of 127.0.0.1
+
+# Use CLI to build transfer and start PLC
+setsid $BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \
+     --project-home wamp build transfer run &> >(
+echo "Start CLI loop"
+while read line; do 
+    # Wait for PLC runtime to output expected value on stdout
+    echo "CLI>> $line"
+    if [[ "$line" == "PLC installed successfully." ]]; then
+        echo "CLI did transfer PLC program"
+        touch ./CLI_OK
+    fi
+done
+echo "End CLI loop"
+) &
+CLI_PID=$!
+
+echo all subprocess started, start polling results
+res=110  # default to ETIMEDOUT
+c=30
+while ((c--)); do
+    if [[ -a ./CLI_OK && -a ./PLC_OK ]]; then
+        echo got results.
+        res=0  # OK success
+        break
+    else
+        echo waiting.... $c
+        sleep 1
+    fi
+done
+
+# Kill PLC and subprocess
+echo will kill PLC:$PLC_PID, SERVER:$SERVER_PID and CLI:$CLI_PID
+kill $PLC_PID 
+kill $CLI_PID 
+kill $SERVER_PID
+
+exit $res
--- a/tests/projects/wamp/.crossbar/config.json	Fri Feb 28 16:56:53 2025 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-{
-    "version": 2,
-    "workers": [
-        {
-            "type": "router",
-            "id": "automation_router",
-            "realms": [
-                {
-                    "name": "Automation",
-                    "roles": [
-                        {
-                            "name": "authenticated",
-                            "permissions": [
-                                {
-                                    "uri": "",
-                                    "match": "prefix",
-                                    "allow": {
-                                        "call": true,
-                                        "register": true,
-                                        "publish": true,
-                                        "subscribe": true
-                                    },
-                                    "disclose": {
-                                        "caller": false,
-                                        "publisher": false
-                                    },
-                                    "cache": true
-                                }
-                            ]
-                        }
-                    ]
-                }
-            ],
-		    "transports": [
-                {
-                    "type": "web",
-                    "endpoint": {
-                        "type": "tcp",
-                        "port": 8888
-                    },
-                    "paths": {
-                        "ws": {
-                            "type": "websocket",
-                            "auth": {
-                                "wampcra": {
-                                    "type": "static",
-                                    "users": {
-                                        "test_wamp_ID": {
-                                            "secret": "z+L5NUdkG18PeHjIyIuYAYMRiBjbedvokLV+a0nBiu04N9bhphzVkbQUfq5OJTJfUwaTG/GasmevRB4XNRhNJSbnYS+PDhXb06r/c+C6IfBwryqQyddiiDh6qh5QueT0eYCuxtDCZODe/TTfTI8ACetscGpCPSmlHmELFgMLCp2iYxxuUMw5ugad8E3BoehKCMkGD7Sb8dKDbrOHdvJQjKqcMkdUn4iaoMYmCFoAfqPShDx00K4O0FaU9a9/SNBN",
-                                            "role": "authenticated"
-                                        }
-                                    }
-                                }
-                            }
-                        }
-                    }
-                }
-            ]
-        }
-    ]
-}
--- a/tests/projects/wamp/README	Fri Feb 28 16:56:53 2025 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-
-/* This project contains wamp client config to be loaded at runtime startup. */
-
-    ./project_files/wampconf.json
-
-Note :
-    wampconf.json is in "Project Files", so it is copied to runtime's working 
-    directory, and then loaded after program transfer + runtime restart.
-
-/* Use pip and python virtual env to obtain crossbar */
-/* Crossbar test router configuration is available in .crossbar directory. */
-/* Start Crossbar command: (directly in project directory)*/
-
-crossbar start
-
-/* Start runtime */
-
-# steal config and PSK from config to start runtime directly with them
-cp ${BEREMIZ_DIR}/tests/projects/wamp/project_files/wampconf.json ${RUNTIME_CONF_DIR}/wampconf.json
-cp ${BEREMIZ_DIR}/tests/projects/wamp/psk/test_service_name.psk ${RUNTIME_KEYSTORE}/psk.txt
-
-# launch runtime pointing to confif ans PSK
-python ${BEREMIZ_DIR}/Beremiz_service.py -c ${RUNTIME_CONF_DIR}/wampconf.json -s ${RUNTIME_KEYSTORE}/psk.txt -n test_service_name ${PLC_WORKING_DIR}
--- a/tests/projects/wamp/beremiz.xml	Fri Feb 28 16:56:53 2025 +0100
+++ b/tests/projects/wamp/beremiz.xml	Fri Feb 28 17:00:14 2025 +0100
@@ -1,4 +1,4 @@
 <?xml version='1.0' encoding='utf-8'?>
-<BeremizRoot xmlns:xsd="http://www.w3.org/2001/XMLSchema" URI_location="WAMP://127.0.0.1:8888#Automation#wamptest">
+<BeremizRoot xmlns:xsd="http://www.w3.org/2001/XMLSchema" URI_location="WAMP://127.0.0.1:8888/ws#Automation#test_wamp_ID">
   <TargetType/>
 </BeremizRoot>
--- a/tests/projects/wamp/project_files/wampconf.json	Fri Feb 28 16:56:53 2025 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-{
-    "ID": "test_wamp_ID", 
-    "active": true, 
-    "protocolOptions": {
-        "autoPingInterval": 60, 
-        "autoPingTimeout": 20
-    }, 
-    "realm": "Automation", 
-    "url": "ws://127.0.0.1:8888/ws"
-}
--- a/tests/projects/wamp/psk/management.json	Fri Feb 28 16:56:53 2025 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-[[test_service_name, ERPC://127.0.0.1:3000, null, 25/02/24-14:51:25]]
--- a/tests/projects/wamp/psk/test_service_name.psk	Fri Feb 28 16:56:53 2025 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-test_service_name:z+L5NUdkG18PeHjIyIuYAYMRiBjbedvokLV+a0nBiu04N9bhphzVkbQUfq5OJTJfUwaTG/GasmevRB4XNRhNJSbnYS+PDhXb06r/c+C6IfBwryqQyddiiDh6qh5QueT0eYCuxtDCZODe/TTfTI8ACetscGpCPSmlHmELFgMLCp2iYxxuUMw5ugad8E3BoehKCMkGD7Sb8dKDbrOHdvJQjKqcMkdUn4iaoMYmCFoAfqPShDx00K4O0FaU9a9/SNBN