-
Notifications
You must be signed in to change notification settings - Fork 876
Fix: Missing default connection config propagation #1179
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
Changes from all commits
88a5401
90de8fc
0c3b15c
34a2c70
b80f0e5
3284d34
e3ae87f
b853a84
60aa6ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@e2b/python-sdk': patch | ||
| 'e2b': patch | ||
| --- | ||
|
|
||
| Fix missing instance config propagation in Sandbox pause/connect and add regression tests for config forwarding + overrides. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { afterEach, assert, describe, test, vi } from 'vitest' | ||
|
|
||
| import { Sandbox } from '../../src' | ||
| import { SandboxApi } from '../../src/sandbox/sandboxApi' | ||
|
|
||
| const baseConfig = { | ||
| apiKey: 'base-api-key', | ||
| domain: 'base.e2b.dev', | ||
| requestTimeoutMs: 1111, | ||
| debug: false, | ||
| headers: { 'X-Test': 'base' }, | ||
| } | ||
|
|
||
| function createSandbox() { | ||
| return new Sandbox({ | ||
| sandboxId: 'sbx-test', | ||
| sandboxDomain: 'sandbox.e2b.dev', | ||
| envdVersion: '0.2.4', | ||
| envdAccessToken: 'tok', | ||
| trafficAccessToken: 'tok', | ||
| ...baseConfig, | ||
| }) | ||
| } | ||
|
|
||
| describe('Sandbox API config propagation', () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| test('passes connectionConfig to public API methods when called without overrides', async () => { | ||
| const pauseSpy = vi.spyOn(SandboxApi, 'pause').mockResolvedValue(true) | ||
| const sandbox = createSandbox() | ||
|
|
||
| await sandbox.pause() | ||
|
|
||
| const opts = pauseSpy.mock.calls[0][1] | ||
| assert.equal(opts?.apiKey, baseConfig.apiKey) | ||
| assert.equal(opts?.domain, baseConfig.domain) | ||
| assert.equal(opts?.requestTimeoutMs, baseConfig.requestTimeoutMs) | ||
| assert.equal(opts?.debug, baseConfig.debug) | ||
| assert.equal(opts?.headers?.['X-Test'], baseConfig.headers['X-Test']) | ||
| }) | ||
|
|
||
| test('lets public method call overrides win over connectionConfig', async () => { | ||
| const pauseSpy = vi.spyOn(SandboxApi, 'pause').mockResolvedValue(true) | ||
| const sandbox = createSandbox() | ||
|
|
||
| await sandbox.pause({ | ||
| domain: 'override.e2b.dev', | ||
| requestTimeoutMs: 9999, | ||
| }) | ||
|
|
||
| const opts = pauseSpy.mock.calls[0][1] | ||
| assert.equal(opts?.apiKey, baseConfig.apiKey) | ||
| assert.equal(opts?.domain, 'override.e2b.dev') | ||
| assert.equal(opts?.requestTimeoutMs, 9999) | ||
| assert.equal(opts?.debug, baseConfig.debug) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -310,7 +310,7 @@ def connect( | |
| SandboxApi._cls_connect( | ||
| sandbox_id=self.sandbox_id, | ||
| timeout=timeout, | ||
| **opts, | ||
| **self.connection_config.get_api_params(**opts), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Using Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is fine |
||
| ) | ||
|
|
||
| return self | ||
|
|
@@ -633,7 +633,7 @@ def pause( | |
|
|
||
| SandboxApi._cls_pause( | ||
| sandbox_id=self.sandbox_id, | ||
| **opts, | ||
| **self.connection_config.get_api_params(**opts), | ||
| ) | ||
|
|
||
| @overload | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from types import SimpleNamespace | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
| from packaging.version import Version | ||
|
|
||
| from e2b import AsyncSandbox | ||
| from e2b.connection_config import ConnectionConfig | ||
| import e2b.sandbox_async.main as sandbox_async_main | ||
|
|
||
|
|
||
| BASE_API_KEY = "base-api-key" | ||
| BASE_DOMAIN = "base.e2b.dev" | ||
| BASE_REQUEST_TIMEOUT = 11 | ||
| BASE_DEBUG = False | ||
| BASE_HEADERS = {"X-Test": "base"} | ||
|
|
||
|
|
||
| def create_sandbox(monkeypatch) -> AsyncSandbox: | ||
| dummy_transport = SimpleNamespace(pool=object()) | ||
|
|
||
| monkeypatch.setattr( | ||
| sandbox_async_main, "get_transport", lambda *_args, **_kwargs: dummy_transport | ||
| ) | ||
| monkeypatch.setattr( | ||
| sandbox_async_main.httpx, "AsyncClient", lambda *args, **kwargs: object() | ||
| ) | ||
| monkeypatch.setattr( | ||
| sandbox_async_main, "Filesystem", lambda *args, **kwargs: object() | ||
| ) | ||
| monkeypatch.setattr( | ||
| sandbox_async_main, "Commands", lambda *args, **kwargs: object() | ||
| ) | ||
| monkeypatch.setattr(sandbox_async_main, "Pty", lambda *args, **kwargs: object()) | ||
| monkeypatch.setattr(sandbox_async_main, "Git", lambda *args, **kwargs: object()) | ||
|
|
||
| return AsyncSandbox( | ||
| sandbox_id="sbx-test", | ||
| sandbox_domain="sandbox.e2b.dev", | ||
| envd_version=Version("0.2.4"), | ||
| envd_access_token="tok", | ||
| traffic_access_token="tok", | ||
| connection_config=ConnectionConfig( | ||
| api_key=BASE_API_KEY, | ||
| domain=BASE_DOMAIN, | ||
| request_timeout=BASE_REQUEST_TIMEOUT, | ||
| debug=BASE_DEBUG, | ||
| headers=BASE_HEADERS, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.skip_debug() | ||
| async def test_pause_passes_connection_config_without_overrides(monkeypatch): | ||
| mock_pause = AsyncMock(return_value="sbx-test") | ||
| monkeypatch.setattr(sandbox_async_main.SandboxApi, "_cls_pause", mock_pause) | ||
|
|
||
| sandbox = create_sandbox(monkeypatch) | ||
| await sandbox.pause() | ||
|
|
||
| mock_pause.assert_awaited_once() | ||
| assert mock_pause.call_args.kwargs["sandbox_id"] == "sbx-test" | ||
| assert mock_pause.call_args.kwargs["api_key"] == BASE_API_KEY | ||
| assert mock_pause.call_args.kwargs["domain"] == BASE_DOMAIN | ||
| assert mock_pause.call_args.kwargs["request_timeout"] == BASE_REQUEST_TIMEOUT | ||
| assert mock_pause.call_args.kwargs["debug"] == BASE_DEBUG | ||
| assert mock_pause.call_args.kwargs["headers"]["X-Test"] == BASE_HEADERS["X-Test"] | ||
|
|
||
|
|
||
| @pytest.mark.skip_debug() | ||
| async def test_pause_applies_overrides(monkeypatch): | ||
| mock_pause = AsyncMock(return_value="sbx-test") | ||
| monkeypatch.setattr(sandbox_async_main.SandboxApi, "_cls_pause", mock_pause) | ||
|
|
||
| sandbox = create_sandbox(monkeypatch) | ||
| await sandbox.pause( | ||
| domain="override.e2b.dev", | ||
| request_timeout=20, | ||
| headers={"X-Extra": "1"}, | ||
| ) | ||
|
|
||
| mock_pause.assert_awaited_once() | ||
| assert mock_pause.call_args.kwargs["sandbox_id"] == "sbx-test" | ||
| assert mock_pause.call_args.kwargs["api_key"] == BASE_API_KEY | ||
| assert mock_pause.call_args.kwargs["domain"] == "override.e2b.dev" | ||
| assert mock_pause.call_args.kwargs["request_timeout"] == 20 | ||
| assert mock_pause.call_args.kwargs["debug"] == BASE_DEBUG | ||
| assert mock_pause.call_args.kwargs["headers"]["X-Test"] == BASE_HEADERS["X-Test"] | ||
| assert mock_pause.call_args.kwargs["headers"]["X-Extra"] == "1" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| from types import SimpleNamespace | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
| from packaging.version import Version | ||
|
|
||
| from e2b import Sandbox | ||
| from e2b.connection_config import ConnectionConfig | ||
| import e2b.sandbox_sync.main as sandbox_sync_main | ||
|
|
||
|
|
||
| BASE_API_KEY = "base-api-key" | ||
| BASE_DOMAIN = "base.e2b.dev" | ||
| BASE_REQUEST_TIMEOUT = 11 | ||
| BASE_DEBUG = False | ||
| BASE_HEADERS = {"X-Test": "base"} | ||
|
|
||
|
|
||
| def create_sandbox(monkeypatch) -> Sandbox: | ||
| dummy_transport = SimpleNamespace(pool=object()) | ||
|
|
||
| monkeypatch.setattr( | ||
| sandbox_sync_main, "get_transport", lambda *_args, **_kwargs: dummy_transport | ||
| ) | ||
| monkeypatch.setattr( | ||
| sandbox_sync_main.httpx, "Client", lambda *args, **kwargs: object() | ||
| ) | ||
| monkeypatch.setattr( | ||
| sandbox_sync_main, "Filesystem", lambda *args, **kwargs: object() | ||
| ) | ||
| monkeypatch.setattr(sandbox_sync_main, "Commands", lambda *args, **kwargs: object()) | ||
| monkeypatch.setattr(sandbox_sync_main, "Pty", lambda *args, **kwargs: object()) | ||
| monkeypatch.setattr(sandbox_sync_main, "Git", lambda *args, **kwargs: object()) | ||
|
|
||
| return Sandbox( | ||
| sandbox_id="sbx-test", | ||
| sandbox_domain="sandbox.e2b.dev", | ||
| envd_version=Version("0.2.4"), | ||
| envd_access_token="tok", | ||
| traffic_access_token="tok", | ||
| connection_config=ConnectionConfig( | ||
| api_key=BASE_API_KEY, | ||
| domain=BASE_DOMAIN, | ||
| request_timeout=BASE_REQUEST_TIMEOUT, | ||
| debug=BASE_DEBUG, | ||
| headers=BASE_HEADERS, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.skip_debug() | ||
| def test_pause_passes_connection_config_without_overrides(monkeypatch): | ||
| mock_pause = Mock(return_value="sbx-test") | ||
| monkeypatch.setattr(sandbox_sync_main.SandboxApi, "_cls_pause", mock_pause) | ||
|
|
||
| sandbox = create_sandbox(monkeypatch) | ||
| sandbox.pause() | ||
|
|
||
| mock_pause.assert_called_once() | ||
| assert mock_pause.call_args.kwargs["sandbox_id"] == "sbx-test" | ||
| assert mock_pause.call_args.kwargs["api_key"] == BASE_API_KEY | ||
| assert mock_pause.call_args.kwargs["domain"] == BASE_DOMAIN | ||
| assert mock_pause.call_args.kwargs["request_timeout"] == BASE_REQUEST_TIMEOUT | ||
| assert mock_pause.call_args.kwargs["debug"] == BASE_DEBUG | ||
| assert mock_pause.call_args.kwargs["headers"]["X-Test"] == BASE_HEADERS["X-Test"] | ||
|
|
||
|
|
||
| @pytest.mark.skip_debug() | ||
| def test_pause_applies_overrides(monkeypatch): | ||
| mock_pause = Mock(return_value="sbx-test") | ||
| monkeypatch.setattr(sandbox_sync_main.SandboxApi, "_cls_pause", mock_pause) | ||
|
|
||
| sandbox = create_sandbox(monkeypatch) | ||
| sandbox.pause( | ||
| domain="override.e2b.dev", | ||
| request_timeout=20, | ||
| headers={"X-Extra": "1"}, | ||
| ) | ||
|
|
||
| mock_pause.assert_called_once() | ||
| assert mock_pause.call_args.kwargs["sandbox_id"] == "sbx-test" | ||
| assert mock_pause.call_args.kwargs["api_key"] == BASE_API_KEY | ||
| assert mock_pause.call_args.kwargs["domain"] == "override.e2b.dev" | ||
| assert mock_pause.call_args.kwargs["request_timeout"] == 20 | ||
| assert mock_pause.call_args.kwargs["debug"] == BASE_DEBUG | ||
| assert mock_pause.call_args.kwargs["headers"]["X-Test"] == BASE_HEADERS["X-Test"] | ||
| assert mock_pause.call_args.kwargs["headers"]["X-Extra"] == "1" |
Uh oh!
There was an error while loading. Please reload this page.