Files
aiops/backend/controller/agent_install_test.go
T
Logan.Li 827d9a31cd fix: 正确识别 Agent 安装与审计失败
- 下载脚本失败时立即终止安装并提示 WAF 拦截
- Agent 服务未运行时不再计为安装成功
- 审计列表兼容后端 status 成功失败字段
2026-07-25 15:08:04 +08:00

61 lines
2.1 KiB
Go

package controller
import (
"strings"
"testing"
)
// TestValidateInstallServerURL 验证 Agent 公网地址仅接受安全的 HTTP(S) origin。 // Logan.Li
func TestValidateInstallServerURL(t *testing.T) {
tests := []struct {
name string
input string
want string
wantErr bool
}{
{name: "HTTP 域名", input: "http://aiops.example.com", want: "http://aiops.example.com"},
{name: "HTTPS 域名", input: "https://aiops.example.com/", want: "https://aiops.example.com"},
{name: "自定义端口", input: "https://aiops.example.com:8443", want: "https://aiops.example.com:8443"},
{name: "拒绝路径", input: "https://aiops.example.com/api", wantErr: true},
{name: "拒绝查询参数", input: "https://aiops.example.com?x=1", wantErr: true},
{name: "拒绝非 HTTP 协议", input: "file:///tmp/agent", wantErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := validateInstallServerURL(test.input)
if test.wantErr {
if err == nil {
t.Fatalf("期望校验失败,实际返回 %q", got)
}
return
}
if err != nil {
t.Fatalf("地址校验失败: %v", err)
}
if got != test.want {
t.Fatalf("期望 %q,实际 %q", test.want, got)
}
})
}
}
// TestAgentInstallCommand 验证下载失败不会被管道末端命令掩盖。 // Logan.Li
func TestAgentInstallCommand(t *testing.T) {
command := agentInstallCommand("https://aiops.example.com", "token-value")
if strings.Contains(command, "| bash") {
t.Fatalf("安装命令不应继续使用 curl 管道: %s", command)
}
if !strings.Contains(command, "curl -fsSL -o") || !strings.Contains(command, "https://aiops.example.com/agent/install.sh") {
t.Fatalf("安装命令缺少脚本下载步骤: %s", command)
}
}
// TestAgentInstallErrorMessage 验证 WAF 拒绝能返回明确提示。 // Logan.Li
func TestAgentInstallErrorMessage(t *testing.T) {
message := agentInstallErrorMessage("https://aiops.example.com", "curl: (22) The requested URL returned error: 403")
if !strings.Contains(message, "WAF") || !strings.Contains(message, "/agent/*") {
t.Fatalf("WAF 错误提示不完整: %s", message)
}
}