1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-2021 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18import sys
19from utils import *
20import logging
21
22hdcPath = "D:\\tools\\hdc\\hdc_std.exe"
23testFilePath = "D:\\test\\testcase\\"
24testFile = ["ActsMediaCppStandardTest", "faultloggertest", "hilogtest", "hipluginmoduleapitest", "HiSysEventCPPTest",
25            "LibhilogCPPtest", "libhilogCtest", "ZoneUtiltest"]
26testPID = []
27filenames = []
28testTime = 0.1 * 360
29processNum = 5
30
31
32def EnvInit():
33    test_fileset = ""
34    for test_fileset in testFile:
35        exec_cmd(hdcPath + " file send " + testFilePath + test_fileset + " /data/local/tmp/" + test_fileset)
36        time.sleep(3)
37    exec_cmd(hdcPath + " shell chmod +x /data/local/tmp/" + test_fileset)
38
39
40def PressTestProcess(testCmd="", testFilename=""):
41    print("Start to PressTest Process with cmd " + testCmd)
42    for i in range(processNum):
43        exec_cmd(testCmd)
44    time.sleep(3)
45    GetPidOfProcess(testFilename)
46
47
48def GetPidOfProcess(processName=""):
49    cmdRet = exec_cmd(hdcPath + " shell \"ps -elf | grep "
50                      + processName + "| grep -v sh" + "| grep -v grep\"", waitOut=True)
51    print("cmdRet is :" + cmdRet)
52    splitlines = cmdRet.strip('\n\t\r').split("\n")
53    for line in splitlines:
54        print("Start go get pid with line:" + line)
55        if len(line.split()) >= 1:
56            print("Start go get pid:" + line)
57            testPID.append(line.split()[1])
58    for pid in testPID:
59        print("PID of each test process is:" + pid)
60
61
62def PressTestCheck():
63    print("Start to PressTestCheck")
64    for pid in testPID:
65        print("PID of each test process is:" + pid)
66        cmdRet = exec_cmd(hdcPath + " shell \"cat /proc/" + pid + "/cmdline\"", waitOut=True)
67        if cmdRet.find(testFile) == -1:
68            print("PID " + str(pid) + " not exist,will remove it")
69            testPID.remove(pid)
70        else:
71            print("cmdRet is " + cmdRet)
72            print("PID " + str(pid) + " still exist")
73
74
75def ProcessTestEnd():
76    for pid in testPID:
77        print("Process with pid " + pid + "will be killed.")
78        cmdRet = exec_cmd(hdcPath + " shell " + " kill " + pid, waitOut=True)
79        print(cmdRet)
80
81
82def ProcessTestResultCheck(testScriptPath):
83    print("Now we will check test result.")
84    exec_cmd("md " + testScriptPath + "\\faultlog")
85    cmdRet = exec_cmd(hdcPath + " shell \"ls -l /data/log/faultlog/temp/\" ", waitOut=True)
86    splitlines = cmdRet.strip('\n\t\r').split("\n")
87    for line in splitlines:
88        print("Get exception file with line " + line)
89        splitlines = line.split()
90        if len(splitlines) >= 8:
91            filenames.append(splitlines[7])
92    for filename in filenames:
93        print("Get exception file " + filename)
94        print("cmd is " + hdcPath + "  file recv /data/log/faultlog/temp/" +
95              filename + " " + testScriptPath + "\\faultlog\\")
96        cmdRet = exec_cmd(hdcPath + "  file recv /data/log/faultlog/temp/" +
97                          filename + " " + testScriptPath + "\\faultlog\\")
98        print("file recv result:" + str(cmdRet))
99    if len(filenames) > 0:
100        return False
101    return True
102
103
104#######################################################
105
106
107if __name__ == "__main__":
108    logging.info("------------------------NEW TEST---------------------------")
109    print("abs path is %s" % (os.path.split(os.path.realpath(__file__))[0]))
110    testScriptPath = os.path.split(os.path.realpath(__file__))[0]
111    testFileset = ""
112    EnvInit()
113    start = datetime.datetime.now()
114    now = datetime.datetime.now()
115    while (now - start).seconds < int(testTime):
116        print("Now is " + str((now - start).seconds))
117        now = datetime.datetime.now()
118        for testFileset in testFile:
119            startTestCmd = "\"cd /data/local/tmp/;/data/local/tmp/" + \
120                           testFileset + " > /dev/null \""
121            PressTestProcess("start " + hdcPath + " shell " + startTestCmd, testFileset)
122            PressTestCheck()
123            while len(testPID) > 0:
124                time.sleep(1)
125                PressTestCheck()
126    ProcessTestEnd()
127    if not ProcessTestResultCheck(testScriptPath):
128        print("Exception found after Test,please check faultlog path")
129        raise Exception("Exception found after Test,please check faultlog path")
130    logging.info("------------------------TEST END---------------------------")
131