diff --git a/source/tools/bclinux/checkcommand/checkcommand.sh b/source/tools/bclinux/checkcommand/checkcommand.sh index 6ef352911f3027b5d9de7a8485938c8addf3fcb7..2e0c3f94deea5e75f5d9f4da3a3d9ab8879afe6c 100644 --- a/source/tools/bclinux/checkcommand/checkcommand.sh +++ b/source/tools/bclinux/checkcommand/checkcommand.sh @@ -1,68 +1,38 @@ -#!/usr/bin/bash - -# 创建日期: 2024-10-15 +#!/bin/bash # 获取脚本所在目录 -OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) -source "$OET_PATH/common_lib.sh" - -# 日志文件路径 -LOG_FILE="/var/log/checkcommand.log" - -# 重定向输出到日志文件 -exec 6>&1 -exec 7>&2 -exec 1>>"$LOG_FILE" -exec 2>>"$LOG_FILE" - -# 定义要执行的测试脚本列表 -TEST_SCRIPTS=("test01_lscpu.sh" "test02_uname.sh") - -# 记录总测试结果 -total_result=0 - -# 执行单个测试脚本的函数 -function run_single_test() { - local script=$1 - local script_path="$OET_PATH/$script" - - #LOG_INFO "开始执行测试脚本: $script_path" - - # 源入测试脚本并调用 main 函数 - source "$script_path" && main "$@" - - # 检查测试脚本的执行结果 - local result=$? - if [ $result -ne 0 ]; then - LOG_ERROR "The result of running the $script_path script is $result" - total_result=$((total_result + 1)) - else - LOG_INFO "The result of running the $script_path script is 0" +SCRIPT_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) +source "$SCRIPT_PATH/common_lib.sh" + +# 执行测试脚本并记录结果 +function run_tests() { + # 获取当前目录下所有以 test 开头的脚本文件 + test_scripts=($(ls test*.sh 2>/dev/null)) + + # 检查是否有符合条件的脚本文件 + if [ ${#test_scripts[@]} -eq 0 ]; then + log_error "No test scripts found in the current directory." + echo "No test scripts found in the current directory." + exit 1 fi -} -# 主函数,执行所有测试脚本 -function main() { - #LOG_INFO "开始执行所有测试脚本..." - - for script in "${TEST_SCRIPTS[@]}"; do - run_single_test "$script" + # 遍历每个测试脚本并执行 + for script in "${test_scripts[@]}"; do + #echo "Running script: $script" + result=$(bash "$script") + exit_code=$? + + if [ $exit_code -eq 0 ]; then + echo "The result of running the $script script is $exit_code" + else + echo "The result of running the $script script is $exit_code" + fi done - - # 恢复标准输出和标准错误 - exec 1>&6 - exec 2>&7 - exec 6>&- - exec 7>&- - - # 检查总测试结果 - if [ $total_result -eq 0 ]; then - echo "所有测试脚本均已执行,查看日志详情/var/log/checkcommand.log" - else - echo "有 $total_result 个测试脚本执行失败" - fi - - #LOG_INFO "所有测试脚本执行完毕" +} + +# 主函数入口 +main() { + run_tests } # 脚本入口 diff --git a/source/tools/bclinux/checkcommand/clear.sh b/source/tools/bclinux/checkcommand/clear.sh new file mode 100644 index 0000000000000000000000000000000000000000..14d4dbb85d1e24c43f00969ba947039c3ad13e9e --- /dev/null +++ b/source/tools/bclinux/checkcommand/clear.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +rm -rf /var/log/checkcommand.log diff --git a/source/tools/bclinux/checkcommand/common_lib.sh b/source/tools/bclinux/checkcommand/common_lib.sh index 77db6fd363c58659d547a8b9ad0582f5d555dc86..68fc0a6d820da79a1e5763d8f55ef50b0184b630 100644 --- a/source/tools/bclinux/checkcommand/common_lib.sh +++ b/source/tools/bclinux/checkcommand/common_lib.sh @@ -1,117 +1,20 @@ -#!/usr/bin/bash +#!/bin/bash + +# 日志级别定义 +LOG_INFO="[INFO]" +LOG_ERROR="[ERROR]" # 日志文件路径 LOG_FILE="/var/log/checkcommand.log" -function LOG_INFO() { - message=$1 - echo "[INFO] $message" >> "$LOG_FILE" -} - -function LOG_ERROR() { - message=$1 - echo "[ERROR] $message" >> "$LOG_FILE" -} - -function LOG_WARN() { - message=$1 - echo "[WARN] $message" >> "$LOG_FILE" -} - -function LOG_DEBUG() { - message=$1 - echo "[DEBUG] $message" >> "$LOG_FILE" +# 记录信息日志 +function log_info() { + local timestamp=$(date "+%Y-%m-%d %H:%M:%S") + echo "${timestamp} ${LOG_INFO} $*" >> "${LOG_FILE}" } -# 结果检查 -function CHECK_RESULT() { - local actual_result=$1 - local expect_result=${2-0} - local mode=${3-0} - local error_log=$4 - local exit_mode=${5-0} - local exec_result=0 - - if [ -z "$actual_result" ]; then - LOG_ERROR "Missing actual error code." - return 1 - fi - - if ! [[ "$mode" =~ ^[0-9]+$ ]]; then - LOG_ERROR "Invalid mode value: $mode" - return 1 - fi - - if ! [[ "$exit_mode" =~ ^[0-9]+$ ]]; then - LOG_ERROR "Invalid exit_mode value: $exit_mode" - return 1 - fi - - if [ "$actual_result" != "$expect_result" ]; then - [ -n "$error_log" ] && LOG_ERROR "$error_log" - ((exec_result++)) - LOG_ERROR "${BASH_SOURCE[1]} line ${BASH_LINENO[0]}" - if [ $exit_mode -eq 1 ]; then - exit 1 - fi - fi - - return 0 -} - -# 执行用例 -function CASE_RESULT() { - local case_result=$1 - local exec_result=0 - - if [ -z "$exec_result" ]; then - if [ $case_result -eq 0 ]; then - LOG_INFO "succeed to execute the case." - exec_result="" - exit 0 - else - LOG_ERROR "failed to execute the case." - exit $case_result - fi - fi - - if [ $exec_result -gt 0 ]; then - LOG_ERROR "failed to execute the case." - exit $exec_result - fi - - LOG_INFO "succeed to execute the case." - exit $exec_result -} - -# 默认测试后清理 -function POST_TEST_DEFAULT() { - LOG_INFO "$0 post_test" -} - -# 主入口 -function main() { - local input_path="$0" - local file_name=$(basename "$input_path" .sh) - local relative_path=${input_path#*local_sh/} - local log_path="/var/log/checkcommand/${relative_path:-$file_name}" - - mkdir -p "$log_path" - exec 6>&1 - exec 7>&2 - exec >>"$log_path"/"$(date +%Y-%m-%d-%T)".log 2>&1 - - trap '[[ $(type -t post_test) ]] && post_test || POST_TEST_DEFAULT' EXIT INT HUP TERM - - if ! rpm -q expect >/dev/null 2>&1; then - if dnf -y install expect; then - LOG_INFO "Succeed to install expect." - else - LOG_ERROR "Failed to install expect." - fi - fi - - [[ $(type -t config_params) ]] && config_params - [[ $(type -t pre_test) ]] && pre_test - [[ $(type -t run_test) ]] && run_test && CASE_RESULT $? +# 记录错误日志 +function log_error() { + local timestamp=$(date "+%Y-%m-%d %H:%M:%S") + echo "${timestamp} ${LOG_ERROR} $*" >> "${LOG_FILE}" } diff --git a/source/tools/bclinux/checkcommand/test01_lscpu.sh b/source/tools/bclinux/checkcommand/test01_lscpu.sh index 8066c39862a09c0c0fd4742ffea436f5d65cb691..7d6be57c8b64731e7e83c057e15ea1928cd32732 100644 --- a/source/tools/bclinux/checkcommand/test01_lscpu.sh +++ b/source/tools/bclinux/checkcommand/test01_lscpu.sh @@ -1,6 +1,6 @@ -#!/usr/bin/bash +#!/bin/bash -# 创建日期: 2024-10-15 +# lscpu - lscpu 是一个非常有用的命令,用于显示有关系统 CPU 架构的详细信息 # 获取脚本所在目录 OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) @@ -18,11 +18,19 @@ function run_test() { lscpu_passed=false # 检查 lscpu 命令 - lscpu &>/dev/null - CHECK_RESULT $? 0 0 "lscpu 命令执行失败" - if [ $? -eq 0 ]; then + lscpu_output=$(lscpu | grep -i 'model name' | awk -F':' '{print $2}' | xargs) + lscpu_result=$? + + if [ $lscpu_result -eq 0 ]; then lscpu_passed=true + #log_info "lscpu 命令: 通过" + log_info "lscpu 命令输出: $lscpu_output" + else + log_error "lscpu 命令: 失败" + log_error "lscpu 命令输出: $lscpu_output" fi + + #log_info "完成测试!" } # 环境清理 diff --git a/source/tools/bclinux/checkcommand/test02_uname.sh b/source/tools/bclinux/checkcommand/test02_uname.sh index 70255912607663382fffc7132b136b35ac28102b..a35a80e2d3047bcfbd32aaaccad7c3b14e788056 100644 --- a/source/tools/bclinux/checkcommand/test02_uname.sh +++ b/source/tools/bclinux/checkcommand/test02_uname.sh @@ -1,6 +1,6 @@ -#!/usr/bin/bash +#!/bin/bash -# 创建日期: 2024-10-15 +# uname - 用于显示系统的内核和其他相关信息。用于了解当前系统的基本信息,包括操作系统、内核版本、主机名等 # 获取脚本所在目录 OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) @@ -18,16 +18,19 @@ function run_test() { uname_passed=false # 检查 uname 命令 - if command -v uname > /dev/null 2>&1; then - uname -a >/dev/null - local result=$? - CHECK_RESULT $result 0 0 "uname -a 命令执行失败" - if [ $? -eq 0 ]; then - uname_passed=true - fi + uname_output=$(uname -a) + uname_result=$? + + if [ $uname_result -eq 0 ]; then + uname_passed=true + #log_info "uname 命令: 通过" + log_info "uname 命令输出: $uname_output" else - CHECK_RESULT 1 0 0 "uname 命令未找到" + log_error "uname 命令: 失败" + log_error "uname 命令输出: $uname_output" fi + + #log_info "完成测试!" } # 环境清理 diff --git a/source/tools/bclinux/checkcommand/test03_version.sh b/source/tools/bclinux/checkcommand/test03_version.sh new file mode 100644 index 0000000000000000000000000000000000000000..b38dc4b93e6e71cfa3513463d05d9afe4b605913 --- /dev/null +++ b/source/tools/bclinux/checkcommand/test03_version.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# 获取操作系统版本信息:/etc/os-release, /etc/redhat-release, /etc/debian_version + +# 获取脚本所在目录 +OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) +source "$OET_PATH/common_lib.sh" + +# 环境准备 +function pre_test() { + OLD_LANG=$LANG + export LANG=en_US.UTF-8 +} + +# 获取系统版本信息 +function get_system_version() { + if [ -f /etc/os-release ]; then + . /etc/os-release + system_version="$NAME $VERSION" + elif [ -f /etc/redhat-release ]; then + system_version=$(cat /etc/redhat-release) + elif [ -f /etc/debian_version ]; then + system_version="Debian $(cat /etc/debian_version)" + else + system_version="Unknown" + fi + + echo "$system_version" +} + +# 测试执行 +function run_test() { + # 获取系统版本信息 + system_version=$(get_system_version) + log_info "当前系统版本: $system_version" +} + +# 环境清理 +function post_test() { + export LANG=${OLD_LANG} +} + +# 用例调用入口 +function main() { + pre_test + run_test + post_test + + # 返回测试结果 + return 0 +} + +# 脚本入口 +main "$@"