任务执行1-19

This commit is contained in:
zxr
2026-06-26 12:51:50 +08:00
parent 175d9f8f94
commit 19908230f2
19 changed files with 2615 additions and 1260 deletions

View File

@@ -0,0 +1,59 @@
package ingest
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"git.apinb.com/ops/logs/internal/config"
)
func TestForwardAlertPostsRawEventIngestPayload(t *testing.T) {
var gotPath string
var gotBody RawEventIngestBody
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
t.Fatalf("decode body: %v", err)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
config.Spec.AlertForward = &config.AlertForwardConf{
Enabled: true,
BaseURL: server.URL,
InternalKey: "internal-key",
}
err := forwardAlert(AlertReceiveBody{
AlertName: "H3C Trap",
Summary: "Interface down",
Description: "Interface down",
SeverityCode: "major",
Labels: map[string]string{
"source_subtype": "snmp_trap",
"ip": "192.168.1.10",
},
Agent: "logs-trap",
RawData: json.RawMessage(`{"trap_oid":"1.3.6.1.4.1"}`),
})
if err != nil {
t.Fatalf("forwardAlert returned error: %v", err)
}
if gotPath != "/Alert/v1/raw-events/ingest" {
t.Fatalf("expected raw event ingest path, got %q", gotPath)
}
if gotBody.SourceType != "trap" {
t.Fatalf("expected trap source type, got %q", gotBody.SourceType)
}
if gotBody.ParseStatus != "parsed" {
t.Fatalf("expected parsed status, got %q", gotBody.ParseStatus)
}
if string(gotBody.RawPayload) != `{"trap_oid":"1.3.6.1.4.1"}` {
t.Fatalf("raw payload changed: %s", string(gotBody.RawPayload))
}
}