264 lines
7.3 KiB
Diff
264 lines
7.3 KiB
Diff
From 49f525d214694d2be5ad551c2a51ec1f50f232ed Mon Sep 17 00:00:00 2001
|
||
From: lvying6 <lvying6@huawei.com>
|
||
Date: Sun, 16 Oct 2022 15:42:33 +0800
|
||
Subject: [PATCH] add test framework and testcasese
|
||
|
||
Signed-off-by: lvying6 <lvying6@huawei.com>
|
||
---
|
||
Makefile | 8 +++++++
|
||
README.md | 21 +++++++++++++++++
|
||
common_helper.h | 1 +
|
||
test/Makefile | 50 ++++++++++++++++++++++++++++++++++++++++
|
||
test/evaluate-test.sh | 16 +++++++++++++
|
||
test/test-driver.c | 15 ++++++++++++
|
||
test/tst-common_helper.c | 46 ++++++++++++++++++++++++++++++++++++
|
||
test/tst-conf-file | 7 ++++++
|
||
8 files changed, 164 insertions(+)
|
||
create mode 100644 test/Makefile
|
||
create mode 100755 test/evaluate-test.sh
|
||
create mode 100644 test/test-driver.c
|
||
create mode 100644 test/tst-common_helper.c
|
||
create mode 100644 test/tst-conf-file
|
||
|
||
diff --git a/Makefile b/Makefile
|
||
index f5ba4bb..cac7521 100644
|
||
--- a/Makefile
|
||
+++ b/Makefile
|
||
@@ -6,6 +6,7 @@ PAHOLE ?= pahole
|
||
READELF ?= readelf
|
||
VMLINUX ?= /usr/lib/debug/lib/modules/`uname -r`/vmlinux
|
||
VMLINUX_HEADER ?= $(OUTPUT)/vmlinux.h
|
||
+MAKE ?= make
|
||
|
||
BTF_PAHOLE_PROBE := $(shell $(READELF) -S $(VMLINUX) | grep .BTF 2>&1)
|
||
INCLUDES := -I$(OUTPUT)
|
||
@@ -39,10 +40,17 @@ all: $(APPS)
|
||
debug: DEBUG_FLAGS = -DBPFDEBUG
|
||
debug: all
|
||
|
||
+export CFLAGS OUTPUT Q
|
||
.PHONY: clean
|
||
clean:
|
||
$(call msg,CLEAN)
|
||
$(Q)rm -rf $(OUTPUT) $(APPS) $(patsubst %,%.bpf.o,$(APPS))
|
||
+ $(Q)$(MAKE) -C test clean
|
||
+
|
||
+.PHONY: check
|
||
+check: all
|
||
+ $(call msg, CHECK)
|
||
+ $(Q)$(MAKE) -C test
|
||
|
||
$(OUTPUT):
|
||
$(call msg,MKDIR,$@)
|
||
diff --git a/README.md b/README.md
|
||
index 1750627..de4fd9d 100644
|
||
--- a/README.md
|
||
+++ b/README.md
|
||
@@ -46,6 +46,27 @@ make
|
||
|
||
编译过程中可能遇到`kenrl-debuginfo`rpm包提供vmlinux路径与`uname -r`不一致的情况导致编译失败。这是由于当前运行的内核版本与`kernel-debuginfo`的版本不一致。openEuler yum源的kernel版本较高,可以执行`dnf update kernel`升级当前kernel到yum源的kernel版本,重启系统,kernel版本便与`kernel-debuginfo`版本一致,再重新编译。
|
||
|
||
+#### 测试
|
||
+##### 执行测试用例
|
||
+```bash
|
||
+make check
|
||
+```
|
||
+执行测试用例依赖于源码构建,会自动编译。测试用例运行结果信息示例如下所示:
|
||
+```bash
|
||
+ CHECK
|
||
+ PASS: tst-common_helper
|
||
+ Test Summary:
|
||
+ Unsupport: 0
|
||
+ Fail: 0
|
||
+ Pass: 1
|
||
+```
|
||
+会显示每个测试用例的执行结果,如`tst-common_helper`测试用例执行成功。最后以`Test Summary:`行开头总结所有测试用例执行情况。`Unsupport`表示不支持在当前测试平台上运行的测试用例数目,`Fail`表示失败的测试用例数目,`Pass`表示成功的测试用例数目。
|
||
+
|
||
+##### 新增测试用例
|
||
+如果要新增对源码中目标文件中的函数进行测试,则在`test`目录下新增前缀为`tst-`,后半部分与测试目标文件名一致的测试文件。如要对`common_helper.c`中的函数进行测试,则新建名为`tst-common_helper.c`的测试文件。
|
||
+
|
||
+测试文件中的测试用例定义在`int do_test(void)`函数中,测试用例首先应该检查能否在测试平台上进行运行,如果不支持,则返回2(unsupport);测试用例运行失败返回1(fail);成功则返回0(pass)。在测试文件结尾添加`#include "test/test-driver.c"`语句即可。
|
||
+
|
||
#### 参与贡献
|
||
|
||
1. Fork 本仓库
|
||
diff --git a/common_helper.h b/common_helper.h
|
||
index 8fc52a6..2680318 100644
|
||
--- a/common_helper.h
|
||
+++ b/common_helper.h
|
||
@@ -2,6 +2,7 @@
|
||
#define _COMMON_HELPER_H
|
||
|
||
#include <syslog.h>
|
||
+#include <stdio.h>
|
||
|
||
#define SHASH 11
|
||
#define SYSLOG (1 << 0)
|
||
diff --git a/test/Makefile b/test/Makefile
|
||
new file mode 100644
|
||
index 0000000..bbbdb7a
|
||
--- /dev/null
|
||
+++ b/test/Makefile
|
||
@@ -0,0 +1,50 @@
|
||
+.DEFAULT_GOAL := check
|
||
+INCLUDES := -I../
|
||
+
|
||
+evaluate-test := evaluate-test.sh
|
||
+# test source code functions in object files
|
||
+test_objs := tst-common_helper
|
||
+
|
||
+# Build C testcases code
|
||
+$(patsubst %, %.o, $(test_objs)): %.o: %.c
|
||
+ $(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@
|
||
+
|
||
+$(test_objs): %: %.o
|
||
+ $(Q)$(CC) $(CFLAGS) $^ ../$(OUTPUT)/$(strip $(subst tst-, ,$@)).o -o $@
|
||
+
|
||
+# all the lines in the recipe be passed to a single invocation of the shell
|
||
+.ONESHELL:
|
||
+SHELL = /bin/sh
|
||
+
|
||
+define run_tests
|
||
+ unsupport=0
|
||
+ fail=0
|
||
+ pass=0
|
||
+ for testcase in $1; do
|
||
+ ./$$testcase >$$testcase.out 2>&1
|
||
+ ret=$$?
|
||
+ $(SHELL) $(evaluate-test) $$testcase $$ret
|
||
+ if [ $$ret -eq 2 ]; then
|
||
+ unsupport=`expr $$unsupport + 1`
|
||
+ else
|
||
+ if [ $$ret -eq 0 ]; then
|
||
+ pass=`expr $$pass + 1`
|
||
+ else
|
||
+ fail=`expr $$fail + 1`
|
||
+ fi
|
||
+ fi
|
||
+ done
|
||
+
|
||
+ echo -e " Test Summary:"
|
||
+ echo -e " Unsupport: $$unsupport"
|
||
+ echo -e " Fail: $$fail"
|
||
+ echo -e " Pass: $$pass"
|
||
+endef
|
||
+
|
||
+.PHONY: check
|
||
+check: $(test_objs)
|
||
+ @$(call run_tests, $<)
|
||
+
|
||
+.PHONY: clean
|
||
+clean:
|
||
+ $(Q)rm -rf $(test_objs) $(patsubst %,%.o,$(test_objs)) $(patsubst %,%.out,$(test_objs))
|
||
diff --git a/test/evaluate-test.sh b/test/evaluate-test.sh
|
||
new file mode 100755
|
||
index 0000000..31a8914
|
||
--- /dev/null
|
||
+++ b/test/evaluate-test.sh
|
||
@@ -0,0 +1,16 @@
|
||
+#!/bin/bash
|
||
+
|
||
+test_name=$1
|
||
+rc=$2
|
||
+
|
||
+if [ $rc -eq 2 ]; then
|
||
+ result="UNSUPPORTED"
|
||
+else
|
||
+ if [ $rc -eq 0 ]; then
|
||
+ result="PASS"
|
||
+ else
|
||
+ result="FAIL"
|
||
+ fi
|
||
+fi
|
||
+
|
||
+echo -e " $result: $test_name"
|
||
diff --git a/test/test-driver.c b/test/test-driver.c
|
||
new file mode 100644
|
||
index 0000000..6e58c9c
|
||
--- /dev/null
|
||
+++ b/test/test-driver.c
|
||
@@ -0,0 +1,15 @@
|
||
+/*
|
||
+ * glibc style testcase:
|
||
+ * this file should be included from testcases, the testcase should define a function:
|
||
+ * int do_test(void)
|
||
+ *
|
||
+ * The do_test return 0 to indicate a passing test,
|
||
+ * 1 to indicate a failing test
|
||
+ * 2 to indicate an unsupported test
|
||
+ */
|
||
+extern int do_test(void);
|
||
+
|
||
+int main(int argc, char **argv)
|
||
+{
|
||
+ return do_test();
|
||
+}
|
||
diff --git a/test/tst-common_helper.c b/test/tst-common_helper.c
|
||
new file mode 100644
|
||
index 0000000..1f1ce21
|
||
--- /dev/null
|
||
+++ b/test/tst-common_helper.c
|
||
@@ -0,0 +1,46 @@
|
||
+#include <string.h>
|
||
+#include "common_helper.h"
|
||
+
|
||
+#define CONFIG_FILE "tst-conf-file"
|
||
+
|
||
+struct expected_option {
|
||
+ const char *name;
|
||
+ const char *expected_val;
|
||
+};
|
||
+
|
||
+struct expected_option test_options[] = {
|
||
+ {"space_integer", "23"},
|
||
+ {"space_float", "3.1415926"},
|
||
+ {"no_space_str", "hello world"},
|
||
+ {"space_str", "hello world"},
|
||
+ {"empty_field", ""}
|
||
+};
|
||
+
|
||
+static int do_test(void)
|
||
+{
|
||
+ int ret = 0;
|
||
+ struct opt **opts = parse_init(SHASH);
|
||
+ if (!opts) {
|
||
+ log(TERM, LOG_ERR, "parse_init failed\n");
|
||
+ return 1;
|
||
+ }
|
||
+
|
||
+ if (parse_config_file(TERM, CONFIG_FILE, opts, SHASH)) {
|
||
+ log(TERM, LOG_ERR, "parse_config_file failed\n");
|
||
+ return 1;
|
||
+ }
|
||
+
|
||
+ for (int i = 0; i < sizeof(test_options) / sizeof(struct expected_option); i++) {
|
||
+ const char *res = config_opt(opts, SHASH, test_options[i].name);
|
||
+ if (strcmp(test_options[i].expected_val, res)) {
|
||
+ ret = 1;
|
||
+ goto out;
|
||
+ }
|
||
+ }
|
||
+
|
||
+out:
|
||
+ parse_fini(opts, SHASH);
|
||
+ return ret;
|
||
+}
|
||
+
|
||
+#include "test/test-driver.c"
|
||
diff --git a/test/tst-conf-file b/test/tst-conf-file
|
||
new file mode 100644
|
||
index 0000000..822de28
|
||
--- /dev/null
|
||
+++ b/test/tst-conf-file
|
||
@@ -0,0 +1,7 @@
|
||
+# Test comments
|
||
+ space_integer = 23
|
||
+ space_float = 3.1415926
|
||
+no_space_str=hello world
|
||
+space_str = hello world
|
||
+ empty_field =
|
||
+
|
||
--
|
||
2.33.0
|
||
|