Skip to content

Commit 8f917d6

Browse files
committed
test: move some common test tools to test/utils.c
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 <[email protected]>
1 parent 1d62f8a commit 8f917d6

4 files changed

Lines changed: 95 additions & 40 deletions

File tree

test/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ zns = executable(
4141

4242
mi = executable(
4343
'test-mi',
44-
['mi.c'],
44+
['mi.c', 'utils.c'],
4545
dependencies: libnvme_mi_test_dep,
4646
include_directories: [incdir, internal_incdir]
4747
)

test/mi.c

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "libnvme-mi.h"
1919

20+
#include "utils.h"
21+
2022
typedef int (*test_submit_cb)(struct nvme_mi_ep *ep,
2123
struct nvme_mi_req *req,
2224
struct nvme_mi_resp *resp,
@@ -509,50 +511,14 @@ struct test {
509511
DEFINE_TEST(admin_err_resp),
510512
};
511513

512-
static void print_log_buf(FILE *logfd)
513-
{
514-
char buf[4096];
515-
int rc;
516-
517-
if (!ftell(logfd))
518-
return;
519-
520-
rewind(logfd);
521-
522-
printf("--- begin test output\n");
523-
524-
while (!feof(logfd) && !ferror(logfd)) {
525-
size_t rlen, wlen, wpos;
526-
527-
rlen = fread(buf, 1, sizeof(buf), logfd);
528-
if (rlen <= 0)
529-
break;
530-
531-
for (wpos = 0; wpos < rlen;) {
532-
wlen = fwrite(buf + wpos, 1, rlen - wpos, stdout);
533-
if (wlen == 0)
534-
break;
535-
wpos += wlen;
536-
}
537-
538-
if (feof(logfd) || ferror((logfd)))
539-
break;
540-
}
541-
542-
printf("--- end test output\n");
543-
rewind(logfd);
544-
rc = ftruncate(fileno(logfd), 0);
545-
assert(!rc);
546-
}
547-
548514
static void run_test(struct test *test, FILE *logfd, nvme_mi_ep_t ep)
549515
{
550516
printf("Running test %s...", test->name);
551517
fflush(stdout);
552518
test->fn(ep);
553519
/* tests will assert on failure; if we're here, we're OK */
554520
printf(" OK\n");
555-
print_log_buf(logfd);
521+
test_print_log_buf(logfd);
556522
}
557523

558524
int main(void)
@@ -562,8 +528,7 @@ int main(void)
562528
unsigned int i;
563529
FILE *fd;
564530

565-
fd = tmpfile();
566-
assert(fd);
531+
fd = test_setup_log();
567532

568533
root = nvme_mi_create_root(fd, DEFAULT_LOGLEVEL);
569534
assert(root);
@@ -578,5 +543,7 @@ int main(void)
578543
nvme_mi_close(ep);
579544
nvme_mi_free_root(root);
580545

546+
test_close_log(fd);
547+
581548
return EXIT_SUCCESS;
582549
}

test/utils.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/*
3+
* This file is part of libnvme.
4+
*
5+
* Common test utilities.
6+
*
7+
* Copyright (c) 2022 Code Construct
8+
*/
9+
10+
#include <err.h>
11+
#include <stdlib.h>
12+
#include <unistd.h>
13+
14+
#include "utils.h"
15+
16+
FILE *test_setup_log(void)
17+
{
18+
FILE *fd;
19+
20+
fd = tmpfile();
21+
if (!fd)
22+
err(EXIT_FAILURE, "can't create temporary file for log buf");
23+
24+
return fd;
25+
}
26+
27+
void test_close_log(FILE *fd)
28+
{
29+
fclose(fd);
30+
}
31+
32+
void test_print_log_buf(FILE *logfd)
33+
{
34+
char buf[4096];
35+
int rc;
36+
37+
if (!ftell(logfd))
38+
return;
39+
40+
rewind(logfd);
41+
42+
printf("--- begin test output\n");
43+
44+
while (!feof(logfd) && !ferror(logfd)) {
45+
size_t rlen, wlen, wpos;
46+
47+
rlen = fread(buf, 1, sizeof(buf), logfd);
48+
if (rlen <= 0)
49+
break;
50+
51+
for (wpos = 0; wpos < rlen;) {
52+
wlen = fwrite(buf + wpos, 1, rlen - wpos, stdout);
53+
if (wlen == 0)
54+
break;
55+
wpos += wlen;
56+
}
57+
58+
if (feof(logfd) || ferror((logfd)))
59+
break;
60+
}
61+
62+
printf("--- end test output\n");
63+
rewind(logfd);
64+
rc = ftruncate(fileno(logfd), 0);
65+
if (rc)
66+
printf("failed to truncate log buf; further output may be invalid\n");
67+
}
68+

test/utils.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2022 Code Construct
5+
*
6+
* Common test utilities for libnvme tests. These have quite strict error
7+
* handling, so the general pattern is to abort/exit on error.
8+
*/
9+
10+
#ifndef _TEST_UTILS_H
11+
#define _TEST_UTILS_H
12+
13+
#include <stdio.h>
14+
15+
FILE *test_setup_log(void);
16+
void test_print_log_buf(FILE *logfd);
17+
void test_close_log(FILE *fd);
18+
19+
#endif /* _TEST_UTILS_H */
20+

0 commit comments

Comments
 (0)