summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiajun Xu <jiajun.xu@intel.com>2010-07-08 02:06:58 +0800
committerRichard Purdie <rpurdie@linux.intel.com>2010-07-15 12:48:25 +0100
commitfd1d661b391dfad1edaac937a17c1165f635031a (patch)
tree3bce526d67faf52385482f38c31679db05f6dbd5
parentbd19169a60d5aaadc278439578d06c8027cfecf3 (diff)
downloadpoky-fd1d661b391dfad1edaac937a17c1165f635031a.tar.gz
test: add automation framework and sanitytest
Automation test is disabled by default. User need set TESTCLASS to qemu in conf/local.conf and run bitbake command "bitbake poky-image-xxx" or "bitbake poky-image-xxx -c qemuimagetest" to trigger it. Currently only the sanity test with two testcases are added. To run the test, user need prepare a testing environment: 1) "expect" should be installed on system 2) NOPASSWD should be set for user to run bitbake Signed-off-by Jiajun Xu <jiajun.xu@intel.com>
-rw-r--r--build/conf/local.conf.sample8
-rw-r--r--meta/classes/image.bbclass3
-rw-r--r--meta/classes/imagetest-dummy.bbclass1
-rw-r--r--meta/classes/imagetest-qemu.bbclass137
-rw-r--r--meta/conf/layer.conf7
-rw-r--r--scripts/qemuimage-testlib202
-rwxr-xr-xscripts/qemuimage-tests/sanity/boot31
-rwxr-xr-xscripts/qemuimage-tests/sanity/ssh40
8 files changed, 429 insertions, 0 deletions
diff --git a/build/conf/local.conf.sample b/build/conf/local.conf.sample
index fc013bcd10..e58c476a7d 100644
--- a/build/conf/local.conf.sample
+++ b/build/conf/local.conf.sample
@@ -153,3 +153,11 @@ ENABLE_BINARY_LOCALE_GENERATION = "1"
153# Poky can try and fetch packaged-staging packages from a http, https or ftp 153# Poky can try and fetch packaged-staging packages from a http, https or ftp
154# mirror. Set this variable to the root of a pstage directory on a server. 154# mirror. Set this variable to the root of a pstage directory on a server.
155#PSTAGE_MIRROR ?= "http://someserver.tld/share/pstage" 155#PSTAGE_MIRROR ?= "http://someserver.tld/share/pstage"
156
157# Set IMAGETEST to qemu if you want to build testcases and start
158# testing in qemu after do_rootfs.
159#IMAGETEST="qemu"
160
161# By default testing will run the sanitytest suite. If you want to run other tests
162# (e.g. bat), list them here
163#TEST_SCEN="sanitytest bat"
diff --git a/meta/classes/image.bbclass b/meta/classes/image.bbclass
index 4a5b83e30b..7318ba9a28 100644
--- a/meta/classes/image.bbclass
+++ b/meta/classes/image.bbclass
@@ -1,5 +1,8 @@
1inherit rootfs_${IMAGE_PKGTYPE} 1inherit rootfs_${IMAGE_PKGTYPE}
2 2
3IMAGETEST ?= "dummy"
4inherit imagetest-${IMAGETEST}
5
3LICENSE = "MIT" 6LICENSE = "MIT"
4PACKAGES = "" 7PACKAGES = ""
5RDEPENDS += "${IMAGE_INSTALL}" 8RDEPENDS += "${IMAGE_INSTALL}"
diff --git a/meta/classes/imagetest-dummy.bbclass b/meta/classes/imagetest-dummy.bbclass
new file mode 100644
index 0000000000..bcacae604f
--- /dev/null
+++ b/meta/classes/imagetest-dummy.bbclass
@@ -0,0 +1 @@
# dummy testclass file
diff --git a/meta/classes/imagetest-qemu.bbclass b/meta/classes/imagetest-qemu.bbclass
new file mode 100644
index 0000000000..742b759208
--- /dev/null
+++ b/meta/classes/imagetest-qemu.bbclass
@@ -0,0 +1,137 @@
1addtask qemuimagetest before do_build
2# after do_rootfs
3do_qemuimagetest[nostamp] = "1"
4do_qemuimagetest[depends] += "qemu-native:do_populate_sysroot"
5
6# Test related variables
7# By default, TEST_DIR is created under WORKDIR
8TEST_DIR ?= "${WORKDIR}/qemuimagetest"
9TEST_LOG ?= "${LOG_DIR}/qemuimagetests"
10TEST_RESULT ?= "${TEST_DIR}/result"
11TEST_LIST ?= "${TEST_DIR}/list"
12TEST_TMP ?= "${TEST_DIR}/tmp"
13TEST_SCEN ?= "sanity"
14
15python do_qemuimagetest() {
16 import sys
17 import re
18 import os
19
20 """
21 Test Controller for Poky Testing.
22 """
23
24 casestr = re.compile(r'(?P<scen>\w+\b)\s*(?P<case>\w+$)')
25 resultstr = re.compile(r'\s*(?P<case>\w+)\s*(?P<pass>\d+)\s*(?P<fail>\d+)\s*(?P<noresult>\d+)')
26
27 """funtion to run each case under scenario"""
28 def runtest(scen, case, fulltestpath):
29 resultpath = bb.data.getVar('TEST_RESULT', d, 1)
30 machine = bb.data.getVar('MACHINE', d, 1)
31 pname = bb.data.getVar('PN', d, 1)
32
33 testpath = bb.data.getVar('TEST_DIR', d, 1)
34
35 """initialize log file for testcase"""
36 logpath = bb.data.getVar('TEST_LOG', d, 1)
37 bb.utils.mkdirhier("%s/%s" % (logpath, scen))
38 caselog = os.path.join(logpath, "%s/log_%s.%s" % (scen, case, bb.data.getVar('DATETIME', d, 1)))
39 os.system("touch %s" % caselog)
40
41
42 """export TEST_TMP, TEST_RESULT, DEPLOY_DIR and QEMUARCH"""
43 os.environ["PATH"] = bb.data.getVar("PATH", d, True)
44 os.environ["TEST_TMP"] = testpath
45 os.environ["TEST_RESULT"] = resultpath
46 os.environ["DEPLOY_DIR"] = bb.data.getVar("DEPLOY_DIR", d, True)
47 os.environ["QEMUARCH"] = machine
48 os.environ["QEMUTARGET"] = pname
49 os.environ["DISPLAY"] = bb.data.getVar("DISPLAY", d, True)
50
51 """run Test Case"""
52 bb.note("Run %s test in scenario %s" % (case, scen))
53 os.system("%s | tee -a %s" % (fulltestpath, caselog))
54
55 """Generate testcase list in runtime"""
56 def generate_list(testfile, testlist):
57 list = []
58 if len(testlist) == 0:
59 raise bb.build.FuncFailed("No testcase defined in TEST_SCEN")
60
61 """remove old testcase list file"""
62 if os.path.exists(testfile):
63 os.remove(testfile)
64 os.system("touch %s" % testfile)
65
66 """check testcase folder and add case to TEST_LIST"""
67 for item in testlist.split(" "):
68 found = False
69 for dir in bb.data.getVar("QEMUIMAGETESTS", d, True).split():
70 casepath = os.path.join(dir, item)
71 if not os.path.isdir(casepath):
72 continue
73 files = os.listdir(casepath)
74 for casefile in files:
75 fulltestcase = "%s/%s" % (casepath, casefile)
76 if os.path.isfile(fulltestcase):
77 list.append((item, casefile, fulltestcase))
78 found = True
79 if not found:
80 raise bb.build.FuncFailed("Testcase folder not found for test %s" % item)
81 return list
82
83 """check testcase folder and create test log folder"""
84 testpath = bb.data.getVar('TEST_DIR', d, 1)
85 bb.utils.mkdirhier(testpath)
86
87 logpath = bb.data.getVar('TEST_LOG', d, 1)
88 bb.utils.mkdirhier(logpath)
89
90 tmppath = bb.data.getVar('TEST_TMP', d, 1)
91 bb.utils.mkdirhier(tmppath)
92
93 """initialize result file"""
94 resultpath = bb.data.getVar('TEST_RESULT', d, 1)
95 bb.utils.mkdirhier(resultpath)
96 resultfile = os.path.join(resultpath, "testresult.%s" % bb.data.getVar('DATETIME', d, 1))
97 sresultfile = os.path.join(resultpath, "testresult.log")
98
99 machine = bb.data.getVar('MACHINE', d, 1)
100
101 if os.path.exists(sresultfile):
102 os.remove(sresultfile)
103 os.system("touch %s" % resultfile)
104 os.symlink(resultfile, sresultfile)
105 f = open(sresultfile, "a")
106 f.write("\tTest Result for %s\n" % machine)
107 f.write("\tTestcase\tPASS\tFAIL\tNORESULT\n")
108 f.close()
109
110 """generate pre-defined testcase list"""
111 testfile = bb.data.getVar('TEST_LIST', d, 1)
112 testlist = bb.data.getVar('TEST_SCEN', d, 1)
113 fulllist = generate_list(testfile, testlist)
114
115 """Begin testing"""
116 for test in fulllist:
117 (scen, case, fullpath) = test
118 runtest(scen, case, fullpath)
119
120 """Print Test Result"""
121 ret = 0
122 f = open(sresultfile, "r")
123 for line in f:
124 m = resultstr.match(line)
125 if m:
126 if m.group('fail') == "1":
127 ret = 1
128 elif m.group('noresult') == "1":
129 ret = 2
130 print line,
131 else:
132 print line,
133 f.close()
134
135 if ret != 0:
136 raise bb.build.FuncFailed("Some testcases fail, pls. check test result and test log!!!")
137}
diff --git a/meta/conf/layer.conf b/meta/conf/layer.conf
index 7b3d539245..a780245c29 100644
--- a/meta/conf/layer.conf
+++ b/meta/conf/layer.conf
@@ -8,5 +8,12 @@ BBFILE_COLLECTIONS += "normal"
8BBFILE_PATTERN_normal := "^${LAYERDIR}/" 8BBFILE_PATTERN_normal := "^${LAYERDIR}/"
9BBFILE_PRIORITY_normal = "5" 9BBFILE_PRIORITY_normal = "5"
10 10
11# Add scripts to PATH
12PATH := "${PATH}:${LAYERDIR}/scripts"
13
14# Set path to qemu image tests included in this layer
15
16QEMUIMAGETESTS := "${OEROOT}/scripts/qemuimage-tests"
17
11require conf/distro/include/poky-default-revisions.inc 18require conf/distro/include/poky-default-revisions.inc
12 19
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
17TYPE="ext3"
18
19# common function for information print
20Test_Error()
21{
22 echo -e "\tTest_Error: $*"
23}
24
25Test_Info()
26{
27 echo -e "\tTest_Info: $*"
28}
29
30# function to run command in $ip_addr via ssh
31Test_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
40eval spawn ssh -o UserKnownHostsFile=$tmpfile root@$ip_addr "$command"
41set timeout $time_out
42expect {
43 "*assword:" { send "\r"; exp_continue}
44 "*(yes/no)?" { send "yes\r"; exp_continue }
45 eof { exit [ lindex [wait] 3 ] }
46}
47EOF`
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
55Test_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
79Test_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
96Test_Kill_Qemu()
97{
98 sudo pkill -9 qemu
99 return $?
100}
101
102# function to check if there is any qemu process
103Test_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
116Test_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
129Test_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}
diff --git a/scripts/qemuimage-tests/sanity/boot b/scripts/qemuimage-tests/sanity/boot
new file mode 100755
index 0000000000..b4c0094f25
--- /dev/null
+++ b/scripts/qemuimage-tests/sanity/boot
@@ -0,0 +1,31 @@
1#!/bin/bash
2#
3# Boot Test Case for Sanity Test
4# The case boot up the Qemu target with `runqemu qemux86`.
5# Then check if qemu and qemu network is up.
6#
7# Author: Jiajun Xu <jiajun.xu@intel.com>
8#
9# This file is licensed under the GNU General Public License,
10# Version 2.
11#
12
13. $OEROOT/scripts/qemuimage-testlib
14
15TIMEOUT=120
16QEMU_IPADDR="192.168.7.2"
17
18# Start qemu and check its network
19Test_Create_Qemu ${QEMU_IPADDR} ${TIMEOUT}
20
21if [ $? -eq 0 ]; then
22 Test_Info "Boot Test PASS"
23 Test_Kill_Qemu
24 Test_Print_Result "Boot" 0
25 exit 0
26else
27 Test_Info "Boot Test FAIL"
28 Test_Kill_Qemu
29 Test_Print_Result "Boot" 1
30 exit 1
31fi
diff --git a/scripts/qemuimage-tests/sanity/ssh b/scripts/qemuimage-tests/sanity/ssh
new file mode 100755
index 0000000000..7dd895951c
--- /dev/null
+++ b/scripts/qemuimage-tests/sanity/ssh
@@ -0,0 +1,40 @@
1#!/bin/bash
2# SSH Test Case for Sanity Test
3# The case boot up the Qemu target with `runqemu qemux86`.
4# Then check if ssh service in qemu is up.
5#
6# Author: Jiajun Xu <jiajun.xu@intel.com>
7#
8# This file is licensed under the GNU General Public License,
9# Version 2.
10#
11
12. $OEROOT/scripts/qemuimage-testlib
13
14TIMEOUT=360
15QEMU_IPADDR="192.168.7.2"
16RET=1
17
18# Start qemu and check its network
19Test_Create_Qemu ${QEMU_IPADDR} ${TIMEOUT}
20
21# If qemu network is up, check ssh service in qemu
22if [ $? -eq 0 ]; then
23 Test_Info "Begin to Test SSH Service in Qemu"
24 Test_SSH_UP ${QEMU_IPADDR} ${TIMEOUT}
25 RET=$?
26else
27 RET=1
28fi
29
30if [ ${RET} -eq 0 ]; then
31 Test_Info "SSH Test PASS"
32 Test_Kill_Qemu
33 Test_Print_Result "SSH" 0
34 exit 0
35else
36 Test_Info "SSH Test FAIL"
37 Test_Kill_Qemu
38 Test_Print_Result "SSH" 1
39 exit 1
40fi