Skip to content

Commit f87fa1c

Browse files
committed
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 <[email protected]>
1 parent d0bdf73 commit f87fa1c

1 file changed

Lines changed: 68 additions & 4 deletions

File tree

test/mi.c

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#undef NDEBUG
88
#include <assert.h>
99
#include <stdlib.h>
10+
#include <stdio.h>
11+
#include <unistd.h>
12+
13+
#include <ccan/array_size/array_size.h>
1014

1115
/* we define a custom transport, so need the internal headers */
1216
#include "nvme/private.h"
@@ -189,21 +193,81 @@ static void test_invalid_crc(nvme_mi_ep_t ep)
189193
assert(rc != 0);
190194
}
191195

196+
#define DEFINE_TEST(name) { #name, test_ ## name }
197+
struct test {
198+
const char *name;
199+
void (*fn)(nvme_mi_ep_t);
200+
} tests[] = {
201+
DEFINE_TEST(read_mi_data),
202+
DEFINE_TEST(transport_fail),
203+
DEFINE_TEST(invalid_crc),
204+
};
205+
206+
static void print_log_buf(FILE *logfd)
207+
{
208+
char buf[4096];
209+
int rc;
210+
211+
if (!ftell(logfd))
212+
return;
213+
214+
rewind(logfd);
215+
216+
printf("--- begin test output\n");
217+
218+
while (!feof(logfd) && !ferror(logfd)) {
219+
size_t rlen, wlen, wpos;
220+
221+
rlen = fread(buf, 1, sizeof(buf), logfd);
222+
if (rlen <= 0)
223+
break;
224+
225+
for (wpos = 0; wpos < rlen;) {
226+
wlen = fwrite(buf + wpos, 1, rlen - wpos, stdout);
227+
if (wlen == 0)
228+
break;
229+
wpos += wlen;
230+
}
231+
232+
if (feof(logfd) || ferror((logfd)))
233+
break;
234+
}
235+
236+
printf("--- end test output\n");
237+
rewind(logfd);
238+
rc = ftruncate(fileno(logfd), 0);
239+
assert(!rc);
240+
}
241+
242+
static void run_test(struct test *test, FILE *logfd, nvme_mi_ep_t ep)
243+
{
244+
printf("Running test %s...", test->name);
245+
fflush(stdout);
246+
test->fn(ep);
247+
/* tests will assert on failure; if we're here, we're OK */
248+
printf(" OK\n");
249+
print_log_buf(logfd);
250+
}
192251

193252
int main(void)
194253
{
195254
nvme_root_t root;
196255
nvme_mi_ep_t ep;
256+
unsigned int i;
257+
FILE *fd;
258+
259+
fd = tmpfile();
260+
assert(fd);
197261

198-
root = nvme_mi_create_root(NULL, DEFAULT_LOGLEVEL);
262+
root = nvme_mi_create_root(fd, DEFAULT_LOGLEVEL);
199263
assert(root);
200264

201265
ep = nvme_mi_open_test(root);
202266
assert(ep);
203267

204-
test_read_mi_data(ep);
205-
test_transport_fail(ep);
206-
test_invalid_crc(ep);
268+
for (i = 0; i < ARRAY_SIZE(tests); i++) {
269+
run_test(&tests[i], fd, ep);
270+
}
207271

208272
nvme_mi_close(ep);
209273
nvme_mi_free_root(root);

0 commit comments

Comments
 (0)