update testcase for testsuite systemd-timesyncd

This commit is contained in:
2026-04-21 06:22:57 +08:00
parent 21935dc5d1
commit d6f5ea7e57
13 changed files with 0 additions and 1217 deletions

View File

@@ -38,57 +38,9 @@
"name": "test_systemd-timesyncd_function_install",
"desc": "检查软件包是否已安装"
},
{
"name": "test_systemd-timesyncd_function_status",
"desc": "检查服务运行状态"
},
{
"name": "test_systemd-timesyncd_function_enable",
"desc": "测试服务开机自启"
},
{
"name": "test_systemd-timesyncd_function_timesync",
"desc": "测试手动触发时间同步"
},
{
"name": "test_systemd-timesyncd_function_ntp_servers",
"desc": "测试配置NTP服务器"
},
{
"name": "test_systemd-timesyncd_function_sync_status",
"desc": "查看时间同步状态"
},
{
"name": "test_systemd-timesyncd_function_install_check",
"desc": "检查软件包是否已安装"
},
{
"name": "test_systemd-timesyncd_function_service_ctrl",
"desc": "测试服务启动停止重启"
},
{
"name": "test_systemd-timesyncd_function_service_enable",
"desc": "测试服务开机自启"
},
{
"name": "test_systemd-timesyncd_function_timedatectl",
"desc": "测试timedatectl基础命令"
},
{
"name": "test_systemd-timesyncd_function_ntp_enable",
"desc": "测试启用禁用NTP同步"
},
{
"name": "test_systemd-timesyncd_function_config_file",
"desc": "测试修改主配置文件"
},
{
"name": "test_systemd-timesyncd_function_force_sync",
"desc": "测试手动触发时间同步"
},
{
"name": "test_systemd-timesyncd_function_log_verify",
"desc": "验证服务日志输出"
}
]
}

View File

@@ -1,92 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-31
# @License : Mulan PSL v2
# @Desc : 测试修改主配置文件
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
# 判断是否已安装systemd-timesyncd
LOG_INFO "检查系统是否已安装systemd-timesyncd"
if dnf list installed systemd-timesyncd &> /dev/null; then
LOG_INFO "systemd-timesyncd已安装"
INSTALLED=true
else
LOG_INFO "systemd-timesyncd未安装"
INSTALLED=false
fi
# 检查yum源中是否有systemd-timesyncd软件包
LOG_INFO "检查yum源中是否有systemd-timesyncd软件包"
if ! dnf list available systemd-timesyncd &> /dev/null; then
LOG_ERROR "yum源中没有systemd-timesyncd软件包"
exit 255
fi
# 如果未安装则安装systemd-timesyncd
if [ "$INSTALLED" = false ]; then
LOG_INFO "安装systemd-timesyncd"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
# 备份原始配置文件
LOG_INFO "备份原始配置文件"
cp /etc/systemd/timesyncd.conf /etc/systemd/timesyncd.conf.bak
CHECK_RESULT $? 0 0 "备份配置文件失败"
# 修改主配置文件
LOG_INFO "修改主配置文件"
sed -i "s/^#NTP=/NTP=ntp.example.com/" /etc/systemd/timesyncd.conf
CHECK_RESULT $? 0 0 "修改配置文件失败"
# 重启systemd-timesyncd服务使配置生效
LOG_INFO "重启systemd-timesyncd服务"
systemctl restart systemd-timesyncd
CHECK_RESULT $? 0 0 "重启systemd-timesyncd服务失败"
# 检查服务状态
LOG_INFO "检查systemd-timesyncd服务状态"
systemctl status systemd-timesyncd | grep -q "active (running)"
CHECK_RESULT $? 0 0 "systemd-timesyncd服务未正常运行"
# 检查配置是否生效
LOG_INFO "检查配置是否生效"
grep -q "^NTP=ntp.example.com" /etc/systemd/timesyncd.conf
CHECK_RESULT $? 0 0 "配置未生效"
# 恢复原始配置文件
LOG_INFO "恢复原始配置文件"
mv /etc/systemd/timesyncd.conf.bak /etc/systemd/timesyncd.conf
CHECK_RESULT $? 0 0 "恢复配置文件失败"
# 重启systemd-timesyncd服务恢复原始配置
LOG_INFO "重启systemd-timesyncd服务恢复原始配置"
systemctl restart systemd-timesyncd
CHECK_RESULT $? 0 0 "重启systemd-timesyncd服务失败"
# 如果之前未安装则卸载systemd-timesyncd
if [ "$INSTALLED" = false ]; then
LOG_INFO "卸载systemd-timesyncd"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
fi
LOG_INFO "测试完成"
}
main "$@"

