]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
test: move some common test tools to test/utils.c
authorJeremy Kerr <jk@codeconstruct.com.au>
Tue, 28 Jun 2022 06:19:55 +0000 (14:19 +0800)
committerJeremy Kerr <jk@codeconstruct.com.au>
Tue, 28 Jun 2022 08:38:57 +0000 (16:38 +0800)
We have a bit of test infrastructure in mi.c that may be helpful for
other tests, so move into a separate file.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
test/meson.build
test/mi.c
test/utils.c [new file with mode: 0644]
test/utils.h [new file with mode: 0644]

index bbd112c80fe5eb91469951adc0161ac8fa47fdcc..4776bcbcb88c1698a88d202fd071395a66ce827c 100644 (file)
@@ -41,7 +41,7 @@ zns = executable(
 
 mi = executable(
     'test-mi',
-    ['mi.c'],
+    ['mi.c', 'utils.c'],
     dependencies: libnvme_mi_test_dep,
     include_directories: [incdir, internal_incdir]
 )
index 94fcbffbecfff5bba88a50433a13cdf8ef41cb64..cdcf7a792f2c9afa7e2edeacc22abdaee340333c 100644 (file)
--- a/test/mi.c
+++ b/test/mi.c
@@ -17,6 +17,8 @@
 
 #include "libnvme-mi.h"
 
+#include "utils.h"
+
 typedef int (*test_submit_cb)(struct nvme_mi_ep *ep,
                              struct nvme_mi_req *req,
                              struct nvme_mi_resp *resp,
@@ -509,42 +511,6 @@ struct test {
        DEFINE_TEST(admin_err_resp),
 };
 
-static void print_log_buf(FILE *logfd)
-{
-       char buf[4096];
-       int rc;
-
-       if (!ftell(logfd))
-               return;
-
-       rewind(logfd);
-
-       printf("--- begin test output\n");
-
-       while (!feof(logfd) && !ferror(logfd)) {
-               size_t rlen, wlen, wpos;
-
-               rlen = fread(buf, 1, sizeof(buf), logfd);
-               if (rlen <= 0)
-                       break;
-
-               for (wpos = 0; wpos < rlen;) {
-                       wlen = fwrite(buf + wpos, 1, rlen - wpos, stdout);
-                       if (wlen == 0)
-                               break;
-                       wpos += wlen;
-               }
-
-               if (feof(logfd) || ferror((logfd)))
-                       break;
-       }
-
-       printf("--- end test output\n");
-       rewind(logfd);
-       rc = ftruncate(fileno(logfd), 0);
-       assert(!rc);
-}
-
 static void run_test(struct test *test, FILE *logfd, nvme_mi_ep_t ep)
 {
        printf("Running test %s...", test->name);
@@ -552,7 +518,7 @@ static void run_test(struct test *test, FILE *logfd, nvme_mi_ep_t ep)
        test->fn(ep);
        /* tests will assert on failure; if we're here, we're OK */
        printf("  OK\n");
-       print_log_buf(logfd);
+       test_print_log_buf(logfd);
 }
 
 int main(void)
@@ -562,8 +528,7 @@ int main(void)
        unsigned int i;
        FILE *fd;
 
-       fd = tmpfile();
-       assert(fd);
+       fd = test_setup_log();
 
        root = nvme_mi_create_root(fd, DEFAULT_LOGLEVEL);
        assert(root);
@@ -578,5 +543,7 @@ int main(void)
        nvme_mi_close(ep);
        nvme_mi_free_root(root);
 
+       test_close_log(fd);
+
        return EXIT_SUCCESS;
 }
diff --git a/test/utils.c b/test/utils.c
new file mode 100644 (file)
index 0000000..60665b8
--- /dev/null
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * This file is part of libnvme.
+ *
+ * Common test utilities.
+ *
+ * Copyright (c) 2022 Code Construct
+ */
+
+#include <err.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "utils.h"
+
+FILE *test_setup_log(void)
+{
+       FILE *fd;
+
+       fd = tmpfile();
+       if (!fd)
+               err(EXIT_FAILURE, "can't create temporary file for log buf");
+
+       return fd;
+}
+
+void test_close_log(FILE *fd)
+{
+       fclose(fd);
+}
+
+void test_print_log_buf(FILE *logfd)
+{
+       char buf[4096];
+       int rc;
+
+       if (!ftell(logfd))
+               return;
+
+       rewind(logfd);
+
+       printf("--- begin test output\n");
+
+       while (!feof(logfd) && !ferror(logfd)) {
+               size_t rlen, wlen, wpos;
+
+               rlen = fread(buf, 1, sizeof(buf), logfd);
+               if (rlen <= 0)
+                       break;
+
+               for (wpos = 0; wpos < rlen;) {
+                       wlen = fwrite(buf + wpos, 1, rlen - wpos, stdout);
+                       if (wlen == 0)
+                               break;
+                       wpos += wlen;
+               }
+
+               if (feof(logfd) || ferror((logfd)))
+                       break;
+       }
+
+       printf("--- end test output\n");
+       rewind(logfd);
+       rc = ftruncate(fileno(logfd), 0);
+       if (rc)
+               printf("failed to truncate log buf; further output may be invalid\n");
+}
+
diff --git a/test/utils.h b/test/utils.h
new file mode 100644 (file)
index 0000000..e86f6e6
--- /dev/null
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * This file is part of libnvme.
+ * Copyright (c) 2022 Code Construct
+ *
+ * Common test utilities for libnvme tests. These have quite strict error
+ * handling, so the general pattern is to abort/exit on error.
+ */
+
+#ifndef _TEST_UTILS_H
+#define _TEST_UTILS_H
+
+#include <stdio.h>
+
+FILE *test_setup_log(void);
+void test_print_log_buf(FILE *logfd);
+void test_close_log(FILE *fd);
+
+#endif /* _TEST_UTILS_H */
+