|
| 1 | +"""Tests for StubDriverClient.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from contextlib import ExitStack |
| 5 | +from unittest.mock import MagicMock, create_autospec |
| 6 | +from uuid import uuid4 |
| 7 | + |
| 8 | +import pytest |
| 9 | +from anyio.from_thread import BlockingPortal |
| 10 | + |
| 11 | +from .base import StubDriverClient |
| 12 | +from jumpstarter.common.utils import serve |
| 13 | +from jumpstarter.driver import Driver |
| 14 | + |
| 15 | + |
| 16 | +class MissingClientDriver(Driver): |
| 17 | + """Test driver that returns a non-existent client class path.""" |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def client(cls) -> str: |
| 21 | + return "nonexistent_driver_package.client.NonExistentClient" |
| 22 | + |
| 23 | + |
| 24 | +def create_stub_client(class_path: str) -> StubDriverClient: |
| 25 | + """Create a StubDriverClient with minimal mocking for testing.""" |
| 26 | + return StubDriverClient( |
| 27 | + uuid=uuid4(), |
| 28 | + labels={"jumpstarter.dev/client": class_path}, |
| 29 | + stub=MagicMock(), |
| 30 | + portal=create_autospec(BlockingPortal, instance=True), |
| 31 | + stack=ExitStack(), |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def test_missing_driver_logs_warning_and_creates_stub(caplog): |
| 36 | + """Test that a missing driver logs a warning and creates a StubDriverClient.""" |
| 37 | + expected_class_path = "nonexistent_driver_package.client.NonExistentClient" |
| 38 | + with caplog.at_level(logging.WARNING): |
| 39 | + with serve(MissingClientDriver()) as client: |
| 40 | + # Should have logged a warning with the exact class path from MissingDriverError |
| 41 | + assert f"Driver client '{expected_class_path}' is not available." in caplog.text |
| 42 | + |
| 43 | + # Should have created a StubDriverClient |
| 44 | + assert isinstance(client, StubDriverClient) |
| 45 | + |
| 46 | + # Using the stub should raise an error |
| 47 | + with pytest.raises(ImportError): |
| 48 | + client.call("some_method") |
| 49 | + |
| 50 | + |
| 51 | +def test_stub_driver_client_streamingcall_raises(): |
| 52 | + """Test that streamingcall() raises ImportError with driver info.""" |
| 53 | + stub = create_stub_client("missing_driver.client.Client") |
| 54 | + with pytest.raises(ImportError) as exc_info: |
| 55 | + # Need to consume the generator to trigger the error |
| 56 | + list(stub.streamingcall("some_method")) |
| 57 | + assert "missing_driver" in str(exc_info.value) |
| 58 | + |
| 59 | + |
| 60 | +def test_stub_driver_client_stream_raises(): |
| 61 | + """Test that stream() raises ImportError with driver info.""" |
| 62 | + stub = create_stub_client("missing_driver.client.Client") |
| 63 | + with pytest.raises(ImportError) as exc_info: |
| 64 | + with stub.stream(): |
| 65 | + pass |
| 66 | + assert "missing_driver" in str(exc_info.value) |
| 67 | + |
| 68 | + |
| 69 | +def test_stub_driver_client_log_stream_raises(): |
| 70 | + """Test that log_stream() raises ImportError with driver info.""" |
| 71 | + stub = create_stub_client("missing_driver.client.Client") |
| 72 | + with pytest.raises(ImportError) as exc_info: |
| 73 | + with stub.log_stream(): |
| 74 | + pass |
| 75 | + assert "missing_driver" in str(exc_info.value) |
| 76 | + |
| 77 | + |
| 78 | +def test_stub_driver_client_error_message_jumpstarter_driver(): |
| 79 | + """Test that error message mentions version mismatch for Jumpstarter drivers.""" |
| 80 | + stub = create_stub_client("jumpstarter_driver_xyz.client.XyzClient") |
| 81 | + with pytest.raises(ImportError) as exc_info: |
| 82 | + stub.call("some_method") |
| 83 | + assert "version mismatch" in str(exc_info.value) |
| 84 | + |
| 85 | + |
| 86 | +def test_stub_driver_client_error_message_third_party(): |
| 87 | + """Test that error message includes install instructions for third-party drivers.""" |
| 88 | + stub = create_stub_client("custom_driver.client.CustomClient") |
| 89 | + with pytest.raises(ImportError) as exc_info: |
| 90 | + stub.call("some_method") |
| 91 | + assert "pip install custom_driver" in str(exc_info.value) |
0 commit comments