qemuimage-testlib 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. TYPE="ext3"
  17. # The folder to hold all scripts running on targets
  18. TOOLS="$POKYBASE/scripts/qemuimage-tests/tools"
  19. # Test Directory on target for testing
  20. TARGET_TEST_DIR="/opt/test"
  21. # Global variable for process id
  22. PID=0
  23. # Global variable for target ip address
  24. TARGET_IPADDR=0
  25. # common function for information print
  26. Test_Error()
  27. {
  28. echo -e "\tTest_Error: $*"
  29. }
  30. Test_Info()
  31. {
  32. echo -e "\tTest_Info: $*"
  33. }
  34. # function to update target ip address
  35. # $1 is the process id of the process, which starts the qemu target
  36. # $2 is the ip address of the target
  37. Test_Update_IPSAVE()
  38. {
  39. local pid=$1
  40. local ip_addr=$2
  41. if [ "$TEST_SERIALIZE" -eq 1 ]; then
  42. echo "$pid $ip_addr" > $TARGET_IPSAVE
  43. fi
  44. }
  45. # function to copy files from host into target
  46. # $1 is the ip address of target
  47. # $2 is the files, which need to be copied into target
  48. # $3 is the path on target, where files are copied into
  49. Test_SCP()
  50. {
  51. local ip_addr=$1
  52. local src=$2
  53. local des=$3
  54. local tmpfile=`mktemp`
  55. local timeout=60
  56. local ret=0
  57. # We use expect to interactive with target by ssh
  58. local exp_cmd=`cat << EOF
  59. eval spawn scp -o UserKnownHostsFile=$tmpfile "$src" root@$ip_addr:"$des"
  60. set timeout $time_out
  61. expect {
  62. "*assword:" { send "\r"; exp_continue}
  63. "*(yes/no)?" { send "yes\r"; exp_continue }
  64. eof { exit [ lindex [wait] 3 ] }
  65. }
  66. EOF`
  67. expect -c "$exp_cmd"
  68. ret=$?
  69. rm -rf $tmpfile
  70. return $ret
  71. }
  72. # function to run command in $ip_addr via ssh
  73. Test_SSH()
  74. {
  75. local ip_addr=$1
  76. shift
  77. local command=$@
  78. local tmpfile=`mktemp`
  79. local timeout=60
  80. local ret=0
  81. local exp_cmd=`cat << EOF
  82. eval spawn ssh -o UserKnownHostsFile=$tmpfile root@$ip_addr "$command"
  83. set timeout $time_out
  84. expect {
  85. "*assword:" { send "\r"; exp_continue}
  86. "*(yes/no)?" { send "yes\r"; exp_continue }
  87. eof { exit [ lindex [wait] 3 ] }
  88. }
  89. EOF`
  90. expect -c "$exp_cmd"
  91. ret=$?
  92. rm -rf $tmpfile
  93. return $ret
  94. }
  95. # function to check if ssh is up in $ip_addr
  96. Test_SSH_UP()
  97. {
  98. local ip_addr=$1
  99. local timeout=$2
  100. local interval=0
  101. # If TEST_SERIALIZE is set, use existing running qemu for testing
  102. if [ ${TEST_SERIALIZE} -eq 1 -a -e ${TARGET_IPSAVE} ]; then
  103. timeout=50
  104. fi
  105. while [ ${interval} -lt ${timeout} ]
  106. do
  107. Test_SSH ${ip_addr} "hostname"
  108. if [ $? -ne 0 ]; then
  109. interval=`expr $interval + 10`
  110. sleep 10
  111. else
  112. Test_Info "We can ssh on ${ip_addr} within ${interval} seconds"
  113. return 0
  114. fi
  115. done
  116. Test_Info "We can not ssh on ${ip_addr} in ${timeout} seconds"
  117. return 1
  118. }
  119. # function to prepare target test environment
  120. # $1 is the ip address of target system
  121. # $2 is the files, which needs to be copied into target
  122. Test_Target_Pre()
  123. {
  124. local ip_addr=$1
  125. local testscript=$2
  126. # Create a pre-defined folder for test scripts
  127. Test_SSH $ip_addr "mkdir -p $TARGET_TEST_DIR"
  128. if [ $? -eq 0 ]; then
  129. # Copy test scripts into target
  130. Test_SCP $ip_addr $testscript $TARGET_TEST_DIR && return 0
  131. else
  132. Test_Error "Fail to create $TARGET_TEST_DIR on target"
  133. return 1
  134. fi
  135. return 1
  136. }
  137. # function to record test result in $TEST_RESULT/testresult.log
  138. Test_Print_Result()
  139. {
  140. local PASS=0
  141. local FAIL=0
  142. local NORESULT=0
  143. if [ $2 -eq 0 ]; then
  144. PASS=1
  145. elif [ $2 -eq 1 ]; then
  146. FAIL=1
  147. else
  148. NORESULT=1
  149. fi
  150. # Format the output of the test result
  151. echo -e "$1 $PASS $FAIL $NORESULT" | awk '{printf("\t"); for(i=1;i<=NF;i++) printf("%-15s",$i); printf("\n");}' >> $TEST_RESULT/testresult.log
  152. }
  153. # Test_Kill_Qemu to kill child pid with parent pid given
  154. # $1 is qemu process id, which needs to be killed
  155. Test_Kill_Qemu()
  156. {
  157. local ret=0
  158. local ppid=0
  159. local i=0
  160. local index=0
  161. local total=0
  162. declare local pid
  163. # Check if $1 pid exists and is a qemu process
  164. ps -fp $PID | grep -iq "qemu"
  165. # Find all children pid of the pid $1
  166. if [ $? -eq 0 ]; then
  167. # Check if there is any child pid of the pid $PID
  168. ppid=$PID
  169. ps -f --ppid $ppid
  170. ret=$?
  171. while [ $ret -eq 0 ]
  172. do
  173. # If yes, get the child pid and check if the child pid has other child pid
  174. # Continue the while loop until there is no child pid found
  175. pid[$i]=`ps -f --ppid $ppid | awk '{if ($2 != "PID") print $2}'`
  176. ppid=${pid[$i]}
  177. i=$((i+1))
  178. ps -f --ppid $ppid
  179. ret=$?
  180. done
  181. # When TEST_SERIALIZE is set, qemu process will not be
  182. # killed until all the cases are finished
  183. if [ ${TEST_SERIALIZE} -eq 1 -a -e ${TEST_STATUS} ]; then
  184. index=`sed -n 2p ${TEST_STATUS} | awk '{print $3}'`
  185. total=`sed -n 2p ${TEST_STATUS} | awk '{print $4}'`
  186. if [ ${index} != ${total} ]; then
  187. Test_Info "Do not kill the qemu process and use it for later testing"
  188. Test_Update_IPSAVE $PID $TARGET_IPADDR
  189. else
  190. # If it is the last case, let's kill it
  191. while [ $i -ne 0 ]
  192. do
  193. i=$((i-1))
  194. kill ${pid[$i]}
  195. sleep 2
  196. done
  197. # Kill the parent id
  198. kill $PID
  199. fi
  200. else
  201. # Kill these children pids from the last one
  202. while [ $i -ne 0 ]
  203. do
  204. i=$((i-1))
  205. kill ${pid[$i]}
  206. sleep 2
  207. done
  208. # Kill the parent id
  209. kill $PID
  210. fi
  211. fi
  212. return
  213. }
  214. # function to check if there is any qemu process
  215. Test_Check_Qemu_UP()
  216. {
  217. local count=`ps -eo command | cut -d " " -f 1 | grep -c "\(^qemu\|.*/qemu\)"`
  218. if [ ${count} -lt 1 ]; then
  219. Test_Info "There is no Qemu process"
  220. return 1
  221. else
  222. Test_Info "There is at least one Qemu process running"
  223. return 0
  224. fi
  225. }
  226. # function to check if network is up
  227. Test_Check_IP_UP()
  228. {
  229. ping -c1 $1
  230. if [ $? -ne 0 ]; then
  231. Test_Info "IP $1 is not up"
  232. return 1
  233. else
  234. Test_Info "IP $1 is up"
  235. return 0
  236. fi
  237. }
  238. # function to find kernel/rootfs image
  239. Test_Find_Image()
  240. {
  241. where=""
  242. kernel=""
  243. arch=""
  244. target=""
  245. extension=""
  246. rootfs=""
  247. while getopts "l:k:a:t:" Option
  248. do
  249. case $Option in
  250. l) where="$OPTARG"
  251. ;;
  252. k) kernel="$OPTARG"
  253. extension="bin"
  254. ;;
  255. a) arch="$OPTARG"
  256. ;;
  257. t) target="$OPTARG"
  258. extension="ext3"
  259. ;;
  260. *) echo "invalid option: -$Option" && return 1
  261. ;;
  262. esac
  263. done
  264. if [ ! -z $kernel ]; then
  265. if [ -L ${where}/${kernel}-${arch}.${extension} ]; then
  266. echo ${where}/${kernel}-${arch}.${extension}
  267. return 0
  268. else
  269. for i in `dir ${where}`
  270. do
  271. # Exclude qemux86-64 when target is qemux86
  272. echo $i | grep "${kernel}.*${arch}.*\.${extension}" | grep -qv "${kernel}.*${arch}-64.*\.${extension}"
  273. if [ $? -eq 0 ]; then
  274. echo ${where}/${i}
  275. return 0
  276. fi
  277. done
  278. return 1
  279. fi
  280. fi
  281. if [ ! -z $target ]; then
  282. if [ -L ${where}/${target}-${arch}.${extension} ]; then
  283. rootfs=`readlink -f ${where}/${target}-${arch}.${extension}`
  284. echo ${rootfs}
  285. return 0
  286. else
  287. for i in `dir ${where}`
  288. do
  289. # Exclude qemux86-64 when target is qemux86
  290. echo $i | grep "${target}-${arch}.*\.${extension}" | grep -qv "${target}-${arch}-64.*\.${extension}"
  291. if [ $? -eq 0 ]; then
  292. echo ${where}/${i}
  293. return 0
  294. fi
  295. done
  296. return 1
  297. fi
  298. fi
  299. return 1
  300. }
  301. # function to parse IP address of target
  302. # $1 is the pid of qemu startup process
  303. Test_Fetch_Target_IP()
  304. {
  305. local opid=$1
  306. local ppid=0
  307. local ip_addr=0
  308. local i=0
  309. declare local pid
  310. # Check if $1 pid exists and contains ipaddr of target
  311. ps -fp $opid | grep -oq "192\.168\.7\.[0-9]*::"
  312. # Find all children pid of the pid $1
  313. # and check if they contain ipaddr of target
  314. if [ $? -ne 0 ]; then
  315. # Check if there is any child pid of the pid $1
  316. ppid=$opid
  317. ps -f --ppid $ppid > /dev/zero
  318. ret=$?
  319. while [ $ret -eq 0 ]
  320. do
  321. # If yes, get the child pid and check if the child pid has other child pid
  322. # Continue the while loop until there is no child pid found
  323. pid[$i]=`ps -f --ppid $ppid | awk '{if ($2 != "PID") print $2}'`
  324. ppid=${pid[$i]}
  325. i=$((i+1))
  326. ps -f --ppid $ppid > /dev/zero
  327. ret=$?
  328. done
  329. # Check these children pids, if they have ipaddr included in command line
  330. while [ $i -ne 0 ]
  331. do
  332. i=$((i-1))
  333. ps -fp ${pid[$i]} | grep -oq "192\.168\.7\.[0-9]*::"
  334. if [ $? -eq 0 ]; then
  335. ip_addr=`ps -fp ${pid[$i]} | grep -o "192\.168\.7\.[0-9]*::" | awk -F":" '{print $1}'`
  336. fi
  337. sleep 1
  338. done
  339. else
  340. ip_addr=`ps -fp $opid | grep -o "192\.168\.7\.[0-9]*::" | awk -F":" '{print $1}'`
  341. fi
  342. echo $ip_addr
  343. return
  344. }
  345. # function to check if qemu and its network
  346. Test_Create_Qemu()
  347. {
  348. local timeout=$1
  349. local ret=1
  350. local up_time=0
  351. which poky-qemu
  352. if [ $? -eq 0 ]; then
  353. RUNQEMU=`which poky-qemu`
  354. else
  355. Test_Error "Can not find poky-qemu in \$PATH, return fail"
  356. exit 1
  357. fi
  358. if [ "$QEMUARCH" = "qemux86" -o "$QEMUARCH" = "qemux86-64" ]; then
  359. KERNEL=$(Test_Find_Image -l ${DEPLOY_DIR}/images -k bzImage -a ${QEMUARCH})
  360. elif [ "$QEMUARCH" = "qemuarm" -o "$QEMUARCH" = "spitz" -o "$QEMUARCH" = "borzoi" -o "$QEMUARCH" = "akita" -o "$QEMUARCH" = "nokia800" -o "$QEMUARCH" = "qemuppc" ]; then
  361. KERNEL=$(Test_Find_Image -l ${DEPLOY_DIR}/images -k zImage -a ${QEMUARCH})
  362. elif [ "$QEMUARCH" = "qemumips" ]; then
  363. KERNEL=$(Test_Find_Image -l ${DEPLOY_DIR}/images -k vmlinux -a ${QEMUARCH})
  364. fi
  365. # If there is no kernel image found, return failed directly
  366. if [ $? -eq 1 ]; then
  367. Test_Info "No kernel image file found under ${DEPLOY_DIR}/images for ${QEMUARCH}, pls. have a check"
  368. return $ret
  369. fi
  370. ROOTFS_IMAGE=$(Test_Find_Image -l ${DEPLOY_DIR}/images -t ${QEMUTARGET} -a ${QEMUARCH})
  371. # If there is no rootfs image found, return failed directly
  372. if [ $? -eq 1 ]; then
  373. Test_Info "No ${QEMUTARGET} rootfs image file found under ${DEPLOY_DIR}/images for ${QEMUARCH}, pls. have a check"
  374. return $ret
  375. fi
  376. TEST_ROOTFS_IMAGE="${TEST_TMP}/${QEMUTARGET}-${QEMUARCH}-test.ext3"
  377. CP=`which cp`
  378. # When TEST_SERIALIZE is set, we use the existing image under tmp folder
  379. if [ ${TEST_SERIALIZE} -eq 1 -a -e "$TARGET_IPSAVE" ]; then
  380. # If TARGET_IPSAVE exists, check PID of the qemu process from it
  381. PID=`awk '{print $1}' $TARGET_IPSAVE`
  382. timeout=50
  383. else
  384. rm -rf $TEST_ROOTFS_IMAGE
  385. $CP $ROOTFS_IMAGE $TEST_ROOTFS_IMAGE
  386. if [ $? -ne 0 ]; then
  387. Test_Info "Image ${ROOTFS_IMAGE} copy to ${TEST_ROOTFS_IMAGE} failed, return fail"
  388. return $ret
  389. fi
  390. export MACHINE=$QEMUARCH
  391. # Create Qemu in localhost VNC Port 1
  392. echo "Running xterm -display ${DISPLAY} -e 'BUILDDIR=${TOPDIR} ${RUNQEMU} ${KERNEL} ${TEST_ROOTFS_IMAGE}' &"
  393. xterm -display ${DISPLAY} -e "BUILDDIR=${TOPDIR} ${RUNQEMU} ${KERNEL} ${TEST_ROOTFS_IMAGE}" &
  394. # Get the pid of the xterm processor, which will be used in Test_Kill_Qemu
  395. PID=$!
  396. fi
  397. while [ ${up_time} -lt 10 ]
  398. do
  399. Test_Check_Qemu_UP
  400. if [ $? -ne 0 ]; then
  401. Test_Info "Wait for qemu up..."
  402. up_time=`expr $up_time + 5`
  403. sleep 5
  404. else
  405. Test_Info "Begin to check if qemu network is up"
  406. break
  407. fi
  408. done
  409. # Parse IP address of target from the qemu command line
  410. if [ ${up_time} -lt ${timeout} ]; then
  411. sleep 5
  412. TARGET_IPADDR=`Test_Fetch_Target_IP $PID`
  413. # If IP address is 0, means there is no qemu process found
  414. if [ ${TARGET_IPADDR} == "0" ]; then
  415. Test_Info "There is no qemu process or qemu ip address found, return failed"
  416. return $ret
  417. fi
  418. fi
  419. while [ ${up_time} -lt ${timeout} ]
  420. do
  421. Test_Check_IP_UP ${TARGET_IPADDR}
  422. if [ $? -eq 0 ]; then
  423. Test_Info "Qemu Network is up, ping with ${TARGET_IPADDR} is OK within ${up_time} seconds"
  424. ret=0
  425. break
  426. else
  427. Test_Info "Wait for Qemu Network up"
  428. up_time=`expr $up_time + 5`
  429. sleep 5
  430. fi
  431. done
  432. if [ $ret -eq 0 ]; then
  433. Test_Info "Qemu and its network is up"
  434. return $ret
  435. else
  436. Test_Info "Qemu or its network is not up in ${timeout} seconds"
  437. Test_Update_IPSAVE $PID $TARGET_IPADDR
  438. return $ret
  439. fi
  440. }