[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 4/7] tools/tests: factor out common helpers



Share fail/verify macros, and the handling or nr_failures.
The tests will no longer have a `main` function, but instead have a
`test_main`. This ensures that `nr_failures` is handled consistently.

This'll make it easier to write new tests that are consistent with the
existing ones, without having to copy/paste.

Signed-off-by: Edwin Török <edwin.torok@xxxxxxxxxx>
---
 tools/tests/Rules.mk                           |  3 +++
 tools/tests/common/tests.c                     | 15 +++++++++++++++
 tools/tests/common/tests.h                     | 18 ++++++++++++++++++
 tools/tests/cpu-policy/test-cpu-policy.c       |  7 +------
 tools/tests/domid/test-domid.c                 | 11 ++---------
 tools/tests/mem-claim/test-mem-claim.c         |  9 ++-------
 .../tests/paging-mempool/test-paging-mempool.c |  9 ++-------
 tools/tests/pdx/test-pdx.c                     |  3 ++-
 tools/tests/rangeset/test-rangeset.c           |  3 ++-
 tools/tests/resource/test-resource.c           |  9 ++-------
 tools/tests/tsx/test-tsx.c                     | 10 ++--------
 tools/tests/vpci/main.c                        |  4 ++--
 tools/tests/xenstore/test-xenstore.c           |  4 +++-
 13 files changed, 56 insertions(+), 49 deletions(-)
 create mode 100644 tools/tests/common/tests.c
 create mode 100644 tools/tests/common/tests.h

diff --git a/tools/tests/Rules.mk b/tools/tests/Rules.mk
index 2de9e94546..9e03e1e0b8 100644
--- a/tools/tests/Rules.mk
+++ b/tools/tests/Rules.mk
@@ -47,11 +47,14 @@ uninstall:
 
 CFLAGS += -D__XEN_TOOLS__
 CFLAGS += $(CFLAGS_xeninclude)
+CFLAGS += -I../common/
 
 %.o: Makefile
 
 $(TARGET): $(TARGET).o
        $(CC) $^ -o $@ $(LDFLAGS) $(APPEND_LDFLAGS)
 
+$(TARGETS): $(XEN_ROOT)/tools/tests/common/tests.o
+
 -include $(DEPS_INCLUDE)
 
diff --git a/tools/tests/common/tests.c b/tools/tests/common/tests.c
new file mode 100644
index 0000000000..43d9ea5442
--- /dev/null
+++ b/tools/tests/common/tests.c
@@ -0,0 +1,15 @@
+#include "tests.h"
+
+unsigned int nr_failures;
+
+int main(int argc, char *argv[argc+1])
+{
+    int rc = test_main(argc, argv);
+
+    if ( nr_failures )
+        printf("Done: %u failures\n", nr_failures);
+    else
+        printf("Done: all ok\n");
+
+    return rc ? rc : !!nr_failures;
+}
diff --git a/tools/tests/common/tests.h b/tools/tests/common/tests.h
new file mode 100644
index 0000000000..f0df616e3e
--- /dev/null
+++ b/tools/tests/common/tests.h
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <sysexits.h>
+
+extern unsigned int nr_failures;
+
+#define fail(fmt, ...)                          \
+({                                              \
+    nr_failures++;                              \
+    (void)printf(fmt, ##__VA_ARGS__);           \
+})
+
+#define verify(exp, fmt, args...) \
+while (!(exp)) { \
+    printf(fmt, ## args); \
+    exit(EX_SOFTWARE); \
+}
+
+extern int test_main(int argc, char *argv[argc+1]);
diff --git a/tools/tests/cpu-policy/test-cpu-policy.c 
b/tools/tests/cpu-policy/test-cpu-policy.c
index 301df2c002..67a36c80d5 100644
--- a/tools/tests/cpu-policy/test-cpu-policy.c
+++ b/tools/tests/cpu-policy/test-cpu-policy.c
@@ -650,7 +650,7 @@ static void test_is_compatible_failure(void)
     }
 }
 