View File

@@ -1,135 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-31
# @License : Mulan PSL v2
# @Desc : 测试手动触发时间同步
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
# 测试手动触发时间同步功能
LOG_INFO "开始测试手动触发时间同步功能"
# 检查系统是否已安装systemd-timesyncd
LOG_INFO "检查systemd-timesyncd是否已安装"
if rpm -q systemd-timesyncd > /dev/null 2>&1; then
LOG_INFO "systemd-timesyncd已安装"
INSTALLED=true
else
LOG_INFO "systemd-timesyncd未安装"
INSTALLED=false
fi
# 检查yum源中是否有systemd-timesyncd软件包
LOG_INFO "检查yum源中是否有systemd-timesyncd软件包"
if ! dnf list available systemd-timesyncd > /dev/null 2>&1; then
LOG_ERROR "yum源中没有systemd-timesyncd软件包"
exit 255
fi
# 如果未安装,则安装软件包
if [ "$INSTALLED" = false ]; then
LOG_INFO "安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
# 检查timesyncd服务状态
LOG_INFO "检查timesyncd服务状态"
systemctl status systemd-timesyncd --no-pager
CHECK_RESULT $? 0 0 "检查timesyncd服务状态失败"
# 检查timedatectl命令是否支持timesync-status参数
LOG_INFO "检查timedatectl命令是否支持timesync-status参数"
if ! timedatectl --help | grep -q "timesync-status"; then
LOG_ERROR "timedatectl命令不支持timesync-status参数"
# 如果是未安装的情况,需要卸载软件包
if [ "$INSTALLED" = false ]; then
LOG_INFO "卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
fi
exit 255
fi
# 检查timedatectl命令是否支持set-ntp参数
LOG_INFO "检查timedatectl命令是否支持set-ntp参数"
if ! timedatectl --help | grep -q "set-ntp"; then
LOG_ERROR "timedatectl命令不支持set-ntp参数"
# 如果是未安装的情况,需要卸载软件包
if [ "$INSTALLED" = false ]; then
LOG_INFO "卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
fi
exit 255
fi
# 确保NTP已启用
LOG_INFO "启用NTP同步"
timedatectl set-ntp true
CHECK_RESULT $? 0 0 "启用NTP同步失败"
# 等待服务稳定
LOG_INFO "等待timesyncd服务稳定"
sleep 2
# 检查初始时间同步状态
LOG_INFO "检查初始时间同步状态"
timedatectl timesync-status --no-pager
CHECK_RESULT $? 0 0 "获取时间同步状态失败"
# 手动触发时间同步
LOG_INFO "手动触发时间同步"
systemctl restart systemd-timesyncd
CHECK_RESULT $? 0 0 "重启timesyncd服务失败"
# 等待同步完成
LOG_INFO "等待时间同步完成"
sleep 3
# 检查时间同步状态
LOG_INFO "检查手动触发后的时间同步状态"
timedatectl timesync-status --no-pager
CHECK_RESULT $? 0 0 "获取手动触发后的时间同步状态失败"
# 检查系统时间是否正确更新
LOG_INFO "检查系统时间"
date
CHECK_RESULT $? 0 0 "获取系统时间失败"
# 清理环境
LOG_INFO "清理测试环境"
# 如果测试前未安装,则卸载软件包
if [ "$INSTALLED" = false ]; then
LOG_INFO "卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
# 检查是否成功卸载
if rpm -q systemd-timesyncd > /dev/null 2>&1; then
LOG_ERROR "卸载systemd-timesyncd失败"
exit 1
else
LOG_INFO "成功卸载systemd-timesyncd"
fi
else
LOG_INFO "保持systemd-timesyncd安装状态"
fi
LOG_INFO "测试手动触发时间同步功能完成"
}
main "$@"

View File

