|
| 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 | + } |
0 commit comments