-int main(int argc, char **argv)
+int test_main(int argc, char **argv)
 {
     printf("CPU Policy unit tests\n");
 
@@ -667,10 +667,5 @@ int main(int argc, char **argv)
     test_is_compatible_success();
     test_is_compatible_failure();
 
-    if ( nr_failures )
-        printf("Done: %u failures\n", nr_failures);
-    else
-        printf("Done: all ok\n");
-
     return !!nr_failures;
 }
diff --git a/tools/tests/domid/test-domid.c b/tools/tests/domid/test-domid.c
index 5915c4699a..de39bae8b0 100644
--- a/tools/tests/domid/test-domid.c
+++ b/tools/tests/domid/test-domid.c
@@ -5,20 +5,13 @@
  * Copyright 2025 Ford Motor Company
  */
 
-#include <sysexits.h>
-
+#include "tests.h"
 #include "harness.h"
 
-#define verify(exp, fmt, args...) \
-while (!(exp)) { \
-    printf(fmt, ## args); \
-    exit(EX_SOFTWARE); \
-}
-
 /*
  * Fail on the first error, since tests are dependent on each other.
  */
-int main(int argc, char **argv)
+int test_main(int argc, char *argv[argc+1])
 {
     domid_t expected, allocated;
 
diff --git a/tools/tests/mem-claim/test-mem-claim.c 
b/tools/tests/mem-claim/test-mem-claim.c
index ad038e45d1..1f3c70aace 100644
--- a/tools/tests/mem-claim/test-mem-claim.c
+++ b/tools/tests/mem-claim/test-mem-claim.c
@@ -11,12 +11,7 @@
 #include <xengnttab.h>
 #include <xen-tools/common-macros.h>
 
-static unsigned int nr_failures;
-#define fail(fmt, ...)                          \
-({                                              \
-    nr_failures++;                              \
-    (void)printf(fmt, ##__VA_ARGS__);           \
-})
+#include "tests.h"
 
 #define MB_PAGES(x) (MB(x) / XC_PAGE_SIZE)
 
@@ -158,7 +153,7 @@ static void run_tests(void)
                     physinfo.outstanding_pages);
 }
 
-int main(int argc, char **argv)
+int test_main(int argc, char *argv[argc+1])
 {
     int rc;
 
diff --git a/tools/tests/paging-mempool/test-paging-mempool.c 
b/tools/tests/paging-mempool/test-paging-mempool.c
index 1ebc13455a..27fd109031 100644
--- a/tools/tests/paging-mempool/test-paging-mempool.c
+++ b/tools/tests/paging-mempool/test-paging-mempool.c
@@ -10,12 +10,7 @@
 #include <xengnttab.h>
 #include <xen-tools/common-macros.h>
 
-static unsigned int nr_failures;
-#define fail(fmt, ...)                          \
-({                                              \
-    nr_failures++;                              \
-    (void)printf(fmt, ##__VA_ARGS__);           \
-})
+#include "tests.h"
 
 static xc_interface *xch;
 static uint32_t domid;
@@ -136,7 +131,7 @@ static void run_tests(void)
                     64 << 20, size_bytes);
 }
 
-int main(int argc, char **argv)
+int test_main(int argc, char *argv[argc+1])
 {
     int rc;
 
diff --git a/tools/tests/pdx/test-pdx.c b/tools/tests/pdx/test-pdx.c
index eefd54c768..d2d143ec76 100644
--- a/tools/tests/pdx/test-pdx.c
+++ b/tools/tests/pdx/test-pdx.c
@@ -5,6 +5,7 @@
  * Copyright (C) 2025 Cloud Software Group
  */
 
+#include "tests.h"
 #include "harness.h"
 
 #include "../../xen/common/pdx.c"
@@ -29,7 +30,7 @@ static void print_ranges(const struct range *r)
     }
 }
 