@@ -1,70 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-31
# @License : Mulan PSL v2
# @Desc : 检查软件包是否已安装
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
LOG_INFO "开始检查软件包是否已安装"
LOG_INFO "步骤1检查yum源中是否存在systemd-timesyncd软件包"
dnf list available systemd-timesyncd 2>/dev/null | grep -q systemd-timesyncd
if [ $? -ne 0 ]; then
LOG_ERROR "yum源中未找到systemd-timesyncd软件包"
exit 255
fi
LOG_INFO "yum源中存在systemd-timesyncd软件包"
LOG_INFO "步骤2检查当前是否已安装systemd-timesyncd"
rpm -q systemd-timesyncd >/dev/null 2>&1
if [ $? -eq 0 ]; then
LOG_INFO "systemd-timesyncd已安装标记为保持安装状态"
INSTALLED_BEFORE=1
else
LOG_INFO "systemd-timesyncd未安装标记为需要测试安装"
INSTALLED_BEFORE=0
fi
LOG_INFO "步骤3根据安装状态决定是否执行安装"
if [ ${INSTALLED_BEFORE} -eq 0 ]; then
LOG_INFO "执行安装systemd-timesyncd"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
LOG_INFO "安装systemd-timesyncd成功"
else
LOG_INFO "已安装,跳过安装步骤"
fi
LOG_INFO "步骤4验证systemd-timesyncd是否已正确安装"
rpm -q systemd-timesyncd >/dev/null 2>&1
CHECK_RESULT $? 0 0 "验证systemd-timesyncd安装状态失败"
LOG_INFO "验证systemd-timesyncd安装状态成功"
LOG_INFO "步骤5清理测试环境恢复原始状态"
if [ ${INSTALLED_BEFORE} -eq 0 ]; then
LOG_INFO "测试前未安装执行卸载systemd-timesyncd"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
LOG_INFO "卸载systemd-timesyncd成功环境已恢复"
else
LOG_INFO "测试前已安装,保持安装状态,无需卸载"
fi
LOG_INFO "测试完成"
}
main "$@"

View File

@@ -1,79 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-31
# @License : Mulan PSL v2
# @Desc : 验证服务日志输出
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
# 检查是否已安装systemd-timesyncd
LOG_INFO "检查systemd-timesyncd是否已安装"
rpm -q systemd-timesyncd
if [ $? -eq 0 ]; then
LOG_INFO "systemd-timesyncd已安装"
installed=true
else
LOG_INFO "systemd-timesyncd未安装"
installed=false
fi
# 检查yum源中是否有systemd-timesyncd软件包
LOG_INFO "检查yum源中是否有systemd-timesyncd软件包"
dnf list available systemd-timesyncd
if [ $? -ne 0 ]; then
LOG_ERROR "yum源中没有systemd-timesyncd软件包"
exit 255
fi
# 如果未安装,则安装软件包
if [ "$installed" = false ]; then
LOG_INFO "安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
# 检查systemd-timesyncd服务是否运行
LOG_INFO "检查systemd-timesyncd服务状态"
systemctl status systemd-timesyncd
CHECK_RESULT $? 0 0 "systemd-timesyncd服务未运行"
# 验证服务日志输出
LOG_INFO "验证systemd-timesyncd服务日志输出"
journalctl -u systemd-timesyncd --no-pager -n 5
CHECK_RESULT $? 0 0 "查看systemd-timesyncd服务日志失败"
# 检查systemd-timesyncd命令参数是否支持
LOG_INFO "检查systemd-timesyncd命令参数支持情况"
timedatectl timesync-status --help
CHECK_RESULT $? 0 0 "timedatectl timesync-status参数不支持"
# 执行timedatectl timesync-status命令
LOG_INFO "执行timedatectl timesync-status命令"
timedatectl timesync-status
CHECK_RESULT $? 0 0 "执行timedatectl timesync-status命令失败"
# 清理环境,如果脚本开始时未安装,则卸载软件包
if [ "$installed" = false ]; then
LOG_INFO "卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
fi
LOG_INFO "测试完成,环境已恢复"
}
main "$@"

View File

