100600bfbSopenharmony_ci#!/usr/bin/env python3
200600bfbSopenharmony_ci# -*- coding: utf-8 -*-
300600bfbSopenharmony_ci# Copyright (C) 2024 Huawei Device Co., Ltd.
400600bfbSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
500600bfbSopenharmony_ci# you may not use this file except in compliance with the License.
600600bfbSopenharmony_ci# You may obtain a copy of the License at
700600bfbSopenharmony_ci#
800600bfbSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
900600bfbSopenharmony_ci#
1000600bfbSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1100600bfbSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1200600bfbSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1300600bfbSopenharmony_ci# See the License for the specific language governing permissions and
1400600bfbSopenharmony_ci# limitations under the License.
1500600bfbSopenharmony_ci
1600600bfbSopenharmony_ciimport pytest
1700600bfbSopenharmony_ciimport re
1800600bfbSopenharmony_ciimport subprocess
1900600bfbSopenharmony_cifrom utils import *
2000600bfbSopenharmony_ci
2100600bfbSopenharmony_ci@print_check_result
2200600bfbSopenharmony_cidef CheckBuildId(output) -> bool:
2300600bfbSopenharmony_ci    ret = re.search("BuildId: (.+?) [\d].[\d].[\d].[\d]", output)
2400600bfbSopenharmony_ci    return ret is not None
2500600bfbSopenharmony_ci
2600600bfbSopenharmony_ci@print_check_result
2700600bfbSopenharmony_cidef CheckOsVersion(output) -> bool:
2800600bfbSopenharmony_ci    ret = re.search("OsVersion: (.+?)[\d].[\d].[\d].[\d]", output)
2900600bfbSopenharmony_ci    return ret is not None
3000600bfbSopenharmony_ci
3100600bfbSopenharmony_ci@print_check_result
3200600bfbSopenharmony_cidef CheckProcVersion(output) -> bool:
3300600bfbSopenharmony_ci    ret = re.search("/proc/version\n\n([^\n]+\n)+\n", output)
3400600bfbSopenharmony_ci    return ret is not None
3500600bfbSopenharmony_ci
3600600bfbSopenharmony_ci@print_check_result
3700600bfbSopenharmony_cidef CheckCmdline(output) -> bool:
3800600bfbSopenharmony_ci    ret = re.search("/proc/cmdline\n\n([^\n]+\n)+\n", output)
3900600bfbSopenharmony_ci    return ret is not None
4000600bfbSopenharmony_ci
4100600bfbSopenharmony_ci@print_check_result
4200600bfbSopenharmony_cidef CheckWakeUpSource(output) -> bool:
4300600bfbSopenharmony_ci    ret = re.search("/sys/kernel/debug/wakeup_sources\n\n([^\n]+\n){4,}", output)
4400600bfbSopenharmony_ci    return ret is not None
4500600bfbSopenharmony_ci
4600600bfbSopenharmony_ci@print_check_result
4700600bfbSopenharmony_cidef CheckUpTime(output) -> bool:
4800600bfbSopenharmony_ci    ret = re.search("cmd is: uptime -p\n(\n[^\n]+)\n", output)
4900600bfbSopenharmony_ci    return ret is not None
5000600bfbSopenharmony_ci
5100600bfbSopenharmony_ci@print_check_result
5200600bfbSopenharmony_cidef CheckPrintEnv(output) -> bool:
5300600bfbSopenharmony_ci    ret = re.search("cmd is: printenv\n\n([^\n]+){4,}\n", output)
5400600bfbSopenharmony_ci    return ret is not None
5500600bfbSopenharmony_ci
5600600bfbSopenharmony_ci@print_check_result
5700600bfbSopenharmony_cidef CheckProcModules(output) -> bool:
5800600bfbSopenharmony_ci    ret = re.search("/proc/modules\n\n([^\n]+)\n", output)
5900600bfbSopenharmony_ci    return ret is not None
6000600bfbSopenharmony_ci
6100600bfbSopenharmony_ci@print_check_result
6200600bfbSopenharmony_cidef CheckLsmod(output) -> bool:
6300600bfbSopenharmony_ci    ret = re.search("cmd is: lsmod\n\n([^\n]+)\n", output)
6400600bfbSopenharmony_ci    return ret is not None
6500600bfbSopenharmony_ci
6600600bfbSopenharmony_ci@print_check_result
6700600bfbSopenharmony_cidef CheckSlabinfo(output) -> bool:
6800600bfbSopenharmony_ci    ret = re.search("/proc/slabinfo\n\n([^\n]+){4,}\n", output)
6900600bfbSopenharmony_ci    return ret is not None
7000600bfbSopenharmony_ci
7100600bfbSopenharmony_ci@print_check_result
7200600bfbSopenharmony_cidef CheckZoneinfo(output) -> bool:
7300600bfbSopenharmony_ci    ret = re.search("/proc/zoneinfo\n\n([^\n]+){4,}\n", output)
7400600bfbSopenharmony_ci    return ret is not None
7500600bfbSopenharmony_ci
7600600bfbSopenharmony_ci@print_check_result
7700600bfbSopenharmony_cidef CheckVmstat(output) -> bool:
7800600bfbSopenharmony_ci    ret = re.search("/proc/vmstat\n\n([^\n]+)\n", output)
7900600bfbSopenharmony_ci    return ret is not None
8000600bfbSopenharmony_ci
8100600bfbSopenharmony_ci@print_check_result
8200600bfbSopenharmony_cidef CheckVmallocinfo(output) -> bool:
8300600bfbSopenharmony_ci    ret = re.search("/proc/vmallocinfo\n\n([^\n]+)\n", output)
8400600bfbSopenharmony_ci    return ret is not None
8500600bfbSopenharmony_ci
8600600bfbSopenharmony_ci
8700600bfbSopenharmony_cidef CheckHidumperHelpOutput(output):
8800600bfbSopenharmony_ci    return "usage:" in output
8900600bfbSopenharmony_ci
9000600bfbSopenharmony_ciclass TestBaseCommand:
9100600bfbSopenharmony_ci
9200600bfbSopenharmony_ci    @pytest.mark.L0
9300600bfbSopenharmony_ci    def test_hidumper_help(self):
9400600bfbSopenharmony_ci        # 校验命令行输出
9500600bfbSopenharmony_ci        CheckCmd("hidumper -h", lambda output : "usage:" in output)
9600600bfbSopenharmony_ci        # 校验命令行重定向输出
9700600bfbSopenharmony_ci        CheckCmdRedirect("hidumper -h", lambda output : "usage:" in output)
9800600bfbSopenharmony_ci
9900600bfbSopenharmony_ci    @pytest.mark.L0
10000600bfbSopenharmony_ci    def test_hidumper_c_all(self):
10100600bfbSopenharmony_ci        CheckFunc = lambda output : all([check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion,
10200600bfbSopenharmony_ci                                                                    CheckCmdline, CheckUpTime, CheckPrintEnv, CheckLsmod,
10300600bfbSopenharmony_ci                                                                    CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]])
10400600bfbSopenharmony_ci        # 校验命令行输出
10500600bfbSopenharmony_ci        CheckCmd("hidumper -c", CheckFunc)
10600600bfbSopenharmony_ci        # 校验命令行重定向输出
10700600bfbSopenharmony_ci        CheckCmdRedirect("hidumper -c", CheckFunc)
10800600bfbSopenharmony_ci        # 校验命令行输出到zip文件
10900600bfbSopenharmony_ci        CheckCmdZip("hidumper -c", CheckFunc)
11000600bfbSopenharmony_ci
11100600bfbSopenharmony_ci    @pytest.mark.L0
11200600bfbSopenharmony_ci    def test_hidumper_c_base(self):
11300600bfbSopenharmony_ci        command = "hidumper -c base"
11400600bfbSopenharmony_ci        CheckFunc = lambda output : all([check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion, CheckCmdline, CheckUpTime]])
11500600bfbSopenharmony_ci        # 校验命令行输出
11600600bfbSopenharmony_ci        CheckCmd(command, CheckFunc)
11700600bfbSopenharmony_ci        # 校验命令行重定向输出
11800600bfbSopenharmony_ci        CheckCmdRedirect(command, CheckFunc)
11900600bfbSopenharmony_ci        # 校验命令行输出到zip文件
12000600bfbSopenharmony_ci        CheckCmdZip(command, CheckFunc)
12100600bfbSopenharmony_ci
12200600bfbSopenharmony_ci    @pytest.mark.L0
12300600bfbSopenharmony_ci    def test_hidumper_c_system(self):
12400600bfbSopenharmony_ci        command = "hidumper -c system"
12500600bfbSopenharmony_ci        CheckFunc = lambda output : all([check(output) for check in [CheckPrintEnv, CheckLsmod, CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]])
12600600bfbSopenharmony_ci        # 校验命令行输出
12700600bfbSopenharmony_ci        CheckCmd(command, CheckFunc)
12800600bfbSopenharmony_ci        # 校验命令行重定向输出
12900600bfbSopenharmony_ci        CheckCmdRedirect(command, CheckFunc)
13000600bfbSopenharmony_ci        # 校验命令行输出到zip文件
13100600bfbSopenharmony_ci        CheckCmdZip(command, CheckFunc)
13200600bfbSopenharmony_ci
13300600bfbSopenharmony_ci    @pytest.mark.L0
13400600bfbSopenharmony_ci    def test_hidumper_e(self):
13500600bfbSopenharmony_ci        command = "hidumper -e"
13600600bfbSopenharmony_ci        CheckFunc = lambda output : "faultlog" in output
13700600bfbSopenharmony_ci        # 校验命令行输出
13800600bfbSopenharmony_ci        CheckCmd(command, CheckFunc)
13900600bfbSopenharmony_ci        # 校验命令行重定向输出
14000600bfbSopenharmony_ci        CheckCmdRedirect(command, CheckFunc)
14100600bfbSopenharmony_ci        # 校验命令行输出到zip文件
14200600bfbSopenharmony_ci        CheckCmdZip(command, CheckFunc)
14300600bfbSopenharmony_ci
14400600bfbSopenharmony_ci    @pytest.mark.L0
14500600bfbSopenharmony_ci    def test_hidumper_error_option(self):
14600600bfbSopenharmony_ci        command = "hdc shell \"hidumper -D -h\""
14700600bfbSopenharmony_ci        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
14800600bfbSopenharmony_ci        assert "option pid missed." in output
14900600bfbSopenharmony_ci
15000600bfbSopenharmony_ci        command = "hdc shell \"hidumper -h -D\""
15100600bfbSopenharmony_ci        output = subprocess.check_output(command, shell=True, encoding="utf-8", text=True)
15200600bfbSopenharmony_ci        assert "usage:" in output
15300600bfbSopenharmony_ci
15400600bfbSopenharmony_ci
15500600bfbSopenharmony_ci
156