131c75014Sopenharmony_ci#!/usr/bin/env python3
231c75014Sopenharmony_ci# coding=utf-8
331c75014Sopenharmony_ci
431c75014Sopenharmony_ci#
531c75014Sopenharmony_ci# Copyright (c) 2020-2021 Huawei Device Co., Ltd.
631c75014Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
731c75014Sopenharmony_ci# you may not use this file except in compliance with the License.
831c75014Sopenharmony_ci# You may obtain a copy of the License at
931c75014Sopenharmony_ci#
1031c75014Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
1131c75014Sopenharmony_ci#
1231c75014Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1331c75014Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1431c75014Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1531c75014Sopenharmony_ci# See the License for the specific language governing permissions and
1631c75014Sopenharmony_ci# limitations under the License.
1731c75014Sopenharmony_ci#
1831c75014Sopenharmony_ciimport sys
1931c75014Sopenharmony_cifrom utils import *
2031c75014Sopenharmony_ciimport logging
2131c75014Sopenharmony_ci
2231c75014Sopenharmony_cihdcPath = "D:\\tools\\hdc\\hdc_std.exe"
2331c75014Sopenharmony_citestFilePath = "D:\\test\\testcase\\"
2431c75014Sopenharmony_citestFile = ["ActsMediaCppStandardTest", "faultloggertest", "hilogtest", "hipluginmoduleapitest", "HiSysEventCPPTest",
2531c75014Sopenharmony_ci            "LibhilogCPPtest", "libhilogCtest", "ZoneUtiltest"]
2631c75014Sopenharmony_citestPID = []
2731c75014Sopenharmony_cifilenames = []
2831c75014Sopenharmony_citestTime = 0.1 * 360
2931c75014Sopenharmony_ciprocessNum = 5
3031c75014Sopenharmony_ci
3131c75014Sopenharmony_ci
3231c75014Sopenharmony_cidef EnvInit():
3331c75014Sopenharmony_ci    test_fileset = ""
3431c75014Sopenharmony_ci    for test_fileset in testFile:
3531c75014Sopenharmony_ci        exec_cmd(hdcPath + " file send " + testFilePath + test_fileset + " /data/local/tmp/" + test_fileset)
3631c75014Sopenharmony_ci        time.sleep(3)
3731c75014Sopenharmony_ci    exec_cmd(hdcPath + " shell chmod +x /data/local/tmp/" + test_fileset)
3831c75014Sopenharmony_ci
3931c75014Sopenharmony_ci
4031c75014Sopenharmony_cidef PressTestProcess(testCmd="", testFilename=""):
4131c75014Sopenharmony_ci    print("Start to PressTest Process with cmd " + testCmd)
4231c75014Sopenharmony_ci    for i in range(processNum):
4331c75014Sopenharmony_ci        exec_cmd(testCmd)
4431c75014Sopenharmony_ci    time.sleep(3)
4531c75014Sopenharmony_ci    GetPidOfProcess(testFilename)
4631c75014Sopenharmony_ci
4731c75014Sopenharmony_ci
4831c75014Sopenharmony_cidef GetPidOfProcess(processName=""):
4931c75014Sopenharmony_ci    cmdRet = exec_cmd(hdcPath + " shell \"ps -elf | grep "
5031c75014Sopenharmony_ci                      + processName + "| grep -v sh" + "| grep -v grep\"", waitOut=True)
5131c75014Sopenharmony_ci    print("cmdRet is :" + cmdRet)
5231c75014Sopenharmony_ci    splitlines = cmdRet.strip('\n\t\r').split("\n")
5331c75014Sopenharmony_ci    for line in splitlines:
5431c75014Sopenharmony_ci        print("Start go get pid with line:" + line)
5531c75014Sopenharmony_ci        if len(line.split()) >= 1:
5631c75014Sopenharmony_ci            print("Start go get pid:" + line)
5731c75014Sopenharmony_ci            testPID.append(line.split()[1])
5831c75014Sopenharmony_ci    for pid in testPID:
5931c75014Sopenharmony_ci        print("PID of each test process is:" + pid)
6031c75014Sopenharmony_ci
6131c75014Sopenharmony_ci
6231c75014Sopenharmony_cidef PressTestCheck():
6331c75014Sopenharmony_ci    print("Start to PressTestCheck")
6431c75014Sopenharmony_ci    for pid in testPID:
6531c75014Sopenharmony_ci        print("PID of each test process is:" + pid)
6631c75014Sopenharmony_ci        cmdRet = exec_cmd(hdcPath + " shell \"cat /proc/" + pid + "/cmdline\"", waitOut=True)
6731c75014Sopenharmony_ci        if cmdRet.find(testFile) == -1:
6831c75014Sopenharmony_ci            print("PID " + str(pid) + " not exist,will remove it")
6931c75014Sopenharmony_ci            testPID.remove(pid)
7031c75014Sopenharmony_ci        else:
7131c75014Sopenharmony_ci            print("cmdRet is " + cmdRet)
7231c75014Sopenharmony_ci            print("PID " + str(pid) + " still exist")
7331c75014Sopenharmony_ci
7431c75014Sopenharmony_ci
7531c75014Sopenharmony_cidef ProcessTestEnd():
7631c75014Sopenharmony_ci    for pid in testPID:
7731c75014Sopenharmony_ci        print("Process with pid " + pid + "will be killed.")
7831c75014Sopenharmony_ci        cmdRet = exec_cmd(hdcPath + " shell " + " kill " + pid, waitOut=True)
7931c75014Sopenharmony_ci        print(cmdRet)
8031c75014Sopenharmony_ci
8131c75014Sopenharmony_ci
8231c75014Sopenharmony_cidef ProcessTestResultCheck(testScriptPath):
8331c75014Sopenharmony_ci    print("Now we will check test result.")
8431c75014Sopenharmony_ci    exec_cmd("md " + testScriptPath + "\\faultlog")
8531c75014Sopenharmony_ci    cmdRet = exec_cmd(hdcPath + " shell \"ls -l /data/log/faultlog/temp/\" ", waitOut=True)
8631c75014Sopenharmony_ci    splitlines = cmdRet.strip('\n\t\r').split("\n")
8731c75014Sopenharmony_ci    for line in splitlines:
8831c75014Sopenharmony_ci        print("Get exception file with line " + line)
8931c75014Sopenharmony_ci        splitlines = line.split()
9031c75014Sopenharmony_ci        if len(splitlines) >= 8:
9131c75014Sopenharmony_ci            filenames.append(splitlines[7])
9231c75014Sopenharmony_ci    for filename in filenames:
9331c75014Sopenharmony_ci        print("Get exception file " + filename)
9431c75014Sopenharmony_ci        print("cmd is " + hdcPath + "  file recv /data/log/faultlog/temp/" +
9531c75014Sopenharmony_ci              filename + " " + testScriptPath + "\\faultlog\\")
9631c75014Sopenharmony_ci        cmdRet = exec_cmd(hdcPath + "  file recv /data/log/faultlog/temp/" +
9731c75014Sopenharmony_ci                          filename + " " + testScriptPath + "\\faultlog\\")
9831c75014Sopenharmony_ci        print("file recv result:" + str(cmdRet))
9931c75014Sopenharmony_ci    if len(filenames) > 0:
10031c75014Sopenharmony_ci        return False
10131c75014Sopenharmony_ci    return True
10231c75014Sopenharmony_ci
10331c75014Sopenharmony_ci
10431c75014Sopenharmony_ci#######################################################
10531c75014Sopenharmony_ci
10631c75014Sopenharmony_ci
10731c75014Sopenharmony_ciif __name__ == "__main__":
10831c75014Sopenharmony_ci    logging.info("------------------------NEW TEST---------------------------")
10931c75014Sopenharmony_ci    print("abs path is %s" % (os.path.split(os.path.realpath(__file__))[0]))
11031c75014Sopenharmony_ci    testScriptPath = os.path.split(os.path.realpath(__file__))[0]
11131c75014Sopenharmony_ci    testFileset = ""
11231c75014Sopenharmony_ci    EnvInit()
11331c75014Sopenharmony_ci    start = datetime.datetime.now()
11431c75014Sopenharmony_ci    now = datetime.datetime.now()
11531c75014Sopenharmony_ci    while (now - start).seconds < int(testTime):
11631c75014Sopenharmony_ci        print("Now is " + str((now - start).seconds))
11731c75014Sopenharmony_ci        now = datetime.datetime.now()
11831c75014Sopenharmony_ci        for testFileset in testFile:
11931c75014Sopenharmony_ci            startTestCmd = "\"cd /data/local/tmp/;/data/local/tmp/" + \
12031c75014Sopenharmony_ci                           testFileset + " > /dev/null \""
12131c75014Sopenharmony_ci            PressTestProcess("start " + hdcPath + " shell " + startTestCmd, testFileset)
12231c75014Sopenharmony_ci            PressTestCheck()
12331c75014Sopenharmony_ci            while len(testPID) > 0:
12431c75014Sopenharmony_ci                time.sleep(1)
12531c75014Sopenharmony_ci                PressTestCheck()
12631c75014Sopenharmony_ci    ProcessTestEnd()
12731c75014Sopenharmony_ci    if not ProcessTestResultCheck(testScriptPath):
12831c75014Sopenharmony_ci        print("Exception found after Test,please check faultlog path")
12931c75014Sopenharmony_ci        raise Exception("Exception found after Test,please check faultlog path")
13031c75014Sopenharmony_ci    logging.info("------------------------TEST END---------------------------")
131