• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
19import time
20from utils import *
21
22@print_check_result
23def CheckBuildId(output) -> bool:
24    ret = re.search("BuildId: (.+?) [\d].[\d].[\d].[\d]", output)
25    return ret is not None
26
27@print_check_result
28def CheckOsVersion(output) -> bool:
29    ret = re.search("OsVersion: (.+?)[\d].[\d].[\d].[\d]", output)
30    return ret is not None
31
32@print_check_result
33def CheckProcVersion(output) -> bool:
34    ret = re.search("/proc/version\n\n([^\n]+\n)+\n", output)
35    return ret is not None
36
37@print_check_result
38def CheckCmdline(output) -> bool:
39    ret = re.search("/proc/cmdline\n\n([^\n]+\n)+\n", output)
40    return ret is not None
41
42@print_check_result
43def CheckWakeUpSource(output) -> bool:
44    ret = re.search("/sys/kernel/debug/wakeup_sources\n\n([^\n]+\n){4,}", output)
45    return ret is not None
46
47@print_check_result
48def CheckUpTime(output) -> bool:
49    ret = re.search("cmd is: uptime -p\n(\n[^\n]+)\n", output)
50    return ret is not None
51
52@print_check_result
53def CheckPrintEnv(output) -> bool:
54    ret = re.search("cmd is: printenv\n\n([^\n]+){4,}\n", output)
55    return ret is not None
56
57@print_check_result
58def CheckProcModules(output) -> bool:
59    ret = re.search("/proc/modules\n\n([^\n]+)\n", output)
60    return ret is not None
61
62@print_check_result
63def CheckLsmod(output) -> bool:
64    ret = re.search("cmd is: lsmod\n\n([^\n]+)\n", output)
65    return ret is not None
66
67@print_check_result
68def CheckSlabinfo(output) -> bool:
69    ret = re.search("/proc/slabinfo\n\n([^\n]+){4,}\n", output)
70    return ret is not None
71
72@print_check_result
73def CheckZoneinfo(output) -> bool:
74    ret = re.search("/proc/zoneinfo\n\n([^\n]+){4,}\n", output)
75    return ret is not None
76
77@print_check_result
78def CheckVmstat(output) -> bool:
79    ret = re.search("/proc/vmstat\n\n([^\n]+)\n", output)
80    return ret is not None
81
82@print_check_result
83def CheckVmallocinfo(output) -> bool:
84    ret = re.search("/proc/vmallocinfo\n\n([^\n]+)\n", output)
85    return ret is not None
86
87
88def CheckHidumperHelpOutput(output):
89    return "usage:" in output
90
91class TestBaseCommand:
92
93    @pytest.mark.L0
94    def test_hidumper_help(self):
95        hidumperTmpCmd = "ERROR_MESSAGE:parse cmd fail"
96        # 校验命令行输出
97        CheckCmd("hidumper -h", lambda output : "mem" in output, hidumperTmpCmd)
98        command = "hdc shell \"hidumper -h |grep mem\""
99        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
100        assert "mem" in output
101
102    @pytest.mark.L0
103    def test_hidumper_lc(self):
104        command = "hidumper -lc"
105        # 设置hisysevent相关信息
106        hidumperTmpCmd = "OPT:lc SUB_OPT:"
107        # 校验命令行输出
108        CheckCmd(command, lambda output : "base                             system" in output, hidumperTmpCmd)
109        # 校验-l拼接不存在字符输出
110        CheckCmd("hidumper -ld", lambda output : "option pid missed." in output, hidumperTmpCmd)
111        CheckCmd("hidumper -lcd", lambda output : "option pid missed." in output, hidumperTmpCmd)
112        CheckCmd("hidumper -l", lambda output : "invalid arg: -1" in output, hidumperTmpCmd)
113
114    @pytest.mark.L0
115    def test_hidumper_c_all(self):
116        hidumperTmpCmd = "OPT:c SUB_OPT:"
117        CheckFunc = lambda output : all([check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion,
118                                                                    CheckCmdline, CheckUpTime, CheckPrintEnv, CheckLsmod,
119                                                                    CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]])
120        # 校验命令行输出
121        CheckCmd("hidumper -c", CheckFunc, hidumperTmpCmd)
122
123    @pytest.mark.L0
124    def test_hidumper_c_base(self):
125        command = "hidumper -c base"
126        hidumperTmpCmd = "OPT:c SUB_OPT:base"
127        CheckFunc = lambda output : all([check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion, CheckCmdline, CheckUpTime]])
128        # 校验命令行输出
129        CheckCmd(command, CheckFunc, hidumperTmpCmd)
130
131    @pytest.mark.L0
132    def test_hidumper_c_system(self):
133        command = "hidumper -c system"
134        hidumperTmpCmd = "OPT:c SUB_OPT:system"
135        CheckFunc = lambda output : all([check(output) for check in [CheckPrintEnv, CheckLsmod, CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]])
136        # 校验命令行输出
137        CheckCmd(command, CheckFunc, hidumperTmpCmd)
138
139    @pytest.mark.L0
140    def test_hidumper_e(self):
141        command = "hidumper -e"
142        hidumperTmpCmd = "OPT:e SUB_OPT:"
143        CheckFunc = lambda output : "faultlog" in output
144        # 校验命令行输出
145        CheckCmd(command, CheckFunc, hidumperTmpCmd)
146
147    @pytest.mark.L0
148    def test_hidumper_error_option(self):
149        command = "hdc shell \"hidumper safsadf -h\""
150        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
151        assert "option pid missed." in output
152
153        command = "hdc shell \"hidumper -h -D\""
154        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
155        assert "usage:" in output
156
157        command = "hdc shell \"hidumper -habc\""
158        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
159        assert "usage:" in output
160
161    @pytest.mark.L2
162    def test_hidumper_service_exit(self):
163        command = "hdc shell \"hidumper -h\""
164        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
165        command = "hdc shell \"pidof hidumper_service\""
166        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
167        assert int(output.strip()) > 0
168        time.sleep(120) # 120s
169        command = "hdc shell \"pidof hidumper_service\""
170        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
171        output = output.strip('\n')
172        assert output == ""
173
174