Skip to content

Commit c5c1fe5

Browse files
committed
testsuite: add dmesg-grep.py helper script
Problem: Sometimes it is useful during testing to wait for a given pattern to appear in the Flux dmesg logs, but waiting for a matching line to appear in `flux-dmesg(1)` output using the shell is brittle and inconvenient. Copy the dmesg-grep.py script from flux-core for this purpose.
1 parent a6135f6 commit c5c1fe5

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

t/scripts/dmesg-grep.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
###############################################################
3+
# Copyright 2019 Lawrence Livermore National Security, LLC
4+
# (c.f. AUTHORS, NOTICE.LLNS, COPYING)
5+
#
6+
# This file is part of the Flux resource manager framework.
7+
# For details, see https://github.com/flux-framework.
8+
#
9+
# SPDX-License-Identifier: LGPL-3.0
10+
###############################################################
11+
#
12+
# Follow Flux dmesg output until a line matches a pattern
13+
#
14+
import argparse
15+
import re
16+
import sys
17+
18+
import flux
19+
from flux.constants import FLUX_RPC_STREAMING
20+
from flux.core.watchers import TimerWatcher
21+
22+
parser = argparse.ArgumentParser(
23+
description="watch the flux dmesg log for a given pattern"
24+
)
25+
parser.add_argument(
26+
"-t",
27+
"--timeout",
28+
help="Timeout with error after some number of seconds",
29+
metavar="SEC",
30+
type=float,
31+
default=1.0,
32+
)
33+
parser.add_argument(
34+
"-v",
35+
"--verbose",
36+
help="Emit each line of dmesg output, not just first matching",
37+
action="count",
38+
default=0,
39+
)
40+
parser.add_argument("pattern")
41+
args = parser.parse_args()
42+
43+
44+
def timer_cb(h, watcher, revents, _arg):
45+
print("Timeout!", file=sys.stderr)
46+
h.reactor_stop_error()
47+
48+
49+
def dmesg_cb(rpc, pattern, verbose=False):
50+
buf = rpc.get_str()
51+
match = pattern.search(buf)
52+
if match:
53+
print(buf)
54+
rpc.flux_handle.reactor_stop()
55+
elif verbose:
56+
print(buf)
57+
rpc.reset()
58+
59+
60+
pattern = re.compile(args.pattern)
61+
62+
h = flux.Flux()
63+
64+
rpc = h.rpc("log.dmesg", {"follow": True}, flags=FLUX_RPC_STREAMING)
65+
rpc.then(dmesg_cb, pattern, verbose=args.verbose)
66+
67+
TimerWatcher(h, args.timeout, timer_cb).start()
68+
if h.reactor_run() < 0:
69+
sys.exit(1)

0 commit comments

Comments
 (0)