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", "asset-host") 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) } if !strings.Contains(command, "--hostname 'asset-host'") { 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) } }