@@ -1,84 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-31
# @License : Mulan PSL v2
# @Desc : 测试启用禁用NTP同步
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
# 测试启用禁用NTP同步功能
LOG_INFO "步骤1检查系统是否已安装systemd-timesyncd"
if rpm -q systemd-timesyncd &>/dev/null; then
LOG_INFO "systemd-timesyncd已安装脚本结束时将保持安装状态"
INSTALLED_BEFORE=1
else
LOG_INFO "systemd-timesyncd未安装将在测试过程中安装"
INSTALLED_BEFORE=0
fi
LOG_INFO "步骤2检查yum源中是否有systemd-timesyncd软件包"
if ! dnf list available systemd-timesyncd &>/dev/null; then
LOG_ERROR "yum源中未找到systemd-timesyncd软件包"
exit 255
fi
LOG_INFO "步骤3如果未安装则安装systemd-timesyncd"
if [ $INSTALLED_BEFORE -eq 0 ]; then
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
LOG_INFO "步骤4检查timedatectl命令是否支持ntp参数"
timedatectl set-ntp true 2>&1 | grep -q "Unknown operation"
if [ $? -eq 0 ]; then
LOG_ERROR "timedatectl命令不支持ntp参数"
if [ $INSTALLED_BEFORE -eq 0 ]; then
dnf remove -y systemd-timesyncd
fi
exit 255
fi
LOG_INFO "步骤5禁用NTP同步"
timedatectl set-ntp false
CHECK_RESULT $? 0 0 "禁用NTP同步失败"
LOG_INFO "步骤6验证NTP同步已禁用"
timedatectl show | grep -q "NTPSynchronized=no"
CHECK_RESULT $? 0 0 "验证NTP同步禁用状态失败"
LOG_INFO "步骤7启用NTP同步"
timedatectl set-ntp true
CHECK_RESULT $? 0 0 "启用NTP同步失败"
LOG_INFO "步骤8验证NTP同步已启用"
timedatectl show | grep -q "NTPSynchronized=yes"
CHECK_RESULT $? 0 0 "验证NTP同步启用状态失败"
LOG_INFO "步骤9清理环境恢复原始状态"
if [ $INSTALLED_BEFORE -eq 0 ]; then
LOG_INFO "卸载测试过程中安装的systemd-timesyncd"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
else
LOG_INFO "保持systemd-timesyncd安装状态不变"
fi
LOG_INFO "测试完成"
}
main "$@"

View File

@@ -1,103 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-27
# @License : Mulan PSL v2
# @Desc : 测试配置NTP服务器
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
LOG_INFO "测试配置NTP服务器"
LOG_INFO "步骤1检查timesyncd软件包是否在yum源中"
dnf list --available systemd-timesyncd > /dev/null 2>&1
if [ $? -ne 0 ]; then
LOG_ERROR "yum源中没有systemd-timesyncd软件包"
exit 255
fi
LOG_INFO "步骤2检查当前系统是否已安装systemd-timesyncd"
rpm -q systemd-timesyncd > /dev/null 2>&1
if [ $? -eq 0 ]; then
INSTALLED=1
LOG_INFO "systemd-timesyncd已安装"
else
INSTALLED=0
LOG_INFO "systemd-timesyncd未安装"
fi
if [ $INSTALLED -eq 0 ]; then
LOG_INFO "步骤3安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
LOG_INFO "步骤4检查timedatectl命令是否支持set-ntp参数"
timedatectl set-ntp --help 2>&1 | grep -q "\-\-set-ntp"
if [ $? -ne 0 ]; then
LOG_ERROR "timedatectl命令不支持set-ntp参数"
exit 255
fi
LOG_INFO "步骤5启用NTP时间同步"
timedatectl set-ntp true
CHECK_RESULT $? 0 0 "启用NTP时间同步失败"
LOG_INFO "步骤6检查NTP服务状态"
timedatectl status | grep -q "NTP enabled: yes"
CHECK_RESULT $? 0 0 "NTP服务未正确启用"
LOG_INFO "步骤7配置NTP服务器"
cat > /etc/systemd/timesyncd.conf.d/test.conf << EOF
[Time]
NTP=0.pool.ntp.org 1.pool.ntp.org
FallbackNTP=2.pool.ntp.org 3.pool.ntp.org
EOF
CHECK_RESULT $? 0 0 "创建NTP服务器配置文件失败"
LOG_INFO "步骤8重启systemd-timesyncd服务"
systemctl restart systemd-timesyncd
CHECK_RESULT $? 0 0 "重启systemd-timesyncd服务失败"
LOG_INFO "步骤9检查NTP服务器配置是否生效"
systemctl status systemd-timesyncd | grep -q "0.pool.ntp.org"
CHECK_RESULT $? 0 0 "NTP服务器配置未生效"
LOG_INFO "步骤10测试NTP时间同步"
timedatectl timesync-status
CHECK_RESULT $? 0 0 "NTP时间同步状态检查失败"
LOG_INFO "步骤11清理NTP服务器配置文件"
rm -f /etc/systemd/timesyncd.conf.d/test.conf
CHECK_RESULT $? 0 0 "删除NTP服务器配置文件失败"
LOG_INFO "步骤12恢复NTP服务配置"
systemctl restart systemd-timesyncd
CHECK_RESULT $? 0 0 "恢复systemd-timesyncd服务失败"
LOG_INFO "步骤13禁用NTP时间同步"
timedatectl set-ntp false
CHECK_RESULT $? 0 0 "禁用NTP时间同步失败"
if [ $INSTALLED -eq 0 ]; then
LOG_INFO "步骤14卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
fi
LOG_INFO "测试完成"
}
main "$@"

