qemuimage-testlib 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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="$COREBASE/scripts/qemuimage-tests/tools"
  19. # The folder to hold all projects for toolchain testing
  20. TOOLCHAIN_PROJECTS="$COREBASE/scripts/qemuimage-tests/toolchain_projects"
  21. # Test Directory on target for testing
  22. TARGET_TEST_DIR="/opt/test"
  23. # Global variable for process id
  24. PID=0
  25. # Global variable for target ip address
  26. TARGET_IPADDR=0
  27. # Global variable for test project version during toolchain test
  28. # Version of cvs is 1.12.13
  29. # Version of iptables is 1.4.11
  30. # Version of sudoku-savant is 1.3
  31. PROJECT_PV=0
  32. # Global variable for test project download URL during toolchain test
  33. # URL of cvs is http://ftp.gnu.org/non-gnu/cvs/source/feature/1.12.13/cvs-1.12.13.tar.bz2
  34. # URL of iptables is http://netfilter.org/projects/iptables/files/iptables-1.4.11.tar.bz2
  35. # URL of sudoku-savant is http://downloads.sourceforge.net/project/sudoku-savant/sudoku-savant/sudoku-savant-1.3/sudoku-savant-1.3.tar.bz2
  36. PROJECT_DOWNLOAD_URL=0
  37. # SDK folder to hold toolchain tarball
  38. TOOLCHAIN_DIR="${DEPLOY_DIR}/sdk"
  39. # Toolchain test folder to hold extracted toolchain tarball
  40. TOOLCHAIN_TEST="/opt"
  41. # common function for information print
  42. Test_Error()
  43. {
  44. echo -e "\tTest_Error: $*"
  45. }
  46. Test_Info()
  47. {
  48. echo -e "\tTest_Info: $*"
  49. }
  50. # function to update target ip address
  51. # $1 is the process id of the process, which starts the qemu target
  52. # $2 is the ip address of the target
  53. Test_Update_IPSAVE()
  54. {
  55. local pid=$1
  56. local ip_addr=$2
  57. if [ "$TEST_SERIALIZE" -eq 1 ]; then
  58. echo "$pid $ip_addr" > $TARGET_IPSAVE
  59. fi
  60. }
  61. # function to copy files from host into target
  62. # $1 is the ip address of target
  63. # $2 is the files, which need to be copied into target
  64. # $3 is the path on target, where files are copied into
  65. Test_SCP()
  66. {
  67. local ip_addr=$1
  68. local src=$2
  69. local des=$3
  70. local tmpfile=`mktemp`
  71. local time_out=60
  72. local ret=0
  73. # We use expect to interactive with target by ssh
  74. local exp_cmd=`cat << EOF
  75. eval spawn scp -o UserKnownHostsFile=$tmpfile "$src" root@$ip_addr:"$des"
  76. set timeout $time_out
  77. expect {
  78. "*assword:" { send "\r"; exp_continue}
  79. "*(yes/no)?" { send "yes\r"; exp_continue }
  80. eof { exit [ lindex [wait] 3 ] }
  81. }
  82. EOF`
  83. expect -c "$exp_cmd"
  84. ret=$?
  85. rm -rf $tmpfile
  86. return $ret
  87. }
  88. # function to run command in $ip_addr via ssh
  89. Test_SSH()
  90. {
  91. local ip_addr=$1
  92. shift
  93. local command=$@
  94. local tmpfile=`mktemp`
  95. local time_out=60
  96. local ret=0
  97. local exp_cmd=`cat << EOF
  98. eval spawn ssh -o UserKnownHostsFile=$tmpfile root@$ip_addr "$command"
  99. set timeout $time_out
  100. expect {
  101. "*assword:" { send "\r"; exp_continue}
  102. "*(yes/no)?" { send "yes\r"; exp_continue }
  103. eof { exit [ lindex [wait] 3 ] }
  104. }
  105. EOF`
  106. expect -c "$exp_cmd"
  107. ret=$?
  108. rm -rf $tmpfile
  109. return $ret
  110. }
  111. # function to check if ssh is up in $ip_addr
  112. Test_SSH_UP()
  113. {
  114. local ip_addr=$1
  115. local timeout=$2
  116. local interval=0
  117. # If TEST_SERIALIZE is set, use existing running qemu for testing
  118. if [ ${TEST_SERIALIZE} -eq 1 -a -e ${TARGET_IPSAVE} ]; then
  119. timeout=50
  120. fi
  121. while [ ${interval} -lt ${timeout} ]
  122. do
  123. Test_SSH ${ip_addr} "hostname"
  124. if [ $? -ne 0 ]; then
  125. interval=`expr $interval + 10`
  126. sleep 10
  127. else
  128. Test_Info "We can ssh on ${ip_addr} within ${interval} seconds"
  129. return 0
  130. fi
  131. done
  132. Test_Info "We can not ssh on ${ip_addr} in ${timeout} seconds"
  133. return 1
  134. }
  135. # function to prepare target test environment
  136. # $1 is the ip address of target system
  137. # $2 is the files, which needs to be copied into target
  138. Test_Target_Pre()
  139. {
  140. local ip_addr=$1
  141. local testscript=$2
  142. # Create a pre-defined folder for test scripts
  143. Test_SSH $ip_addr "mkdir -p $TARGET_TEST_DIR"
  144. if [ $? -eq 0 ]; then
  145. # Copy test scripts into target
  146. Test_SCP $ip_addr $testscript $TARGET_TEST_DIR && return 0
  147. else
  148. Test_Error "Fail to create $TARGET_TEST_DIR on target"
  149. return 1
  150. fi
  151. return 1
  152. }
  153. # function to record test result in $TEST_RESULT/testresult.log
  154. Test_Print_Result()
  155. {
  156. local PASS=0
  157. local FAIL=0
  158. local NORESULT=0
  159. if [ $2 -eq 0 ]; then
  160. PASS=1
  161. elif [ $2 -eq 1 ]; then
  162. FAIL=1
  163. else
  164. NORESULT=1
  165. fi
  166. # Format the output of the test result
  167. echo -e "$1 $PASS $FAIL $NORESULT" | awk '{printf("\t"); for(i=1;i<=NF;i++) printf("%-15s",$i); printf("\n");}' >> $TEST_RESULT/testresult.log
  168. }
  169. # Test_Kill_Qemu to kill child pid with parent pid given
  170. # $1 is qemu process id, which needs to be killed
  171. Test_Kill_Qemu()
  172. {
  173. local ret=0
  174. local ppid=0
  175. local i=0
  176. local index=0
  177. local total=0
  178. declare local pid
  179. # Check if $1 pid exists and is a qemu process
  180. ps -fp $PID | grep -iq "qemu"
  181. # Find all children pid of the pid $1
  182. if [ $? -eq 0 ]; then
  183. # Check if there is any child pid of the pid $PID
  184. ppid=$PID
  185. ps -f --ppid $ppid
  186. ret=$?
  187. while [ $ret -eq 0 ]
  188. do
  189. # If yes, get the child pid and check if the child pid has other child pid
  190. # Continue the while loop until there is no child pid found
  191. pid[$i]=`ps -f --ppid $ppid | awk '{if ($2 != "PID") print $2}'`
  192. ppid=${pid[$i]}
  193. i=$((i+1))
  194. ps -f --ppid $ppid
  195. ret=$?
  196. done
  197. # When TEST_SERIALIZE is set, qemu process will not be
  198. # killed until all the cases are finished
  199. if [ ${TEST_SERIALIZE} -eq 1 -a -e ${TEST_STATUS} ]; then
  200. index=`sed -n 2p ${TEST_STATUS} | awk '{print $3}'`
  201. total=`sed -n 2p ${TEST_STATUS} | awk '{print $4}'`
  202. if [ ${index} != ${total} ]; then
  203. Test_Info "Do not kill the qemu process and use it for later testing"
  204. Test_Update_IPSAVE $PID $TARGET_IPADDR
  205. else
  206. # If it is the last case, let's kill it
  207. while [ $i -ne 0 ]
  208. do
  209. i=$((i-1))
  210. kill ${pid[$i]}
  211. sleep 2
  212. done
  213. # Kill the parent id
  214. kill $PID
  215. fi
  216. else
  217. # Kill these children pids from the last one
  218. while [ $i -ne 0 ]
  219. do
  220. i=$((i-1))
  221. kill ${pid[$i]}
  222. sleep 2
  223. done
  224. # Kill the parent id
  225. kill $PID
  226. fi
  227. fi
  228. return
  229. }
  230. # function to check if there is any qemu process
  231. Test_Check_Qemu_UP()
  232. {
  233. local count=`ps -eo command | cut -d " " -f 1 | grep -c "\(^qemu\|.*/qemu\)"`
  234. if [ ${count} -lt 1 ]; then
  235. Test_Info "There is no Qemu process"
  236. return 1
  237. else
  238. Test_Info "There is at least one Qemu process running"
  239. return 0
  240. fi
  241. }
  242. # function to check if network is up
  243. Test_Check_IP_UP()
  244. {
  245. ping -c1 $1
  246. if [ $? -ne 0 ]; then
  247. Test_Info "IP $1 is not up"
  248. return 1
  249. else
  250. Test_Info "IP $1 is up"
  251. return 0
  252. fi
  253. }
  254. # function to find kernel/rootfs image
  255. Test_Find_Image()
  256. {
  257. where=""
  258. kernel=""
  259. arch=""
  260. target=""
  261. extension=""
  262. rootfs=""
  263. while getopts "l:k:a:t:" Option
  264. do
  265. case $Option in
  266. l) where="$OPTARG"
  267. ;;
  268. k) kernel="$OPTARG"
  269. extension="bin"
  270. ;;
  271. a) arch="$OPTARG"
  272. ;;
  273. t) target="$OPTARG"
  274. extension="ext3"
  275. ;;
  276. *) echo "invalid option: -$Option" && return 1
  277. ;;
  278. esac
  279. done
  280. if [ ! -z $kernel ]; then
  281. if [ -L ${where}/${kernel}-${arch}.${extension} ]; then
  282. echo ${where}/${kernel}-${arch}.${extension}
  283. return 0
  284. else
  285. for i in `dir ${where}`
  286. do
  287. # Exclude qemux86-64 when target is qemux86
  288. echo $i | grep "${kernel}.*${arch}.*\.${extension}" | grep -qv "${kernel}.*${arch}-64.*\.${extension}"
  289. if [ $? -eq 0 ]; then
  290. echo ${where}/${i}
  291. return 0
  292. fi
  293. done
  294. return 1
  295. fi
  296. fi
  297. if [ ! -z $target ]; then
  298. if [ -L ${where}/${target}-${arch}.${extension} ]; then
  299. rootfs=`readlink -f ${where}/${target}-${arch}.${extension}`
  300. echo ${rootfs}
  301. return 0
  302. else
  303. for i in `dir ${where}`
  304. do
  305. # Exclude qemux86-64 when target is qemux86
  306. echo $i | grep "${target}-${arch}.*\.${extension}" | grep -qv "${target}-${arch}-64.*\.${extension}"
  307. if [ $? -eq 0 ]; then
  308. echo ${where}/${i}
  309. return 0
  310. fi
  311. done
  312. return 1
  313. fi
  314. fi
  315. return 1
  316. }
  317. # function to parse IP address of target
  318. # $1 is the pid of qemu startup process
  319. Test_Fetch_Target_IP()
  320. {
  321. local opid=$1
  322. local ppid=0
  323. local ip_addr=0
  324. local i=0
  325. declare local pid
  326. # Check if $1 pid exists and contains ipaddr of target
  327. ps -fp $opid | grep -oq "192\.168\.7\.[0-9]*::"
  328. # Find all children pid of the pid $1
  329. # and check if they contain ipaddr of target
  330. if [ $? -ne 0 ]; then
  331. # Check if there is any child pid of the pid $1
  332. ppid=$opid
  333. ps -f --ppid $ppid > /dev/zero
  334. ret=$?
  335. while [ $ret -eq 0 ]
  336. do
  337. # If yes, get the child pid and check if the child pid has other child pid
  338. # Continue the while loop until there is no child pid found
  339. pid[$i]=`ps -f --ppid $ppid | awk '{if ($2 != "PID") print $2}'`
  340. ppid=${pid[$i]}
  341. i=$((i+1))
  342. ps -f --ppid $ppid > /dev/zero
  343. ret=$?
  344. done
  345. # Check these children pids, if they have ipaddr included in command line
  346. while [ $i -ne 0 ]
  347. do
  348. i=$((i-1))
  349. ps -fp ${pid[$i]} | grep -oq "192\.168\.7\.[0-9]*::"
  350. if [ $? -eq 0 ]; then
  351. ip_addr=`ps -fp ${pid[$i]} | grep -o "192\.168\.7\.[0-9]*::" | awk -F":" '{print $1}'`
  352. fi
  353. sleep 1
  354. done
  355. else
  356. ip_addr=`ps -fp $opid | grep -o "192\.168\.7\.[0-9]*::" | awk -F":" '{print $1}'`
  357. fi
  358. echo $ip_addr
  359. return
  360. }
  361. # function to check if qemu and its network
  362. Test_Create_Qemu()
  363. {
  364. local timeout=$1
  365. local ret=1
  366. local up_time=0
  367. which runqemu
  368. if [ $? -eq 0 ]; then
  369. RUNQEMU=`which runqemu`
  370. else
  371. Test_Error "Can not find runqemu in \$PATH, return fail"
  372. return 1
  373. fi
  374. if [ "$QEMUARCH" = "qemux86" -o "$QEMUARCH" = "qemux86-64" ]; then
  375. KERNEL=$(Test_Find_Image -l ${DEPLOY_DIR}/images -k bzImage -a ${QEMUARCH})
  376. elif [ "$QEMUARCH" = "qemuarm" -o "$QEMUARCH" = "spitz" -o "$QEMUARCH" = "borzoi" -o "$QEMUARCH" = "akita" -o "$QEMUARCH" = "nokia800" -o "$QEMUARCH" = "qemuppc" ]; then
  377. KERNEL=$(Test_Find_Image -l ${DEPLOY_DIR}/images -k zImage -a ${QEMUARCH})
  378. elif [ "$QEMUARCH" = "qemumips" ]; then
  379. KERNEL=$(Test_Find_Image -l ${DEPLOY_DIR}/images -k vmlinux -a ${QEMUARCH})
  380. fi
  381. # If there is no kernel image found, return failed directly
  382. if [ $? -eq 1 ]; then
  383. Test_Info "No kernel image file found under ${DEPLOY_DIR}/images for ${QEMUARCH}, pls. have a check"
  384. return $ret
  385. fi
  386. ROOTFS_IMAGE=$(Test_Find_Image -l ${DEPLOY_DIR}/images -t ${QEMUTARGET} -a ${QEMUARCH})
  387. # If there is no rootfs image found, return failed directly
  388. if [ $? -eq 1 ]; then
  389. Test_Info "No ${QEMUTARGET} rootfs image file found under ${DEPLOY_DIR}/images for ${QEMUARCH}, pls. have a check"
  390. return $ret
  391. fi
  392. TEST_ROOTFS_IMAGE="${TEST_TMP}/${QEMUTARGET}-${QEMUARCH}-test.ext3"
  393. CP=`which cp`
  394. # When TEST_SERIALIZE is set, we use the existing image under tmp folder
  395. if [ ${TEST_SERIALIZE} -eq 1 -a -e "$TARGET_IPSAVE" ]; then
  396. # If TARGET_IPSAVE exists, check PID of the qemu process from it
  397. PID=`awk '{print $1}' $TARGET_IPSAVE`
  398. timeout=50
  399. else
  400. rm -rf $TEST_ROOTFS_IMAGE
  401. echo "Copying rootfs $ROOTFS_IMAGE to $TEST_ROOTFS_IMAGE"
  402. $CP $ROOTFS_IMAGE $TEST_ROOTFS_IMAGE
  403. if [ $? -ne 0 ]; then
  404. Test_Info "Image ${ROOTFS_IMAGE} copy to ${TEST_ROOTFS_IMAGE} failed, return fail"
  405. return $ret
  406. fi
  407. export MACHINE=$QEMUARCH
  408. # Create Qemu in localhost VNC Port 1
  409. echo "Running xterm -display ${DISPLAY} -e 'OE_TMPDIR=${OE_TMPDIR} ${RUNQEMU} ${KERNEL} ${TEST_ROOTFS_IMAGE} && /bin/sleep 60' &"
  410. xterm -display ${DISPLAY} -e "OE_TMPDIR=${OE_TMPDIR} ${RUNQEMU} ${KERNEL} ${TEST_ROOTFS_IMAGE} && /bin/sleep 60" &
  411. # Get the pid of the xterm processor, which will be used in Test_Kill_Qemu
  412. PID=$!
  413. fi
  414. while [ ${up_time} -lt 10 ]
  415. do
  416. Test_Check_Qemu_UP
  417. if [ $? -ne 0 ]; then
  418. Test_Info "Wait for qemu up..."
  419. up_time=`expr $up_time + 5`
  420. sleep 5
  421. else
  422. Test_Info "Begin to check if qemu network is up"
  423. break
  424. fi
  425. done
  426. # Parse IP address of target from the qemu command line
  427. if [ ${up_time} -lt ${timeout} ]; then
  428. sleep 5
  429. TARGET_IPADDR=`Test_Fetch_Target_IP $PID`
  430. # If IP address is 0, means there is no qemu process found
  431. if [ ${TARGET_IPADDR} == "0" ]; then
  432. Test_Info "There is no qemu process or qemu ip address found, return failed"
  433. return $ret
  434. fi
  435. fi
  436. while [ ${up_time} -lt ${timeout} ]
  437. do
  438. Test_Check_IP_UP ${TARGET_IPADDR}
  439. if [ $? -eq 0 ]; then
  440. Test_Info "Qemu Network is up, ping with ${TARGET_IPADDR} is OK within ${up_time} seconds"
  441. ret=0
  442. break
  443. else
  444. Test_Info "Wait for Qemu Network up"
  445. up_time=`expr $up_time + 5`
  446. sleep 5
  447. fi
  448. done
  449. if [ $ret -eq 0 ]; then
  450. Test_Info "Qemu and its network is up"
  451. return $ret
  452. else
  453. Test_Info "Qemu or its network is not up in ${timeout} seconds"
  454. Test_Update_IPSAVE $PID $TARGET_IPADDR
  455. return $ret
  456. fi
  457. }
  458. # Function to prepare test project for toolchain test
  459. # $1 is the folder holding test project file
  460. # $2 is the test project name
  461. Test_Project_Prepare()
  462. {
  463. local toolchain_dir=$1
  464. local ret=1
  465. if [ ! -d ${toolchain_dir} ]; then
  466. mkdir -p ${toolchain_dir}
  467. ret=$?
  468. if [ $ret -ne 0 ]; then
  469. Test_Info "Create ${toolchain_dir} fail, return"
  470. return $ret
  471. fi
  472. fi
  473. ret=0
  474. # Download test project tarball if it does not exist
  475. if [ ! -f ${toolchain_dir}/${2}-${PROJECT_PV}.${suffix} ]; then
  476. wget -c -t 5 $PROJECT_DOWNLOAD_URL -O ${toolchain_dir}/${2}-${PROJECT_PV}.${suffix}
  477. ret=$?
  478. fi
  479. # Extract the test project into ${TEST_TMP}
  480. if [ $ret -eq 0 ]; then
  481. tar jxf ${toolchain_dir}/${2}-${PROJECT_PV}.${suffix} -C ${TEST_TMP}
  482. ret=$?
  483. if [ $ret -eq 0 ]; then
  484. Test_Info "Extract ${2}-${PROJECT_PV}.${suffix} into ${TEST_TMP} successfully"
  485. return $ret
  486. else
  487. Test_Info "Fail to extract ${2}-${PROJECT_PV}.${suffix} into ${TEST_TMP}"
  488. return $ret
  489. fi
  490. else
  491. Test_Info "Fail to download ${2}-${PROJECT_PV}.${suffix} from $PROJECT_DOWNLOAD_URL"
  492. rm -rf ${toolchain_dir}/${2}-${PROJECT_PV}.${suffix}
  493. return $ret
  494. fi
  495. }
  496. # Function to prepare toolchain environment
  497. # $1 is toolchain directory to hold toolchain tarball
  498. # $2 is prefix name for toolchain tarball
  499. Test_Toolchain_Prepare()
  500. {
  501. local toolchain_dir=$1
  502. local sdk_name=$2
  503. local ret=1
  504. if [ ! -d ${toolchain_dir} ]; then
  505. Test_Info "No directory ${toolchain_dir}, which holds toolchain tarballs"
  506. return 1
  507. fi
  508. # Check if there is any toolchain tarball under $toolchain_dir with prefix $sdk_name
  509. for i in `dir ${toolchain_dir}`
  510. do
  511. echo $i | grep "${sdk_name}-toolchain-gmae"
  512. if [ $? -eq 0 ]; then
  513. rm -rf ${TEST_TMP}/opt
  514. tar jxf ${toolchain_dir}/${i} -C ${TEST_TMP}
  515. ret=$?
  516. break
  517. fi
  518. done
  519. if [ $ret -eq 0 ]; then
  520. Test_Info "Check if /opt is accessible for non-root user"
  521. # Check if the non-root test user has write access of $TOOLCHAIN_TEST
  522. if [ -d ${TOOLCHAIN_TEST} ]; then
  523. touch ${TOOLCHAIN_TEST}
  524. if [ $? -ne 0 ]; then
  525. Test_Info "Has no right to modify folder $TOOLCHAIN_TEST, pls. chown it to test user"
  526. return 2
  527. fi
  528. else
  529. mkdir -p ${TOOLCHAIN_TEST}
  530. if [ $? -ne 0 ]; then
  531. Test_Info "Has no right to create folder $TOOLCHAIN_TEST, pls. create it and chown it to test user"
  532. return 2
  533. fi
  534. fi
  535. # If there is a toolchain folder under $TOOLCHAIN_TEST, let's remove it
  536. if [ -d ${TOOLCHAIN_TEST}/poky ]; then
  537. rm -rf ${TOOLCHAIN_TEST}/poky
  538. fi
  539. # Copy toolchain into $TOOLCHAIN_TEST
  540. cp -r ${TEST_TMP}/opt/poky ${TOOLCHAIN_TEST}
  541. ret=$?
  542. if [ $ret -eq 0 ]; then
  543. Test_Info "Successfully copy toolchain into $TOOLCHAIN_TEST"
  544. return $ret
  545. else
  546. Test_Info "Meet error when copy toolchain into $TOOLCHAIN_TEST"
  547. return $ret
  548. fi
  549. else
  550. Test_Info "No tarball named ${sdk_name}-toolchain-gmae under ${toolchain_dir}"
  551. return $ret
  552. fi
  553. }
  554. # Function to execute command and exit if run out of time
  555. # $1 is timeout value
  556. # $2 is the command to be executed
  557. Test_Time_Out()
  558. {
  559. local timeout=$1
  560. shift
  561. local command=$*
  562. local date=0
  563. local tmp=`mktemp`
  564. local ret=1
  565. local pid=0
  566. local ppid=0
  567. local i=0
  568. declare local pid_l
  569. # Run command in background
  570. ($command; echo $? > $tmp) &
  571. pid=$!
  572. while ps -e -o pid | grep -qw $pid; do
  573. if [ $date -ge $timeout ]; then
  574. Test_Info "$timeout Timeout when running command $command"
  575. rm -rf $tmp
  576. # Find all child processes of pid and kill them
  577. ppid=$pid
  578. ps -f --ppid $ppid
  579. ret=$?
  580. while [ $ret -eq 0 ]
  581. do
  582. # If yes, get the child pid and check if the child pid has other child pid
  583. # Continue the while loop until there is no child pid found
  584. pid_l[$i]=`ps -f --ppid $ppid | awk '{if ($2 != "PID") print $2}'`
  585. ppid=${pid_l[$i]}
  586. i=$((i+1))
  587. ps -f --ppid $ppid
  588. ret=$?
  589. done
  590. # Kill these children pids from the last one
  591. while [ $i -ne 0 ]
  592. do
  593. i=$((i-1))
  594. kill ${pid_l[$i]}
  595. sleep 2
  596. done
  597. # Kill the parent id
  598. kill $pid
  599. return 1
  600. fi
  601. sleep 5
  602. date=`expr $date + 5`
  603. done
  604. ret=`cat $tmp`
  605. rm -rf $tmp
  606. return $ret
  607. }
  608. # Function to test toolchain
  609. # $1 is test project name
  610. # $2 is the timeout value
  611. Test_Toolchain()
  612. {
  613. local test_project=$1
  614. local timeout=$2
  615. local ret=1
  616. local suffix="tar.bz2"
  617. local env_setup=""
  618. local pro_install="${TEST_TMP}/pro_install"
  619. # Set value for PROJECT_PV and PROJECT_DOWNLOAD_URL accordingly
  620. if [ $test_project == "cvs" ]; then
  621. PROJECT_PV=1.12.13
  622. PROJECT_DOWNLOAD_URL="http://ftp.gnu.org/non-gnu/cvs/source/feature/1.12.13/cvs-1.12.13.tar.bz2"
  623. elif [ $test_project == "iptables" ]; then
  624. PROJECT_PV=1.4.11
  625. PROJECT_DOWNLOAD_URL="http://netfilter.org/projects/iptables/files/iptables-1.4.11.tar.bz2"
  626. elif [ $test_project == "sudoku-savant" ]; then
  627. PROJECT_PV=1.3
  628. PROJECT_DOWNLOAD_URL="http://downloads.sourceforge.net/project/sudoku-savant/sudoku-savant/sudoku-savant-1.3/sudoku-savant-1.3.tar.bz2"
  629. else
  630. Test_Info "Unknown test project name $test_project"
  631. return 1
  632. fi
  633. # Download test project and extract it
  634. Test_Project_Prepare $TOOLCHAIN_PROJECTS $test_project
  635. if [ $? -ne 0 ]; then
  636. Test_Info "Prepare test project file failed"
  637. return 1
  638. fi
  639. # Extract toolchain tarball into ${TEST_TMP}
  640. Test_Toolchain_Prepare $TOOLCHAIN_DIR $SDK_NAME
  641. ret=$?
  642. if [ $ret -ne 0 ]; then
  643. Test_Info "Prepare toolchain test environment failed"
  644. return $ret
  645. fi
  646. if [ ! -d ${pro_install} ]; then
  647. mkdir -p ${pro_install}
  648. fi
  649. # Begin to build test project in toolchain environment
  650. env_setup=`find ${TOOLCHAIN_TEST}/poky -name "environment-setup*"`
  651. source $env_setup
  652. if [ $test_project == "cvs" -o $test_project == "iptables" ]; then
  653. cd ${TEST_TMP}/${test_project}-${PROJECT_PV}
  654. Test_Time_Out $timeout ./configure ${CONFIGURE_FLAGS} || { Test_Info "configure failed with $test_project"; return 1; }
  655. Test_Time_Out $timeout make -j4 || { Test_Info "make failed with $test_project"; return 1; }
  656. Test_Time_Out $timeout make install DESTDIR=${pro_install} || { Test_Info "make failed with $test_project"; return 1; }
  657. cd -
  658. ret=0
  659. elif [ $test_project == "sudoku-savant" ]; then
  660. cd ${TEST_TMP}/${test_project}-${PROJECT_PV}
  661. Test_Time_Out $timeout ./configure ${CONFIGURE_FLAGS} || { Test_Info "configure failed with $test_project"; return 1; }
  662. Test_Time_Out $timeout make -j4 || { Test_Info "make failed with $test_project"; return 1; }
  663. cd -
  664. ret=0
  665. else
  666. Test_Info "Unknown test project $test_project"
  667. ret=1
  668. fi
  669. return $ret
  670. }