Skip to contentSkip to conversation

lib: add TST_RES_ serious macros

RFC PATCH In review Li Wang 3 participants 5 messages Jun 26, 2024 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

RFC PATCH 1/2 lib: add TST_RES_ serious macros

This patch introduces new macros to encapsulate existing tst_res and tst_brk functions in the LTP library. These macros simplify the usage of tst_ functions by providing a more user-friendly interface for generating standardized test output.

  TST_RES_TINFO()    -> tst_res(TINFO, ...)
  TST_RES_TWARN()    -> tst_res(TINFO, "WARNING: ", ...)
  TST_RES_TPASS()    -> tst_res(TPASS, ...)
  TST_RES_TDEBUG()   -> tst_res(TDEBUG, ...)
  TST_RES_TFAIL()    -> tst_res(TFAIL,  ...)

TST_BRK_TCONF() -> tst_brk(TCONF, ...)
TST_BRK_TBROK() -> tst_brk(TBROK, ...)

  TST_RES_TPASS_ER() -> tst_res(TPASS | errno, ...)
  TST_RES_TFAIL_ER() -> tst_res(TFAIL | errno, ...)
  TST_BRK_TBROK_ER() -> tst_brk(TBROK | errno, ... )

The macros handle various scenarios including simple messages and messages with error flags, ensuring consistent logging of file and line information.

Additionally, a new test case in tst_res_macros.c demonstrates the usage of these macros.

---

Notes:
At the begining I wanted to implement one macro to implement functionality like handling both messages and printing errno with the message. Still, after trying some complex macro definition usages I found that this made things too complex and confusing, so why not define more macros for special use, and then this patch was signed.

Maybe something like TST_RES_TFAIL_ER is not a good name, but please help to give another, or any advise will be appreciated.

 include/tst_test_macros.h         | 34 +++++++++++++++++++++
 lib/newlib_tests/tst_res_macros.c | 50 +++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 lib/newlib_tests/tst_res_macros.c
include/tst_test_macros.h +34 -0
index 22b39fb14..e36df9c0d 100644
@@ -368,4 +368,38 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
 #define TST_EXP_EQ_SSZ(VAL_A, VAL_B) \
 		TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, ssize_t, "%zi")
 