View File

@@ -1,88 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-31
# @License : Mulan PSL v2
# @Desc : 测试服务启动停止重启
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
set -e
# 环境检查
LOG_INFO "检查是否已安装systemd-timesyncd服务"
if systemctl list-unit-files | grep -q systemd-timesyncd.service; then
LOG_INFO "systemd-timesyncd已安装"
INSTALLED=true
else
LOG_INFO "systemd-timesyncd未安装"
INSTALLED=false
fi
# 检查软件包是否在yum源中
LOG_INFO "检查yum源中是否存在systemd-timesyncd软件包"
if ! dnf list available systemd-timesyncd 2>/dev/null | grep -q systemd-timesyncd; then
LOG_ERROR "yum源中不存在systemd-timesyncd软件包"
exit 255
fi
# 如果未安装则安装
if [ "$INSTALLED" = false ]; then
LOG_INFO "安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
# 测试服务启动
LOG_INFO "测试启动systemd-timesyncd服务"
systemctl start systemd-timesyncd
CHECK_RESULT $? 0 0 "启动systemd-timesyncd服务失败"
LOG_INFO "检查服务状态"
systemctl status systemd-timesyncd | grep -q "active (running)"
CHECK_RESULT $? 0 0 "服务未处于运行状态"
# 测试服务停止
LOG_INFO "测试停止systemd-timesyncd服务"
systemctl stop systemd-timesyncd
CHECK_RESULT $? 0 0 "停止systemd-timesyncd服务失败"
LOG_INFO "检查服务状态"
systemctl status systemd-timesyncd | grep -q "inactive (dead)"
CHECK_RESULT $? 0 0 "服务未停止"
# 测试服务重启
LOG_INFO "测试重启systemd-timesyncd服务"
systemctl restart systemd-timesyncd
CHECK_RESULT $? 0 0 "重启systemd-timesyncd服务失败"
LOG_INFO "检查服务状态"
systemctl status systemd-timesyncd | grep -q "active (running)"
CHECK_RESULT $? 0 0 "服务重启后未处于运行状态"
# 环境恢复
if [ "$INSTALLED" = false ]; then
LOG_INFO "卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
else
LOG_INFO "保持systemd-timesyncd安装状态"
fi
LOG_INFO "测试完成"
}
main "$@"

View File

@@ -1,87 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-31
# @License : Mulan PSL v2
# @Desc : 测试服务开机自启
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
LOG_INFO "开始测试systemd-timesyncd服务的开机自启功能"
LOG_INFO "步骤1: 检查环境是否已安装systemd-timesyncd"
if dnf list installed systemd-timesyncd &>/dev/null; then
LOG_INFO "systemd-timesyncd已安装测试完成后将保持安装状态"
installed_before_test=1
else
LOG_INFO "systemd-timesyncd未安装将在测试过程中安装"
installed_before_test=0
fi
LOG_INFO "步骤2: 检查yum源中是否有systemd-timesyncd软件包"
if ! dnf list available systemd-timesyncd &>/dev/null; then
LOG_ERROR "yum源中未找到systemd-timesyncd软件包"
exit 255
fi
if [ $installed_before_test -eq 0 ]; then
LOG_INFO "步骤3: 安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
LOG_INFO "步骤4: 检查systemctl enable命令是否支持"
if ! systemctl enable --help &>/dev/null; then
LOG_ERROR "systemctl命令不支持enable参数"
if [ $installed_before_test -eq 0 ]; then
dnf remove -y systemd-timesyncd
fi
exit 255
fi
LOG_INFO "步骤5: 启用systemd-timesyncd服务开机自启"
systemctl enable systemd-timesyncd
CHECK_RESULT $? 0 0 "启用systemd-timesyncd服务开机自启失败"
LOG_INFO "步骤6: 验证systemd-timesyncd服务是否已启用开机自启"
systemctl is-enabled systemd-timesyncd
CHECK_RESULT $? 0 0 "systemd-timesyncd服务未正确启用开机自启"
LOG_INFO "步骤7: 禁用systemd-timesyncd服务开机自启测试清理"
systemctl disable systemd-timesyncd
CHECK_RESULT $? 0 0 "禁用systemd-timesyncd服务开机自启失败"
if [ $installed_before_test -eq 0 ]; then
LOG_INFO "步骤8: 卸载systemd-timesyncd软件包恢复环境"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
fi
LOG_INFO "步骤9: 验证环境已恢复"
if [ $installed_before_test -eq 1 ]; then
LOG_INFO "验证systemd-timesyncd是否保持安装状态"
dnf list installed systemd-timesyncd &>/dev/null
CHECK_RESULT $? 0 0 "systemd-timesyncd未保持安装状态"
else
LOG_INFO "验证systemd-timesyncd是否已卸载"
! dnf list installed systemd-timesyncd &>/dev/null
CHECK_RESULT $? 0 0 "systemd-timesyncd未正确卸载"
fi
LOG_INFO "systemd-timesyncd服务开机自启功能测试完成"
}
main "$@"

