|
| 1 | +""" |
| 2 | +workspace_id 跨模块 E2E 测试 / Cross-module workspace_id E2E test |
| 3 | +
|
| 4 | +验证 SDK 在 create / get / list 接口上正确传递和回填 ``workspace_id``。 |
| 5 | +Verifies the SDK correctly passes and back-fills ``workspace_id`` across |
| 6 | +create / get / list interfaces for resource modules. |
| 7 | +
|
| 8 | +环境变量 / Environment variables: |
| 9 | +- ``AGENTRUN_TEST_WORKSPACE_ID``:用于本测试的工作空间 ID。未配置则跳过整个文件。 |
| 10 | + Workspace ID to use for this test; the entire file is skipped if not set. |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from agentrun.credential import ( |
| 18 | + Credential, |
| 19 | + CredentialClient, |
| 20 | + CredentialConfig, |
| 21 | + CredentialCreateInput, |
| 22 | + CredentialListInput, |
| 23 | +) |
| 24 | +from agentrun.sandbox import Template |
| 25 | +from agentrun.sandbox.model import PageableInput, TemplateInput, TemplateType |
| 26 | +from agentrun.utils.exception import ResourceNotExistError |
| 27 | + |
| 28 | +WORKSPACE_ID = os.getenv("AGENTRUN_TEST_WORKSPACE_ID") |
| 29 | + |
| 30 | +pytestmark = pytest.mark.skipif( |
| 31 | + not WORKSPACE_ID, |
| 32 | + reason=( |
| 33 | + "AGENTRUN_TEST_WORKSPACE_ID not configured; skipping workspace_id E2E" |
| 34 | + ), |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +class TestWorkspaceId: |
| 39 | + """workspace_id 跨模块 E2E 测试""" |
| 40 | + |
| 41 | + @pytest.fixture |
| 42 | + def credential_name(self, unique_name: str) -> str: |
| 43 | + return f"{unique_name}-ws-cred" |
| 44 | + |
| 45 | + @pytest.fixture |
| 46 | + def template_name(self, unique_name: str) -> str: |
| 47 | + return f"{unique_name}-ws-tpl" |
| 48 | + |
| 49 | + async def test_credential_with_workspace_id_async( |
| 50 | + self, credential_name: str |
| 51 | + ): |
| 52 | + """凭证创建时指定 workspace_id,回读与列举均能拿到该 workspace_id""" |
| 53 | + client = CredentialClient() |
| 54 | + ws = WORKSPACE_ID # type: ignore[assignment] |
| 55 | + assert ws is not None |
| 56 | + |
| 57 | + cred: Credential | None = None |
| 58 | + try: |
| 59 | + # 1. 创建带 workspace_id 的凭证 |
| 60 | + cred = await Credential.create_async( |
| 61 | + CredentialCreateInput( |
| 62 | + credential_name=credential_name, |
| 63 | + description="E2E workspace_id test", |
| 64 | + credential_config=CredentialConfig.inbound_api_key( |
| 65 | + "sk-test-ws-e2e" |
| 66 | + ), |
| 67 | + workspace_id=ws, |
| 68 | + ) |
| 69 | + ) |
| 70 | + assert cred.credential_name == credential_name |
| 71 | + assert ( |
| 72 | + cred.workspace_id == ws |
| 73 | + ), f"create 返回的 workspace_id 不匹配: {cred.workspace_id!r}" |
| 74 | + |
| 75 | + # 2. get 接口回读 workspace_id |
| 76 | + cred_fetched = await client.get_async( |
| 77 | + credential_name=credential_name |
| 78 | + ) |
| 79 | + assert ( |
| 80 | + cred_fetched.workspace_id == ws |
| 81 | + ), f"get 返回的 workspace_id 不匹配: {cred_fetched.workspace_id!r}" |
| 82 | + |
| 83 | + # 3. list 接口按 workspace_id 过滤,本次创建的资源应在结果中 |
| 84 | + list_results = await client.list_async( |
| 85 | + CredentialListInput(workspace_id=ws) |
| 86 | + ) |
| 87 | + names = [item.credential_name for item in list_results] |
| 88 | + assert credential_name in names, ( |
| 89 | + f"list(workspace_id={ws!r}) 未返回刚创建的凭证" |
| 90 | + f" {credential_name!r}," |
| 91 | + f"实际返回 {names!r}" |
| 92 | + ) |
| 93 | + # 列表项的 workspace_id 也应该是同一个 |
| 94 | + for item in list_results: |
| 95 | + if item.credential_name == credential_name: |
| 96 | + assert item.workspace_id == ws |
| 97 | + finally: |
| 98 | + if cred is not None: |
| 99 | + try: |
| 100 | + await cred.delete_async() |
| 101 | + except ResourceNotExistError: |
| 102 | + pass |
| 103 | + |
| 104 | + async def test_template_with_workspace_id_async(self, template_name: str): |
| 105 | + """Sandbox Template 创建时指定 workspace_id,回读与列举均能拿到该 workspace_id""" |
| 106 | + ws = WORKSPACE_ID # type: ignore[assignment] |
| 107 | + assert ws is not None |
| 108 | + |
| 109 | + template: Template | None = None |
| 110 | + try: |
| 111 | + # 1. 创建带 workspace_id 的 Template |
| 112 | + template = await Template.create_async( |
| 113 | + TemplateInput( |
| 114 | + template_name=template_name, |
| 115 | + template_type=TemplateType.CODE_INTERPRETER, |
| 116 | + description="E2E workspace_id test", |
| 117 | + cpu=2.0, |
| 118 | + memory=4096, |
| 119 | + disk_size=512, |
| 120 | + sandbox_idle_timeout_in_seconds=600, |
| 121 | + sandbox_ttlin_seconds=600, |
| 122 | + workspace_id=ws, |
| 123 | + ) |
| 124 | + ) |
| 125 | + assert template.template_name == template_name |
| 126 | + assert ( |
| 127 | + template.workspace_id == ws |
| 128 | + ), f"create 返回的 workspace_id 不匹配: {template.workspace_id!r}" |
| 129 | + |
| 130 | + # 2. get 接口回读 workspace_id |
| 131 | + template_fetched = await Template.get_by_name_async(template_name) |
| 132 | + assert ( |
| 133 | + template_fetched.workspace_id == ws |
| 134 | + ), f"get 返回的 workspace_id 不匹配: {template_fetched.workspace_id!r}" |
| 135 | + |
| 136 | + # 3. list 接口按 workspace_id 过滤 |
| 137 | + list_results = await Template.list_templates_async( |
| 138 | + PageableInput(workspace_id=ws, page_size=100) |
| 139 | + ) |
| 140 | + names = [t.template_name for t in list_results or []] |
| 141 | + assert template_name in names, ( |
| 142 | + f"list_templates(workspace_id={ws!r}) 未返回刚创建的" |
| 143 | + f" Template {template_name!r},实际返回 {names!r}" |
| 144 | + ) |
| 145 | + finally: |
| 146 | + if template is not None: |
| 147 | + try: |
| 148 | + await Template.delete_by_name_async(template_name) |
| 149 | + except ResourceNotExistError: |
| 150 | + pass |
0 commit comments