diff options
Diffstat (limited to 'scripts/qemuimage-testlib')
| -rw-r--r-- | scripts/qemuimage-testlib | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/scripts/qemuimage-testlib b/scripts/qemuimage-testlib new file mode 100644 index 0000000000..4e27f8bc6f --- /dev/null +++ b/scripts/qemuimage-testlib | |||
| @@ -0,0 +1,202 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | # Common function for test | ||
| 3 | # Expect should be installed for SSH Testing | ||
| 4 | # To execute `runqemu`, NOPASSWD needs to be set in /etc/sudoers for user | ||
| 5 | # For example, for user "builder", /etc/sudoers can be like following: | ||
| 6 | # ######### | ||
| 7 | # #Members of the admin group may gain root privileges | ||
| 8 | # %builder ALL=(ALL) NOPASSWD: NOPASSWD: ALL | ||
| 9 | # ######### | ||
| 10 | # | ||
| 11 | # Author: Jiajun Xu <jiajun.xu@intel.com> | ||
| 12 | # | ||
| 13 | # This file is licensed under the GNU General Public License, | ||
| 14 | # Version 2. | ||
| 15 | # | ||
| 16 | |||
| 17 | TYPE="ext3" | ||
| 18 | |||
| 19 | # common function for information print | ||
| 20 | Test_Error() | ||
| 21 | { | ||
| 22 | echo -e "\tTest_Error: $*" | ||
| 23 | } | ||
| 24 | |||
| 25 | Test_Info() | ||
| 26 | { | ||
| 27 | echo -e "\tTest_Info: $*" | ||
| 28 | } | ||
| 29 | |||
| 30 | # function to run command in $ip_addr via ssh | ||
| 31 | Test_SSH() | ||
| 32 | { | ||
| 33 | local ip_addr=$1 | ||
| 34 | shift | ||
| 35 | local command=$@ | ||
| 36 | local tmpfile=`mktemp` | ||
| 37 | local timeout=60 | ||
| 38 | local ret=0 | ||
| 39 | local exp_cmd=`cat << EOF | ||
| 40 | eval spawn ssh -o UserKnownHostsFile=$tmpfile root@$ip_addr "$command" | ||
| 41 | set timeout $time_out | ||
| 42 | expect { | ||
| 43 | "*assword:" { send "\r"; exp_continue} | ||
| 44 | "*(yes/no)?" { send "yes\r"; exp_continue } | ||
| 45 | eof { exit [ lindex [wait] 3 ] } | ||
| 46 | } | ||
| 47 | EOF` | ||
| 48 | expect -c "$exp_cmd" | ||
| 49 | ret=$? | ||
| 50 | rm -rf $tmpfile | ||
| 51 | return $ret | ||
| 52 | } | ||
| 53 | |||
| 54 | # function to check if ssh is up in $ip_addr | ||
| 55 | Test_SSH_UP() | ||
| 56 | { | ||
| 57 | local ip_addr=$1 | ||
| 58 | local timeout=$2 | ||
| 59 | local interval=0 | ||
| 60 | |||
| 61 | while [ ${interval} -lt ${timeout} ] | ||
| 62 | do | ||
| 63 | Test_SSH ${ip_addr} "hostname" | ||
| 64 | if [ $? -ne 0 ]; then | ||
| 65 | interval=`expr $interval + 10` | ||
| 66 | sleep 10 | ||
| 67 | else | ||
| 68 | Test_Info "We can ssh on ${ip_addr} now" | ||
| 69 | return 0 | ||
| 70 | fi | ||
| 71 | |||
| 72 | done | ||
| 73 | |||
| 74 | Test_Info "We can not ssh on ${ip_addr} in ${timeout}" | ||
| 75 | return 1 | ||
| 76 | } | ||
| 77 | |||
| 78 | # function to record test result in $TEST_RESULT/testresult.log | ||
| 79 | Test_Print_Result() | ||
| 80 | { | ||
| 81 | local PASS=0 | ||
| 82 | local FAIL=0 | ||
| 83 | local NORESULT=0 | ||
| 84 | if [ $2 -eq 0 ]; then | ||
| 85 | PASS=1 | ||
| 86 | elif [ $2 -eq 1 ]; then | ||
| 87 | FAIL=1 | ||
| 88 | else | ||
| 89 | NORESULT=1 | ||
| 90 | fi | ||
| 91 | |||
| 92 | echo -e "\t$1\t\t$PASS\t$FAIL\t$NORESULT" >> $TEST_RESULT/testresult.log | ||
| 93 | } | ||
| 94 | |||
| 95 | # function to kill qemu | ||
| 96 | Test_Kill_Qemu() | ||
| 97 | { | ||
| 98 | sudo pkill -9 qemu | ||
| 99 | return $? | ||
| 100 | } | ||
| 101 | |||
| 102 | # function to check if there is any qemu process | ||
| 103 | Test_Check_Qemu_UP() | ||
| 104 | { | ||
| 105 | local count=`ps -ef | grep -c qemu` | ||
| 106 | if [ ${count} -lt 2 ]; then | ||
| 107 | Test_Info "There is no Qemu process" | ||
| 108 | return 1 | ||
| 109 | else | ||
| 110 | Test_Info "There is at least Qemu process running" | ||
| 111 | return 0 | ||
| 112 | fi | ||
| 113 | } | ||
| 114 | |||
| 115 | # function to check if network is up | ||
| 116 | Test_Check_IP_UP() | ||
| 117 | { | ||
| 118 | ping -c1 $1 | ||
| 119 | if [ $? -ne 0 ]; then | ||
| 120 | Test_Info "IP $1 is not up" | ||
| 121 | return 1 | ||
| 122 | else | ||
| 123 | Test_Info "IP $1 is up" | ||
| 124 | return 0 | ||
| 125 | fi | ||
| 126 | } | ||
| 127 | |||
| 128 | # function to check if qemu and its network | ||
| 129 | Test_Create_Qemu() | ||
| 130 | { | ||
| 131 | local ret=1 | ||
| 132 | local up_time=0 | ||
| 133 | local ip_addr=$1 | ||
| 134 | local timeout=$2 | ||
| 135 | local kernel="" | ||
| 136 | local rootfs="" | ||
| 137 | |||
| 138 | which poky-qemu | ||
| 139 | if [ $? -eq 0 ]; then | ||
| 140 | RUNQEMU=`which poky-qemu` | ||
| 141 | else | ||
| 142 | Test_Error "Can not find poky-qemu in \$PATH, return fail" | ||
| 143 | exit 1 | ||
| 144 | fi | ||
| 145 | |||
| 146 | if [ "$QEMUARCH" = "qemux86" ]; then | ||
| 147 | KERNEL=${DEPLOY_DIR}/images/bzImage-${QEMUARCH}.bin | ||
| 148 | elif [ "$QEMUARCH" = "qemuarm" -o "$QEMUARCH" = "spitz" -o "$QEMUARCH" = "borzoi" -o "$QEMUARCH" = "akita" -o "$QEMUARCH" = "nokia800" ]; then | ||
| 149 | KERNEL=${DEPLOY_DIR}/images/zImage-${QEMUARCH}.bin | ||
| 150 | fi | ||
| 151 | |||
| 152 | ROOTFS_IMAGE=`readlink -f ${DEPLOY_DIR}/images/${QEMUTARGET}-${QEMUARCH}.ext3` | ||
| 153 | TEST_ROOTFS_IMAGE="${TEST_TMP}/${QEMUTARGET}-${QEMUARCH}-test.ext3" | ||
| 154 | |||
| 155 | CP=`which cp` | ||
| 156 | if [ -e "$TEST_ROOTFS_IMAGE" ]; then | ||
| 157 | rm -rf $TEST_ROOTFS_IMAGE | ||
| 158 | fi | ||
| 159 | $CP $ROOTFS_IMAGE $TEST_ROOTFS_IMAGE | ||
| 160 | |||
| 161 | export MACHINE=$QEMUARCH | ||
| 162 | # Create Qemu in localhost VNC Port 1 | ||
| 163 | |||
| 164 | xterm -display ${DISPLAY} -e "${RUNQEMU} ${KERNEL} ${TEST_ROOTFS_IMAGE}" & | ||
| 165 | |||
| 166 | sleep 5 | ||
| 167 | |||
| 168 | while [ ${up_time} -lt ${timeout} ] | ||
| 169 | do | ||
| 170 | Test_Check_Qemu_UP | ||
| 171 | if [ $? -ne 0 ]; then | ||
| 172 | Test_Info "Wait for qemu up..." | ||
| 173 | up_time=`expr $up_time + 5` | ||
| 174 | sleep 5 | ||
| 175 | else | ||
| 176 | Test_Info "Begin to check if qemu network is up" | ||
| 177 | break | ||
| 178 | fi | ||
| 179 | done | ||
| 180 | |||
| 181 | while [ ${up_time} -lt ${timeout} ] | ||
| 182 | do | ||
| 183 | Test_Check_IP_UP ${ip_addr} | ||
| 184 | if [ $? -eq 0 ]; then | ||
| 185 | Test_Info "Qemu Network is up, ping with ${ip_addr} is OK" | ||
| 186 | ret=0 | ||
| 187 | break | ||
| 188 | else | ||
| 189 | Test_Info "Wait for Qemu Network up" | ||
| 190 | up_time=`expr $up_time + 5` | ||
| 191 | sleep 5 | ||
| 192 | fi | ||
| 193 | done | ||
| 194 | |||
| 195 | if [ $ret -eq 0 ]; then | ||
| 196 | Test_Info "Qemu and its network is up" | ||
| 197 | return $ret | ||
| 198 | else | ||
| 199 | Test_Info "Qemu or its network is not up in ${timeout}" | ||
| 200 | return $ret | ||
| 201 | fi | ||
| 202 | } | ||
