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 20@print_check_result 21def check_storaged_u_p(output): 22 result = re.search("cmd is: storaged -u -p\n\n", output) 23 return result is not None 24 25@print_check_result 26def check_df_k(output): 27 result = re.search("cmd is: df -k\n\n([^\n]+\n){4,}", output) 28 return result is not None 29 30@print_check_result 31def check_lsof(output): 32 result = re.search("cmd is: lsof\n\n([^\n]+\n){4,}", output) 33 return result is not None 34 35@print_check_result 36def check_iotop(output): 37 result = re.search("cmd is: iotop -n 1 -m 100\n([^\n]+\n){4,}", output) 38 return result is not None 39 40@print_check_result 41def check_proc_mount(output): 42 result = re.search("/proc/mounts\n\n([^\n]+\n){4,}", output) 43 return result is not None 44 45def CheckStorageWithoutPid(output): 46 ret = all(check(output) for check in [check_storaged_u_p, check_df_k, check_lsof, check_iotop, check_proc_mount]) 47 assert re.search("Filesystem", output) is not None 48 assert re.search("tmpfs", output) is not None 49 assert re.search("block", output) is not None 50 assert re.search("dev", output) is not None 51 return ret 52 53def CheckStorageWithPid(output): 54 result = re.search("storage io", output) 55 return result is not None 56 57class TestHidumperStorage: 58 @pytest.mark.L0 59 def test_storage_all(self): 60 command = "hidumper --storage" 61 hidumperTmpCmd = "OPT:storage SUB_OPT:" 62 # 校验命令行输出 63 CheckCmd(command, CheckStorageWithoutPid, hidumperTmpCmd) 64 65 @pytest.mark.L0 66 def test_storage_pid(self): 67 command = f"hidumper --storage 1" 68 hidumperTmpCmd = "OPT:storage SUB_OPT:" 69 # 校验命令行输出 70 CheckCmd(command, CheckStorageWithPid, hidumperTmpCmd) 71 # 校验命令行重定向输出 72 CheckCmdRedirect(command, CheckStorageWithPid, None, hidumperTmpCmd) 73 # 校验命令行输出到zip文件 74 CheckCmdZip(command, CheckStorageWithPid) 75 76 @pytest.mark.L3 77 def test_storage_error_pid(self): 78 command = f"hidumper --storage 2147483647;hidumper --storage -2147483647" 79 hidumperTmpCmd = "OPT:storage SUB_OPT:" 80 # 校验命令行输出 81 CheckCmd(command, lambda output : "hidumper: No such process: 2147483647\nhidumper: option pid missed. 2" in output, hidumperTmpCmd) 82 command = f"hidumper --storage 2147483648;hidumper --storage -2147483648" 83 CheckCmd(command, lambda output : "hidumper: option pid missed. 2" in output, hidumperTmpCmd)