60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
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))
|
|
}
|
|
}
|