From 27ca42d4af23faf7f0f73efedb855ed3b5764c01 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 6 Feb 2026 16:52:33 +0800 Subject: [PATCH] add arpwatch test case --- .../arpwatch/tc_service_arpwatch_fun001.yaml | 23 +++++ .../arpwatch/tc_service_arpwatch_fun002.yaml | 26 +++++ .../arpwatch/tc_service_arpwatch_fun001.py | 98 +++++++++++++++++++ .../arpwatch/tc_service_arpwatch_fun002.py | 46 +++++++++ 4 files changed, 193 insertions(+) create mode 100644 docs/system_base/arpwatch/tc_service_arpwatch_fun001.yaml create mode 100644 docs/system_base/arpwatch/tc_service_arpwatch_fun002.yaml create mode 100644 tests/system_base/arpwatch/tc_service_arpwatch_fun001.py create mode 100644 tests/system_base/arpwatch/tc_service_arpwatch_fun002.py diff --git a/docs/system_base/arpwatch/tc_service_arpwatch_fun001.yaml b/docs/system_base/arpwatch/tc_service_arpwatch_fun001.yaml new file mode 100644 index 0000000000..e28b213ead --- /dev/null +++ b/docs/system_base/arpwatch/tc_service_arpwatch_fun001.yaml @@ -0,0 +1,23 @@ +作者: douzhihcong +优先级: P1 +支持架构: noarch +执行方式: 自动 +测试类型: 功能测试 +通用标签: local,arpwatch.service +用例描述: arpwatch.service基本功能 +修改人: douzhihcong + +前置条件: +安装arpwatch +测试步骤: +- 测试arpwatch.service重启功能 +-测试arpwatch.service停止功能 +-测试arpwatch.service启动功能 +-判断当前arpwatch.service启用状态,测试disable,enable功能 +-查看日志中从测试开始时间(log_time)起的日志中是否有fail或error关键字(排除无关消息)。 +-测试reload +期望结果: +-服务正常启动,停止,查看,重启 +-disable,enable功能正常 +-日志无报错 +-reload返回预期结果 diff --git a/docs/system_base/arpwatch/tc_service_arpwatch_fun002.yaml b/docs/system_base/arpwatch/tc_service_arpwatch_fun002.yaml new file mode 100644 index 0000000000..b629737dff --- /dev/null +++ b/docs/system_base/arpwatch/tc_service_arpwatch_fun002.yaml @@ -0,0 +1,26 @@ +作者: douzhihcong +优先级: P1 +支持架构: noarch +执行方式: 自动 +测试类型: 功能测试 +通用标签: local,arpwatch +用例描述: 测试 arpwatch 服务的网络接口监控和数据记录功能 +修改人: douzhihcong + +前置条件: +- 系统已安装 arpwatch 软件包 +- arpwatch 服务已启动 + +测试步骤: +- 获取第一个非本地环回网络接口名称 +- 使用获取的网络接口启动 arpwatch 监控 +- 检查 /var/lib/arpwatch/arp.dat 文件的初始大小 +- 执行 ping 命令生成网络流量 +- 再次检查 /var/lib/arpwatch/arp.dat 文件的当前大小 +- 比较当前文件大小是否大于等于初始大小 + +期望结果: +- 成功获取到有效的网络接口名称 +- arpwatch 能够正常在指定网络接口上启动监控 +- ping 命令执行成功 +- 当前文件大小大于等于初始文件大小,表示 arpwatch 成功记录了网络数据 diff --git a/tests/system_base/arpwatch/tc_service_arpwatch_fun001.py b/tests/system_base/arpwatch/tc_service_arpwatch_fun001.py new file mode 100644 index 0000000000..7b7c342747 --- /dev/null +++ b/tests/system_base/arpwatch/tc_service_arpwatch_fun001.py @@ -0,0 +1,98 @@ +""" +@File: tc_service_arpwatch_fun001.py +@Time: 2026/2/6 15:30:20 +@Author: douzhihcong +@Version: 1.0 +@Contact: douzhihcong@inspur.com +@License: Mulan PSL v2 +@Modify: douzhihcong +""" +import subprocess + +from common.basetest import LocalTest + + +class Test(LocalTest): + """ + See tc_service_arpwatch_fun001.yaml for details + + :avocado: tags=P1,noarch,local,arpwatch + """ + PARAM_DIC = {"pkg_name": "arpwatch"} + + def setUp(self): + super().setUp(self.PARAM_DIC) + + def test(self): + service = 'arpwatch.service' + # test_execution + code, log_time = self.cmd("date '+%Y-%m-%d %T'") + # test_restart + code, result = self.cmd(f"systemctl restart {service}") + self.assertFalse(code, f"{service} restart failed") + self.cmd('sleep 5') + code, result = self.cmd(f"systemctl status {service}| grep 'Active: active'") + self.assertFalse(code, f"{service} restart failed") + code, result = self.cmd(f"systemctl stop {service}") + self.assertFalse(code, f"{service} stop failed") + self.cmd('sleep 5') + code, result = self.cmd(f"systemctl status {service}| grep 'Active: inactive'") + self.assertFalse(code, f"{service} stop failed") + code, result = self.cmd(f"systemctl start {service}") + self.assertFalse(code, f"{service} start failed") + self.cmd('sleep 5') + code, result = self.cmd(f"systemctl status {service}| grep 'Active: active'") + self.assertFalse(code, f"{service} start failed") + # test_enabled + command = f'systemctl is-enabled {service}' + result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + status1 = result.stdout.decode('utf-8').strip() + if status1 == "enabled": + code, symlink_file = self.cmd( + f'systemctl disable {service} 2>&1 | grep "Removed" | awk \'{{print $2}}\' | awk \'{{print substr($0,1,length($0)-1)}}\' | tr -d \'\"\'') + command = f'find {symlink_file}' + result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + self.assertTrue(result.returncode, f"{service} disable failed") + self.cmd(f'systemctl enable {service}') + code, result = self.cmd(f'find {symlink_file}') + self.assertFalse(code, f"{service} enable failed") + elif status1 == "disabled": + code, symlink_file = self.cmd( + f'systemctl enable "{service}" 2>&1 | grep "Created symlink" | awk \'{{print $3}}\' | tr -d \'\"\'') + code, result = self.cmd(f'find {symlink_file}') + self.assertFalse(code, f"{service} enable failed") + self.cmd(f'systemctl disable {service}') + command = f'find {symlink_file}' + result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + self.assertTrue(result.returncode, f"{service} disable failed") + elif status1 == "masked": + self.log.info("Unit is masked, ignoring.") + elif status1 == "static": + self.log.info( + "The unit files have no installation config,This means they are not meant to be enabled using systemctl.") + else: + self.log.info("Unit is indirect, ignoring.") + # execution + command = f' journalctl --since {log_time} -u {service} | grep -i "fail\|error" | grep -v -i "DEBUG\|INFO\|WARNING"' + result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + self.assertTrue(result.returncode, f"There is an error message for the log of {service}") + # test_reload + self.cmd(f'systemctl start {service}') + command = f'systemctl cat {service} | grep -q "ExecReload"' + result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if not result.returncode: + code, result = self.cmd(f'systemctl reload {service}') + self.assertFalse(code, f"reload unit {service} failed") + else: + code, result = self.cmd(f'systemctl reload {service} 2>&1 | grep "Job type reload is not applicable"') + self.assertFalse(code, f"Job type reload is not applicable for unit {service}") + code, result = self.cmd(f'systemctl status {service} | grep "Active: active"') + if code: + command = f'systemctl status {service} | grep "inactive (dead)"' + result1 = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if not result1.returncode: + code2, result = self.cmd(f'systemctl status {service} | grep "Condition check" | grep "skip"') + self.assertFalse(code2, f'{service} reload causes the service status to change') + + def tearDown(self): + super().tearDown(self.PARAM_DIC) diff --git a/tests/system_base/arpwatch/tc_service_arpwatch_fun002.py b/tests/system_base/arpwatch/tc_service_arpwatch_fun002.py new file mode 100644 index 0000000000..d66ba84c92 --- /dev/null +++ b/tests/system_base/arpwatch/tc_service_arpwatch_fun002.py @@ -0,0 +1,46 @@ +""" +@File: tc_service_arpwatch_fun002.py +@Time: 2026/2/6 15:30:20 +@Author: douzhihcong +@Version: 1.0 +@Contact: douzhihcong@inspur.com +@License: Mulan PSL v2 +@Modify: douzhihcong +""" +import subprocess + +from common.basetest import LocalTest + + +class Test(LocalTest): + """ + See tc_service_arpwatch_fun002.yaml for details + + :avocado: tags=P1,noarch,local,arpwatch + """ + PARAM_DIC = {"pkg_name": "arpwatch"} + + def setUp(self): + super().setUp(self.PARAM_DIC) + self.cmd('systemctl start arpwatch.service') + + def test(self): + code,result=self.cmd("ip link show | grep -v \"lo\" | awk -F: '$0 !~ \"^ \" {print $2}' | head -n 1 | tr -d ' '") + first_interface=result.strip() + code,result=self.cmd(f'arpwatch -i {first_interface}') + self.assertFalse(code,"Check arpwatch interface monitoring failed") + code,result=self.cmd('stat -c %s /var/lib/arpwatch/arp.dat') + initial_size=result.strip() + self.cmd(' ping -c 4 127.0.0.1') + code, result = self.cmd('stat -c %s /var/lib/arpwatch/arp.dat') + current_size = result.strip() + if current_size >= initial_size: + code=0 + else: + code = 1 + self.assertFalse(code, 'Check arpwatch data recording failed') + + + def tearDown(self): + super().tearDown(self.PARAM_DIC) + -- Gitee