-
Notifications
You must be signed in to change notification settings - Fork 33
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
5
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
5 commits
Select commit
Hold shift + click to select a range
0a7cd46
feat: add Ctrl-] x3 hotkey to power cycle board from serial console
evakhoni 61075fe
fix: reconnect console on serial stream drop after power cycle
evakhoni 899cdd0
test: add console hotkey tests using PTY-simulated stdin
evakhoni 05fbd46
refactor: add safe power discovery with explicit config override
evakhoni f807a7f
fix(pyserial): expand power client allowlist and dedup Proxy duplicates
evakhoni 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
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
188 changes: 188 additions & 0 deletions
188
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,188 @@ | ||
| 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://", power_control_method=["cycle"])) as client: | ||
| # No root attribute set → should return None | ||
| assert client._find_power_client() is None | ||
|
|
||
|
|
||
| def test_find_power_client_auto_discover(): | ||
| power = MagicMock(spec=["cycle", "children", "labels"]) | ||
| power.children = {} | ||
| power.labels = {"jumpstarter.dev/client": "jumpstarter_driver_power.client.PowerClient"} | ||
| root = MagicMock(spec=["children", "labels"]) | ||
| root.children = {"power": power} | ||
| root.labels = {} | ||
|
|
||
| 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() | ||
|
|
||
|
|
||
| def test_find_power_client_ambiguous(): | ||
| power1 = MagicMock(spec=["children", "labels"]) | ||
| power1.children = {} | ||
| power1.labels = { | ||
| "jumpstarter.dev/client": "jumpstarter_driver_power.client.PowerClient", | ||
| "jumpstarter.dev/name": "power1", | ||
| } | ||
| power2 = MagicMock(spec=["children", "labels"]) | ||
| power2.children = {} | ||
| power2.labels = { | ||
| "jumpstarter.dev/client": "jumpstarter_driver_power.client.PowerClient", | ||
| "jumpstarter.dev/name": "power2", | ||
| } | ||
| root = MagicMock(spec=["children", "labels"]) | ||
| root.children = {"power1": power1, "power2": power2} | ||
| root.labels = {} | ||
|
|
||
| with serve(PySerial(url="loop://")) as client: | ||
| object.__setattr__(client, "root", root) | ||
| assert client._find_power_client() is None | ||
|
|
||
|
|
||
| def test_find_power_client_explicit_ref(): | ||
| power = MagicMock(spec=["children", "labels"]) | ||
| power.children = {} | ||
| power.labels = {"jumpstarter.dev/client": "jumpstarter_driver_power.client.PowerClient"} | ||
| other = MagicMock(spec=["children", "labels"]) | ||
| other.children = {} | ||
| other.labels = {"jumpstarter.dev/client": "other.driver.Client"} | ||
| root = MagicMock(spec=["children", "labels"]) | ||
| root.children = {"power": power, "other": other} | ||
| root.labels = {} | ||
|
|
||
| with serve(PySerial(url="loop://", power_control_ref="power")) as client: | ||
| object.__setattr__(client, "root", root) | ||
| assert client._find_power_client() is power | ||
|
|
||
|
|
||
| def test_find_power_client_explicit_ref_missing(): | ||
| root = MagicMock(spec=["children", "labels"]) | ||
| root.children = {} | ||
| root.labels = {} | ||
|
|
||
| with serve(PySerial(url="loop://", power_control_ref="nonexistent")) as client: | ||
| object.__setattr__(client, "root", root) | ||
| assert client._find_power_client() is None | ||
|
|
||
|
|
||
| def test_find_power_client_non_power_ignored(): | ||
| gpio = MagicMock(spec=["children", "labels"]) | ||
| gpio.children = {} | ||
| gpio.labels = {"jumpstarter.dev/client": "jumpstarter_driver_gpiod.client.DigitalOutputClient"} | ||
| root = MagicMock(spec=["children", "labels"]) | ||
| root.children = {"gpio": gpio} | ||
| root.labels = {} | ||
|
|
||
| with serve(PySerial(url="loop://")) as client: | ||
| object.__setattr__(client, "root", root) | ||
| assert client._find_power_client() is None | ||
|
|
||
|
|
||
| def test_make_power_cycle_custom_method(): | ||
| called_sequence = [] | ||
| power = MagicMock() | ||
| power.off = MagicMock(side_effect=lambda: called_sequence.append("off")) | ||
| power.on = MagicMock(side_effect=lambda: called_sequence.append("on")) | ||
|
|
||
| with serve(PySerial(url="loop://", power_control_method=["off", "on"])) as client: | ||
| cycle_fn = client._make_power_cycle(power) | ||
| client.portal.call(cycle_fn) | ||
| assert called_sequence == ["off", "on"] | ||
|
|
||
|
|
||
| def test_find_power_client_disabled_via_empty_list(): | ||
| power = MagicMock(spec=["children", "labels"]) | ||
| power.children = {} | ||
| power.labels = {"jumpstarter.dev/client": "jumpstarter_driver_power.client.PowerClient"} | ||
| root = MagicMock(spec=["children", "labels"]) | ||
| root.children = {"power": power} | ||
| root.labels = {} | ||
|
|
||
| with serve(PySerial(url="loop://", power_control_method=[])) as client: | ||
| object.__setattr__(client, "root", root) | ||
| assert client._find_power_client() is None | ||
|
|
||
|
|
||
| def test_make_power_cycle_missing_method(): | ||
| power = MagicMock(spec=["children", "labels"]) | ||
| power.children = {} | ||
| power.labels = {} | ||
|
|
||
| with serve(PySerial(url="loop://", power_control_method=["nonexistent_method"])) as client: | ||
| result = client._make_power_cycle(power) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_make_power_cycle_with_sleep(): | ||
| called_sequence = [] | ||
| power = MagicMock() | ||
| power.off = MagicMock(side_effect=lambda: called_sequence.append("off")) | ||
| power.on = MagicMock(side_effect=lambda: called_sequence.append("on")) | ||
|
|
||
| with serve(PySerial(url="loop://", power_control_method=["off", "sleep:0.01", "on"])) as client: | ||
| cycle_fn = client._make_power_cycle(power) | ||
| client.portal.call(cycle_fn) | ||
| assert called_sequence == ["off", "on"] | ||
|
|
||
|
|
||
| def test_find_power_client_disabled_via_none(): | ||
| power = MagicMock(spec=["children", "labels"]) | ||
| power.children = {} | ||
| power.labels = {"jumpstarter.dev/client": "jumpstarter_driver_power.client.PowerClient"} | ||
| root = MagicMock(spec=["children", "labels"]) | ||
| root.children = {"power": power} | ||
| root.labels = {} | ||
|
|
||
| with serve(PySerial(url="loop://", power_control_method=None)) as client: | ||
| object.__setattr__(client, "root", root) | ||
| assert client._find_power_client() is None | ||
|
|
||
|
|
||
| def test_collect_power_clients_dedup_proxy(): | ||
| # Simulate Proxy scenario: same power driver instance appears twice in tree | ||
| # (once via proxy delegation, once via direct parent) | ||
| from uuid import uuid4 | ||
| shared_uuid = uuid4() | ||
|
|
||
| power1 = MagicMock(spec=["children", "labels", "uuid"]) | ||
| power1.children = {} | ||
| power1.labels = { | ||
| "jumpstarter.dev/client": "jumpstarter_driver_power.client.PowerClient", | ||
| "jumpstarter.dev/name": "power", | ||
| } | ||
| power1.uuid = shared_uuid | ||
|
|
||
| power2 = MagicMock(spec=["children", "labels", "uuid"]) | ||
| power2.children = {} | ||
| power2.labels = { | ||
| "jumpstarter.dev/client": "jumpstarter_driver_power.client.PowerClient", | ||
| "jumpstarter.dev/name": "power", | ||
| } | ||
| power2.uuid = shared_uuid # Same UUID as power1 | ||
|
|
||
| root = MagicMock(spec=["children", "labels"]) | ||
| root.children = {"power1": power1, "power2": power2} | ||
| root.labels = {} | ||
|
|
||
| with serve(PySerial(url="loop://")) as client: | ||
| object.__setattr__(client, "root", root) | ||
| # Should find only one power client despite two references | ||
| assert client._find_power_client() is power1 |
Oops, something went wrong.
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.
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.
or even enabled by default.. but a flag :D
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.
Yep, that's a valid concern. I think I found a middle ground: conservative auto-discovery that disables on first trouble + explicit config when needed.
Added
power_control_refandpower_control_methodfields — you can now specify which device and what sequence to call:Auto-discovery now uses a strict allowlist (only
PowerClient/VirtualPowerClientlabels) and disables itself if it finds multiple candidates. No flag needed since it's conservative by design.Ready for re-review when you have time!
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.
Btw if needed - there's an option to disable it via the config, by setting