View File

@@ -1,110 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-27
# @License : Mulan PSL v2
# @Desc : 检查服务运行状态
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
# 定义日志函数(如未定义)
LOG_INFO() {
echo "[INFO] $*"
}
LOG_ERROR() {
echo "[ERROR] $*"
}
# 定义检查结果函数(如未定义)
CHECK_RESULT() {
local actual=$1
local expect=$2
local mode=$3
local msg=$4
if [ $mode -eq 0 ]; then
if [ $actual -ne $expect ]; then
LOG_ERROR "$msg"
exit $actual
fi
else
if [ $actual -eq $expect ]; then
LOG_ERROR "$msg"
exit 1
fi
fi
LOG_INFO "$msg success"
}
# 定义SSH_CMD函数如未定义
SSH_CMD() {
local cmd=$1
local ip=$2
local password=$3
local user=${4:-root}
sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$user@$ip" "$cmd"
}
# 测试脚本开始
LOG_INFO "开始测试检查systemd-timesyncd服务运行状态"
# 检查是否已安装systemd-timesyncd
LOG_INFO "步骤1检查systemd-timesyncd是否已安装"
if rpm -q systemd-timesyncd &>/dev/null; then
LOG_INFO "systemd-timesyncd已安装标记为已安装状态"
INSTALLED=1
else
LOG_INFO "systemd-timesyncd未安装标记为未安装状态"
INSTALLED=0
fi
# 如果未安装,则尝试安装
if [ $INSTALLED -eq 0 ]; then
LOG_INFO "步骤2检查yum源中是否有systemd-timesyncd软件包"
dnf list available systemd-timesyncd &>/dev/null
if [ $? -ne 0 ]; then
LOG_ERROR "yum源中未找到systemd-timesyncd软件包"
exit 255
fi
LOG_INFO "步骤3安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
# 检查服务运行状态
LOG_INFO "步骤4检查systemd-timesyncd服务运行状态"
systemctl is-active systemd-timesyncd &>/dev/null
CHECK_RESULT $? 0 0 "systemd-timesyncd服务未运行"
# 检查服务是否启用
LOG_INFO "步骤5检查systemd-timesyncd服务是否启用"
systemctl is-enabled systemd-timesyncd &>/dev/null
CHECK_RESULT $? 0 0 "systemd-timesyncd服务未启用"
# 恢复环境
LOG_INFO "步骤6恢复测试环境"
if [ $INSTALLED -eq 0 ]; then
LOG_INFO "卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
else
LOG_INFO "保持systemd-timesyncd安装状态"
fi
LOG_INFO "测试完成systemd-timesyncd服务运行状态检查通过"
}
main "$@"

View File

@@ -1,59 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-27
# @License : Mulan PSL v2
# @Desc : 查看时间同步状态
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
LOG_INFO "开始测试:查看时间同步状态"
LOG_INFO "步骤1检查系统是否已安装systemd-timesyncd"
if ! command -v timedatectl &> /dev/null; then
LOG_INFO "未找到timedatectl命令检查systemd-timesyncd是否在yum源中"
if ! dnf list available systemd-timesyncd --quiet &> /dev/null; then
LOG_ERROR "yum源中未找到systemd-timesyncd软件包"
exit 255
fi
LOG_INFO "安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
INSTALLED=1
else
LOG_INFO "systemd-timesyncd已安装"
INSTALLED=0
fi
LOG_INFO "步骤2检查timedatectl命令是否支持timesync-status参数"
timedatectl --help | grep -q "timesync-status"
CHECK_RESULT $? 0 255 "timedatectl命令不支持timesync-status参数"
LOG_INFO "步骤3查看时间同步状态"
timedatectl timesync-status
CHECK_RESULT $? 0 0 "查看时间同步状态失败"
LOG_INFO "步骤4清理环境"
if [ $INSTALLED -eq 1 ]; then
LOG_INFO "卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
fi
LOG_INFO "测试完成"
}
main "$@"