+/* Test result print macros */
+#define TST_RES_TINFO(MESSAGE, ...) \
+	tst_res(TINFO, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TCONF(MESSAGE, ...) \
+	tst_res(TCONF, MESSAGE, ##__VA_ARGS__)
+
+#define TST_BRK_TCONF(MESSAGE, ...) \
+	tst_brk(TCONF, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TWARN(MESSAGE, ...) \
+	tst_res(TINFO, "WARNING: "MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TPASS(MESSAGE, ...) \
+	tst_res(TPASS, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TPASS_ER(flag, MESSAGE, ...) \
+	tst_res(TPASS | flag, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TDEBUG(MESSAGE, ...) \
+	tst_res(TDEBUG, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TFAIL(MESSAGE, ...) \
+	tst_res(TFAIL, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TFAIL_ER(flag, MESSAGE, ...) \
+	tst_res(TFAIL | flag, MESSAGE, ##__VA_ARGS__)
+
+#define TST_BRK_TBROK(MESSAGE, ...) \
+	tst_brk(TBROK, MESSAGE, ##__VA_ARGS__)
+
+#define TST_BRK_TBROK_ER(flag, MESSAGE, ...) \
+	tst_brk(TBROK | flag, MESSAGE, ##__VA_ARGS__)
+
 #endif	/* TST_TEST_MACROS_H__ */
lib/newlib_tests/tst_res_macros.c new file +50 -0

50 lines not on this page · raw message

Signature · 5 lines
2.45.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#1 lore raw

RFC PATCH 2/2 testcase: use new TST_RES macros

This is a damo for use TST_RES_ MACRO (randomly replace some), we might not want to replace tst_res() all by macros, to keep both methods exist probably more flexiable.

---
 lib/tst_kconfig.c                               |  2 +-
 lib/tst_supported_fs_types.c                    |  2 +-
 testcases/kernel/kvm/kvm_pagefault01.c          |  2 +-
 testcases/kernel/mem/hugetlb/lib/hugetlb.c      |  2 +-
 testcases/kernel/syscalls/ipc/semctl/semctl08.c |  2 +-
 testcases/kernel/syscalls/madvise/madvise01.c   |  6 +++---
 testcases/kernel/syscalls/madvise/madvise02.c   | 10 +++++-----
 7 files changed, 13 insertions(+), 13 deletions(-)
lib/tst_kconfig.c +1 -1
index e16ca1400..3678669c7 100644
@@ -606,7 +606,7 @@ void tst_kcmdline_parse(struct tst_kcmdline_var params[], size_t params_len)
 		break;
 		default:
 			if (b_pos + 1 >= sizeof(buf)) {
-				tst_res(TWARN, "Buffer overflowed while parsing /proc/cmdline");
+				TST_RES_TWARN("Buffer overflowed while parsing /proc/cmdline");
 				while (line[l_pos] != '\0' && line[l_pos] != ' ' && line[l_pos] != '\n')
 					l_pos++;
 
lib/tst_supported_fs_types.c +1 -1
index bbbb8df19..fbee7f23b 100644
@@ -158,7 +158,7 @@ const char **tst_get_supported_fs_types(const char *const *skiplist)
 	only_fs = getenv("LTP_SINGLE_FS_TYPE");
 
 	if (only_fs) {
-		tst_res(TINFO, "WARNING: testing only %s", only_fs);
+		TST_RES_TWARN("testing only %s", only_fs);
 		if (tst_fs_is_supported(only_fs))
 			fs_types[0] = only_fs;
 		return fs_types;
testcases/kernel/kvm/kvm_pagefault01.c +1 -1
index 16b3137c0..e6ed1bacb 100644
@@ -200,7 +200,7 @@ static void disable_tdp(void)
 		reload_module("kvm_intel", "ept=0");
 
 	if (read_bool_sys_param(TDP_MMU_SYSFILE) > 0)
-		tst_res(TINFO, "WARNING: tdp_mmu is enabled, beware of false negatives");
+		TST_RES_TWARN("tdp_mmu is enabled, beware of false negatives");
 }
 
 static void setup(void)
testcases/kernel/mem/hugetlb/lib/hugetlb.c +1 -1
index 43a677ce9..14fc5a905 100644
@@ -106,7 +106,7 @@ void rm_shm(int shm_id)
 	 * check for # of attaches ?
 	 */
 	if (shmctl(shm_id, IPC_RMID, NULL) == -1) {
-		tst_res(TINFO, "WARNING: shared memory deletion failed.");
+		TST_RES_TWARN("shared memory deletion failed.");
 		tst_res(TINFO, "This could lead to IPC resource problems.");
 		tst_res(TINFO, "id = %d", shm_id);
 	}
testcases/kernel/syscalls/ipc/semctl/semctl08.c +1 -1
index 1878bd49d..64160af05 100644
@@ -40,7 +40,7 @@ static void run(void)
 		tst_res(TPASS, "time_high fields cleared by the kernel");
 
 	if (semctl(semid, 0, IPC_RMID, arg) == -1)
-		tst_res(TINFO, "WARNING: semaphore deletion failed.");
+		TST_RES_TWARN("semaphore deletion failed.");
 }
 
 static struct tst_test test = {
testcases/kernel/syscalls/madvise/madvise01.c +3 -3
index 884c59b0b..ba2c79536 100644
@@ -102,15 +102,15 @@ static void verify_madvise(unsigned int i)
 
 	if (TST_RET == -1) {
 		if (TST_ERR == EINVAL) {
-			tst_res(TCONF, "%s is not supported", tc->name);
+			TST_BRK_TCONF("%s is not supported", tc->name);
 		} else {
-			tst_res(TFAIL, "madvise test for %s failed with "
+			TST_RES_TFAIL("madvise test for %s failed with "
 					"return = %ld, errno = %d : %s",
 					tc->name, TST_RET, TST_ERR,
 					tst_strerrno(TFAIL | TTERRNO));
 		}
 	} else {
-		tst_res(TPASS, "madvise test for %s PASSED", tc->name);
+		TST_RES_TPASS("madvise test for %s PASSED", tc->name);
 	}
 }
 
testcases/kernel/syscalls/madvise/madvise02.c +5 -5
index cc3faf5cf..bfe6e55c4 100644
@@ -92,7 +92,7 @@ static void tcases_filter(void)
 		switch (tc->advice) {
 		case MADV_DONTNEED:
 			if (mlock(file1, st.st_size) < 0)
-				tst_brk(TBROK | TERRNO, "mlock failed");
+				TST_BRK_TBROK_ER(TERRNO, "mlock failed");
 		break;
 		case MADV_MERGEABLE:
 		case MADV_UNMERGEABLE:
@@ -160,22 +160,22 @@ static void advice_test(unsigned int i)
 	struct tcase *tc = &tcases[i];
 
 	if (tc->skip == 1) {
-		tst_res(TCONF, "%s is not supported", tc->name);
+		TST_RES_TCONF("%s is not supported", tc->name);
 		return;
 	}
 
 	TEST(madvise(*(tc->addr), st.st_size, tc->advice));
 	if (TST_RET == -1) {
 		if (TST_ERR == tc->exp_errno) {
-			tst_res(TPASS | TTERRNO, "%s failed as expected", tc->name);
+			TST_RES_TPASS_ER(TTERRNO, "%s failed as expected", tc->name);
 		} else {
-			tst_res(TFAIL | TTERRNO,
+			TST_RES_TFAIL_ER(TTERRNO,
 					"%s failed unexpectedly; expected - %d : %s",
 					tc->name, tc->exp_errno,
 					tst_strerrno(TFAIL | TTERRNO));
 		}
 	} else {
-		tst_res(TFAIL, "madvise succeeded unexpectedly");
+		TST_RES_TFAIL("madvise succeeded unexpectedly");
 	}
 }
 
Signature · 5 lines
2.45.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Li Wang

Hi Li,

Sorry but I don't see the point on it. What we can do with tst_res() is enough and this patch adds one more layer of complexity to the main library. Both approaches are equivalent, also from the point of view of the ending result.

I will let other maintainers discussing this feature, but from my point of view (as reviewer), I would reject it.

Best regards,
Andrea

Quoted 141 lines · Li Wang

This patch introduces new macros to encapsulate existing tst_res and tst_brk functions in the LTP library. These macros simplify the usage of tst_ functions by providing a more user-friendly interface for generating standardized test output.

   TST_RES_TINFO()    -> tst_res(TINFO, ...)
   TST_RES_TWARN()    -> tst_res(TINFO, "WARNING: ", ...)
   TST_RES_TPASS()    -> tst_res(TPASS, ...)
   TST_RES_TDEBUG()   -> tst_res(TDEBUG, ...)
   TST_RES_TFAIL()    -> tst_res(TFAIL,  ...)
   TST_BRK_TCONF()    -> tst_brk(TCONF, ...)
   TST_BRK_TBROK()    -> tst_brk(TBROK,  ...)

   TST_RES_TPASS_ER() -> tst_res(TPASS | errno, ...)
   TST_RES_TFAIL_ER() -> tst_res(TFAIL | errno, ...)
   TST_BRK_TBROK_ER() -> tst_brk(TBROK | errno, ... )

The macros handle various scenarios including simple messages and messages with error flags, ensuring consistent logging of file and line information.

Additionally, a new test case in tst_res_macros.c demonstrates the usage of these macros.

---

Notes:
At the begining I wanted to implement one macro to implement functionality like handling both messages and printing errno with the message. Still, after trying some complex macro definition usages I found that this made things too complex and confusing, so why not define more macros for special use, and then this patch was signed.

Maybe something like TST_RES_TFAIL_ER is not a good name, but please help to give another, or any advise will be appreciated.

  include/tst_test_macros.h         | 34 +++++++++++++++++++++
  lib/newlib_tests/tst_res_macros.c | 50 +++++++++++++++++++++++++++++++
  2 files changed, 84 insertions(+)

create mode 100644 lib/newlib_tests/tst_res_macros.c

include/tst_test_macros.h +34 -0
index 22b39fb14..e36df9c0d 100644
@@ -368,4 +368,38 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  #define TST_EXP_EQ_SSZ(VAL_A, VAL_B) \
  		TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, ssize_t, "%zi")
 
+/* Test result print macros */
+#define TST_RES_TINFO(MESSAGE, ...) \
+	tst_res(TINFO, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TCONF(MESSAGE, ...) \
+	tst_res(TCONF, MESSAGE, ##__VA_ARGS__)
+
+#define TST_BRK_TCONF(MESSAGE, ...) \
+	tst_brk(TCONF, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TWARN(MESSAGE, ...) \
+	tst_res(TINFO, "WARNING: "MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TPASS(MESSAGE, ...) \
+	tst_res(TPASS, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TPASS_ER(flag, MESSAGE, ...) \
+	tst_res(TPASS | flag, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TDEBUG(MESSAGE, ...) \
+	tst_res(TDEBUG, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TFAIL(MESSAGE, ...) \
+	tst_res(TFAIL, MESSAGE, ##__VA_ARGS__)
+
+#define TST_RES_TFAIL_ER(flag, MESSAGE, ...) \
+	tst_res(TFAIL | flag, MESSAGE, ##__VA_ARGS__)
+
+#define TST_BRK_TBROK(MESSAGE, ...) \
+	tst_brk(TBROK, MESSAGE, ##__VA_ARGS__)
+
+#define TST_BRK_TBROK_ER(flag, MESSAGE, ...) \
+	tst_brk(TBROK | flag, MESSAGE, ##__VA_ARGS__)
+
  #endif	/* TST_TEST_MACROS_H__ */
lib/newlib_tests/tst_res_macros.c new file +50 -0
new file mode 100644
index 000000000..74b656da0
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Li Wang <[email protected]>
+ */
+
+#include "tst_test.h"
+
+static void do_test(void)
+{
+	int i = 10, j = 20;
+	char *str = "test";
+
+	TST_RES_TINFO("message");
+	TST_RES_TINFO("message, i = %d", i);
+	TST_RES_TDEBUG("message, i = %d", i);
+
+	TST_RES_TPASS("message");
+	TST_RES_TPASS("message, i = %d, j = %d, str = %s", i, j, str);
+	TST_RES_TPASS_ER(TTERRNO, "message, i = %d, j = %d", i, j);
+
+	TST_RES_TWARN("message");
+	TST_RES_TWARN("message, i = %d", i);
+
+	TST_RES_TFAIL("message");
+	TST_RES_TFAIL("message, i = %d, j = %d", i, j);
+
+	TST_RES_TFAIL_ER(TERRNO, "message");
+	TST_RES_TFAIL_ER(TERRNO, "message, i = %d, j = %d", i, j);
+	TST_RES_TFAIL_ER(TERRNO, "message, i = %d, str = %s", i, str);
+
+	TST_RES_TFAIL_ER(TTERRNO, "message");
+	TST_RES_TFAIL_ER(TTERRNO, "message, i = %d", i);
+	TST_RES_TFAIL_ER(TTERRNO, "message, i = %d, str = %s", i, str);
+
+	TST_BRK_TBROK("message");
+	TST_BRK_TBROK("message, i = %d, j = %d", i, j);
+
+	TST_BRK_TBROK_ER(TERRNO, "message");
+	TST_BRK_TBROK_ER(TERRNO, "message, i = %d, str = %s", i, str);
+
+	TST_BRK_TBROK_ER(TTERRNO, "message");
+	TST_BRK_TBROK_ER(TTERRNO, "message, i = %d", i);
+	TST_BRK_TBROK_ER(TTERRNO, "message, i = %d, str = %s", i, str);
+
+	TST_BRK_TCONF("message, i = %d, j = %d, str = %s", i, j, str);
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
In reply to Li Wang

Hi!

Quoted 18 lines

This patch introduces new macros to encapsulate existing tst_res and tst_brk functions in the LTP library. These macros simplify the usage of tst_ functions by providing a more user-friendly interface for generating standardized test output.

  TST_RES_TINFO()    -> tst_res(TINFO, ...)
  TST_RES_TWARN()    -> tst_res(TINFO, "WARNING: ", ...)
  TST_RES_TPASS()    -> tst_res(TPASS, ...)
  TST_RES_TDEBUG()   -> tst_res(TDEBUG, ...)
  TST_RES_TFAIL()    -> tst_res(TFAIL,  ...)

TST_BRK_TCONF() -> tst_brk(TCONF, ...)
TST_BRK_TBROK() -> tst_brk(TBROK, ...)

  TST_RES_TPASS_ER() -> tst_res(TPASS | errno, ...)
  TST_RES_TFAIL_ER() -> tst_res(TFAIL | errno, ...)
  TST_BRK_TBROK_ER() -> tst_brk(TBROK | errno, ... )
Quoted 5 lines

The macros handle various scenarios including simple messages and messages with error flags, ensuring consistent logging of file and line information.

Additionally, a new test case in tst_res_macros.c demonstrates the usage of these macros.

I actually like the function better. It makes sense to add macros when they do something more complicated e.g. the TST_EXP_ macros save a lot of code, but in this case I do not think that they add any value, the change is only cosmetic.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#4 lore raw

Hi Cyril and Andrea,

Quoted 6 lines · Cyril Hrubis
Quoted 2 lines

The macros handle various scenarios including simple messages and

messages

Quoted 1 line

with error flags, ensuring consistent logging of file and line

information.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

Additionally, a new test case in tst_res_macros.c demonstrates the usage of these macros.

I actually like the function better. It makes sense to add macros when they do something more complicated e.g. the TST_EXP_ macros save a lot of code, but in this case I do not think that they add any value, the change is only cosmetic.

Thanks for the feedback, I admit that those macros are a bit redundant.

But back to the starting point [1], my original intention was just to mask TWARN
as a failure in the test log. It keeps boring our engineers when reviewing the warnings
in the automation job.

What about just adding one macro to save energy on TWARN review?

 #define TST_RES_TWARN(MESSAGE, ...) \
        tst_res(TINFO, "WARNING: "MESSAGE, ##__VA_ARGS__)

[1] https://lists.linux.it/pipermail/ltp/2024-May/038561.html

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp