from __future__ import annotations import importlib.util import json from pathlib import Path SCRIPT_PATH = ( Path(__file__).resolve().parents[2] / "agentkit" / "skills" / "builtin" / "devops" / "scripts" / "devops_api.py" ) def _load_devops_api(): spec = importlib.util.spec_from_file_location("devops_api_under_test", SCRIPT_PATH) assert spec and spec.loader module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module class _FakeResponse: def __init__(self, payload: dict, status: int = 200): self.payload = payload self.status = status def __enter__(self): return self def __exit__(self, *_exc): return False def read(self) -> bytes: return json.dumps(self.payload).encode("utf-8") def test_list_cloudconfig_changes_adds_ops_url_and_uses_read_only_get( monkeypatch, ): mod = _load_devops_api() captured = {} def _fake_urlopen(request, timeout): captured["url"] = request.full_url captured["method"] = request.get_method() captured["api_key"] = request.get_header("X-api-key") return _FakeResponse( { "errcode": "OK", "total": 1, "items": [ { "id": 62289, "reason": "upgrade tikv patch", "applicant": "kaixing.yang@pingcap.com", } ], } ) monkeypatch.setenv("DEVOPS_API_KEY", "secret") monkeypatch.delenv("DEVOPS_BASE_URL", raising=False) monkeypatch.delenv("DEVOPS_FRONTEND_URL", raising=False) monkeypatch.setattr(mod.urllib.request, "urlopen", _fake_urlopen) args = mod.build_parser().parse_args( [ "list-cloudconfig-changes", "--cluster-id", "10698648422638660035", "--cluster-type", "DEDICATED_TIDB", "--per-page", "5", ] ) result = args.func(args) assert captured["method"] == "GET" assert captured["api_key"] == "secret" assert captured["url"].startswith( "https://prod.devops.pingcap.com/api/v1/devops/cloudconfig/cluster/changes?" ) assert "cluster_id=10698648422638660035" in captured["url"] assert result["items"][0]["url"] == "https://ops.tidbcloud.com/confighistories/62289" def test_get_cloudconfig_change_uses_staging_frontend_url(monkeypatch): mod = _load_devops_api() def _fake_urlopen(request, timeout): return _FakeResponse({"errcode": "OK", "change": {"id": 123, "reason": "test"}}) monkeypatch.setenv("DEVOPS_API_TOKEN", "secret") monkeypatch.setenv("DEVOPS_BASE_URL", "https://staging.devops.pingcap.com") monkeypatch.delenv("DEVOPS_FRONTEND_URL", raising=False) monkeypatch.setattr(mod.urllib.request, "urlopen", _fake_urlopen) args = mod.build_parser().parse_args(["get-cloudconfig-change", "--change-id", "123"]) result = args.func(args) assert result["change"]["url"] == "https://ops-staging.tidbcloud.com/confighistories/123"