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 pytest 17import re 18from utils import * 19 20def CheckCpuUsageOutput(output): 21 ret = re.search("PID Total Usage User Space Kernel Space Page Fault Minor Page Fault Major Name\n([^\n]+\n){4,}", output) 22 return ret is not None 23 24def CheckCpuUsageWithPidOutput(output): 25 ret = re.search("PID Total Usage User Space Kernel Space Page Fault Minor Page Fault Major Name\n([^\n]+\n){1,}", output) 26 return ret is not None 27 28def CheckCpufreqOutput(output): 29 ret = re.search("cmd is: cat /sys/devices/system/cpu/cpu\d/cpufreq/cpuinfo_cur_freq\n\n\d+", output) 30 return ret is not None 31 32class TestHidumperCpu: 33 34 @pytest.mark.L0 35 def test_cpuusage_all(self): 36 command = "hidumper --cpuusage" 37 hidumperTmpCmd = "OPT:cpuusage SUB_OPT:" 38 # 校验命令行输出 39 CheckCmd(command, CheckCpuUsageOutput, hidumperTmpCmd) 40 # 校验命令行重定向输出 41 CheckCmdRedirect(command, CheckCpuUsageOutput, None, hidumperTmpCmd) 42 # 校验命令行输出到zip文件 43 CheckCmdZip(command, CheckCpuUsageOutput) 44 45 @pytest.mark.L0 46 def test_cpuusage_pid(self): 47 command = "hidumper --cpuusage 1" 48 hidumperTmpCmd = "OPT:cpuusage SUB_OPT:" 49 # 校验命令行输出 50 CheckCmd(command, CheckCpuUsageWithPidOutput, hidumperTmpCmd) 51 # 校验命令行重定向输出 52 CheckCmdRedirect(command, CheckCpuUsageWithPidOutput, None, hidumperTmpCmd) 53 # 校验命令行输出到zip文件 54 CheckCmdZip(command, CheckCpuUsageWithPidOutput) 55 56 @pytest.mark.L3 57 def test_cpuusage_error_pid(self): 58 command = f"hidumper --cpuusage 2147483647;hidumper --cpuusage -2147483647" 59 hidumperTmpCmd = "OPT:cpuusage SUB_OPT:" 60 # 校验命令行输出 61 CheckCmd(command, lambda output : "hidumper: No such process: 2147483647\nhidumper: option pid missed. 2" in output, hidumperTmpCmd) 62 command = f"hidumper --cpuusage 2147483648;hidumper --cpuusage -2147483648" 63 CheckCmd(command, lambda output : "hidumper: option pid missed. 2" in output, hidumperTmpCmd) 64 65 @pytest.mark.L0 66 def test_cpufreq(self): 67 command = "hidumper --cpufreq" 68 hidumperTmpCmd = "OPT:cpufreq SUB_OPT:" 69 # 校验命令行输出 70 CheckCmd(command, CheckCpufreqOutput, hidumperTmpCmd) 71 # 校验命令行重定向输出 72 CheckCmdRedirect(command, CheckCpufreqOutput, None, hidumperTmpCmd) 73 # 校验命令行输出到zip文件 74 CheckCmdZip(command, CheckCpufreqOutput)