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 113 @pytest.mark.L0 114 def test_hidumper_c_all(self): 115 hidumperTmpCmd = "OPT:c SUB_OPT:" 116 CheckFunc = lambda output : all([check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion, 117 CheckCmdline, CheckUpTime, CheckPrintEnv, CheckLsmod, 118 CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]]) 119 # 校验命令行输出 120 CheckCmd("hidumper -c", CheckFunc, hidumperTmpCmd) 121 122 @pytest.mark.L0 123 def test_hidumper_c_base(self): 124 command = "hidumper -c base" 125 hidumperTmpCmd = "OPT:c SUB_OPT:base" 126 CheckFunc = lambda output : all([check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion, CheckCmdline, CheckUpTime]]) 127 # 校验命令行输出 128 CheckCmd(command, CheckFunc, hidumperTmpCmd) 129 130 @pytest.mark.L0 131 def test_hidumper_c_system(self): 132 command = "hidumper -c system" 133 hidumperTmpCmd = "OPT:c SUB_OPT:system" 134 CheckFunc = lambda output : all([check(output) for check in [CheckPrintEnv, CheckLsmod, CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]]) 135 # 校验命令行输出 136 CheckCmd(command, CheckFunc, hidumperTmpCmd) 137 138 @pytest.mark.L0 139 def test_hidumper_e(self): 140 command = "hidumper -e" 141 hidumperTmpCmd = "OPT:e SUB_OPT:" 142 CheckFunc = lambda output : "faultlog" in output 143 # 校验命令行输出 144 CheckCmd(command, CheckFunc, hidumperTmpCmd) 145 146 @pytest.mark.L0 147 def test_hidumper_error_option(self): 148 command = "hdc shell \"hidumper safsadf -h\"" 149 output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True) 150 assert "option pid missed." in output 151 152 command = "hdc shell \"hidumper -h -D\"" 153 output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True) 154 assert "usage:" in output 155 156 command = "hdc shell \"hidumper -habc\"" 157 output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True) 158 assert "usage:" in output 159 160 @pytest.mark.L2 161 def test_hidumper_service_exit(self): 162 command = "hdc shell \"hidumper -h\"" 163 output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True) 164 command = "hdc shell \"pidof hidumper_service\"" 165 output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True) 166 assert int(output.strip()) > 0 167 time.sleep(120) # 120s 168 command = "hdc shell \"pidof hidumper_service\"" 169 output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True) 170 output = output.strip('\n') 171 assert output == "" 172 173