• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright (C) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17from ast import Return
18import os
19import sys
20import platform
21import shutil
22from unittest import suite
23
24def removedir(rootdir):
25    for root,dirs,files in os.walk(rootdir, topdown=False):
26        for name in files:
27            os.remove(os.path.join(rootdir,name))
28        for name in dirs:
29            os.rmdir(os.path.join(rootdir,name))
30    os.rmdir(rootdir)
31
32def new_report(bakdir, str):
33    files = os.listdir(bakdir)
34    lists = [] #列出目录的下所有文件和文件夹保存到lists
35    for f in files:
36        # if f.startswith(str):
37        if "latest" in f:
38            continue
39        lists.append(f)
40
41    lists.sort(key=lambda fn:os.path.getmtime(bakdir + "/" + fn))  # 按时间排序
42    file_new = os.path.join(bakdir,lists[-1]) # 获取最新的文件保存到file_new
43    print("latest file:", file_new)
44    return file_new
45
46
47if __name__ == '__main__':
48    suitename = sys.argv[1]
49    mustpassfile = sys.argv[2]
50    latestpath = new_report("reports", "")
51    tmpfile = "tmptestsuite.xml"
52    putfile = "/result/"+suitename+".xml"
53    tasklogfile = suitename+".qpa"
54    putdir = latestpath+putfile
55    tasklogpath = tasklogfile
56    mustpasspath = "testcases/vulkandata/vk-default/"+mustpassfile
57    curtime = ""
58    if sys.platform.startswith('linux'):
59        print('os is Linux')
60    elif sys.platform.startswith('win'):
61        print('os is  Windows')
62    elif sys.platform.startswith('darwin'):
63        print('os is  macOS')
64    else:
65        print('unknown os', sys.platform)
66    timelist = latestpath.split("/")
67    if len(timelist) > 1:
68        curtime = timelist[1].replace("\n", "")
69    else:
70        timelist = latestpath.split("\\")
71        curtime = timelist[1].replace("\n", "")
72    testcaselist = []
73    mustpasslist = []
74    total = 0
75    passcnt = 0
76    failcnt = 0
77    unavailablecnt = 0
78
79    with open(mustpasspath) as mustpassbuf:
80        for mustpassline in mustpassbuf:
81            mustpasslist.append(mustpassline.strip().strip("\r").strip("\n"))
82    print("mustfile line:", len(mustpasslist))
83    #读取最近的tasklog文件
84    print("tasklogpath :", tasklogpath)
85    with open(tasklogpath) as tasklogbuf:
86        #从tasklog文件中获取运行的testcase的信息
87        casename = ""
88        testbegin = 0
89        testresult = 0
90        for tasklogline in tasklogbuf:
91            # if "[Start test suite [" in tasklogline:
92            #     suitelist = tasklogline.split("[Start test suite [")
93            #     suiteitem = suitelist[1].split("]")
94            #     suitename = suiteitem[0]
95            if "#beginTestCaseResult " in tasklogline:
96                freslist = tasklogline.split("#beginTestCaseResult ")
97                casename = freslist[1]
98                testbegin = 1
99            if "#endTestCaseResult" in tasklogline:
100                if testbegin == 1 and testresult == 0:
101                    total += 1
102                    failcnt += 1
103                    testcaselist.append(casename+"@@@false@@@run")
104                testbegin = 0
105                testresult = 0
106            if "<Result StatusCode=\"Pass\">" in tasklogline:
107                total +=1
108                passcnt += 1
109                testresult = 1
110                testcaselist.append(casename+"@@@true@@@run")
111            if "<Result StatusCode=\"NotSupported\">" in tasklogline:
112                total +=1
113                passcnt += 1
114                testresult = 1
115                testcaselist.append(casename+"@@@true@@@run")
116            if "<Result StatusCode=\"Fail\">" in tasklogline:
117                total +=1
118                failcnt += 1
119                testresult = 1
120                testcaselist.append(casename+"@@@false@@@run")
121                #print("tasklogfile line:", caseline[0], caseline[1])
122
123    i = 0
124    j = 0
125    notfindlist = []
126    while i < len(mustpasslist):
127        isfind = False
128        j = 0
129        while j < len(testcaselist):
130            if mustpasslist[i] in testcaselist[j]:
131                isfind = True
132                break
133            j += 1
134        if isfind == False:
135            notfindlist.append(mustpasslist[i]+"@@@false@@@run")
136            failcnt += 1
137            total += 1
138        i += 1
139    testcaselist += notfindlist
140    #将testcase信息生成文件
141    xmlfile = open(tmpfile, mode='w+')
142    xmlfile.write("<?xml version='1.0' encoding='UTF-8'?>\n")
143    xmlfile.write("<testsuites name=\"{}\" timestamp=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" tests=\"{}\" ignored=\"0\" unavailable=\"{}\" productinfo=\"{}\">\n".format(suitename, curtime, failcnt, total, unavailablecnt, "{}"))
144    xmlfile.write("  <testsuite name=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" ignored=\"0\" tests=\"{}\" message=\"\">\n".format(suitename, failcnt, total))
145    for casename in testcaselist:
146        casename = casename.replace("\n","")
147        loccasename = casename.split("@@@")
148        # print("loccasename : ", loccasename)
149        recasename = loccasename[0]
150        caseresult = loccasename[1]
151        casestate = loccasename[2]
152        xmlfile.write("    <testcase name=\"{}\" status=\"{}\" time=\"0.0\" classname=\"{}\" result=\"{}\" level=\"1\" message=\"\" />\n".format(recasename, casestate, suitename, caseresult))
153    xmlfile.write("  </testsuite>\n")
154    xmlfile.write("</testsuites>\n")
155    xmlfile.close()
156    #将tmp文件替换xts框架的result
157    if os.path.exists(latestpath+"/result") == False:
158        os.mkdir(latestpath+"/result")
159    # os.system(r"cp {} {}".format(tmpfile, putdir))
160    print("putdir :", putdir)
161    shutil.copy2(tmpfile,putdir)
162
163