summaryrefslogtreecommitdiffstats
path: root/meta/classes/imagetest-qemu.bbclass
blob: 0d18f065a4e25c21f4cdd736a31482d60606dd12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Test related variables
# By default, TEST_DIR is created under WORKDIR
TEST_DIR ?= "${WORKDIR}/qemuimagetest"
TEST_LOG ?= "${LOG_DIR}/qemuimagetests"
TEST_RESULT ?= "${TEST_DIR}/result"
TEST_TMP ?= "${TEST_DIR}/tmp"
TEST_SCEN ?= "sanity"
TEST_STATUS ?= "${TEST_TMP}/status"
TARGET_IPSAVE ?= "${TEST_TMP}/target_ip"
TEST_SERIALIZE ?= "1"

python do_qemuimagetest() {
    qemuimagetest_main(d)
}
addtask qemuimagetest before do_build after do_rootfs
do_qemuimagetest[nostamp] = "1"
do_qemuimagetest[depends] += "qemu-native:do_populate_sysroot"

python do_qemuimagetest_standalone() {
    qemuimagetest_main(d)
}
addtask qemuimagetest_standalone
do_qemuimagetest_standalone[nostamp] = "1"
do_qemuimagetest_standalone[depends] += "qemu-native:do_populate_sysroot"

