Skip to content

labgrid-client: Add an internal console#1870

Open
sjg20 wants to merge 5 commits into
labgrid-project:masterfrom
sjg20:push9-cons-small
Open

labgrid-client: Add an internal console#1870
sjg20 wants to merge 5 commits into
labgrid-project:masterfrom
sjg20:push9-cons-small

Conversation

@sjg20
Copy link
Copy Markdown
Contributor

@sjg20 sjg20 commented May 13, 2026

The microcom-based console used by labgrid-client has a few limitations:

  • console output is lost between when the board is reset and microcom connects;
  • microcom has no txdelay handling, so boards can miss characters when the client is sending input quickly (e.g. pasted test commands);
  • between labgrid-client console starting and microcom attaching, a few characters can echo back to the caller, which can break test systems which rely on output.

For many use cases, microcom is more than is needed. This PR adds a small internal terminal that solves the above. Enable it with:

  • labgrid-client console -i (or --internal), or
  • LG_CONSOLE=internal in the environment, so existing scripts can opt in without being rewritten

Press Ctrl-] twice (quickly) to exit.

Refactoring done along the way:

  • Move all terminal handling into a new file labgrid/util/term.py, keeping client.py thinner
  • Split _check_allowed() into a side-effect-free is_allowed() plus the existing raise-on-failure helper, so the new terminal loop can poll without try/except
  • Flush the console device only when needed, so console data is preserved across strategy-driver → interactive transitions when the internal terminal is in use

What is the feature used for? Console access in environments where microcom is unavailable or its limitations bite: U-Boot tests, post-reset capture, and any flow that needs txdelay.

How does labgrid benefit? No external dependency for a basic console, plus clean handoff between strategy-driver use and interactive use without losing data.

How was it verified? Used the internal console against a QEMU target end-to-end. The unit tests under tests/test_term.py and tests/test_client_unit.py cover both the external (microcom/telnet) and internal paths: keystroke handling, txdelay, exit-char deadline, logfile output, listen-only mode and check-allowed termination.

No hardware-specific changes.

Checklist

  • Documentation for the feature
  • Tests for the feature
  • Add a section on how to use the feature to doc/usage.rst
  • PR has been tested
  • Man pages have been regenerated

sjg20 added 5 commits May 12, 2026 13:33
At present if a console device is used by a strategy driver it is then
removed by the client immediately afterwards. If the 'console' command
is used, this means that any console data is lost.

This is done so that the console device is release for microcom, so
move the logic in with the microcom logic. With the forthcoming internal
terminal, it will not be needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
It is useful to be able to see if a place is allowed without raising an
error and needing a try...except block. Add a function to handle this
and update _check_allowed() to use it.

This will be used by the updated terminal suport.

Signed-off-by: Simon Glass <sjg@chromium.org>
There is quite a lot of code here, so move the terminal function into
its own file. This will make it easier to extend it later.

Signed-off-by: Simon Glass <sjg@chromium.org>
At present Labgrid uses microcom as its console. This has some
limitations:

- console output is lost between when the board is reset and microcom
  connects
- txdelay cannot be handled in microcom, meaning that boards may fail
  to receive expected output
- the console may echo a few characters back to the caller in the time
  between when 'labgrid-client console' is executed and when microcom
  starts (which causes failures with U-Boot test system)

For many use cases, microcom is more than is needed, so provide a simple
internal terminal which resolved the above problems.

It is enabled by a '-i' option to the 'console' command, as well as an
environment variable, so that it can be adjustly without updating a lot
of scripts.

To exit, press Ctrl-] twice, quickly.

Series-changes: 4
- Get internal console working with qemu
- Show a prompt when starting, to indicate it is waiting for the board

Signed-off-by: Simon Glass <sjg@chromium.org>
Add unit tests for the terminal handling in term.py, covering the
external console launch (microcom and telnet fallback), the internal
console read/write loop, and terminal setup and teardown. Also add
tests for the new is_allowed() helper and the new parser argument for
the internal console.

Use real pipes for stdin rather than mocking os.read(), giving more
realistic coverage of the keystroke and txdelay paths. The exit-char
deadline test uses a threading.Event to synchronise with the run
loop, avoiding brittle fixed sleeps.

Fix an UnboundLocalError in term.internal() where log_fd could be
referenced in the finally block before being assigned.

Also fix a duplicate --logfile append in term.external() when using
microcom.

Signed-off-by: Simon Glass <sjg@chromium.org>
Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

❌ Patch coverage is 83.22981% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 48.1%. Comparing base (db1cba3) to head (a575230).
⚠️ Report is 3 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
labgrid/remote/client.py 27.0% 27 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #1870     +/-   ##
========================================
+ Coverage    46.0%   48.1%   +2.1%     
========================================
  Files         180     181      +1     
  Lines       14439   14541    +102     
========================================
+ Hits         6654    7008    +354     
+ Misses       7785    7533    -252     
Flag Coverage Δ
3.10 48.1% <83.2%> (+2.1%) ⬆️
3.11 48.1% <83.2%> (+2.1%) ⬆️
3.12 48.1% <83.2%> (+2.1%) ⬆️
3.13 48.1% <83.2%> (+2.1%) ⬆️
3.14 48.1% <83.2%> (+2.1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant