1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (C) 2024 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 16import subprocess 17import pytest 18import time 19 20 21def get_shell_result(cmd, words=''): 22 print(f"\nexecuting command: {cmd}") 23 output = subprocess.check_output(cmd.split()).decode() 24 print(f"\noutput: {output}") 25 if len(words) > 0: 26 assert words in output 27 28 29class TestHitraceCmd: 30 def prepare_steps(self): 31 word_cmds = { 32 "hdc shell hitrace --stop_bgsrv", 33 "hdc shell hitrace --trace_finish_nodump", 34 "hdc shell hitrace --trace_finish_nodump --record", 35 } 36 37 for word_cmd in word_cmds: 38 get_shell_result(word_cmd) 39 40 @pytest.mark.L0 41 def test_help1(self): 42 word_cmds = { 43 'hdc shell hitrace -h -b 2048':'running_state is SHOW_HELP', 44 'hdc shell hitrace -b 2048 -h':'running_state is SHOW_HELP', 45 'hdc shell hitrace -h -b':'error: parsing args failed', 46 'hdc shell hitrace -b -h': 'error: buffer size is illegal input.' 47 } 48 49 for word_cmd in word_cmds: 50 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 51 52 @pytest.mark.L0 53 def test_help2(self): 54 word_cmds = { 55 'hdc shell hitrace -h --trace_begin': 'cannot coexist.', 56 'hdc shell hitrace --trace_begin app -h': 'cannot coexist.' 57 } 58 59 for word_cmd in word_cmds: 60 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 61 62 @pytest.mark.L0 63 def test_list1(self): 64 word_cmds = { 65 'hdc shell hitrace -l':'running_state is SHOW_LIST_CATEGORY', 66 } 67 68 for word_cmd in word_cmds: 69 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 70 71 @pytest.mark.L0 72 def test_list2(self): 73 word_cmds = { 74 'hdc shell hitrace -b 102400 -l':'running_state is SHOW_LIST_CATEGORY', 75 'hdc shell hitrace app -l':'running_state is SHOW_LIST_CATEGORY', 76 'hdc shell hitrace --trace_begin -l': 'cannot coexist.', 77 } 78 79 for word_cmd in word_cmds: 80 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 81 82 @pytest.mark.L0 83 def test_unsupported_tag(self): 84 word_cmds = { 85 'hdc shell hitrace --trace_begin appoo': 'error: appoo is not support category on this device.', 86 } 87 88 for word_cmd in word_cmds: 89 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 90 91 @pytest.mark.L0 92 def test_capture_trace(self): 93 word_cmds1 = { 94 'hdc shell hitrace -b 256 -t 5 sched --trace_begin':'hitrace enter, running_state is RECORDING_LONG_BEGIN', 95 'hdc shell cat /sys/kernel/debug/tracing/tracing_on':'1', 96 } 97 98 word_cmds2 = { 99 "hdc shell hitrace --trace_finish_nodump", 100 } 101 102 for word_cmd in word_cmds1: 103 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 104 105 for word_cmd in word_cmds2: 106 get_shell_result(word_cmd) 107 108 @pytest.mark.L0 109 def test_double_capture_trace(self): 110 word_cmds1 = { 111 'hdc shell hitrace -b 256 -t 5 sched --trace_begin': 'hitrace enter, running_state is RECORDING_LONG_BEGIN', 112 } 113 114 word_cmds2 = { 115 'hdc shell hitrace -b 256 -t 5 sched --trace_begin': 'error: OpenRecording failed, errorCode(1103)', 116 } 117 118 word_cmds3 = { 119 'hdc shell hitrace --trace_finish_nodump', 120 } 121 122 for word_cmd in word_cmds1: 123 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 124 125 for word_cmd in word_cmds2: 126 get_shell_result(word_cmd, word_cmds2.get(word_cmd)) 127 128 for cmd in word_cmds3: 129 get_shell_result(cmd) 130 131 @pytest.mark.L0 132 def test_capture_tags_trace(self): 133 word_cmds1 = { 134 'hdc shell hitrace ability accesscontrol accessibility account ace animation sched --trace_begin':'hitrace enter, running_state is RECORDING_LONG_BEGIN', 135 } 136 137 word_cmds2 = { 138 'hdc shell cat /sys/kernel/debug/tracing/tracing_on':'1', 139 } 140 141 word_cmds3 = { 142 'hdc shell hitrace --trace_dump -o /data/local/tmp/trace.ftrace': 'trace read done', 143 } 144 145 word_cmds4 = [ 146 "hdc shell hitrace --trace_finish_nodump", 147 ] 148 149 for word_cmd in word_cmds1: 150 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 151 152 for word_cmd in word_cmds2: 153 get_shell_result(word_cmd, word_cmds2.get(word_cmd)) 154 155 for word_cmd in word_cmds3: 156 get_shell_result(word_cmd, word_cmds3.get(word_cmd)) 157 158 for word_cmd in word_cmds4: 159 get_shell_result(word_cmd) 160 161 @pytest.mark.L0 162 def test_capture_trace_with_overwrite_and_traceclock(self): 163 word_cmds1 = { 164 'hdc shell hitrace sched --trace_begin --overwrite --trace_clock boot': 'clockType:boot overwrite:0', 165 } 166 167 word_cmds2 = { 168 'hdc shell hitrace --trace_dump -o /data/local/tmp/trace.ftrace': 'trace read done, output: /data/local/tmp/trace.ftrace', 169 } 170 171 word_cmds3 = { 172 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 173 } 174 175 word_cmds4 = [ 176 "hdc shell hitrace --trace_finish_nodump", 177 ] 178 179 for word_cmd in word_cmds1: 180 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 181 182 for word_cmd in word_cmds2: 183 get_shell_result(word_cmd, word_cmds2.get(word_cmd)) 184 185 for word_cmd in word_cmds3: 186 get_shell_result(word_cmd, word_cmds3.get(word_cmd)) 187 188 for word_cmd in word_cmds4: 189 get_shell_result(word_cmd) 190 191 @pytest.mark.L0 192 def test_raw_parameter(self): 193 word_cmds = { 194 'hdc shell hitrace --stop_bgsrv': '', 195 'hdc shell hitrace sched --trace_begin --raw': 'RECORDING_LONG_BEGIN and RECORDING_SHORT_RAW cannot coexist', 196 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 197 'hdc shell hitrace sched --trace_begin --record --raw': 'RECORDING_LONG_BEGIN_RECORD and RECORDING_SHORT_RAW cannot coexist', 198 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 199 'hdc shell hitrace sched -t 10 --raw': 'hitrace enter, running_state is RECORDING_SHORT_RAW', 200 } 201 202 for word_cmd in word_cmds: 203 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 204 205 @pytest.mark.L0 206 def test_text_parameter(self): 207 word_cmds = { 208 'hdc shell hitrace --stop_bgsrv': '', 209 'hdc shell hitrace sched --trace_begin --text': 'RECORDING_LONG_BEGIN and RECORDING_SHORT_TEXT cannot coexist', 210 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 211 'hdc shell hitrace sched --trace_begin --record --text': 'RECORDING_LONG_BEGIN_RECORD and RECORDING_SHORT_TEXT cannot coexist', 212 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 213 'hdc shell hitrace sched -t 10 --text': 'start capture, please wait 10s', 214 } 215 216 for word_cmd in word_cmds: 217 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 218 219 @pytest.mark.L0 220 def test_capture_trace_with_filesize_and_filename(self): 221 word_cmds1 = { 222 'hdc shell hitrace --stop_bgsrv': '', 223 'hdc shell hitrace sched --trace_begin --file_size 51200': 'The current state does not support specifying the file size', 224 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 225 'hdc shell hitrace sched --trace_dump -o /data/local/tmp/trace.ftrace': 'trace read done, output: /data/local/tmp/trace.ftrace', 226 } 227 228 word_cmds2 = [ 229 "hdc shell hitrace sched --trace_finish_nodump", 230 ] 231 232 for word_cmd in word_cmds1: 233 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 234 235 for word_cmd in word_cmds2: 236 get_shell_result(word_cmd) 237 238 @pytest.mark.L0 239 def test_capture_trace_record_with_filesize_and_filename(self): 240 word_cmds1 = { 241 'hdc shell hitrace --stop_bgsrv': '', 242 'hdc shell hitrace sched --trace_begin --record --file_size 51200': 'tags:sched bufferSize:18432 overwrite:1 fileSize:51200', 243 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 244 'hdc shell hitrace --trace_dump --record -o /data/local/tmp/trace.ftrace': 'error: "--record" is set incorrectly. eg: "--trace_begin --record", "--trace_finish --record"', 245 } 246 247 word_cmds2 = [ 248 "hdc shell hitrace --trace_finish_nodump --record", 249 ] 250 251 for word_cmd in word_cmds1: 252 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 253 254 for word_cmd in word_cmds2: 255 get_shell_result(word_cmd) 256 257 @pytest.mark.L0 258 def test_capture_trace_with_buffersize_time_trackclock_and_overwrite(self): 259 word_cmds1 = { 260 'hdc shell hitrace --stop_bgsrv': '', 261 'hdc shell hitrace sched --trace_begin -b 10240 -t 5 --trace_clock global --overwrite': 'tags:sched bufferSize:10240 clockType:global overwrite:0', 262 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 263 'hdc shell hitrace --trace_dump -o /data/local/tmp/trace.ftrace': 'trace read done, output: /data/local/tmp/trace.ftrace', 264 } 265 266 word_cmds2 = [ 267 "hdc shell hitrace --trace_finish_nodump", 268 ] 269 270 for word_cmd in word_cmds1: 271 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 272 273 for word_cmd in word_cmds2: 274 get_shell_result(word_cmd) 275 276 @pytest.mark.L0 277 def test_capture_trace_with_wrong_time(self): 278 word_cmds = { 279 'hdc shell hitrace --stop_bgsrv': '', 280 'hdc shell hitrace sched --trace_begin -b 10240 -t -1 --trace_clock global --overwrite': 'error: "-t -1" to be greater than zero. eg: "--time 5"', 281 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 282 } 283 284 for word_cmd in word_cmds: 285 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 286 287 @pytest.mark.L0 288 def test_capture_trace_with_wrong_buffersize(self): 289 word_cmds = { 290 'hdc shell hitrace sched --trace_begin -b -10240 -t 5 --trace_clock global --overwrite': 'parsing args failed, exit', 291 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 292 } 293 294 for word_cmd in word_cmds: 295 get_shell_result(word_cmd, word_cmds.get(word_cmd)) 296 297 @pytest.mark.L0 298 def test_capture_trace_with_max_and_min_buffersize(self): 299 word_cmds1 = { 300 'hdc shell hitrace sched --trace_begin -b 307200': 'hitrace enter, running_state is RECORDING_LONG_BEGIN', 301 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 302 } 303 304 word_cmds2 = [ 305 "hdc shell hitrace --trace_finish_nodump", 306 ] 307 308 word_cmds4 = { 309 'hdc shell hitrace sched --trace_begin -b 256': 'hitrace enter, running_state is RECORDING_LONG_BEGIN', 310 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 311 } 312 313 word_cmds5 = [ 314 "hdc shell hitrace --trace_finish_nodump", 315 ] 316 317 for word_cmd in word_cmds1: 318 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 319 320 for word_cmd in word_cmds2: 321 get_shell_result(word_cmd) 322 323 for word_cmd in word_cmds4: 324 get_shell_result(word_cmd, word_cmds4.get(word_cmd)) 325 326 for word_cmd in word_cmds5: 327 get_shell_result(word_cmd) 328 329 @pytest.mark.L0 330 def test_wrong_command(self): 331 word_cmds1 = { 332 'hdc shell hitrace sched --trace_begin --trace_finish': 'the parameter is set incorrectly', 333 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 334 } 335 336 word_cmds2 = { 337 'hdc shell hitrace sched --trace_begin --trace_dump': 'the parameter is set incorrectly', 338 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 339 } 340 341 for word_cmd in word_cmds1: 342 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 343 344 for word_cmd in word_cmds2: 345 get_shell_result(word_cmd, word_cmds2.get(word_cmd)) 346 347 @pytest.mark.L0 348 def test_start_and_stop_bgsrv(self): 349 word_cmds1 = { 350 'hdc shell hitrace --stop_bgsrv': '', 351 'hdc shell hitrace --trace_finish_nodump': '', 352 'hdc shell hitrace --start_bgsrv': 'hitrace enter, running_state is SNAPSHOT_START', 353 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 354 } 355 356 word_cmds2 = { 357 'hdc shell hitrace --stop_bgsrv': 'hitrace enter, running_state is SNAPSHOT_STOP', 358 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 359 } 360 361 for word_cmd in word_cmds1: 362 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 363 364 for word_cmd in word_cmds2: 365 get_shell_result(word_cmd, word_cmds2.get(word_cmd)) 366 367 @pytest.mark.L0 368 def test_snapshot_with_wrong_command(self): 369 word_cmds1 = { 370 'hdc shell hitrace --stop_bgsrv': '', 371 'hdc shell hitrace --start_bgsrv': 'hitrace enter, running_state is SNAPSHOT_START', 372 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 373 } 374 375 word_cmds2 = { 376 'hdc shell hitrace --start_bgsrv': 'error: OpenSnapshot failed, errorCode(1103)', 377 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '1', 378 } 379 380 word_cmds3 = { 381 'hdc shell hitrace --stop_bgsrv': 'hitrace enter, running_state is SNAPSHOT_STOP', 382 'hdc shell cat /sys/kernel/debug/tracing/tracing_on': '0', 383 } 384 385 for word_cmd in word_cmds1: 386 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 387 388 for word_cmd in word_cmds2: 389 get_shell_result(word_cmd, word_cmds2.get(word_cmd)) 390 391 for word_cmd in word_cmds3: 392 get_shell_result(word_cmd, word_cmds3.get(word_cmd)) 393 394 @pytest.mark.L0 395 def test_capture_cpu_idle(self): 396 get_shell_result('hdc shell hitrace --stop_bgsrv', '') 397 word_cmds1 = { 398 'hdc shell hitrace -t 5 idle': 'cpu_idle', 399 } 400 401 for word_cmd in word_cmds1: 402 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 403 404 @pytest.mark.L0 405 def test_max_parameters_cmd(self): 406 hitrace_cmd = 'hdc shell hitrace -t 5' 407 for i in range(1, 256): 408 hitrace_cmd = '{} {}'.format(hitrace_cmd, 'app') 409 word_cmds1 = { 410 hitrace_cmd: 'error: the number of input arguments exceeds the upper limit', 411 } 412 413 for word_cmd in word_cmds1: 414 get_shell_result(word_cmd, word_cmds1.get(word_cmd)) 415 416 417