Skip to content

Commit c08b0eb

Browse files
authored
Add an option for not forking each test in quark-test. Issue #130
This makes it easier to run a specific test in gdb, without having to follow the child, same for strace.
1 parent 4c37f4a commit c08b0eb

File tree

3 files changed

+60
-24
lines changed

3 files changed

+60
-24
lines changed

docs/quark-test.8.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ <h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a><
2525
<table class="Nm">
2626
<tr>
2727
<td><code class="Nm">quark-test</code></td>
28-
<td>[<code class="Fl">-bkv</code>] [<code class="Fl">-x</code>
28+
<td>[<code class="Fl">-1bkv</code>] [<code class="Fl">-x</code>
2929
<var class="Ar">test</var>] [<var class="Ar">tests ...</var>]</td>
3030
</tr>
3131
</table>
@@ -70,6 +70,9 @@ <h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIP
7070
return value of <code class="Nm">quark-test</code>.</p>
7171
<p class="Pp">The options are as follows:</p>
7272
<dl class="Bl-tag">
73+
<dt id="1"><a class="permalink" href="#1"><code class="Fl">-1</code></a></dt>
74+
<dd>Don't run tests in a child process, useful for debugging with gdb, strace
75+
and whatnot.</dd>
7376
<dt id="b"><a class="permalink" href="#b"><code class="Fl">-b</code></a></dt>
7477
<dd>Run only EBPF tests.</dd>
7578
<dt id="h"><a class="permalink" href="#h"><code class="Fl">-h</code></a></dt>
@@ -134,7 +137,7 @@ <h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
134137
</div>
135138
<table class="foot">
136139
<tr>
137-
<td class="foot-date">May 4, 2025</td>
140+
<td class="foot-date">May 8, 2025</td>
138141
<td class="foot-os">Linux</td>
139142
</tr>
140143
</table>

quark-test.8

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
.Nd quark's test utility
77
.Sh SYNOPSIS
88
.Nm quark-test
9-
.Op Fl bkv
9+
.Op Fl 1bkv
1010
.Op Fl x Ar test
1111
.Op Ar tests ...
1212
.Nm quark-test
@@ -33,6 +33,9 @@ The number of failed tests is the return value of
3333
.Pp
3434
The options are as follows:
3535
.Bl -tag -width Dtb
36+
.It Fl 1
37+
Don't run tests in a child process, useful for debugging with gdb, strace and
38+
whatnot.
3639
.It Fl b
3740
Run only EBPF tests.
3841
.It Fl h

quark-test.c

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ enum {
5454
YELLOW
5555
};
5656

57-
static int bflag; /* run bpf tests */
58-
static int kflag; /* run kprobe tests */
57+
static int noforkflag; /* don't fork on each test */
58+
static int bflag; /* run bpf tests */
59+
static int kflag; /* run kprobe tests */
5960

6061
static int
6162
fancy_tty(void)
@@ -266,7 +267,7 @@ usage(void)
266267
{
267268
fprintf(stderr, "usage: %s -h\n",
268269
program_invocation_short_name);
269-
fprintf(stderr, "usage: %s [-bkv] [-x test] [tests ...]\n",
270+
fprintf(stderr, "usage: %s [-1bkv] [-x test] [tests ...]\n",
270271
program_invocation_short_name);
271272
fprintf(stderr, "usage: %s -l\n",
272273
program_invocation_short_name);
@@ -1082,14 +1083,39 @@ lookup_test(const char *name)
10821083
return (NULL);
10831084
}
10841085

1086+
static int
1087+
run_test_doit(struct test *t, struct quark_queue_attr *qa)
1088+
{
1089+
int nfd, r;
1090+
struct quark_queue_attr qa_copy;
1091+
1092+
/*
1093+
* Check for FD leaks
1094+
*/
1095+
nfd = num_open_fd();
1096+
assert(nfd == 3);
1097+
qa_copy = *qa;
1098+
r = t->func(t, &qa_copy);
1099+
nfd = num_open_fd();
1100+
if (nfd != 3) {
1101+
fprintf(stderr,
1102+
"FDLEAK DETECTED! %d opened descriptors, expected 3\n",
1103+
nfd);
1104+
dump_open_fd(stderr);
1105+
if (r == 0)
1106+
r = 1;
1107+
}
1108+
1109+
return (r);
1110+
}
10851111
/*
10861112
* A test runs as a subprocess to avoid contamination.
10871113
*/
10881114
static int
10891115
run_test(struct test *t, struct quark_queue_attr *qa)
10901116
{
10911117
pid_t child;
1092-
int status, x, linepos, be, nfd, r;
1118+
int status, x, linepos, be, r;
10931119
int child_stderr[2];
10941120
FILE *child_stream;
10951121
char *child_buf;
@@ -1119,6 +1145,22 @@ run_test(struct test *t, struct quark_queue_attr *qa)
11191145
return (0);
11201146
}
11211147

1148+
if (noforkflag) {
1149+
r = run_test_doit(t, qa);
1150+
if (r == 0) {
1151+
x = color(GREEN);
1152+
printf("ok\n");
1153+
color(x);
1154+
} else {
1155+
x = color(RED);
1156+
printf("failed\n");
1157+
color(x);
1158+
}
1159+
fflush(stdout);
1160+
1161+
return (r);
1162+
}
1163+
11221164
/*
11231165
* Create a pipe to save the child stderr, so we don't get crappy
11241166
* interleaved output with the parent.
@@ -1136,22 +1178,7 @@ run_test(struct test *t, struct quark_queue_attr *qa)
11361178
close(child_stderr[1]);
11371179
close(child_stderr[0]);
11381180

1139-
/*
1140-
* Check for FD leaks
1141-
*/
1142-
nfd = num_open_fd();
1143-
assert(nfd == 3);
1144-
r = t->func(t, qa);
1145-
nfd = num_open_fd();
1146-
if (nfd != 3) {
1147-
fprintf(stderr,
1148-
"FDLEAK DETECTED! %d opened descriptors, expected 3\n",
1149-
nfd);
1150-
dump_open_fd(stderr);
1151-
if (r == 0)
1152-
r = 1;
1153-
}
1154-
exit(r);
1181+
exit(run_test_doit(t, qa));
11551182
}
11561183
close(child_stderr[1]);
11571184

@@ -1292,8 +1319,11 @@ main(int argc, char *argv[])
12921319
int ch, failed, x;
12931320
struct test *t;
12941321

1295-
while ((ch = getopt(argc, argv, "bhklNvVx:")) != -1) {
1322+
while ((ch = getopt(argc, argv, "1bhklNvVx:")) != -1) {
12961323
switch (ch) {
1324+
case '1':
1325+
noforkflag = 1;
1326+
break;
12971327
case 'b':
12981328
bflag = 1;
12991329
break;

0 commit comments

Comments
 (0)