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