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        # 校验命令行输出
38        CheckCmd(command, CheckCpuUsageOutput)
39        # 校验命令行重定向输出
40        CheckCmdRedirect(command, CheckCpuUsageOutput)
41        # 校验命令行输出到zip文件
42        CheckCmdZip(command, CheckCpuUsageOutput)
43
44    @pytest.mark.L0
45    def test_cpuusage_pid(self):
46        command = "hidumper --cpuusage 1"
47        # 校验命令行输出
48        CheckCmd(command, CheckCpuUsageWithPidOutput)
49        # 校验命令行重定向输出
50        CheckCmdRedirect(command, CheckCpuUsageWithPidOutput)
51        # 校验命令行输出到zip文件
52        CheckCmdZip(command, CheckCpuUsageWithPidOutput)
53
54    @pytest.mark.L0
55    def test_cpufreq(self):
56        command = "hidumper --cpufreq"
57        # 校验命令行输出
58        CheckCmd(command, CheckCpufreqOutput)
59        # 校验命令行重定向输出
60        CheckCmdRedirect(command, CheckCpufreqOutput)
61        # 校验命令行输出到zip文件
62        CheckCmdZip(command, CheckCpufreqOutput)