View File

@@ -1,141 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-31
# @License : Mulan PSL v2
# @Desc : 测试timedatectl基础命令
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
# 检查是否已安装systemd-timesyncd
LOG_INFO "检查系统是否已安装systemd-timesyncd"
rpm -q systemd-timesyncd > /dev/null 2>&1
if [ $? -eq 0 ]; then
LOG_INFO "systemd-timesyncd已安装测试结束后将保持安装状态"
INSTALLED=1
else
LOG_INFO "systemd-timesyncd未安装将在测试前安装并在测试后卸载"
INSTALLED=0
fi
# 检查yum源中是否有systemd-timesyncd软件包
LOG_INFO "检查yum源中是否有systemd-timesyncd软件包"
dnf list available systemd-timesyncd > /dev/null 2>&1
if [ $? -ne 0 ]; then
LOG_ERROR "yum源中未找到systemd-timesyncd软件包"
exit 255
fi
# 如果未安装则安装systemd-timesyncd
if [ $INSTALLED -eq 0 ]; then
LOG_INFO "安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
# 测试timedatectl命令是否存在
LOG_INFO "测试timedatectl命令是否存在"
which timedatectl > /dev/null 2>&1
CHECK_RESULT $? 0 0 "timedatectl命令不存在"
# 测试timedatectl status命令
LOG_INFO "测试timedatectl status命令"
timedatectl status
CHECK_RESULT $? 0 0 "timedatectl status命令执行失败"
# 测试timedatectl show命令
LOG_INFO "测试timedatectl show命令"
timedatectl show
CHECK_RESULT $? 0 0 "timedatectl show命令执行失败"
# 测试timedatectl show --property=Timezone命令
LOG_INFO "测试timedatectl show --property=Timezone命令"
timedatectl show --property=Timezone
CHECK_RESULT $? 0 0 "timedatectl show --property=Timezone命令执行失败"
# 测试timedatectl show --property=TimeUSec命令
LOG_INFO "测试timedatectl show --property=TimeUSec命令"
timedatectl show --property=TimeUSec
CHECK_RESULT $? 0 0 "timedatectl show --property=TimeUSec命令执行失败"
# 测试timedatectl list-timezones命令
LOG_INFO "测试timedatectl list-timezones命令"
timedatectl list-timezones | head -5
CHECK_RESULT $? 0 0 "timedatectl list-timezones命令执行失败"
# 测试timedatectl set-timezone命令临时设置测试后恢复
LOG_INFO "测试timedatectl set-timezone命令"
ORIGINAL_TZ=$(timedatectl show --property=Timezone --value)
LOG_INFO "当前时区为:$ORIGINAL_TZ"
# 检查Asia/Shanghai时区是否存在
timedatectl list-timezones | grep -q "Asia/Shanghai"
if [ $? -eq 0 ]; then
LOG_INFO "设置时区为Asia/Shanghai"
timedatectl set-timezone Asia/Shanghai
CHECK_RESULT $? 0 0 "timedatectl set-timezone命令执行失败"
# 验证时区设置
NEW_TZ=$(timedatectl show --property=Timezone --value)
if [ "$NEW_TZ" = "Asia/Shanghai" ]; then
LOG_INFO "时区设置成功,当前时区为:$NEW_TZ"
else
LOG_ERROR "时区设置失败,当前时区为:$NEW_TZ"
exit 1
fi
# 恢复原始时区
LOG_INFO "恢复原始时区:$ORIGINAL_TZ"
timedatectl set-timezone "$ORIGINAL_TZ"
CHECK_RESULT $? 0 0 "恢复原始时区失败"
else
LOG_INFO "Asia/Shanghai时区不存在跳过set-timezone测试"
fi
# 测试不支持的参数
LOG_INFO "测试timedatectl不支持的参数"
timedatectl --invalid-parameter > /dev/null 2>&1
if [ $? -eq 0 ]; then
LOG_ERROR "不支持的参数未正确识别"
exit 255
else
LOG_INFO "不支持的参数已正确识别"
fi
# 测试无效的属性
LOG_INFO "测试timedatectl无效的属性"
timedatectl show --property=InvalidProperty > /dev/null 2>&1
if [ $? -eq 0 ]; then
LOG_ERROR "无效的属性未正确识别"
exit 255
else
LOG_INFO "无效的属性已正确识别"
fi
# 环境清理如果测试前未安装则卸载systemd-timesyncd
if [ $INSTALLED -eq 0 ]; then
LOG_INFO "清理环境卸载systemd-timesyncd"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
LOG_INFO "环境已恢复到测试前状态"
else
LOG_INFO "测试前已安装systemd-timesyncd保持安装状态"
fi
LOG_INFO "timedatectl基础命令测试完成"
}
main "$@"

