diff --git a/docs/zh/docs/sysSentry/figures/test1.png b/docs/zh/docs/sysSentry/figures/test1.png new file mode 100644 index 0000000000000000000000000000000000000000..0f73382741cae354dfec9bd4d1dc915493f5351a Binary files /dev/null and b/docs/zh/docs/sysSentry/figures/test1.png differ diff --git a/docs/zh/docs/sysSentry/figures/test2.png b/docs/zh/docs/sysSentry/figures/test2.png new file mode 100644 index 0000000000000000000000000000000000000000..2769d89dfc0a698bb89a0644c0a1bdecc664868c Binary files /dev/null and b/docs/zh/docs/sysSentry/figures/test2.png differ diff --git a/docs/zh/docs/sysSentry/figures/test3.png b/docs/zh/docs/sysSentry/figures/test3.png new file mode 100644 index 0000000000000000000000000000000000000000..d4aa9f572aff4527a414409a1c900a8ded5c3a6e Binary files /dev/null and b/docs/zh/docs/sysSentry/figures/test3.png differ diff --git a/docs/zh/docs/sysSentry/figures/test4.png b/docs/zh/docs/sysSentry/figures/test4.png new file mode 100644 index 0000000000000000000000000000000000000000..574c57b56eb966a6c0cdeacdbc4e703052b104f9 Binary files /dev/null and b/docs/zh/docs/sysSentry/figures/test4.png differ diff --git "a/docs/zh/docs/sysSentry/\344\272\214\346\254\241\345\274\200\345\217\221\346\214\207\345\215\227.md" "b/docs/zh/docs/sysSentry/\345\274\200\345\217\221\346\214\207\345\215\227.md" similarity index 80% rename from "docs/zh/docs/sysSentry/\344\272\214\346\254\241\345\274\200\345\217\221\346\214\207\345\215\227.md" rename to "docs/zh/docs/sysSentry/\345\274\200\345\217\221\346\214\207\345\215\227.md" index 866c143f97e3e49dbaec66cd9bfc7f43b6fbe6e9..2ac8542d5c744c62b3f8ea08b3c7ddaf4d9d71b8 100644 --- "a/docs/zh/docs/sysSentry/\344\272\214\346\254\241\345\274\200\345\217\221\346\214\207\345\215\227.md" +++ "b/docs/zh/docs/sysSentry/\345\274\200\345\217\221\346\214\207\345\215\227.md" @@ -1,4 +1,4 @@ -# 二次开发指南 +# 开发指南 sysSentry作为一款巡检任务管理框架,除了管理已提供的插件外,也支持对用户开发的插件进行管理,想要开发一款新的插件并通过sysSentry框架进行管理,需要实现以下能力: 1. 编写插件管理配置文件 @@ -485,4 +485,149 @@ Type "help", "copyright", "credits" or "license" for more information. >>> from sentryCollector.collect_plugin import get_io_data >>> get_io_data(1, ["sda"], ["bio"], ["read"]) {'ret': 0, 'message': '{"sda": {"bio": {"read": [0, 0, 0, 0]}}}'} +``` + +## 测试指南 +### 添加用例 + +测试用例代码放在selftest/test/目录下,测试用例文件要求以`test_`开头。 + +#### python代码测试用例 + +建议统一使用pytest测试框架编写用例,pytest使用教程参考:https://pytest.cn/en/8.2.x/getting-started.html + +#### shell代码测试用例 +##### 测试用例模板 + +``` +source "libs/lib.sh" +source "libs/expect.sh" + +function pre_test() +{ + +} + +function do_test() +{ + +} + +function post_test() +{ + +} + +run_testcase +``` + +说明: +1. libs/lib.sh里面定义了run_testcase方法,该方法用于执行测试用例。 +2. libs/expect.sh里面定义了expect_eq、expect_true等判断方法,这些方法可以用于在测试用例中添加观察点,判断某命令的输出实际结果与预期结果是否相符,只要有一处不符合预期都会导致当前测试用例执行失败。 +3. pre_test等方法功能说明 + +| 函数名 | 函数功能 | +|--------------|--------------------------------| +| pre_test | 用于测试正式开始前的准备工作,比如数据清理,读取环境配置等等 | +| do_test | 执行主要的测试步骤,测试人员需要在这里添加测试观察点 | +| post_test | 用于测试环境的清理和恢复工作,包括恢复测试过程中用例对环境的修改、清理产生的临时文件等 | +| run_testcase | 运行测试用例,该方法会依次调用pre_test、do_test、post_test方法,并在任意时刻结束时调用post_test方法。| + +##### 测试用例常用函数 + +wait.sh提供检测一定周期内指定命令是否执行成功的功能,详情如下: + +| 函数名 | 函数功能说明 | 使用示例 | +|-----|--------|------| +| wait_cmd_ok | 按照指定次数和时间间隔循环等待命令的返回值,直到成功 | wait_cmd_ok "ls exist_file.txt" 0.1 10 | +| wait_cmd_nok | 按照指定次数和时间间隔循环等待命令的返回值,直到失败 | wait_cmd_nok "ls not_exist_file.txt" 0.1 10 | + +log.sh提供日志打印功能,详情如下: + +| 函数名 | 函数功能说明 | 使用示例 | +|-----|--------|------| +| log_info | 打印输出[INFO] 信息 | log_info "this is a info log" | +| log_warn | 打印输出[WARN] 信息 | log_info "this is a warn log" | +| log_error | 打印输出[ERROR] 信息 | log_info "this is a error log" | + +expect.sh提供设置测试观察点需要的函数,用于在用例中判断某些结果是否与预期相符。详情如下: + +| 函数名 | 函数功能说明 | 使用示例 | +|-----|--------|------| +| expect_eq | 检测某数值是否等于指定结果,常用于判断前一个命令的退出码是否等于0 | expect_eq $? 0 "命令执行失败,预期成功" | +| expect_ne | 检测某数值是否不等于指定结果,常用于判断命令执行是否报错(退出码非0) | expect_ne $? 0 "命令执行成功,预期失败" | +| expect_gt | 检测第一个数字是否大于第二个数字 | expect_gt $i 1000 "参数i的值小于等于1000,预期i>1000" | +| expect_true | 检查给定的命令是否执行成功 | expect_true "ll" "ll命令执行失败,预期成功" | +| expect_false | 检查给定的命令是否执行失败 |expect_false "ll" "ll命令执行成功,预期失败" | +| expect_str_eq | 检查给定的2个字符串是否相同 | expect_str_eq $str1 $str2 "str1应该与str2相同,但是实际不同" | +| expect_str_ne | 检查给定的2个字符串是否不同 | expect_str_ne $str1 $str2 "str1应该与str2不同,但是实际相同" | +| expect_task_status_eq | 检测对应巡检任务(task)的状态是否等于预期结果 | expect_task_status_eq "cpu_sentry" "RUNNING" "cpu巡检任务没有在运行" | + +注意:每个函数expect函数后面的说明信息属于错误信息,当测试观察点不通过时会打印对应的错误信息。函数调用时用户可以不传入错误信息,这样不方便用例失败时查看错误原因。 + +common.sh提供测试sysSentry基本功能所需函数,详情如下: + +| 函数名 | 函数功能说明 | 使用示例 | +|-----|--------|------| +|common_pre_test | | | +|update_test_config | | | +|add_test_config | | | +| look_pid | 检查某进程是否存在,如果不存在则返回0,否则返回1 | look_pid "test_task" | + +### 执行用例 + +#### 执行全部用例 + +执行所有测试用例的命令:`make test` + +如果有测试用例失败,错误信息会默认打印在控制台。测试效果如下: + +![输入图片说明](figures/test1.png) + +如果用户期望错误信息不直接输出到控制台,则可以使用命令:`make test log="file"` + +此时,如果有测试用例失败,用例报错信息将会保存到文件中,文件位置在selftest/error_log/。测试效果如下: + +![输入图片说明](figures/test2.png) + +#### 执行单一用例 + +用户可以通过命令`make test t=test_xxx`指定需要执行单个用例,比如: + +![输入图片说明](figures/test3.png) + +某个指定用例名不对,找不到对应的测试文件,那么会打印提示信息,比如: + +![输入图片说明](figures/test4.png) + +#### 添加用例白名单,跳过执行用例 + +将对应用例添加到testsuite_skip_list文件即可。如果用户只期望在某些架构上跳过用例,也可以指定用例跳过执行的架构。配置示例如下: + +``` +# USAGE: +# if it's skipped on all of the arch: +# testsuite +# +# if it's skipped on part of the arch: +# testsuite:arch1 +# testsuite:arch2 +test_cpu_sentry +test_sentryctl_end_exception:x86_64 +``` +说明: +1. 测试用例与架构之间使用英文":"号分割,架构名称后面不要跟任何内容,包括注释信息。比如下面配置是不合法的: + +`test_sentryctl_end_exception:x86_64 # skip x86_64 arch` + +2. 测试用例后面不指定架构,则表明所有架构都跳过执行。 +3. 如果某个用例要在多个架构中跳过执行,则每个架构单独一行配置。比如: + +``` +# 不合法配置: +test_sentryctl_end_exception:x86_64:i686 + +# 合法配置 +test_sentryctl_end_exception:x86_64 +test_sentryctl_end_exception:i686 ``` \ No newline at end of file diff --git a/docs/zh/menu/index.md b/docs/zh/menu/index.md index 79f7d9dc0c3a9dff8eae77ea51a52566406ec196..d6d7f7fe70ea2d8e5c0523ef2d00cae00f73cff5 100644 --- a/docs/zh/menu/index.md +++ b/docs/zh/menu/index.md @@ -192,5 +192,5 @@ headless: true - [安装和使用]({{< relref "./docs/sysSentry/安装和使用.md" >}}) - [平均阈值慢盘检测插件]({{< relref "./docs/sysSentry/平均阈值慢盘检测插件.md" >}}) - [AI阈值慢盘检测插件]({{< relref "./docs/sysSentry/AI阈值慢盘检测插件.md" >}}) - - [二次开发指南]({{< relref "./docs/sysSentry/二次开发指南.md" >}}) + - [开发指南]({{< relref "./docs/sysSentry/开发指南.md" >}}) - [常见问题与解决方法]({{< relref "./docs/sysSentry/常见问题与解决方法.md" >}})