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 os.path.exists(tasklogpath): 59 size = os.path.getsize(tasklogpath) 60 if size == 0: 61 os.remove(tasklogpath) 62 else: 63 raise Exception('qpa文件不存在') 64 if sys.platform.startswith('linux'): 65 print('os is Linux') 66 elif sys.platform.startswith('win'): 67 print('os is Windows') 68 elif sys.platform.startswith('darwin'): 69 print('os is macOS') 70 else: 71 print('unknown os', sys.platform) 72 timelist = latestpath.split("/") 73 if len(timelist) > 1: 74 curtime = timelist[1].replace("\n", "") 75 else: 76 timelist = latestpath.split("\\") 77 curtime = timelist[1].replace("\n", "") 78 testcaselist = [] 79 testcaselist1 = [] 80 mustpasslist = [] 81 total = 0 82 passcnt = 0 83 failcnt = 0 84 unavailablecnt = 0 85 86 with open(mustpasspath) as mustpassbuf: 87 for mustpassline in mustpassbuf: 88 mustpasslist.append(mustpassline.strip().strip("\r").strip("\n")) 89 print("mustfile line:", len(mustpasslist)) 90 #读取最近的tasklog文件 91 print("tasklogpath :", tasklogpath) 92 with open(tasklogpath, errors = 'ignore') as tasklogbuf: 93 #从tasklog文件中获取运行的testcase的信息 94 casename = "" 95 testbegin = 0 96 testresult = 0 97 for tasklogline in tasklogbuf: 98 if "#beginTestCaseResult " in tasklogline: 99 freslist = tasklogline.split("#beginTestCaseResult ") 100 casename = freslist[1] 101 testbegin = 1 102 continue 103 elif "#endTestCaseResult" in tasklogline: 104 if testbegin == 1 and testresult == 0: 105 total += 1 106 failcnt += 1 107 testcaselist.append(casename+"@@@false@@@run") 108 testcaselist1.append(casename.strip("\n")) 109 testbegin = 0 110 testresult = 0 111 continue 112 elif "<Result StatusCode=\"Pass\">" in tasklogline: 113 total +=1 114 passcnt += 1 115 testresult = 1 116 testcaselist.append(casename+"@@@true@@@run") 117 testcaselist1.append(casename.strip("\n")) 118 continue 119 elif "<Result StatusCode=\"NotSupported\">" in tasklogline: 120 total +=1 121 passcnt += 1 122 testresult = 1 123 testcaselist.append(casename+"@@@true@@@run") 124 testcaselist1.append(casename.strip("\n")) 125 continue 126 elif "<Result StatusCode=\"Fail\">" in tasklogline: 127 total +=1 128 failcnt += 1 129 testresult = 1 130 testcaselist.append(casename+"@@@false@@@run") 131 testcaselist1.append(casename.strip("\n")) 132 #print("tasklogfile line:", caseline[0], caseline[1]) 133 134 i = 0 135 j = 0 136 notfindlist = [] 137 while i < len(mustpasslist): 138 isfind = False 139 #j = 0 140 #while j < len(testcaselist): 141 if mustpasslist[i] in testcaselist1: 142 isfind = True 143 #break 144 j += 1 145 if isfind == False: 146 notfindlist.append(f"{mustpasslist[i]}@@@false@@@run") 147 failcnt += 1 148 total += 1 149 i += 1 150 testcaselist += notfindlist 151 #将testcase信息生成文件 152 with open(tmpfile, mode='w+') as f: 153 f.write("<?xml version='1.0' encoding='UTF-8'?>\n") 154 f.write("<testsuites name=\"{}\" timestamp=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" tests=\"{}\" ignored=\"0\" unavailable=\"{}\" productinfo=\"{}\">\n".format(suitename, curtime, failcnt, total, unavailablecnt, "{}")) 155 f.write(" <testsuite name=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" ignored=\"0\" tests=\"{}\" message=\"\">\n".format(suitename, failcnt, total)) 156 for casename in testcaselist: 157 casename = casename.replace("\n","") 158 loccasename = casename.split("@@@") 159 # print("loccasename : ", loccasename) 160 recasename = loccasename[0] 161 caseresult = loccasename[1] 162 casestate = loccasename[2] 163 f.write(" <testcase name=\"{}\" status=\"{}\" time=\"0.0\" classname=\"{}\" result=\"{}\" level=\"1\" message=\"\" />\n".format(recasename, casestate, suitename, caseresult)) 164 f.write(" </testsuite>\n") 165 f.write("</testsuites>\n") 166 #将tmp文件替换xts框架的result 167 if os.path.exists(latestpath+"/result") == False: 168 os.mkdir(latestpath+"/result") 169 # os.system(r"cp {} {}".format(tmpfile, putdir)) 170 print("putdir :", putdir) 171 shutil.copy2(tmpfile,putdir) 172 173