-
Notifications
You must be signed in to change notification settings - Fork 32
feat: add Ctrl-] x3 hotkey to power cycle board from serial console #791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
evakhoni
wants to merge
3
commits into
jumpstarter-dev:main
Choose a base branch
from
evakhoni:feat/console-power-hotkey
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
python/packages/jumpstarter-driver-pyserial/jumpstarter_driver_pyserial/client_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import threading | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from .driver import PySerial | ||
| from jumpstarter.common.utils import serve | ||
|
|
||
|
|
||
| def test_find_power_client_no_root(): | ||
| with serve(PySerial(url="loop://")) as client: | ||
| assert client._find_power_client() is None | ||
|
|
||
|
|
||
| def test_find_power_client_with_cycle(): | ||
| power = MagicMock(spec=["cycle", "children"]) | ||
| power.children = {} | ||
| root = MagicMock(spec=["children"]) | ||
| root.children = {"power": power} | ||
|
|
||
| with serve(PySerial(url="loop://")) as client: | ||
| object.__setattr__(client, "root", root) | ||
| assert client._find_power_client() is power | ||
|
|
||
|
|
||
| def test_make_power_cycle_calls_cycle(): | ||
| called = threading.Event() | ||
| power = MagicMock() | ||
| power.cycle = MagicMock(side_effect=lambda: called.set()) | ||
|
|
||
| with serve(PySerial(url="loop://")) as client: | ||
| cycle_fn = client._make_power_cycle(power) | ||
| client.portal.call(cycle_fn) | ||
| assert called.is_set() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
python/packages/jumpstarter-driver-pyserial/jumpstarter_driver_pyserial/console_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import os | ||
| import threading | ||
| import time | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from .console import Console | ||
| from .driver import PySerial | ||
| from jumpstarter.common.utils import serve | ||
|
|
||
|
|
||
| def _start_console(client, on_power_cycle=None): | ||
| """Run Console.run() in a thread with a PTY substituted for stdin. | ||
|
|
||
| Returns (master_fd, thread, result_dict). Write keypresses to master_fd; | ||
| the result dict gets an 'exc' key if the console thread raises. | ||
| """ | ||
| master_fd, slave_fd = os.openpty() | ||
| slave_file = os.fdopen(slave_fd, "rb", buffering=0) | ||
|
|
||
| mock_stdin = MagicMock() | ||
| mock_stdin.fileno.return_value = slave_fd | ||
| mock_stdin.buffer = slave_file | ||
|
|
||
| result = {} | ||
|
|
||
| def _run(): | ||
| with patch("sys.stdin", mock_stdin): | ||
| console = Console(serial_client=client, on_power_cycle=on_power_cycle) | ||
| try: | ||
| console.run() | ||
| except Exception as e: | ||
| result["exc"] = e | ||
| slave_file.close() | ||
|
|
||
| t = threading.Thread(target=_run, daemon=True) | ||
| t.start() | ||
| return master_fd, t, result | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| def test_ctrl_b_exits(): | ||
| with serve(PySerial(url="loop://")) as client: | ||
| master_fd, t, result = _start_console(client) | ||
| try: | ||
| time.sleep(0.1) | ||
| os.write(master_fd, b"a") | ||
| os.write(master_fd, b"\x02\x02\x02") | ||
| t.join(timeout=5) | ||
| finally: | ||
| os.close(master_fd) | ||
|
|
||
| assert not t.is_alive(), "console did not exit after Ctrl-B x3" | ||
| assert "exc" not in result | ||
|
|
||
|
|
||
| def test_ctrl_bracket_triggers_power_cycle(): | ||
| power_cycled = threading.Event() | ||
|
|
||
| async def on_power_cycle(): | ||
| power_cycled.set() | ||
|
|
||
| with serve(PySerial(url="loop://")) as client: | ||
| master_fd, t, result = _start_console(client, on_power_cycle=on_power_cycle) | ||
| try: | ||
| time.sleep(0.1) | ||
| os.write(master_fd, b"\x1d\x1d\x1d") | ||
| assert power_cycled.wait(timeout=5), "power cycle was not triggered" | ||
| assert t.is_alive(), "console exited after power cycle" | ||
| os.write(master_fd, b"\x02\x02\x02") | ||
| t.join(timeout=5) | ||
| finally: | ||
| os.close(master_fd) | ||
|
|
||
| assert not t.is_alive() | ||
| assert "exc" not in result | ||
|
|
||
|
|
||
| def test_ctrl_bracket_without_power_client(): | ||
| with serve(PySerial(url="loop://")) as client: | ||
| master_fd, t, result = _start_console(client, on_power_cycle=None) | ||
| try: | ||
| time.sleep(0.1) | ||
| os.write(master_fd, b"\x1d\x1d\x1d") | ||
| time.sleep(0.1) | ||
| assert t.is_alive(), "console exited unexpectedly on Ctrl-] without power client" | ||
| os.write(master_fd, b"\x02\x02\x02") | ||
| t.join(timeout=5) | ||
| finally: | ||
| os.close(master_fd) | ||
|
|
||
| assert not t.is_alive() | ||
| assert "exc" not in result | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should provide a config entry to let admins specify the power device via a "ref". While I think that finding it automatically is cool, and will work out of the box in most cases, imagine environments where you have multiple power controls , and the power controls could not be what you are expecting, or the reset mechanism is different.
Even in some cases the method to be called could be "reset()", i.e. in the esp32 controller.
So I would take a config with "ref" + method in the serial config
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i.e.
or something like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah config sounds good. but if unset let it fallback to the auto discovery WDYT? otherwise I guess the user base would be rather small..
+1 on the reset(), i thought I seen it somewhere but forgot it was esp32.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO auto detect continues to be risky, it could pick up the wrong power device if you had multiple doing different things in your setup. We could provide a flag for auto-detection, but explain very clearly what it does, and should be disabled by default.