Merge pull request #212 from cgeoffroy/pr/fix_manualexamples_textoverlap
[osm/vim-emu.git] / utils / ci / check_manual_usage_example.sh
index 5bf4e84..2d43e36 100755 (executable)
@@ -5,9 +5,31 @@ set -e
 set -x
 
 W() {
-    # Wait until a line appears in the screen session
-    local T=${2:-10s}
-    timeout -k 3s ${T} grep -q "^${1}" <(tail -f screenlog.0)
+    # Wait until a line appears in the screen session.
+    # It starts from the beginning of the log and exits after the first match.
+    local T=${2:-20s}
+    #timeout -k 3s ${T} stdbuf -o0 grep -q -m 1 "^${1}" <(tail -F -n+0 screenlog.0)
+    # (HACK) As Jenkins blocks subshell, we must use an intermediate script
+    local SUBF=$(mktemp)
+    chmod +x ${SUBF}
+    cat > ${SUBF} <<- EOF
+       #!/bin/bash -e
+       while true; do
+           if strings screenlog.0 | grep -m 1 "\${1}"; then
+               exit 0
+           fi
+           sleep 0.5s
+       done
+       EOF
+    local RES=0
+    timeout -k 3s ${T} ${SUBF} "${1}" || RES=$?
+    rm -f ${SUBF}
+    if [ ! "$RES" = "0" ]; then
+        sync
+        echo -e "\n\n\n(Debug) Error while waiting for a pattern to appear in screenlog.0\n\n\n"
+        strings screenlog.0
+    fi
+    return ${RES}
 }
 
 Cmd() {
@@ -22,18 +44,24 @@ if ! screen --version | grep 'Screen version'; then
     sudo apt-get install -y screen
     screen --version | grep 'Screen version'
 fi
+if ! timeout --version; then
+    # Install coreutils for the timeout command
+    sudo apt-get update -qq -y
+    sudo apt-get install -y coreutils
+    timeout --version
+fi
 # Initial cleanup
-pkill 'screen' || true
+pkill -f 'SCREEN -L -S sonemu' || true
 screen -wipe || true
 rm -f screenlog.0
 
 
 # Start containernet with a topology
-screen -L -S sonemu -d -m python src/emuvim/examples/simple_topology.py
+screen -L -S sonemu -d -m sudo python src/emuvim/examples/simple_topology.py
 # Setup screen for immediate flusing
 screen -S sonemu -X logfile flush 0
 # Wait for the cli to start
-W '*** Starting CLI:'
+W '^*** Starting CLI:' 60s
 # Print nodes
 Cmd 'nodes'
 # Start vnf1
@@ -42,20 +70,23 @@ son-emu-cli compute start -d datacenter1 -n vnf1 && sleep 1s
 son-emu-cli compute start -d datacenter1 -n vnf2 && sleep 1s
 # List compute nodes
 son-emu-cli compute list && sleep 1s
+sync # avoid text overlapping
 # Gather some infos
+Cmd 'sh sync'
 Cmd 'sh echo "... starting various checks"'
-Cmd 'vnf1 ifconfig && echo "... checked vnf1"'
-W "... checked vnf1"
-Cmd 'vnf2 ifconfig && echo "... checked vnf2"'
-W "... checked vnf2"
+sync # avoid text overlapping
+Cmd 'vnf1 ifconfig && echo -e "\\n... checked vnf1"'
+W "^... checked vnf1"
+Cmd 'vnf2 ifconfig && echo -e "\\n... checked vnf2"'
+W "^... checked vnf2"
 # Try to ping vnfs
-Cmd 'vnf1 ping -c 2 vnf2 || echo "... checked ping"'
-W "... checked ping" 20s
+Cmd 'vnf1 ping -c 2 vnf2 && echo -e "\\n... checked ping"'
+W "^... checked ping" 20s
 Cmd 'quit'
 # Wait for sonemu to end
 W '*** Done'
 
-echo -e '\n\n************i****** Result ******************\n\n'
+echo -e '\n\n******************* Result ******************\n\n'
 strings screenlog.0
 echo -e '\n\n*********************************************\n\n'
 
@@ -68,7 +99,3 @@ else
     echo 'Ping is broken !'
     exit 1
 fi
-
-
-# Cleanup
-rm -f screenlog.0