forked from nzin/prometheus-cachethq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachethq_test.go
178 lines (150 loc) · 5.48 KB
/
cachethq_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// server is a test HTTP server used to provide mock API responses.
mockServer *httptest.Server
// status updated by cachetHQ bridge
finalStatus int
)
// setup sets up a test HTTP server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func setupMockCachetHQ(t *testing.T) {
// fake CachetHQ server
mux = http.NewServeMux()
mockServer = httptest.NewServer(mux)
finalStatus = 0
mux.HandleFunc("/api/v1/components",
func(w http.ResponseWriter, r *http.Request) {
fmt.Println("mock GET /api/v1/components")
assert.Equal(t, "1", r.FormValue("page"))
assert.Equal(t, "GET", r.Method)
fmt.Fprint(w, `
{"meta": {
"pagination": {
"total": 1,
"count": 1,
"per_page": 20,
"current_page": 1,
"total_pages": 1,
"links": {
"next_page": null,
"previous_page": null
}
}
},
"data": [
{
"id": 1,
"name": "component21",
"description": "This is a component",
"link": "",
"status": 1,
"order": 0,
"group_id": 0,
"created_at": "2015-07-24 14:42:10",
"updated_at": "2015-07-24 14:42:10",
"deleted_at": null,
"status_name": "Operational"
}
]}`)
})
mux.HandleFunc("/api/v1/incidents",
func(w http.ResponseWriter, r *http.Request) {
fmt.Println("mock POST /api/v1/incidents")
assert.Equal(t, "POST", r.Method)
type incidentStruct struct {
Name string `json:"name"`
Message string `json:"message"`
Status int `json:"status"`
ComponentID int `json:"component_id"`
ComponentStatus int `json:"component_status"`
}
decoder := json.NewDecoder(r.Body)
var incident incidentStruct
err := decoder.Decode(&incident)
if err == nil {
finalStatus = incident.Status
}
})
}
// teardown closes the test HTTP server.
func teardown() {
mockServer.Close()
}
// the mock cachetHQ defines 1 component: "Component21"
func TestCachetHqComponent21(t *testing.T) {
setupMockCachetHQ(t)
defer teardown()
config := PrometheusCachetConfig{
LabelName: "alertname",
PrometheusToken: "promToken",
LogLevel: LOG_DEBUG,
Cachet: NewCachetImpl(mockServer.URL, "1234567890abcdef", &http.Client{}),
}
router := PrepareGinRouter(&config)
server := &http.Server{
Addr: fmt.Sprintf(":9999"),
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go server.ListenAndServe()
time.Sleep(500 * time.Millisecond)
defer server.Close()
// send an alert
url := "http://localhost:9999/alert"
var jsonStr = []byte(`{"receiver":"cachethq-receiver","status":"firing","alerts":[{"status":"firing","labels":{"alertname":"component21"},"annotations":{},"startsAt":"2018-05-22T20:00:32.729840058-04:00","endsAt":"0001-01-01T00:00: 00Z","generatorURL":""}],"groupLabels":{"alertname":"component21"},"commonLabels":{"alertname":"component21"},"commonAnnotations":{},"externalURL":"http://localhost.localdomain:9093","version":"4","groupKey":"{}:{alertname=\"component21\"}"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Authorization", "Bearer "+config.PrometheusToken)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
assert.Nil(t, err, "Not able to send POST alert request")
defer resp.Body.Close()
// the status HAS been updated
assert.Equal(t, 2, finalStatus)
}
func TestCachetHqComponent22(t *testing.T) {
setupMockCachetHQ(t)
defer teardown()
config := PrometheusCachetConfig{
LabelName: "alertname",
PrometheusToken: "promToken",
LogLevel: LOG_DEBUG,
Cachet: NewCachetImpl(mockServer.URL, "1234567890abcdef", &http.Client{}),
}
router := PrepareGinRouter(&config)
server := &http.Server{
Addr: fmt.Sprintf(":9998"),
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go server.ListenAndServe()
defer server.Close()
// send an alert
url := "http://localhost:9998/alert"
var jsonStr = []byte(`{"receiver":"cachethq-receiver","status":"firing","alerts":[{"status":"firing","labels":{"alertname":"component22"},"annotations":{},"startsAt":"2018-05-22T20:00:32.729840058-04:00","endsAt":"0001-01-01T00:00: 00Z","generatorURL":""}],"groupLabels":{"alertname":"component22"},"commonLabels":{"alertname":"component22"},"commonAnnotations":{},"externalURL":"http://localhost.localdomain:9093","version":"4","groupKey":"{}:{alertname=\"component22\"}"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Authorization", "Bearer "+config.PrometheusToken)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
assert.Nil(t, err, "Not able to send POST alert request")
defer resp.Body.Close()
// the status has NOT been updated because "component22" does not exist
assert.Equal(t, 0, finalStatus)
}