View File

@@ -1,121 +0,0 @@
#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-27
# @License : Mulan PSL v2
# @Desc : 测试手动触发时间同步
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function run_test() {
LOG_INFO "开始测试手动触发时间同步功能"
# 检查系统是否已安装systemd-timesyncd
LOG_INFO "检查systemd-timesyncd是否已安装"
if systemctl status systemd-timesyncd &> /dev/null; then
LOG_INFO "systemd-timesyncd已安装测试结束后将保持安装状态"
INSTALLED=true
else
LOG_INFO "systemd-timesyncd未安装将在测试步骤中安装"
INSTALLED=false
fi
# 检查yum源中是否存在systemd-timesyncd软件包
LOG_INFO "检查yum源中是否存在systemd-timesyncd软件包"
if ! dnf list available systemd-timesyncd &> /dev/null; then
LOG_ERROR "yum源中未找到systemd-timesyncd软件包"
exit 255
fi
# 如果未安装,则安装软件包
if [ "$INSTALLED" = false ]; then
LOG_INFO "安装systemd-timesyncd软件包"
dnf install -y systemd-timesyncd
CHECK_RESULT $? 0 0 "安装systemd-timesyncd失败"
fi
# 检查timedatectl命令是否支持timesync-status参数
LOG_INFO "检查timedatectl命令是否支持timesync-status参数"
if ! timedatectl --help | grep -q "timesync-status"; then
LOG_ERROR "timedatectl命令不支持timesync-status参数"
exit 255
fi
# 检查timedatectl命令是否支持set-ntp参数
LOG_INFO "检查timedatectl命令是否支持set-ntp参数"
if ! timedatectl --help | grep -q "set-ntp"; then
LOG_ERROR "timedatectl命令不支持set-ntp参数"
exit 255
fi
# 确保systemd-timesyncd服务已启用并运行
LOG_INFO "启用并启动systemd-timesyncd服务"
systemctl enable --now systemd-timesyncd
CHECK_RESULT $? 0 0 "启用并启动systemd-timesyncd服务失败"
# 测试手动触发时间同步
LOG_INFO "测试手动触发时间同步"
timedatectl set-ntp false
CHECK_RESULT $? 0 0 "禁用自动时间同步失败"
# 记录当前时间
LOG_INFO "记录当前时间"
OLD_TIME=$(date +%s)
LOG_INFO "当前时间戳: $OLD_TIME"
# 手动触发时间同步
LOG_INFO "手动触发时间同步"
systemctl restart systemd-timesyncd
CHECK_RESULT $? 0 0 "手动触发时间同步失败"
# 等待时间同步完成
LOG_INFO "等待时间同步完成"
sleep 5
# 检查时间同步状态
LOG_INFO "检查时间同步状态"
timedatectl timesync-status
CHECK_RESULT $? 0 0 "获取时间同步状态失败"
# 验证时间是否已同步
LOG_INFO "验证时间是否已同步"
NEW_TIME=$(date +%s)
LOG_INFO "同步后时间戳: $NEW_TIME"
# 如果时间差大于10秒认为同步失败
TIME_DIFF=$((NEW_TIME - OLD_TIME))
if [ $TIME_DIFF -gt 10 ] || [ $TIME_DIFF -lt -10 ]; then
LOG_ERROR "时间同步失败,时间差过大: $TIME_DIFF秒"
exit 1
else
LOG_INFO "时间同步成功,时间差: $TIME_DIFF秒"
fi
# 恢复自动时间同步
LOG_INFO "恢复自动时间同步"
timedatectl set-ntp true
CHECK_RESULT $? 0 0 "恢复自动时间同步失败"
# 清理环境:如果测试前未安装,则卸载软件包
if [ "$INSTALLED" = false ]; then
LOG_INFO "卸载systemd-timesyncd软件包"
dnf remove -y systemd-timesyncd
CHECK_RESULT $? 0 0 "卸载systemd-timesyncd失败"
fi
LOG_INFO "测试手动触发时间同步功能完成"
}
main "$@"