-
Notifications
You must be signed in to change notification settings - Fork 15
Gracefully handle missing driver client imports #142
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
Merged
mangelajo
merged 5 commits into
jumpstarter-dev:main
from
evakhoni:import/python-pr-805
Jan 26, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fd3daa6
gracefully handle missing drivers with stub client
evakhoni 879de66
tests for missing driver and stub
evakhoni af3f542
handle missing driver at exporter side separately
evakhoni e68ae6a
suppress double warning, handle missing class
evakhoni 085af1f
handle any attribute by the stub driver
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
101 changes: 101 additions & 0 deletions
101
python/packages/jumpstarter/jumpstarter/client/base_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,101 @@ | ||
| """Tests for StubDriverClient.""" | ||
|
|
||
| import logging | ||
| from contextlib import ExitStack | ||
| from unittest.mock import MagicMock, create_autospec | ||
| from uuid import uuid4 | ||
|
|
||
| import pytest | ||
| from anyio.from_thread import BlockingPortal | ||
|
|
||
| from .base import StubDriverClient | ||
| from jumpstarter.common.utils import serve | ||
| from jumpstarter.driver import Driver | ||
|
|
||
|
|
||
| class MissingClientDriver(Driver): | ||
| """Test driver that returns a non-existent client class path.""" | ||
|
|
||
| @classmethod | ||
| def client(cls) -> str: | ||
| return "nonexistent_driver_package.client.NonExistentClient" | ||
|
|
||
|
|
||
| def create_stub_client(class_path: str) -> StubDriverClient: | ||
| """Create a StubDriverClient with minimal mocking for testing.""" | ||
| return StubDriverClient( | ||
| uuid=uuid4(), | ||
| labels={"jumpstarter.dev/client": class_path}, | ||
| stub=MagicMock(), | ||
| portal=create_autospec(BlockingPortal, instance=True), | ||
| stack=ExitStack(), | ||
| ) | ||
|
|
||
|
|
||
| def test_missing_driver_logs_warning_and_creates_stub(caplog): | ||
| """Test that a missing driver logs a warning and creates a StubDriverClient.""" | ||
| expected_class_path = "nonexistent_driver_package.client.NonExistentClient" | ||
| with caplog.at_level(logging.WARNING): | ||
| with serve(MissingClientDriver()) as client: | ||
| # Should have logged a warning with the exact class path from MissingDriverError | ||
| assert f"Driver client '{expected_class_path}' is not available." in caplog.text | ||
|
|
||
| # Should have created a StubDriverClient | ||
| assert isinstance(client, StubDriverClient) | ||
|
|
||
| # Using the stub should raise an error | ||
| with pytest.raises(ImportError): | ||
| client.call("some_method") | ||
|
|
||
|
|
||
| def test_stub_driver_client_streamingcall_raises(): | ||
| """Test that streamingcall() raises ImportError with driver info.""" | ||
| stub = create_stub_client("missing_driver.client.Client") | ||
| with pytest.raises(ImportError) as exc_info: | ||
| # Need to consume the generator to trigger the error | ||
| list(stub.streamingcall("some_method")) | ||
| assert "missing_driver" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_stub_driver_client_stream_raises(): | ||
| """Test that stream() raises ImportError with driver info.""" | ||
| stub = create_stub_client("missing_driver.client.Client") | ||
| with pytest.raises(ImportError) as exc_info: | ||
| with stub.stream(): | ||
| pass | ||
| assert "missing_driver" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_stub_driver_client_log_stream_raises(): | ||
| """Test that log_stream() raises ImportError with driver info.""" | ||
| stub = create_stub_client("missing_driver.client.Client") | ||
| with pytest.raises(ImportError) as exc_info: | ||
| with stub.log_stream(): | ||
| pass | ||
| assert "missing_driver" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_stub_driver_client_error_message_jumpstarter_driver(): | ||
| """Test that error message mentions version mismatch for Jumpstarter drivers.""" | ||
| stub = create_stub_client("jumpstarter_driver_xyz.client.XyzClient") | ||
| with pytest.raises(ImportError) as exc_info: | ||
| stub.call("some_method") | ||
| assert "version mismatch" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_stub_driver_client_error_message_third_party(): | ||
| """Test that error message includes install instructions for third-party drivers.""" | ||
| stub = create_stub_client("custom_driver.client.CustomClient") | ||
| with pytest.raises(ImportError) as exc_info: | ||
| stub.call("some_method") | ||
| assert "pip install custom_driver" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_stub_driver_client_arbitrary_method_raises(): | ||
| """Test that accessing arbitrary methods like .on() raises ImportError, not AttributeError.""" | ||
| stub = create_stub_client("jumpstarter_driver_power.client.PowerClient") | ||
| # Accessing .on() should raise ImportError with helpful message, not AttributeError | ||
| with pytest.raises(ImportError) as exc_info: | ||
| stub.on() | ||
| assert "jumpstarter_driver_power" in str(exc_info.value) | ||
| assert "version mismatch" in str(exc_info.value) |
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
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
3 changes: 2 additions & 1 deletion
3
python/packages/jumpstarter/jumpstarter/common/importlib_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
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
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.
Uh oh!
There was an error while loading. Please reload this page.