• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2# Copyright (c) 2022 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from ast import parse
16import json
17import sys
18import os
19import time
20import argparse
21import re
22import subprocess
23import shlex
24import datetime
25
26def GetDirSize(dir_path):
27    if  not os.path.exists(dir_path):
28        PrintToLog("\n\nERROR: %s, dir are not exist!!!\n" % dir_path)
29        PrintToLog("End of check, test failed!")
30        sys.exit(99)
31    size = 0
32    for root, dirs, files in os.walk(dir_path):
33        for name in files:
34            if not os.path.islink(os.path.join(root, name)):
35                sz = os.path.getsize(os.path.join(root, name))
36                size += sz
37    PrintToLog('total size: {:.2f}M'.format(size/1024/1024))
38    return size
39
40def PrintToLog(str):
41    time = datetime.datetime.now()
42    str = "[{}] {}".format(time, str)
43    print(str)
44    with open(os.path.join(args.save_path, 'L2_mini_test_{}.log'.format(args.device_num)), mode='a', encoding='utf-8') as log_file:
45        console = sys.stdout
46        sys.stdout = log_file
47        print(str)
48        sys.stdout = console
49    log_file.close()
50
51def EnterCmd(mycmd, waittime = 0, printresult = 1):
52    if mycmd == "":
53        return
54    global CmdRetryCnt
55    CmdRetryCnt = 1
56    EnterCmdRetry = 2
57    while EnterCmdRetry:
58        EnterCmdRetry -= 1
59        try:
60            p = subprocess.Popen(mycmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
61            result, unused_err = p.communicate(timeout=25)
62            try:
63                result=result.decode(encoding="utf-8")
64            except UnicodeDecodeError:
65                result=result.decode('gbk', errors='ignore')
66            break
67        except Exception as e:
68            result = 'retry failed again'
69            PrintToLog(e)
70            CmdRetryCnt += 1
71            p.kill()
72    if printresult == 1:
73        with open(os.path.join(args.save_path, 'mini_test_{}.bat'.format(args.device_num)), mode='a', encoding='utf-8') as cmd_file:
74            cmd_file.write(mycmd + '\n')
75        cmd_file.close()
76        PrintToLog(mycmd)
77        PrintToLog(result)
78        sys.stdout.flush()
79    if waittime != 0:
80        time.sleep(waittime)
81        if printresult == 1:
82            with open(os.path.join(args.save_path, 'mini_test_{}.bat'.format(args.device_num)), mode='a', encoding='utf-8') as cmd_file:
83                cmd_file.write("ping -n {} 127.0.0.1>null\n".format(waittime))
84            cmd_file.close()
85    return result
86
87def EnterShellCmd(shellcmd, waittime = 0, printresult = 1):
88    if shellcmd == "":
89        return
90    cmd = "hdc_std -t {} shell \"{}\"".format(args.device_num, shellcmd)
91    return EnterCmd(cmd, waittime, printresult)
92
93if __name__ == "__main__":
94    parser = argparse.ArgumentParser(description='manual to this script')
95    parser.add_argument('--save_path', type=str, default = 'D:\\DeviceTestTools\\screenshot')
96    parser.add_argument('--device_num', type=str, default = 'null')
97    parser.add_argument('--archive_path', type=str, default = 'Z:\workspace\ohos_L2\ohos\out\\rk3568\packages\phone')
98    args = parser.parse_args()
99
100    if args.device_num == 'null':
101        result = EnterCmd("hdc_std list targets", 1, 0)
102        print(result)
103        args.device_num = result.split()[0]
104
105    PrintToLog("\n\n########## First check key processes start ##############")
106    lose_process = []
107    process_pid = {}
108
109    two_check_process_list = ['huks_service', 'hilogd', 'hdf_devmgr', 'samgr', 'foundation', 'accesstoken_ser',]
110    other_process_list = ['softbus_server', 'deviceauth_service']
111
112    for pname in two_check_process_list:
113        pids = EnterCmd("hdc_std -t {} shell pidof {}".format(args.device_num, pname), 0, 1)
114        try:
115            pidlist = pids.split()
116            int(pidlist[0])
117            for pid in pidlist:
118                int(pid)
119            process_pid[pname] = pidlist
120        except:
121            lose_process.append(pname)
122    all_p = EnterShellCmd("ps -elf")
123    for pname in other_process_list:
124        findp = all_p.find(pname, 0, len(all_p))
125        if findp == -1:
126            lose_process.append(pname)
127
128    if lose_process:
129        PrintToLog("\n\nERROR: %s, These processes are not exist!!!\n" % lose_process)
130        PrintToLog("End of check, test failed!")
131        sys.exit(99)
132    else:
133        PrintToLog("First processes check is ok\n")
134
135    res = EnterCmd("hdc_std -t {} shell hidumper --mem".format(args.device_num), 0, 1)
136    process_usage = int(res.split(':')[-1].split()[0]) / 1024
137    if process_usage > 40:
138        PrintToLog(
139            "ERROR: Processes usage cannot be greater than 40M, but currently it's actually %.2fM" % process_usage)
140        PrintToLog("End of check, test failed!")
141        sys.exit(99)
142
143    time.sleep(10)
144
145    PrintToLog("\n\n########## Second check key processes start ##############")
146    second_check_lose_process = []
147    for pname in two_check_process_list:
148        pids = EnterCmd("hdc_std -t {} shell pidof {}".format(args.device_num, pname), 0, 1)
149        try:
150            pidlist = pids.split()
151            if process_pid[pname] != pidlist:
152                if pname in two_check_process_list:
153                    PrintToLog("ERROR: pid of %s is different the first check" % pname)
154                    PrintToLog("SmokeTest find some fatal problems!")
155                    PrintToLog("End of check, test failed!")
156                    sys.exit(99)
157                else:
158                    PrintToLog("WARNNING: pid of %s is different the first check" % pname)
159            elif len(pidlist) != 1:
160                if pname in two_check_process_list:
161                    PrintToLog("ERROR: pid of %s is not only one" % pname)
162                    PrintToLog("SmokeTest find some fatal problems!")
163                    PrintToLog("End of check, test failed!")
164                    sys.exit(99)
165                else:
166                    PrintToLog("WARNNING: pid of %s is not only one" % pname)
167        except:
168            second_check_lose_process.append(pname)
169
170    if second_check_lose_process:
171        PrintToLog("ERROR: pid of %s is not exist" % pname)
172        PrintToLog("SmokeTest find some fatal problems!")
173        PrintToLog("End of check, test failed!")
174        sys.exit(99)
175    else:
176        PrintToLog("Second processes check is ok\n")
177
178    target_dir = os.path.normpath(os.path.join(args.archive_path, "system"))
179    PrintToLog(target_dir)
180    ret_size = GetDirSize(target_dir)/1024/1024
181    PrintToLog('Size of system is :{:.2f}M'.format(ret_size))
182    if ret_size > 50:
183        PrintToLog('ERROR: Size of system({:.2f}M) is over the upper limit(50M)'.format(ret_size))
184        PrintToLog("End of check, test failed!")
185        sys.exit(99)
186
187    target_dir = os.path.normpath(os.path.join(args.archive_path, "data"))
188    ret_size = GetDirSize(target_dir)/1024/1024
189    PrintToLog('Size of data is :{:.2f}M'.format(ret_size))
190    if ret_size > 50:
191        PrintToLog('ERROR: Size of data({:.2f}M) is over the upper limit(50M)'.format(ret_size))
192        PrintToLog("End of check, test failed!")
193        sys.exit(99)
194
195    target_dir = os.path.normpath(os.path.join(args.archive_path, "updater"))
196    ret_size = GetDirSize(target_dir)/1024/1024
197    PrintToLog('Size of updater is :{:.2f}M'.format(ret_size))
198    if ret_size > 50:
199        PrintToLog('ERROR: Size of updater({:.2f}M) is over the upper limit(50M)'.format(ret_size))
200        PrintToLog("End of check, test failed!")
201        sys.exit(99)
202
203    target_dir = os.path.normpath(os.path.join(args.archive_path, "vendor"))
204    ret_size = GetDirSize(target_dir)/1024/1024
205    PrintToLog('Size of vendor is :{:.2f}M'.format(ret_size))
206    if ret_size > 50:
207        PrintToLog('ERROR: Size of vendor({:.2f}M) is over the upper limit(50M)'.format(ret_size))
208        PrintToLog("End of check, test failed!")
209        sys.exit(99)
210
211    PrintToLog("All testcase is ok")
212    PrintToLog("End of check, test succeeded!")
213    sys.exit(0)
214