|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +function cleanup { |
| 4 | + sleep 1 |
| 5 | + pkill -9 semu |
| 6 | +} |
| 7 | + |
| 8 | +function ASSERT { |
| 9 | + $* |
| 10 | + local RES=$? |
| 11 | + if [ $RES -ne 0 ]; then |
| 12 | + echo 'Assert failed: "' $* '"' |
| 13 | + exit $RES |
| 14 | + fi |
| 15 | +} |
| 16 | + |
| 17 | +cleanup |
| 18 | + |
| 19 | +# macOS needs more time to boot compared to Linux, so the timeout is set to |
| 20 | +# 600 seconds for macOS to handle the longer startup. For Linux, 90 seconds |
| 21 | +# is sufficient due to its faster boot process. |
| 22 | +UNAME_S=$(uname -s) |
| 23 | +if [[ ${UNAME_S} == "Darwin" ]]; then |
| 24 | + TIMEOUT=600 |
| 25 | +else # Linux |
| 26 | + TIMEOUT=30 |
| 27 | +fi |
| 28 | + |
| 29 | +function TEST_NETDEV { |
| 30 | + local NETDEV=$1 |
| 31 | + local CMD_PREFIX="" |
| 32 | + |
| 33 | + if [ $NETDEV == tap ]; then |
| 34 | + CMD_PREFIX="sudo " |
| 35 | + fi |
| 36 | + |
| 37 | +ASSERT expect <<DONE |
| 38 | + set timeout ${TIMEOUT} |
| 39 | + spawn ${CMD_PREFIX}make check NETDEV=${NETDEV} |
| 40 | + expect "buildroot login:" { send "root\n" } timeout { exit 1 } |
| 41 | + expect "# " { send "uname -a\n" } timeout { exit 2 } |
| 42 | + if { "$NETDEV" == "tap" } { |
| 43 | + exec sudo ip addr add 192.168.10.1/24 dev tap0 |
| 44 | + exec sudo ip link set tap0 up |
| 45 | + expect "riscv32 GNU/Linux" { send "ip l set eth0 up\n" } timeout { exit 3 } |
| 46 | + expect "# " { send "ip a add 192.168.10.2/24 dev eth0\n" } |
| 47 | + expect "# " { send "ping -c 3 192.168.10.1\n" } |
| 48 | + expect "3 packets transmitted, 3 packets received, 0% packet loss" { } timeout { exit 4 } |
| 49 | + } elseif { "$NETDEV" == "user" } { |
| 50 | + # Test slirp configuration |
| 51 | + expect "riscv32 GNU/Linux" { send "\x01"; send "x" } timeout { exit 3 } |
| 52 | + } |
| 53 | +DONE |
| 54 | +} |
| 55 | + |
| 56 | +# Network devices |
| 57 | +NETWORK_DEVICES=(tap user) |
| 58 | + |
| 59 | +for NETDEV in "${NETWORK_DEVICES[@]}"; do |
| 60 | + cleanup |
| 61 | + echo "Test network device: $NETDEV" |
| 62 | + TEST_NETDEV $NETDEV |
| 63 | +done |
| 64 | + |
| 65 | +ret=$? |
| 66 | +cleanup |
| 67 | + |
| 68 | +MESSAGES=("OK!" \ |
| 69 | + "Fail to boot" \ |
| 70 | + "Fail to login" \ |
| 71 | + "Fail to run commands" \ |
| 72 | + "Fail to transfer packet" \ |
| 73 | +) |
| 74 | + |
| 75 | +COLOR_G='\e[32;01m' # Green |
| 76 | +COLOR_N='\e[0m' |
| 77 | +printf "\n[ ${COLOR_G}${MESSAGES[$ret]}${COLOR_N} ]\n" |
| 78 | + |
| 79 | +exit ${ret} |
0 commit comments