def qemuimagetest_main(d):
    import sys
    import re
    import shutil
    import subprocess
    
    """
    Test Controller for automated testing.
    """
    
    casestr = re.compile(r'(?P<scen>\w+\b):(?P<case>\S+$)')
    resultstr = re.compile(r'\s*(?P<case>\w+)\s*(?P<pass>\d+)\s*(?P<fail>\d+)\s*(?P<noresult>\d+)')
    machine = d.getVar('MACHINE', True)
    pname = d.getVar('PN', True)
    
    """function to save test cases running status"""
    def teststatus(test, status, index, length):
        test_status = d.getVar('TEST_STATUS', True)
        if not os.path.exists(test_status):
            raise bb.build.FuncFailed("No test status file existing under TEST_TMP")

        f = open(test_status, "w")
        f.write("\t%-15s%-15s%-15s%-15s\n" % ("Case", "Status", "Number", "Total"))
        f.write("\t%-15s%-15s%-15s%-15s\n" % (case, status, index, length))
        f.close()

    """funtion to run each case under scenario"""
    def runtest(scen, case, fulltestpath):
        resultpath = d.getVar('TEST_RESULT', True)
        tmppath = d.getVar('TEST_TMP', True)

        """initialize log file for testcase"""
        logpath = d.getVar('TEST_LOG', True)
        bb.utils.mkdirhier("%s/%s" % (logpath, scen))
        caselog = os.path.join(logpath, "%s/log_%s.%s" % (scen, case, d.getVar('DATETIME', True)))
        subprocess.call("touch %s" % caselog, shell=True)
        
        """export TEST_TMP, TEST_RESULT, DEPLOY_DIR and QEMUARCH"""
        os.environ["PATH"] = d.getVar("PATH", True)
        os.environ["TEST_TMP"] = tmppath
        os.environ["TEST_RESULT"] = resultpath
        os.environ["DEPLOY_DIR"] = d.getVar("DEPLOY_DIR", True)
        os.environ["QEMUARCH"] = machine
        os.environ["QEMUTARGET"] = pname
        os.environ["COREBASE"] = d.getVar("COREBASE", True)
        os.environ["TOPDIR"] = d.getVar("TOPDIR", True)
        os.environ["OE_TMPDIR"] = d.getVar("TMPDIR", True)
        os.environ["TEST_STATUS"] = d.getVar("TEST_STATUS", True)
        os.environ["TARGET_IPSAVE"] = d.getVar("TARGET_IPSAVE", True)
        os.environ["TEST_SERIALIZE"] = d.getVar("TEST_SERIALIZE", True)
        os.environ["SDK_NAME"] = d.getVar("SDK_NAME", True)

        # Add in all variables from the user's original environment which
        # haven't subsequntly been set/changed
        origbbenv = d.getVar("BB_ORIGENV", False) or {}
        for key in origbbenv:
            if key in os.environ:
                continue
            value = origbbenv.getVar(key, True)
            if value is not None:
                os.environ[key] = str(value)

        """run Test Case"""
        bb.note("Run %s test in scenario %s" % (case, scen))
        subprocess.call("%s" % fulltestpath, shell=True)
    
    """function to check testcase list and remove inappropriate cases"""
    def check_list(list):
        final_list = []
        for test in list:
            (scen, case, fullpath) = test

            """Skip rpm/smart if package_rpm not set for PACKAGE_CLASSES"""
            if case.find("smart") != -1 or case.find("rpm") != -1:
                if d.getVar("PACKAGE_CLASSES", True).find("rpm", 0, 11) == -1:
                    bb.note("skip rpm/smart cases since package_rpm not set in PACKAGE_CLASSES")
                    continue
                else:
                    final_list.append((scen, case, fullpath))
            else:
                    final_list.append((scen, case, fullpath))

        if not final_list:
            raise bb.build.FuncFailed("There is no suitable testcase for this target")

        return final_list

    """Generate testcase list in runtime"""
    def generate_list(testlist):
        list = []
        final_list = []
        if len(testlist) == 0:
            raise bb.build.FuncFailed("No testcase defined in TEST_SCEN")

        """check testcase folder and add case list according to TEST_SCEN"""
        for item in testlist.split(" "):
            n = casestr.match(item)
            if n:
                item = n.group('scen')
                casefile = n.group('case')
                for dir in d.getVar("QEMUIMAGETESTS", True).split():
                    fulltestcase = os.path.join(dir, item, casefile)
                    if not os.path.isfile(fulltestcase):
                        raise bb.build.FuncFailed("Testcase %s not found" % fulltestcase)
                    list.append((item, casefile, fulltestcase))
            else:
                for dir in d.getVar("QEMUIMAGETESTS", True).split():
                    scenlist = os.path.join(dir, "scenario", machine, pname)
                    if not os.path.isfile(scenlist):
                        raise bb.build.FuncFailed("No scenario list file named %s found" % scenlist)

                    f = open(scenlist, "r")
                    for line in f:
                       if item != line.split()[0]:
                           continue
                       else:
                           casefile = line.split()[1]

                       fulltestcase = os.path.join(dir, item, casefile)
                       if not os.path.isfile(fulltestcase):
                            raise bb.build.FuncFailed("Testcase %s not found" % fulltestcase)
                       list.append((item, casefile, fulltestcase))
        final_list = check_list(list)
        return final_list

    """Clean tmp folder for testing"""
    def clean_tmp():
        tmppath = d.getVar('TEST_TMP', True)

        if os.path.isdir(tmppath):
            for f in os.listdir(tmppath):
                tmpfile = os.path.join(tmppath, f)
                if os.path.isfile(tmpfile):
                    os.remove(tmpfile)
                elif os.path.isdir(tmpfile):
                    shutil.rmtree(tmpfile, True)

    """Before running testing, clean temp folder first"""
    clean_tmp()

    """check testcase folder and create test log folder"""
    testpath = d.getVar('TEST_DIR', True)
    bb.utils.mkdirhier(testpath)
    
    logpath = d.getVar('TEST_LOG', True)
    bb.utils.mkdirhier(logpath)

    tmppath = d.getVar('TEST_TMP', True)
    bb.utils.mkdirhier(tmppath)

    """initialize test status file"""
    test_status = d.getVar('TEST_STATUS', True)
    if os.path.exists(test_status):
        os.remove(test_status)
    subprocess.call("touch %s" % test_status, shell=True)

    """initialize result file"""
    resultpath = d.getVar('TEST_RESULT', True)
    bb.utils.mkdirhier(resultpath)
    resultfile = os.path.join(resultpath, "testresult.%s" % d.getVar('DATETIME', True))
    sresultfile = os.path.join(resultpath, "testresult.log")

    machine = d.getVar('MACHINE', True)

    if os.path.exists(sresultfile):
        os.remove(sresultfile)
    subprocess.call("touch %s" % resultfile, shell=True)
    os.symlink(resultfile, sresultfile)
    f = open(sresultfile, "a")
    f.write("\tTest Result for %s %s\n" % (machine, pname))
    f.write("\t%-15s%-15s%-15s%-15s\n" % ("Testcase", "PASS", "FAIL", "NORESULT"))
    f.close()
    
    """generate pre-defined testcase list"""
    testlist = d.getVar('TEST_SCEN', True)
    fulllist = generate_list(testlist)

    """Begin testing"""
    for index,test in enumerate(fulllist):
        (scen, case, fullpath) = test
        teststatus(case, "running", index, (len(fulllist) - 1))
        runtest(scen, case, fullpath)
        teststatus(case, "finished", index, (len(fulllist) - 1))
    
    """Print Test Result"""
    ret = 0
    f = open(sresultfile, "r")
    for line in f:
        m = resultstr.match(line)
        if m:
            if m.group('fail') == "1":
                ret = 1
            elif m.group('noresult') == "1":
                ret = 2
            line = line.strip('\n')
            bb.note(line)
        else:
            line = line.strip('\n')
            bb.note(line)
    f.close()

    """Clean temp files for testing"""
    clean_tmp()

    if ret != 0:
        raise bb.build.FuncFailed("Some testcases fail, pls. check test result and test log!!!")