• 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
25import serial
26import threading
27
28def GetDirSize(dir_path):
29    if  not os.path.exists(dir_path):
30        PrintToLog("\n\nERROR: %s, dir are not exist!!!\n" % dir_path)
31        PrintToLog("End of check, test failed!")
32        sys.exit(99)
33    size = 0
34    for root, dirs, files in os.walk(dir_path):
35        for name in files:
36            sz = os.path.getsize(os.path.join(root, name))
37            print('{} : {}byte'.format(os.path.join(root, name), sz))
38            size += sz
39    PrintToLog('total size: {:.2f}M'.format(size/1024/1024))
40    return size
41
42def PrintToLog(str):
43    time = datetime.datetime.now()
44    str = "[{}] {}".format(time, str)
45    print(str)
46    with open(os.path.join(args.save_path, 'L1_mini_test.log'), mode='a', encoding='utf-8') as log_file:
47        console = sys.stdout
48        sys.stdout = log_file
49        print(str)
50        sys.stdout = console
51    log_file.close()
52
53def WriteToComPort(com_port, cmd):
54    len = com_port.write(cmd.encode('utf-8'))
55    return
56
57def ReadFromComPort(com_port, timeout):
58    time_start = datetime.datetime.now()
59    time_end = time_start
60    global com_output
61    com_output = ''
62    while (time_end - time_start).seconds < timeout:
63        com_output_once = ''
64        while com_port.inWaiting() > 0:
65            com_output_once += com_port.read(com_port.inWaiting()).decode()
66        if com_output_once != '':
67            com_output += com_output_once
68            print('{}'.format(com_output_once), end='')
69        time_end = datetime.datetime.now()
70    return com_output
71
72if __name__ == "__main__":
73    parser = argparse.ArgumentParser(description='manual to this script')
74    parser.add_argument('--com_port', type=str, default = 'COM8')
75    parser.add_argument('--com_baudrate', type=int, default = 115200)
76    parser.add_argument('--save_path', type=str, default = 'D:\\DeviceTestTools\\screenshot')
77    parser.add_argument('--archive_path', type=str, default = 'D:\DeviceTestTools')
78    args = parser.parse_args()
79
80    com_port = serial.Serial(args.com_port, args.com_baudrate)
81    if com_port.isOpen():
82        PrintToLog("{} is open successed".format(com_port))
83    else:
84        PrintToLog("{} is open failed".format(com_port))
85        PrintToLog("End of check, test failed!")
86        sys.exit(99)
87
88    res = com_port.write("free".encode('utf-8'));
89    read_com_thread = threading.Thread(target=ReadFromComPort, args=(com_port, 10))
90    read_com_thread.setDaemon(True)
91    print('read wait:')
92    read_com_thread.start()
93    time.sleep(1)
94    WriteToComPort(com_port, '\r\n\r\n\r\n')
95    WriteToComPort(com_port, 'task\r\n')
96    print('enter task')
97    time.sleep(3)
98    print(com_output)
99    foundation_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*foundation', com_output)
100    print(foundation_proc_find)
101    if type(foundation_proc_find) == list and len(foundation_proc_find) > 0:
102        PrintToLog('found foundation process')
103    else:
104        PrintToLog('not found foundation process')
105        PrintToLog("End of check, test failed!")
106        sys.exit(99)
107    shell_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*shell', com_output)
108    print(shell_proc_find)
109    if type(shell_proc_find) == list and len(shell_proc_find) > 0:
110        PrintToLog('found shell process')
111    else:
112        PrintToLog('not found shell process')
113        PrintToLog("End of check, test failed!")
114        sys.exit(99)
115    apphilogcat_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*apphilogcat', com_output)
116    print(apphilogcat_proc_find)
117    if type(apphilogcat_proc_find) == list and len(apphilogcat_proc_find) > 0:
118        PrintToLog('found apphilogcat process')
119    else:
120        PrintToLog('not found apphilogcat process')
121        PrintToLog("End of check, test failed!")
122        sys.exit(99)
123    deviceauth_service_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*deviceauth_service', com_output)
124    print(deviceauth_service_proc_find)
125    if type(deviceauth_service_proc_find) == list and len(deviceauth_service_proc_find) > 0:
126        PrintToLog('found deviceauth_service process')
127    else:
128        PrintToLog('not found deviceauth_service process')
129        PrintToLog("End of check, test failed!")
130        sys.exit(99)
131    softbus_server_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*softbus_server', com_output)
132    print(softbus_server_proc_find)
133    if type(softbus_server_proc_find) == list and len(softbus_server_proc_find) > 0:
134        PrintToLog('found softbus_server process')
135    else:
136        PrintToLog('not found softbus_server process')
137        PrintToLog("End of check, test failed!")
138        sys.exit(99)
139    mem_find = re.findall('Mem:\s*\d*\s*\d*\s*\d*\s*\d*', com_output)
140    print(mem_find)
141    if len(mem_find) > 0:
142        mem_size = int(mem_find[0].split()[2]) / 1024 / 1024
143    else:
144        PrintToLog('Error:can find memory usage info!')
145        PrintToLog("End of check, test failed!")
146        sys.exit(99)
147    if mem_size > 25:
148        PrintToLog(
149            'Error:memory usage is over the upper limit(25M),now is {:.2f}'.format(mem_size))
150        PrintToLog("End of check, test failed!")
151        sys.exit(99)
152
153    target_dir = os.path.normpath(os.path.join(args.archive_path, "rootfs"))
154    ret_size = GetDirSize(target_dir)/1024/1024
155    PrintToLog('Size of rootfs is ({:.2f}M)'.format(ret_size))
156    if ret_size > 15:
157        PrintToLog('ERROR: Size of rootfs({:.2f}M) is over the upper limit(15M)'.format(ret_size))
158        PrintToLog("End of check, test failed!")
159        sys.exit(99)
160
161    target_dir = os.path.normpath(os.path.join(args.archive_path, "userfs"))
162    ret_size = GetDirSize(target_dir)/1024/1024
163    PrintToLog('Size of userfs is ({:.2f}M)'.format(ret_size))
164    if ret_size > 15:
165        PrintToLog('ERROR: Size of userfs({:.2f}M) is over the upper limit(15M)'.format(ret_size))
166        PrintToLog("End of check, test failed!")
167        sys.exit(99)
168
169    PrintToLog("End of check, test succeeded!")
170    sys.exit(0)
171