forked from criteo-forks/alertmanager2es
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
149 lines (128 loc) · 4.1 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"testing"
"time"
)
func TestHappyPath(t *testing.T) {
var (
request *http.Request
requestBody []byte
)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
if r.Body == nil {
t.Fatal("got empty response body")
return
}
requestBody, err = ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
request = r
io.WriteString(w, "OK\n")
}))
defer s.Close()
esURL = s.URL
w, r := recordedRequest(t, s.URL+"/webhook")
handler(w, r)
expectedCode := http.StatusOK
if w.Code != expectedCode {
t.Fatalf("expected HTTP status %d, got %d", expectedCode, w.Code)
}
u, _ := url.Parse(s.URL)
expected := u.Host
if request.Host != expected {
t.Fatalf("expected request host %s, got %s", expected, request.Host)
}
expected = "POST"
if request.Method != expected {
t.Fatalf("expected request method %s, got %s", expected, request.Method)
}
expected = fmt.Sprintf("/alertmanager-%s/alert_group", time.Now().Format("2006.01"))
if request.RequestURI != expected {
t.Fatalf("expected request path %s, got %s", expected, request.RequestURI)
}
// the timestamp changes every second, making it hard to test, so strip it off before comparing
lengthOfTimestamp := 42
l := len(esDocument) - lengthOfTimestamp
if !bytes.Equal(esDocument[:l], requestBody[:l]) {
t.Fatalf("expected payload %q, got %q", esDocument[:l], requestBody[:l])
}
}
func TestErrorsPassedThrough(t *testing.T) {
// Mock Elasticsearch server returns 404s
s := httptest.NewServer(http.NotFoundHandler())
defer s.Close()
esURL = s.URL
w, r := recordedRequest(t, s.URL+"/webhook")
handler(w, r)
expectedCode := http.StatusInternalServerError
if w.Code != expectedCode {
t.Fatalf("expected HTTP status %d, got %d", expectedCode, w.Code)
}
}
func TestUserAgentSetWhenPostingToElasticsearch(t *testing.T) {
var userAgent string
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userAgent = r.UserAgent()
}))
defer s.Close()
esURL = s.URL
w, r := recordedRequest(t, s.URL+"/webhook")
handler(w, r)
expected := regexp.MustCompile(`^alertmanager2es .*? \(go\d.\d(?:.\d)?\)$`)
if !expected.MatchString(userAgent) {
t.Fatalf("expected user agent to match %s, got %q", expected.String(), userAgent)
}
}
func recordedRequest(t *testing.T, url string) (*httptest.ResponseRecorder, *http.Request) {
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", url, bytes.NewBuffer(amNotification))
if err != nil {
t.Fatal(err)
}
return w, r
}
var amNotification = []byte(`{
"alerts": [
{
"annotations": {
"link": "https://example.com/Foo+Bar",
"summary": "Alert summary"
},
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "https://example.com",
"labels": {
"alertname": "Foo_Bar",
"instance": "foo"
},
"startsAt": "2017-02-02T16:51:13.507955756Z",
"status": "firing"
}
],
"commonAnnotations": {
"link": "https://example.com/Foo+Bar",
"summary": "Alert summary"
},
"commonLabels": {
"alertname": "Foo_Bar",
"instance": "foo"
},
"externalURL": "https://alertmanager.example.com",
"groupLabels": {
"alertname": "Foo_Bar"
},
"receiver": "alertmanager2es",
"status": "firing",
"version": "4",
"groupKey": "{}/{}/{notify=\"default\":{alertname=\"Foo_Bar\", instance=\"foo\"}"
}`)
var esDocument = []byte(`{"alerts":[{"annotations":{"link":"https://example.com/Foo+Bar","summary":"Alert summary"},"endsAt":"0001-01-01T00:00:00Z","generatorURL":"https://example.com","labels":{"alertname":"Foo_Bar","instance":"foo"},"startsAt":"2017-02-02T16:51:13.507955756Z","status":"firing"}],"commonAnnotations":{"link":"https://example.com/Foo+Bar","summary":"Alert summary"},"commonLabels":{"alertname":"Foo_Bar","instance":"foo"},"externalURL":"https://alertmanager.example.com","groupLabels":{"alertname":"Foo_Bar"},"receiver":"alertmanager2es","status":"firing","version":"4","groupKey":"{}/{}/{notify=\"default\":{alertname=\"Foo_Bar\", instance=\"foo\"}","@timestamp":"2017-02-02T19:37:22+01:00"}`)