Skip to content

Commit 01d3b2b

Browse files
authored
fix: unify deploy provider environment handling (#973)
* fix: unify deploy provider environment handling * fix: normalize studio provider during serve * fix: isolate deploy provider env --------- Co-authored-by: shanchunhua <shanchunhua@bytedance.com>
1 parent d26b478 commit 01d3b2b

15 files changed

Lines changed: 720 additions & 90 deletions

File tree

‎tests/cli/test_cli_clean.py‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from typing import Any
18+
19+
from click.testing import CliRunner
20+
21+
from veadk.cli.cli_clean import clean
22+
23+
24+
class _FakeVeFaaS:
25+
calls: list[dict[str, Any]] = []
26+
27+
def __init__(self, **kwargs: Any) -> None:
28+
self.calls.append({"init": kwargs, "deleted": []})
29+
30+
def find_app_id_by_name(self, name: str) -> str | None:
31+
self.calls[-1].setdefault("lookups", []).append(name)
32+
if len(self.calls[-1]["lookups"]) == 1:
33+
return "app-123"
34+
return None
35+
36+
def delete(self, app_id: str | None) -> None:
37+
self.calls[-1]["deleted"].append(app_id)
38+
39+
40+
def test_clean_defaults_to_volcengine_environment(
41+
monkeypatch,
42+
) -> None:
43+
monkeypatch.delenv("AGENTKIT_CLOUD_PROVIDER", raising=False)
44+
monkeypatch.delenv("CLOUD_PROVIDER", raising=False)
45+
monkeypatch.setenv("VOLCENGINE_ACCESS_KEY", "volc-ak")
46+
monkeypatch.setenv("VOLCENGINE_SECRET_KEY", "volc-sk")
47+
monkeypatch.setenv("VOLCENGINE_SESSION_TOKEN", "volc-token")
48+
monkeypatch.setenv("REGION", "cn-shanghai")
49+
monkeypatch.setattr("veadk.integrations.ve_faas.ve_faas.VeFaaS", _FakeVeFaaS)
50+
_FakeVeFaaS.calls.clear()
51+
52+
result = CliRunner().invoke(
53+
clean,
54+
["--vefaas-app-name", "studio-app"],
55+
input="y\n",
56+
)
57+
58+
assert result.exit_code == 0, result.output
59+
assert _FakeVeFaaS.calls == [
60+
{
61+
"init": {
62+
"access_key": "volc-ak",
63+
"secret_key": "volc-sk",
64+
"session_token": "volc-token",
65+
"region": "cn-shanghai",
66+
"provider": "volcengine",
67+
},
68+
"deleted": ["app-123"],
69+
"lookups": ["studio-app", "studio-app"],
70+
}
71+
]
72+
73+
74+
def test_clean_uses_byteplus_environment(
75+
monkeypatch,
76+
) -> None:
77+
monkeypatch.setenv("CLOUD_PROVIDER", "byteplus")
78+
monkeypatch.delenv("AGENTKIT_CLOUD_PROVIDER", raising=False)
79+
monkeypatch.setenv("BYTEPLUS_ACCESS_KEY", "byteplus-ak")
80+
monkeypatch.setenv("BYTEPLUS_SECRET_KEY", "byteplus-sk")
81+
monkeypatch.setenv("BYTEPLUS_SESSION_TOKEN", "byteplus-token")
82+
monkeypatch.setenv("BYTEPLUS_REGION", "ap-southeast-1")
83+
monkeypatch.setenv("VOLCENGINE_ACCESS_KEY", "volc-ak")
84+
monkeypatch.setenv("VOLCENGINE_SECRET_KEY", "volc-sk")
85+
monkeypatch.setattr("veadk.integrations.ve_faas.ve_faas.VeFaaS", _FakeVeFaaS)
86+
_FakeVeFaaS.calls.clear()
87+
88+
result = CliRunner().invoke(
89+
clean,
90+
["--vefaas-app-name", "studio-app"],
91+
input="y\n",
92+
)
93+
94+
assert result.exit_code == 0, result.output
95+
assert _FakeVeFaaS.calls[0]["init"] == {
96+
"access_key": "byteplus-ak",
97+
"secret_key": "byteplus-sk",
98+
"session_token": "byteplus-token",
99+
"region": "ap-southeast-1",
100+
"provider": "byteplus",
101+
}
102+
assert _FakeVeFaaS.calls[0]["deleted"] == ["app-123"]
103+
104+
105+
def test_clean_explicit_byteplus_options_override_environment(
106+
monkeypatch,
107+
) -> None:
108+
monkeypatch.setenv("CLOUD_PROVIDER", "volcengine")
109+
monkeypatch.setenv("BYTEPLUS_ACCESS_KEY", "env-ak")
110+
monkeypatch.setenv("BYTEPLUS_SECRET_KEY", "env-sk")
111+
monkeypatch.setenv("BYTEPLUS_SESSION_TOKEN", "env-token")
112+
monkeypatch.setattr("veadk.integrations.ve_faas.ve_faas.VeFaaS", _FakeVeFaaS)
113+
_FakeVeFaaS.calls.clear()
114+
115+
result = CliRunner().invoke(
116+
clean,
117+
[
118+
"--provider",
119+
"byteplus",
120+
"--region",
121+
"ap-southeast-1",
122+
"--byteplus-access-key",
123+
"cli-ak",
124+
"--byteplus-secret-key",
125+
"cli-sk",
126+
"--byteplus-session-token",
127+
"cli-token",
128+
"--vefaas-app-name",
129+
"studio-app",
130+
],
131+
input="y\n",
132+
)
133+
134+
assert result.exit_code == 0, result.output
135+
assert _FakeVeFaaS.calls[0]["init"] == {
136+
"access_key": "cli-ak",
137+
"secret_key": "cli-sk",
138+
"session_token": "cli-token",
139+
"region": "ap-southeast-1",
140+
"provider": "byteplus",
141+
}

‎tests/cli/test_cli_deploy.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
import os
18+
from pathlib import Path
19+
from types import SimpleNamespace
20+
from typing import Any
21+
22+
from click.testing import CliRunner
23+
24+
from veadk.cli.cli_deploy import deploy
25+
from veadk.config import veadk_environments
26+
27+
28+
def _write_agent_project(path: Path) -> None:
29+
path.mkdir()
30+
(path / "__init__.py").write_text("from . import agent\n", encoding="utf-8")
31+
(path / "agent.py").write_text("root_agent = object()\n", encoding="utf-8")
32+
33+
34+
def test_deploy_reads_byteplus_provider_environment(
35+
monkeypatch,
36+
tmp_path: Path,
37+
) -> None:
38+
project = tmp_path / "agent-proj"
39+
_write_agent_project(project)
40+
captured: dict[str, Any] = {}
41+
42+
def fake_cookiecutter(
43+
template: str,
44+
output_dir: str,
45+
no_input: bool,
46+
extra_context: dict[str, Any],
47+
) -> None:
48+
captured["template"] = template
49+
captured["no_input"] = no_input
50+
captured["extra_context"] = extra_context
51+
generated_agent_dir = (
52+
Path(output_dir) / extra_context["local_dir_name"] / "src" / "agent_proj"
53+
)
54+
generated_agent_dir.mkdir(parents=True)
55+
56+
async def fake_main() -> None:
57+
captured["main_env"] = {
58+
"CLOUD_PROVIDER": os.environ.get("CLOUD_PROVIDER"),
59+
"AGENTKIT_CLOUD_PROVIDER": os.environ.get("AGENTKIT_CLOUD_PROVIDER"),
60+
"BYTEPLUS_REGION": os.environ.get("BYTEPLUS_REGION"),
61+
"REGION": os.environ.get("REGION"),
62+
"VOLCENGINE_ACCESS_KEY": os.environ.get("VOLCENGINE_ACCESS_KEY"),
63+
"VOLCENGINE_SECRET_KEY": os.environ.get("VOLCENGINE_SECRET_KEY"),
64+
"VOLCENGINE_SESSION_TOKEN": os.environ.get("VOLCENGINE_SESSION_TOKEN"),
65+
}
66+
67+
monkeypatch.setenv("CLOUD_PROVIDER", "byteplus")
68+
monkeypatch.delenv("AGENTKIT_CLOUD_PROVIDER", raising=False)
69+
monkeypatch.delenv("REGION", raising=False)
70+
monkeypatch.delenv("VOLCENGINE_ACCESS_KEY", raising=False)
71+
monkeypatch.delenv("VOLCENGINE_SECRET_KEY", raising=False)
72+
monkeypatch.delenv("VOLCENGINE_SESSION_TOKEN", raising=False)
73+
monkeypatch.setenv("BYTEPLUS_REGION", "ap-southeast-1")
74+
monkeypatch.setenv("BYTEPLUS_ACCESS_KEY", "byteplus-ak")
75+
monkeypatch.setenv("BYTEPLUS_SECRET_KEY", "byteplus-sk")
76+
monkeypatch.setenv("BYTEPLUS_SESSION_TOKEN", "byteplus-token")
77+
for key in (
78+
"CLOUD_PROVIDER",
79+
"AGENTKIT_CLOUD_PROVIDER",
80+
"BYTEPLUS_REGION",
81+
):
82+
monkeypatch.delitem(veadk_environments, key, raising=False)
83+
monkeypatch.setattr("veadk.utils.misc.formatted_timestamp", lambda: "20260824")
84+
monkeypatch.setattr("cookiecutter.main.cookiecutter", fake_cookiecutter)
85+
monkeypatch.setattr(
86+
"veadk.utils.misc.load_module_from_file",
87+
lambda **_kwargs: SimpleNamespace(main=fake_main),
88+
)
89+
90+
result = CliRunner().invoke(
91+
deploy,
92+
[
93+
"--vefaas-app-name",
94+
"studio-app",
95+
"--path",
96+
str(project),
97+
],
98+
)
99+
100+
assert result.exit_code == 0, result.output
101+
assert captured["extra_context"]["provider"] == "byteplus"
102+
assert captured["extra_context"]["region"] == "ap-southeast-1"
103+
assert captured["main_env"] == {
104+
"CLOUD_PROVIDER": "byteplus",
105+
"AGENTKIT_CLOUD_PROVIDER": "byteplus",
106+
"BYTEPLUS_REGION": "ap-southeast-1",
107+
"REGION": "ap-southeast-1",
108+
"VOLCENGINE_ACCESS_KEY": "byteplus-ak",
109+
"VOLCENGINE_SECRET_KEY": "byteplus-sk",
110+
"VOLCENGINE_SESSION_TOKEN": "byteplus-token",
111+
}

‎tests/cli/test_frontend_sandbox_options.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ def test_serve_provider_is_bootstrapped_before_command_modules_load(
3434

3535
_bootstrap_serve_provider(["frontend"])
3636

37-
assert os.environ["AGENTKIT_CLOUD_PROVIDER"] == "volcengine"
38-
assert os.environ["CLOUD_PROVIDER"] == "volcengine"
39-
40-
_bootstrap_serve_provider(["studio", "--provider", "byteplus"])
41-
4237
assert os.environ["AGENTKIT_CLOUD_PROVIDER"] == "byteplus"
4338
assert os.environ["CLOUD_PROVIDER"] == "byteplus"
4439

40+
_bootstrap_serve_provider(["studio", "--provider", "volcengine"])
41+
42+
assert os.environ["AGENTKIT_CLOUD_PROVIDER"] == "volcengine"
43+
assert os.environ["CLOUD_PROVIDER"] == "volcengine"
44+
4545

4646
@pytest.mark.parametrize("command", [frontend, studio])
4747
def test_sandbox_tool_options_are_shared_by_local_serve_commands(
@@ -162,7 +162,7 @@ def test_local_studio_mounts_snapshot_tools_into_sandbox_services() -> None:
162162

163163

164164
@pytest.mark.parametrize("command", [frontend, studio])
165-
def test_local_serve_commands_default_to_volcengine(
165+
def test_local_serve_commands_defer_provider_resolution_when_not_explicit(
166166
monkeypatch: pytest.MonkeyPatch,
167167
command: Command,
168168
) -> None:
@@ -176,7 +176,7 @@ def test_local_serve_commands_default_to_volcengine(
176176
result = CliRunner().invoke(command)
177177

178178
assert result.exit_code == 0, result.output
179-
assert captured["provider"] == "volcengine"
179+
assert captured["provider"] is None
180180

181181

182182
@pytest.mark.parametrize("command", [frontend, studio])

‎tests/cli/test_generated_agent_backend_codegen_extended.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
}
8989

9090

91+
@pytest.fixture(autouse=True)
92+
def _default_to_volcengine_provider(monkeypatch: pytest.MonkeyPatch) -> None:
93+
monkeypatch.delenv("CLOUD_PROVIDER", raising=False)
94+
monkeypatch.delenv("AGENTKIT_CLOUD_PROVIDER", raising=False)
95+
96+
9197
def _file_map(project: GeneratedProject) -> dict[str, str]:
9298
return {file.path: file.content for file in project.files}
9399

‎tests/cli/test_studio_deploy_permissions.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from __future__ import annotations
1616

17+
import os
18+
1719
import pytest
1820
from click.testing import CliRunner
1921

@@ -357,6 +359,55 @@ def _precheck(*, specs, **_kwargs):
357359
assert "Pre-check only: no cloud resources were created." in result.output
358360

359361

362+
def test_cli_restores_provider_process_env_after_byteplus_deploy(
363+
monkeypatch,
364+
) -> None:
365+
for key in (
366+
"CLOUD_PROVIDER",
367+
"AGENTKIT_CLOUD_PROVIDER",
368+
"BYTEPLUS_REGION",
369+
"BYTEPLUS_ACCESS_KEY",
370+
"BYTEPLUS_SECRET_KEY",
371+
"BYTEPLUS_SESSION_TOKEN",
372+
"IAM_ROLE",
373+
):
374+
monkeypatch.delenv(key, raising=False)
375+
376+
def _precheck(*, specs, **_kwargs):
377+
return [
378+
permissions.PermissionResult(spec=spec, satisfied=True) for spec in specs
379+
]
380+
381+
monkeypatch.setattr(
382+
permissions,
383+
"run_studio_deploy_permission_precheck",
384+
_precheck,
385+
)
386+
387+
result = CliRunner().invoke(
388+
studio,
389+
[
390+
"deploy",
391+
"--vefaas-app-name",
392+
"studio-test",
393+
"--provider",
394+
"byteplus",
395+
"--byteplus-access-key",
396+
"ak",
397+
"--byteplus-secret-key",
398+
"sk",
399+
"--precheck-only",
400+
],
401+
)
402+
403+
assert result.exit_code == 0, result.output
404+
assert os.environ.get("CLOUD_PROVIDER") is None
405+
assert os.environ.get("AGENTKIT_CLOUD_PROVIDER") is None
406+
assert os.environ.get("BYTEPLUS_REGION") is None
407+
assert os.environ.get("BYTEPLUS_ACCESS_KEY") is None
408+
assert os.environ.get("BYTEPLUS_SECRET_KEY") is None
409+
410+
360411
def test_cli_precheck_only_rejects_overlong_site_title_before_iam(monkeypatch) -> None:
361412
precheck_called = False
362413

0 commit comments

Comments
 (0)