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
18import subprocess
19from utils import *
20
21@print_check_result
22def CheckBuildId(output) -> bool:
23    ret = re.search("BuildId: (.+?) [\d].[\d].[\d].[\d]", output)
24    return ret is not None
25
26@print_check_result
27def CheckOsVersion(output) -> bool:
28    ret = re.search("OsVersion: (.+?)[\d].[\d].[\d].[\d]", output)
29    return ret is not None
30
31@print_check_result
32def CheckProcVersion(output) -> bool:
33    ret = re.search("/proc/version\n\n([^\n]+\n)+\n", output)
34    return ret is not None
35
36@print_check_result
37def CheckCmdline(output) -> bool:
38    ret = re.search("/proc/cmdline\n\n([^\n]+\n)+\n", output)
39    return ret is not None
40
41@print_check_result
42def CheckWakeUpSource(output) -> bool:
43    ret = re.search("/sys/kernel/debug/wakeup_sources\n\n([^\n]+\n){4,}", output)
44    return ret is not None
45
46@print_check_result
47def CheckUpTime(output) -> bool:
48    ret = re.search("cmd is: uptime -p\n(\n[^\n]+)\n", output)
49    return ret is not None
50
51@print_check_result
52def CheckPrintEnv(output) -> bool:
53    ret = re.search("cmd is: printenv\n\n([^\n]+){4,}\n", output)
54    return ret is not None
55
56@print_check_result
57def CheckProcModules(output) -> bool:
58    ret = re.search("/proc/modules\n\n([^\n]+)\n", output)
59    return ret is not None
60
61@print_check_result
62def CheckLsmod(output) -> bool:
63    ret = re.search("cmd is: lsmod\n\n([^\n]+)\n", output)
64    return ret is not None
65
66@print_check_result
67def CheckSlabinfo(output) -> bool:
68    ret = re.search("/proc/slabinfo\n\n([^\n]+){4,}\n", output)
69    return ret is not None
70
71@print_check_result
72def CheckZoneinfo(output) -> bool:
73    ret = re.search("/proc/zoneinfo\n\n([^\n]+){4,}\n", output)
74    return ret is not None
75
76@print_check_result
77def CheckVmstat(output) -> bool:
78    ret = re.search("/proc/vmstat\n\n([^\n]+)\n", output)
79    return ret is not None
80
81@print_check_result
82def CheckVmallocinfo(output) -> bool:
83    ret = re.search("/proc/vmallocinfo\n\n([^\n]+)\n", output)
84    return ret is not None
85
86
87def CheckHidumperHelpOutput(output):
88    return "usage:" in output
89
90class TestBaseCommand:
91
92    @pytest.mark.L0
93    def test_hidumper_help(self):
94        # 校验命令行输出
95        CheckCmd("hidumper -h", lambda output : "usage:" in output)
96        # 校验命令行重定向输出
97        CheckCmdRedirect("hidumper -h", lambda output : "usage:" in output)
98
99    @pytest.mark.L0
100    def test_hidumper_c_all(self):
101        CheckFunc = lambda output : all([check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion,
102                                                                    CheckCmdline, CheckUpTime, CheckPrintEnv, CheckLsmod,
103                                                                    CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]])
104        # 校验命令行输出
105        CheckCmd("hidumper -c", CheckFunc)
106        # 校验命令行重定向输出
107        CheckCmdRedirect("hidumper -c", CheckFunc)
108        # 校验命令行输出到zip文件
109        CheckCmdZip("hidumper -c", CheckFunc)
110
111    @pytest.mark.L0
112    def test_hidumper_c_base(self):
113        command = "hidumper -c base"
114        CheckFunc = lambda output : all([check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion, CheckCmdline, CheckUpTime]])
115        # 校验命令行输出
116        CheckCmd(command, CheckFunc)
117        # 校验命令行重定向输出
118        CheckCmdRedirect(command, CheckFunc)
119        # 校验命令行输出到zip文件
120        CheckCmdZip(command, CheckFunc)
121
122    @pytest.mark.L0
123    def test_hidumper_c_system(self):
124        command = "hidumper -c system"
125        CheckFunc = lambda output : all([check(output) for check in [CheckPrintEnv, CheckLsmod, CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]])
126        # 校验命令行输出
127        CheckCmd(command, CheckFunc)
128        # 校验命令行重定向输出
129        CheckCmdRedirect(command, CheckFunc)
130        # 校验命令行输出到zip文件
131        CheckCmdZip(command, CheckFunc)
132
133    @pytest.mark.L0
134    def test_hidumper_e(self):
135        command = "hidumper -e"
136        CheckFunc = lambda output : "faultlog" in output
137        # 校验命令行输出
138        CheckCmd(command, CheckFunc)
139        # 校验命令行重定向输出
140        CheckCmdRedirect(command, CheckFunc)
141        # 校验命令行输出到zip文件
142        CheckCmdZip(command, CheckFunc)
143
144    @pytest.mark.L0
145    def test_hidumper_error_option(self):
146        command = "hdc shell \"hidumper -D -h\""
147        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
148        assert "option pid missed." in output
149
150        command = "hdc shell \"hidumper -h -D\""
151        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
152        assert "usage:" in output
153
154
155
156