From: Jeremy Kerr Date: Fri, 10 Jun 2022 01:41:52 +0000 (+0800) Subject: test/mi: Improve test OK reporting, print log data separately X-Git-Tag: v1.1-rc0~42^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=f87fa1c93af12234a1cb86003a1135b0022e7c9c;p=users%2Fsagi%2Flibnvme.git test/mi: Improve test OK reporting, print log data separately Currently, we have a confusing output for tests (all of which currenly pass): $ ./obj/test/test-mi crc mismatch $ echo $? 0 This change adds reporting for each test, and separates the nvme_msg log data from the report: $ ./obj/test/test-mi Running test read_mi_data... OK Running test transport_fail... OK Running test invalid_crc... OK --- begin test output crc mismatch --- end test output Signed-off-by: Jeremy Kerr --- diff --git a/test/mi.c b/test/mi.c index 0edd7061..dbf6374a 100644 --- a/test/mi.c +++ b/test/mi.c @@ -7,6 +7,10 @@ #undef NDEBUG #include #include +#include +#include + +#include /* we define a custom transport, so need the internal headers */ #include "nvme/private.h" @@ -189,21 +193,81 @@ static void test_invalid_crc(nvme_mi_ep_t ep) assert(rc != 0); } +#define DEFINE_TEST(name) { #name, test_ ## name } +struct test { + const char *name; + void (*fn)(nvme_mi_ep_t); +} tests[] = { + DEFINE_TEST(read_mi_data), + DEFINE_TEST(transport_fail), + DEFINE_TEST(invalid_crc), +}; + +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); + fflush(stdout); + test->fn(ep); + /* tests will assert on failure; if we're here, we're OK */ + printf(" OK\n"); + print_log_buf(logfd); +} int main(void) { nvme_root_t root; nvme_mi_ep_t ep; + unsigned int i; + FILE *fd; + + fd = tmpfile(); + assert(fd); - root = nvme_mi_create_root(NULL, DEFAULT_LOGLEVEL); + root = nvme_mi_create_root(fd, DEFAULT_LOGLEVEL); assert(root); ep = nvme_mi_open_test(root); assert(ep); - test_read_mi_data(ep); - test_transport_fail(ep); - test_invalid_crc(ep); + for (i = 0; i < ARRAY_SIZE(tests); i++) { + run_test(&tests[i], fd, ep); + } nvme_mi_close(ep); nvme_mi_free_root(root);