Skip to content

Commit

Permalink
testsuite: add dmesg-grep.py helper script
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
grondo committed Nov 14, 2023
1 parent a6135f6 commit c5c1fe5
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions t/scripts/dmesg-grep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
###############################################################
# Copyright 2019 Lawrence Livermore National Security, LLC
# (c.f. AUTHORS, NOTICE.LLNS, COPYING)
#
# This file is part of the Flux resource manager framework.
# For details, see https://github.com/flux-framework.
#
# SPDX-License-Identifier: LGPL-3.0
###############################################################
#
# Follow Flux dmesg output until a line matches a pattern
#
import argparse
import re
import sys

import flux
from flux.constants import FLUX_RPC_STREAMING
from flux.core.watchers import TimerWatcher

parser = argparse.ArgumentParser(
description="watch the flux dmesg log for a given pattern"
)
parser.add_argument(
"-t",
"--timeout",
help="Timeout with error after some number of seconds",
metavar="SEC",
type=float,
default=1.0,
)
parser.add_argument(
"-v",
"--verbose",
help="Emit each line of dmesg output, not just first matching",
action="count",
default=0,
)
parser.add_argument("pattern")
args = parser.parse_args()


def timer_cb(h, watcher, revents, _arg):
print("Timeout!", file=sys.stderr)
h.reactor_stop_error()


def dmesg_cb(rpc, pattern, verbose=False):
buf = rpc.get_str()
match = pattern.search(buf)
if match:
print(buf)
rpc.flux_handle.reactor_stop()
elif verbose:
print(buf)
rpc.reset()


pattern = re.compile(args.pattern)

h = flux.Flux()

rpc = h.rpc("log.dmesg", {"follow": True}, flags=FLUX_RPC_STREAMING)
rpc.then(dmesg_cb, pattern, verbose=args.verbose)

TimerWatcher(h, args.timeout, timer_cb).start()
if h.reactor_run() < 0:
sys.exit(1)

0 comments on commit c5c1fe5

Please sign in to comment.