-int main(int argc, char **argv)
+int main(int argc, char *argv[argc+1])
 {
     static const struct {
         struct range ranges[MAX_RANGES];
diff --git a/tools/tests/rangeset/test-rangeset.c 
b/tools/tests/rangeset/test-rangeset.c
index c14a908b4f..3f8ac95097 100644
--- a/tools/tests/rangeset/test-rangeset.c
+++ b/tools/tests/rangeset/test-rangeset.c
@@ -5,6 +5,7 @@
  * Copyright (C) 2025 Cloud Software Group
  */
 
+#include "tests.h"
 #include "harness.h"
 
 struct range {
@@ -140,7 +141,7 @@ static void print_both(struct rangeset *r, const struct 
range *expected,
         printf("[%ld, %ld]\n", expected[i].start, expected[i].end);
 }
 
-int main(int argc, char **argv)
+int test_main(int argc, char *argv[argc+1])
 {
     struct rangeset *r = rangeset_new(NULL, NULL, 0);
     unsigned int i;
diff --git a/tools/tests/resource/test-resource.c 
b/tools/tests/resource/test-resource.c
index a7f2d04643..4155b62507 100644
--- a/tools/tests/resource/test-resource.c
+++ b/tools/tests/resource/test-resource.c
@@ -9,12 +9,7 @@
 #include <xengnttab.h>
 #include <xen-tools/common-macros.h>
 
-static unsigned int nr_failures;
-#define fail(fmt, ...)                          \
-({                                              \
-    nr_failures++;                              \
-    (void)printf(fmt, ##__VA_ARGS__);           \
-})
+#include "tests.h"
 
 static xc_interface *xch;
 static xenforeignmemory_handle *fh;
@@ -255,7 +250,7 @@ static void test_domain_configurations(void)
     }
 }
 
-int main(int argc, char **argv)
+int test_main(int argc, char *argv[argc+1])
 {
     int rc;
 
diff --git a/tools/tests/tsx/test-tsx.c b/tools/tests/tsx/test-tsx.c
index 5af04953f3..ab099e9038 100644
--- a/tools/tests/tsx/test-tsx.c
+++ b/tools/tests/tsx/test-tsx.c
@@ -30,6 +30,7 @@
 #include <xenguest.h>
 #include <xen-tools/common-macros.h>
 
+#include "tests.h"
 #include "xg_private.h"
 
 enum {
@@ -44,13 +45,6 @@ enum {
 #define MSR_TSX_CTRL                        0x00000122
 #define MSR_MCU_OPT_CTRL                    0x00000123
 
-static unsigned int nr_failures;
-#define fail(fmt, ...)                          \
-({                                              \
-    nr_failures++;                              \
-    (void)printf(fmt, ##__VA_ARGS__);           \
-})
-
 static xc_interface *xch;
 
 /*
@@ -540,7 +534,7 @@ static void test_tsx(void)
     test_guests();
 }
 
-int main(int argc, char **argv)
+int test_main(int argc, char *argv[argc+1])
 {
     printf("TSX tests\n");
 
diff --git a/tools/tests/vpci/main.c b/tools/tests/vpci/main.c
index 3753417e86..0e4f24aace 100644
--- a/tools/tests/vpci/main.c
+++ b/tools/tests/vpci/main.c
@@ -16,6 +16,7 @@
  * License along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "tests.h"
 #include "emul.h"
 
 /* Single vcpu (current), and single domain with a single PCI device. */
@@ -175,8 +176,7 @@ void multiwrite4_check(unsigned int reg)
     multiread4_check(reg, val);
 }
 
-int
-main(int argc, char **argv)
+int test_main(int argc, char *argv[argc+1])
 {
     /* Index storage by offset. */
     uint32_t r0 = 0xdeadbeef;
diff --git a/tools/tests/xenstore/test-xenstore.c 
b/tools/tests/xenstore/test-xenstore.c
index 7a9bd9afb3..470cbeed30 100644
--- a/tools/tests/xenstore/test-xenstore.c
+++ b/tools/tests/xenstore/test-xenstore.c
@@ -33,6 +33,8 @@
 
 #include <xen-tools/common-macros.h>
 
+#include "tests.h"
+
 #define TEST_PATH "xenstore-test"
 #define WRITE_BUFFERS_N    10
 #define WRITE_BUFFERS_SIZE 4000
@@ -445,7 +447,7 @@ static void cleanup(void)
     }
 }
 
-int main(int argc, char *argv[])
+int test_main(int argc, char *argv[argc+1])
 {
     int opt, t, iters = 1, ret = 0, randtime = 0;
     char *test = NULL;
-- 
2.47.3




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.