| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | export DOCKER_HOST="unix:///var/run/docker.sock" |
| 3 | |
| 4 | set -e |
| 5 | set -x |
| 6 | |
| 7 | W() { |
| cgeoffroy | 349dc0b | 2017-03-15 16:24:58 +0100 | [diff] [blame] | 8 | # Wait until a line appears in the screen session. |
| 9 | # It starts from the beginning of the log and exits after the first match. |
| 10 | local T=${2:-20s} |
| 11 | #timeout -k 3s ${T} stdbuf -o0 grep -q -m 1 "^${1}" <(tail -F -n+0 screenlog.0) |
| 12 | # (HACK) As Jenkins blocks subshell, we must use an intermediate script |
| 13 | local SUBF=$(mktemp) |
| 14 | chmod +x ${SUBF} |
| 15 | cat > ${SUBF} <<- EOF |
| 16 | #!/bin/bash -e |
| 17 | while true; do |
| 18 | if strings screenlog.0 | grep -m 1 "\${1}"; then |
| 19 | exit 0 |
| 20 | fi |
| 21 | sleep 0.5s |
| 22 | done |
| 23 | EOF |
| cgeoffroy | 0412c62 | 2017-04-11 10:58:53 +0200 | [diff] [blame] | 24 | local RES=0 |
| 25 | timeout -k 3s ${T} ${SUBF} "${1}" || RES=$? |
| cgeoffroy | 349dc0b | 2017-03-15 16:24:58 +0100 | [diff] [blame] | 26 | rm -f ${SUBF} |
| cgeoffroy | 0412c62 | 2017-04-11 10:58:53 +0200 | [diff] [blame] | 27 | if [ ! "$RES" = "0" ]; then |
| 28 | sync |
| cgeoffroy | 5ca7aae | 2017-04-11 11:20:23 +0200 | [diff] [blame] | 29 | echo -e "\n\n\n(Debug) Error while waiting for a pattern to appear in screenlog.0\n\n\n" |
| cgeoffroy | 0412c62 | 2017-04-11 10:58:53 +0200 | [diff] [blame] | 30 | strings screenlog.0 |
| 31 | fi |
| cgeoffroy | 349dc0b | 2017-03-15 16:24:58 +0100 | [diff] [blame] | 32 | return ${RES} |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Cmd() { |
| 36 | # Send a command to the screen session, aka into the containernet prompt |
| 37 | screen -S sonemu -X stuff "${1}^M" |
| 38 | } |
| 39 | |
| cgeoffroy | 7bb91b1 | 2017-04-12 10:49:52 +0200 | [diff] [blame] | 40 | Vnf() { |
| 41 | # Send a command inside the vnf1 container |
| 42 | docker exec -t "mn.${1}" /bin/bash -c "${2}" && sync |
| 43 | } |
| 44 | |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 45 | |
| 46 | if ! screen --version | grep 'Screen version'; then |
| 47 | # Install screen and do an initial cleanup |
| 48 | sudo apt-get update -qq -y |
| 49 | sudo apt-get install -y screen |
| 50 | screen --version | grep 'Screen version' |
| 51 | fi |
| cgeoffroy | ae307a9 | 2017-03-17 10:48:26 +0100 | [diff] [blame] | 52 | if ! timeout --version; then |
| 53 | # Install coreutils for the timeout command |
| 54 | sudo apt-get update -qq -y |
| 55 | sudo apt-get install -y coreutils |
| 56 | timeout --version |
| 57 | fi |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 58 | # Initial cleanup |
| cgeoffroy | e2a4849 | 2017-04-12 10:48:34 +0200 | [diff] [blame] | 59 | pkill --signal KILL -f 'SCREEN -L -S sonemu' || true |
| 60 | sleep 1s |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 61 | screen -wipe || true |
| 62 | rm -f screenlog.0 |
| 63 | |
| 64 | |
| 65 | # Start containernet with a topology |
| cgeoffroy | 4b9ba68 | 2017-03-17 12:51:18 +0100 | [diff] [blame] | 66 | screen -L -S sonemu -d -m sudo python src/emuvim/examples/simple_topology.py |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 67 | # Setup screen for immediate flusing |
| 68 | screen -S sonemu -X logfile flush 0 |
| 69 | # Wait for the cli to start |
| cgeoffroy | 736c4aa | 2017-03-17 11:48:15 +0100 | [diff] [blame] | 70 | W '^*** Starting CLI:' 60s |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 71 | # Print nodes |
| 72 | Cmd 'nodes' |
| 73 | # Start vnf1 |
| 74 | son-emu-cli compute start -d datacenter1 -n vnf1 && sleep 1s |
| 75 | # Start vnf2 |
| 76 | son-emu-cli compute start -d datacenter1 -n vnf2 && sleep 1s |
| 77 | # List compute nodes |
| 78 | son-emu-cli compute list && sleep 1s |
| cgeoffroy | 6f86be7 | 2017-03-17 13:26:50 +0100 | [diff] [blame] | 79 | sync # avoid text overlapping |
| cgeoffroy | 1d6978b | 2017-04-11 11:05:56 +0200 | [diff] [blame] | 80 | # Gather some infos |
| cgeoffroy | 75591cd | 2017-04-11 11:21:27 +0200 | [diff] [blame] | 81 | Cmd 'sh sync' |
| 82 | Cmd 'sh echo "... starting various checks"' |
| cgeoffroy | 7bb91b1 | 2017-04-12 10:49:52 +0200 | [diff] [blame] | 83 | sync # avoid text overlappin |
| 84 | Cmd 'links' |
| 85 | Vnf vnf1 'ifconfig' |
| 86 | Vnf vnf2 'ifconfig' |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 87 | # Try to ping vnfs |
| cgeoffroy | 7bb91b1 | 2017-04-12 10:49:52 +0200 | [diff] [blame] | 88 | IP_2=$(Vnf vnf2 'ip -f inet -o addr show vnf2-eth0' | cut -d\ -f 7 | cut -d/ -f 1) |
| 89 | # IP_1=$(Vnf vnf1 'ip -f inet -o addr show vnf1-eth0' | cut -d\ -f 7 | cut -d/ -f 1) |
| 90 | OUTPUT_A=$(Vnf vnf1 "ping -v -c 2 ${IP_2}") |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 91 | Cmd 'quit' |
| 92 | # Wait for sonemu to end |
| cgeoffroy | 1d6978b | 2017-04-11 11:05:56 +0200 | [diff] [blame] | 93 | W '*** Done' |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 94 | |
| cgeoffroy | 349dc0b | 2017-03-15 16:24:58 +0100 | [diff] [blame] | 95 | echo -e '\n\n******************* Result ******************\n\n' |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 96 | strings screenlog.0 |
| 97 | echo -e '\n\n*********************************************\n\n' |
| 98 | |
| 99 | |
| 100 | # Check the ping result |
| cgeoffroy | 7bb91b1 | 2017-04-12 10:49:52 +0200 | [diff] [blame] | 101 | if echo ${OUTPUT_A} | grep ', 2 received'; then |
| cgeoffroy | 549e20f | 2017-03-03 16:10:40 +0100 | [diff] [blame] | 102 | echo 'No problems detected' |
| 103 | exit 0 |
| 104 | else |
| 105 | echo 'Ping is broken !' |
| 106 | exit 1 |
| 107 | fi |